@@ -166,49 +166,65 @@ bool QgsWFSSharedData::createCache()
166
166
cacheFields.append ( QgsField ( QgsWFSConstants::FIELD_HEXWKB_GEOM, QVariant::String, " string" ) );
167
167
168
168
bool ogrWaySuccessfull = false ;
169
+ QString geometryFieldname ( " __spatialite_geometry" );
169
170
#ifdef USE_OGR_FOR_DB_CREATION
170
- // Creating a spatialite database can be quite slow on some file systems
171
- // so we create a GDAL in-memory file, and then copy it on
172
- // the file system.
173
- QString vsimemFilename;
174
- QStringList datasourceOptions;
175
- datasourceOptions.push_back ( " INIT_WITH_EPSG=NO" );
176
- vsimemFilename.sprintf ( " /vsimem/qgis_wfs_cache_template_%p/features.sqlite" , this );
177
- mCacheTablename = CPLGetBasename ( vsimemFilename.toStdString ().c_str () );
178
- VSIUnlink ( vsimemFilename.toStdString ().c_str () );
179
- QgsVectorFileWriter* writer = new QgsVectorFileWriter ( vsimemFilename, " " ,
180
- cacheFields, QGis::WKBPolygon, nullptr , " SpatiaLite" , datasourceOptions );
181
- if ( writer->hasError () == QgsVectorFileWriter::NoError )
171
+ // Only GDAL >= 2.0 can use an alternate geometry field name
172
+ #if GDAL_VERSION_MAJOR < 2
173
+ const bool hasGeometryField = cacheFields.fieldNameIndex ( " geometry" ) >= 0 ;
174
+ if ( !hasGeometryField )
175
+ #endif
182
176
{
183
- delete writer;
184
-
185
- // Copy the temporary database back to disk
186
- vsi_l_offset nLength = 0 ;
187
- GByte* pabyData = VSIGetMemFileBuffer ( vsimemFilename.toStdString ().c_str (), &nLength, TRUE );
188
- VSILFILE* fp = VSIFOpenL ( mCacheDbname .toStdString ().c_str (), " wb " );
189
- if ( fp != nullptr )
177
+ #if GDAL_VERSION_MAJOR < 2
178
+ geometryFieldname = " GEOMETRY" ;
179
+ #endif
180
+ // Creating a spatialite database can be quite slow on some file systems
181
+ // so we create a GDAL in-memory file, and then copy it on
182
+ // the file system.
183
+ QString vsimemFilename;
184
+ QStringList datasourceOptions;
185
+ QStringList layerOptions;
186
+ datasourceOptions.push_back ( " INIT_WITH_EPSG=NO" );
187
+ layerOptions.push_back ( " LAUNDER=NO" ); // to get exact matches for field names, especially regarding case
188
+ #if GDAL_VERSION_MAJOR >= 2
189
+ layerOptions.push_back ( " GEOMETRY_NAME=__spatialite_geometry" );
190
+ #endif
191
+ vsimemFilename.sprintf ( " /vsimem/qgis_wfs_cache_template_%p/features.sqlite" , this );
192
+ mCacheTablename = CPLGetBasename ( vsimemFilename.toStdString ().c_str () );
193
+ VSIUnlink ( vsimemFilename.toStdString ().c_str () );
194
+ QgsVectorFileWriter* writer = new QgsVectorFileWriter ( vsimemFilename, " " ,
195
+ cacheFields, QGis::WKBPolygon, nullptr , " SpatiaLite" , datasourceOptions, layerOptions );
196
+ if ( writer->hasError () == QgsVectorFileWriter::NoError )
190
197
{
191
- VSIFWriteL ( pabyData, 1 , nLength, fp );
192
- VSIFCloseL ( fp );
193
- CPLFree ( pabyData );
198
+ delete writer;
199
+
200
+ // Copy the temporary database back to disk
201
+ vsi_l_offset nLength = 0 ;
202
+ GByte* pabyData = VSIGetMemFileBuffer ( vsimemFilename.toStdString ().c_str (), &nLength, TRUE );
203
+ VSILFILE* fp = VSIFOpenL ( mCacheDbname .toStdString ().c_str (), " wb " );
204
+ if ( fp != nullptr )
205
+ {
206
+ VSIFWriteL ( pabyData, 1 , nLength, fp );
207
+ VSIFCloseL ( fp );
208
+ CPLFree ( pabyData );
209
+ }
210
+ else
211
+ {
212
+ CPLFree ( pabyData );
213
+ QgsMessageLog::logMessage ( tr ( " Cannot create temporary SpatiaLite cache" ), tr ( " WFS" ) );
214
+ return false ;
215
+ }
216
+
217
+ ogrWaySuccessfull = true ;
194
218
}
195
219
else
196
220
{
197
- CPLFree ( pabyData );
198
- QgsMessageLog::logMessage ( tr ( " Cannot create temporary SpatiaLite cache" ), tr ( " WFS" ) );
199
- return false ;
221
+ // Be tolerant on failures. Some (Windows) GDAL >= 1.11 builds may
222
+ // not define SPATIALITE_412_OR_LATER, and thus the call to
223
+ // spatialite_init() may cause failures, which will require using the
224
+ // slower method
225
+ delete writer;
226
+ VSIUnlink ( vsimemFilename.toStdString ().c_str () );
200
227
}
201
-
202
- ogrWaySuccessfull = true ;
203
- }
204
- else
205
- {
206
- // Be tolerant on failures. Some (Windows) GDAL >= 1.11 builds may
207
- // not define SPATIALITE_412_OR_LATER, and thus the call to
208
- // spatialite_init() may cause failures, which will require using the
209
- // slower method
210
- delete writer;
211
- VSIUnlink ( vsimemFilename.toStdString ().c_str () );
212
228
}
213
229
#endif
214
230
if ( !ogrWaySuccessfull )
@@ -300,12 +316,12 @@ bool QgsWFSSharedData::createCache()
300
316
if ( rc != SQLITE_OK )
301
317
ret = false ;
302
318
303
- sql = QString ( " SELECT AddGeometryColumn('%1','geometry ',0,'POLYGON',2)" ).arg ( mCacheTablename );
319
+ sql = QString ( " SELECT AddGeometryColumn('%1','%2 ',0,'POLYGON',2)" ).arg ( mCacheTablename ). arg ( geometryFieldname );
304
320
rc = sqlite3_exec ( db, sql.toUtf8 (), nullptr , nullptr , nullptr );
305
321
if ( rc != SQLITE_OK )
306
322
ret = false ;
307
323
308
- sql = QString ( " SELECT CreateSpatialIndex('%1','geometry ')" ).arg ( mCacheTablename );
324
+ sql = QString ( " SELECT CreateSpatialIndex('%1','%2 ')" ).arg ( mCacheTablename ). arg ( geometryFieldname );
309
325
rc = sqlite3_exec ( db, sql.toUtf8 (), nullptr , nullptr , nullptr );
310
326
if ( rc != SQLITE_OK )
311
327
ret = false ;
@@ -336,7 +352,7 @@ bool QgsWFSSharedData::createCache()
336
352
// regarding crashes, since this is a temporary DB
337
353
QgsDataSourceURI dsURI;
338
354
dsURI.setDatabase ( mCacheDbname );
339
- dsURI.setDataSource ( " " , mCacheTablename , " geometry " , " " , " ogc_fid" );
355
+ dsURI.setDataSource ( " " , mCacheTablename , geometryFieldname , " " , " ogc_fid" );
340
356
QStringList pragmas;
341
357
pragmas << " synchronous=OFF" ;
342
358
pragmas << " journal_mode=MEMORY" ;
0 commit comments