@@ -71,10 +71,30 @@ QgsVectorFileWriter::QgsVectorFileWriter(
71
71
QString vectorFileName = theVectorFileName;
72
72
QString fileEncoding = theFileEncoding;
73
73
74
+ QStringList dsOptions = datasourceOptions;
75
+
76
+ QString ogrDriverName;
77
+ if ( driverName == " MapInfo MIF" )
78
+ {
79
+ ogrDriverName = " MapInfo File" ;
80
+ }
81
+ else if ( driverName == " SpatiaLite" )
82
+ {
83
+ ogrDriverName = " SQLite" ;
84
+ if ( !dsOptions.contains ( " SPATIALITE=YES" ) )
85
+ {
86
+ dsOptions.append ( " SPATIALITE=YES" );
87
+ }
88
+ }
89
+ else
90
+ {
91
+ ogrDriverName = driverName;
92
+ }
93
+
74
94
// find driver in OGR
75
95
OGRSFDriverH poDriver;
76
96
QgsApplication::registerOgrDrivers ();
77
- poDriver = OGRGetDriverByName ( driverName .toLocal8Bit ().data () );
97
+ poDriver = OGRGetDriverByName ( ogrDriverName .toLocal8Bit ().data () );
78
98
79
99
if ( poDriver == NULL )
80
100
{
@@ -157,22 +177,22 @@ QgsVectorFileWriter::QgsVectorFileWriter(
157
177
}
158
178
159
179
char **options = NULL ;
160
- if ( !datasourceOptions .isEmpty () )
180
+ if ( !dsOptions .isEmpty () )
161
181
{
162
- options = new char *[ datasourceOptions .size ()+1 ];
163
- for ( int i = 0 ; i < datasourceOptions .size (); i++ )
182
+ options = new char *[ dsOptions .size ()+1 ];
183
+ for ( int i = 0 ; i < dsOptions .size (); i++ )
164
184
{
165
- options[i] = CPLStrdup ( datasourceOptions [i].toLocal8Bit ().data () );
185
+ options[i] = CPLStrdup ( dsOptions [i].toLocal8Bit ().data () );
166
186
}
167
- options[ datasourceOptions .size ()] = NULL ;
187
+ options[ dsOptions .size ()] = NULL ;
168
188
}
169
189
170
190
// create the data source
171
191
mDS = OGR_Dr_CreateDataSource ( poDriver, TO8 ( vectorFileName ), options );
172
192
173
193
if ( options )
174
194
{
175
- for ( int i = 0 ; i < datasourceOptions .size (); i++ )
195
+ for ( int i = 0 ; i < dsOptions .size (); i++ )
176
196
CPLFree ( options[i] );
177
197
delete [] options;
178
198
options = NULL ;
@@ -603,6 +623,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
603
623
bool skipAttributeCreation,
604
624
QString *newFilename )
605
625
{
626
+ QgsDebugMsg ( " fileName = " + fileName );
606
627
const QgsCoordinateReferenceSystem* outputCRS;
607
628
QgsCoordinateTransform* ct = 0 ;
608
629
int shallTransform = false ;
@@ -626,6 +647,8 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
626
647
QgsVectorFileWriter* writer =
627
648
new QgsVectorFileWriter ( fileName, fileEncoding, skipAttributeCreation ? QgsFieldMap () : layer->pendingFields (), layer->wkbType (), outputCRS, driverName, datasourceOptions, layerOptions, newFilename );
628
649
650
+ QgsDebugMsg ( " newFilename = " + *newFilename );
651
+
629
652
// check whether file creation was successful
630
653
WriterError err = writer->hasError ();
631
654
if ( err != NoError )
@@ -797,6 +820,7 @@ QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
797
820
QgsApplication::registerOgrDrivers ();
798
821
int const drvCount = OGRGetDriverCount ();
799
822
823
+ QStringList writableDrivers;
800
824
for ( int i = 0 ; i < drvCount; ++i )
801
825
{
802
826
OGRSFDriverH drv = OGRGetDriver ( i );
@@ -805,18 +829,54 @@ QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
805
829
QString drvName = OGR_Dr_GetName ( drv );
806
830
if ( OGR_Dr_TestCapability ( drv, " CreateDataSource" ) != 0 )
807
831
{
808
- QString longName;
809
- QString trLongName;
810
- QString glob;
811
- QString exts;
812
- if ( QgsVectorFileWriter::driverMetadata ( drvName, longName, trLongName, glob, exts ) && !trLongName.isEmpty () )
832
+ // Add separate format for Mapinfo MIF (MITAB is OGR default)
833
+ if ( drvName == " MapInfo File" )
813
834
{
814
- resultMap. insert ( trLongName, drvName ) ;
835
+ writableDrivers << " MapInfo MIF " ;
815
836
}
837
+ else if ( drvName == " SQLite" )
838
+ {
839
+ // Unfortunately it seems that there is no simple way to detect if
840
+ // OGR SQLite driver is compiled with SpatiaLite support.
841
+ // We have HAVE_SPATIALITE in QGIS, but that may differ from OGR
842
+ // http://lists.osgeo.org/pipermail/gdal-dev/2012-November/034580.html
843
+ // -> test if creation failes
844
+ QString option = " SPATIALITE=YES" ;
845
+ char **options = new char *[2 ];
846
+ options[0 ] = CPLStrdup ( option.toLocal8Bit ().data () );
847
+ options[1 ] = NULL ;
848
+ OGRSFDriverH poDriver;
849
+ QgsApplication::registerOgrDrivers ();
850
+ poDriver = OGRGetDriverByName ( drvName.toLocal8Bit ().data () );
851
+ if ( poDriver )
852
+ {
853
+ OGRDataSourceH ds = OGR_Dr_CreateDataSource ( poDriver, TO8 ( QString ( " /vsimem/spatialitetest.sqlite" ) ), options );
854
+ if ( ds )
855
+ {
856
+ writableDrivers << " SpatiaLite" ;
857
+ OGR_DS_Destroy ( ds );
858
+ }
859
+ }
860
+ CPLFree ( options[0 ] );
861
+ delete [] options;
862
+ }
863
+ writableDrivers << drvName;
816
864
}
817
865
}
818
866
}
819
867
868
+ foreach ( QString drvName, writableDrivers )
869
+ {
870
+ QString longName;
871
+ QString trLongName;
872
+ QString glob;
873
+ QString exts;
874
+ if ( QgsVectorFileWriter::driverMetadata ( drvName, longName, trLongName, glob, exts ) && !trLongName.isEmpty () )
875
+ {
876
+ resultMap.insert ( trLongName, drvName );
877
+ }
878
+ }
879
+
820
880
return resultMap;
821
881
}
822
882
@@ -942,10 +1002,18 @@ bool QgsVectorFileWriter::driverMetadata( QString driverName, QString &longName,
942
1002
}
943
1003
else if ( driverName.startsWith ( " MapInfo File" ) )
944
1004
{
945
- longName = " Mapinfo File" ;
946
- trLongName = QObject::tr ( " Mapinfo File" );
947
- glob = " *.mif *.tab" ;
948
- ext = " mif tab" ;
1005
+ longName = " Mapinfo TAB" ;
1006
+ trLongName = QObject::tr ( " Mapinfo TAB" );
1007
+ glob = " *.tab" ;
1008
+ ext = " tab" ;
1009
+ }
1010
+ // 'MapInfo MIF' is internal QGIS addition to distinguish between MITAB and MIF
1011
+ else if ( driverName.startsWith ( " MapInfo MIF" ) )
1012
+ {
1013
+ longName = " Mapinfo MIF" ;
1014
+ trLongName = QObject::tr ( " Mapinfo MIF" );
1015
+ glob = " *.mif" ;
1016
+ ext = " mif" ;
949
1017
}
950
1018
else if ( driverName.startsWith ( " DGN" ) )
951
1019
{
@@ -975,6 +1043,14 @@ bool QgsVectorFileWriter::driverMetadata( QString driverName, QString &longName,
975
1043
glob = " *.sqlite" ;
976
1044
ext = " sqlite" ;
977
1045
}
1046
+ // QGIS internal addition for SpatialLite
1047
+ else if ( driverName.startsWith ( " SpatiaLite" ) )
1048
+ {
1049
+ longName = " SpatiaLite" ;
1050
+ trLongName = QObject::tr ( " SpatiaLite" );
1051
+ glob = " *.sqlite" ;
1052
+ ext = " sqlite" ;
1053
+ }
978
1054
else if ( driverName.startsWith ( " DXF" ) )
979
1055
{
980
1056
longName = " AutoCAD DXF" ;
0 commit comments