@@ -62,11 +62,52 @@ QgsVectorLayerExporter::QgsVectorLayerExporter( const QString &uri,
62
62
{
63
63
mProvider = nullptr ;
64
64
65
+ QMap<QString, QVariant> modifiedOptions ( options );
66
+
67
+ // For GPKG/Spatialite, we explicitly ask not to create a spatial index at
68
+ // layer creation since this would slow down inserts. Defer its creation
69
+ // to end of exportLayer() or destruction of this object.
70
+ if ( geometryType != QgsWkbTypes::NoGeometry &&
71
+ providerKey == QLatin1String ( " ogr" ) &&
72
+ options.contains ( QStringLiteral ( " driverName" ) ) &&
73
+ ( options[ QStringLiteral ( " driverName" ) ].toString ().compare ( QLatin1String ( " GPKG" ), Qt::CaseInsensitive ) == 0 ||
74
+ options[ QStringLiteral ( " driverName" ) ].toString ().compare ( QLatin1String ( " SQLite" ), Qt::CaseInsensitive ) == 0 ) )
75
+ {
76
+ QStringList modifiedLayerOptions;
77
+ if ( options.contains ( QStringLiteral ( " layerOptions" ) ) )
78
+ {
79
+ QStringList layerOptions = options.value ( QStringLiteral ( " layerOptions" ) ).toStringList ();
80
+ for ( const QString &layerOption : layerOptions )
81
+ {
82
+ if ( layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=YES" ), Qt::CaseInsensitive ) == 0 ||
83
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=ON" ), Qt::CaseInsensitive ) == 0 ||
84
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=TRUE" ), Qt::CaseInsensitive ) == 0 ||
85
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=1" ), Qt::CaseInsensitive ) == 0 )
86
+ {
87
+ // do nothing
88
+ }
89
+ else if ( layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=NO" ), Qt::CaseInsensitive ) == 0 ||
90
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=OFF" ), Qt::CaseInsensitive ) == 0 ||
91
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=FALSE" ), Qt::CaseInsensitive ) == 0 ||
92
+ layerOption.compare ( QLatin1String ( " SPATIAL_INDEX=0" ), Qt::CaseInsensitive ) == 0 )
93
+ {
94
+ mCreateSpatialIndex = false ;
95
+ }
96
+ else
97
+ {
98
+ modifiedLayerOptions << layerOption;
99
+ }
100
+ }
101
+ }
102
+ modifiedLayerOptions << QStringLiteral ( " SPATIAL_INDEX=FALSE" );
103
+ modifiedOptions[ QStringLiteral ( " layerOptions" ) ] = modifiedLayerOptions;
104
+ }
105
+
65
106
// create an empty layer
66
107
QString errMsg;
67
108
QgsProviderRegistry *pReg = QgsProviderRegistry::instance ();
68
109
mError = pReg->createEmptyLayer ( providerKey, uri, fields, geometryType, crs, overwrite, mOldToNewAttrIdx ,
69
- errMsg, !options .isEmpty () ? &options : nullptr );
110
+ errMsg, !modifiedOptions .isEmpty () ? &modifiedOptions : nullptr );
70
111
if ( errorCode () )
71
112
{
72
113
mErrorMessage = errMsg;
@@ -132,6 +173,12 @@ QgsVectorLayerExporter::QgsVectorLayerExporter( const QString &uri,
132
173
QgsVectorLayerExporter::~QgsVectorLayerExporter ()
133
174
{
134
175
flushBuffer ();
176
+
177
+ if ( mCreateSpatialIndex )
178
+ {
179
+ createSpatialIndex ();
180
+ }
181
+
135
182
delete mProvider ;
136
183
}
137
184
@@ -222,6 +269,7 @@ bool QgsVectorLayerExporter::flushBuffer()
222
269
223
270
bool QgsVectorLayerExporter::createSpatialIndex ()
224
271
{
272
+ mCreateSpatialIndex = false ;
225
273
if ( mProvider && ( mProvider ->capabilities () & QgsVectorDataProvider::CreateSpatialIndex ) != 0 )
226
274
{
227
275
return mProvider ->createSpatialIndex ();
@@ -433,7 +481,7 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
433
481
}
434
482
int errors = writer->errorCount ();
435
483
436
- if ( !writer->createSpatialIndex () )
484
+ if ( writer-> mCreateSpatialIndex && !writer->createSpatialIndex () )
437
485
{
438
486
if ( writer->errorCode () && errorMessage )
439
487
{
0 commit comments