@@ -209,6 +209,7 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
209
209
, ogrDriver( 0 )
210
210
, valid( false )
211
211
, featuresCounted( -1 )
212
+ , mDeletedFeatures( false )
212
213
{
213
214
QgsCPLErrorHandler handler;
214
215
@@ -1212,15 +1213,16 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
1212
1213
1213
1214
bool QgsOgrProvider::createSpatialIndex ()
1214
1215
{
1216
+ if ( ogrDriverName != " ESRI Shapefile" )
1217
+ return false ;
1218
+
1215
1219
QgsCPLErrorHandler handler;
1216
1220
1217
1221
QString layerName = FROM8 ( OGR_FD_GetName ( OGR_L_GetLayerDefn ( ogrOrigLayer ) ) );
1218
1222
1219
1223
// run REPACK on shape files
1220
- if ( ogrDriverName == " ESRI Shapefile " )
1224
+ if ( mDeletedFeatures )
1221
1225
{
1222
- QString layerName = FROM8 ( OGR_FD_GetName ( OGR_L_GetLayerDefn ( ogrOrigLayer ) ) );
1223
-
1224
1226
QString sql = QString ( " REPACK %1" ).arg ( layerName ); // don't quote the layer name as it works with spaces in the name and won't work if the name is quoted
1225
1227
QgsDebugMsg ( QString ( " SQL: %1" ).arg ( sql ) );
1226
1228
OGR_DS_ExecuteSQL ( ogrDataSource, mEncoding ->fromUnicode ( sql ).constData (), NULL , NULL );
@@ -1233,26 +1235,45 @@ bool QgsOgrProvider::createSpatialIndex()
1233
1235
QgsMessageLog::logMessage ( tr ( " Possible corruption after REPACK detected. %1 still exists. This may point to a permission or locking problem of the original DBF." ).arg ( packedDbf ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
1234
1236
1235
1237
OGR_DS_Destroy ( ogrDataSource );
1238
+ ogrLayer = ogrOrigLayer = 0 ;
1236
1239
1237
1240
ogrDataSource = OGROpen ( TO8F ( mFilePath ), true , NULL );
1238
-
1239
- if ( mLayerName .isNull () )
1241
+ if ( ogrDataSource )
1240
1242
{
1241
- ogrOrigLayer = OGR_DS_GetLayer ( ogrDataSource, mLayerIndex );
1243
+ if ( mLayerName .isNull () )
1244
+ {
1245
+ ogrOrigLayer = OGR_DS_GetLayer ( ogrDataSource, mLayerIndex );
1246
+ }
1247
+ else
1248
+ {
1249
+ ogrOrigLayer = OGR_DS_GetLayerByName ( ogrDataSource, TO8 ( mLayerName ) );
1250
+ }
1251
+
1252
+ if ( !ogrOrigLayer )
1253
+ {
1254
+ QgsMessageLog::logMessage ( tr ( " Original layer could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
1255
+ valid = false ;
1256
+ }
1257
+
1258
+ ogrLayer = ogrOrigLayer;
1242
1259
}
1243
1260
else
1244
1261
{
1245
- ogrOrigLayer = OGR_DS_GetLayerByName ( ogrDataSource, TO8 ( mLayerName ) );
1262
+ QgsMessageLog::logMessage ( tr ( " Original datasource could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
1263
+ valid = false ;
1246
1264
}
1247
-
1248
- ogrLayer = ogrOrigLayer;
1249
1265
}
1250
1266
}
1267
+
1268
+ mDeletedFeatures = false ;
1251
1269
}
1252
1270
1253
- QString sql = QString ( " CREATE SPATIAL INDEX ON %1" ).arg ( quotedIdentifier ( layerName ) ); // quote the layer name so spaces are handled
1254
- QgsDebugMsg ( QString ( " SQL: %1" ).arg ( sql ) );
1255
- OGR_DS_ExecuteSQL ( ogrDataSource, mEncoding ->fromUnicode ( sql ).constData (), OGR_L_GetSpatialFilter ( ogrOrigLayer ), " " );
1271
+ if ( ogrDataSource )
1272
+ {
1273
+ QString sql = QString ( " CREATE SPATIAL INDEX ON %1" ).arg ( quotedIdentifier ( layerName ) ); // quote the layer name so spaces are handled
1274
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( sql ) );
1275
+ OGR_DS_ExecuteSQL ( ogrDataSource, mEncoding ->fromUnicode ( sql ).constData (), OGR_L_GetSpatialFilter ( ogrOrigLayer ), " " );
1276
+ }
1256
1277
1257
1278
QFileInfo fi ( mFilePath ); // to get the base name
1258
1279
// find out, if the .qix file is there
@@ -1281,7 +1302,11 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
1281
1302
bool returnvalue = true ;
1282
1303
for ( QgsFeatureIds::const_iterator it = id.begin (); it != id.end (); ++it )
1283
1304
{
1284
- if ( !deleteFeature ( *it ) )
1305
+ if ( deleteFeature ( *it ) )
1306
+ {
1307
+ mDeletedFeatures = true ;
1308
+ }
1309
+ else
1285
1310
{
1286
1311
returnvalue = false ;
1287
1312
}
0 commit comments