@@ -271,7 +271,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL )
271
271
272
272
// getting the total number of features in the layer
273
273
// TODO: This can be expensive, do we really need it!
274
- featuresCounted = OGR_L_GetFeatureCount ( ogrLayer, TRUE );
274
+ recalculateFeatureCount ( );
275
275
276
276
// get the extent_ (envelope) of the layer
277
277
QgsDebugMsg ( " Starting get extent" );
@@ -771,7 +771,6 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
771
771
{
772
772
f.setFeatureId ( OGR_F_GetFID ( feature ) );
773
773
}
774
- ++featuresCounted;
775
774
OGR_F_Destroy ( feature );
776
775
return returnValue;
777
776
}
@@ -792,7 +791,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
792
791
{
793
792
returnvalue = false ;
794
793
}
795
- featuresCounted = OGR_L_GetFeatureCount ( ogrLayer, TRUE ); // new feature count
794
+ recalculateFeatureCount ();
796
795
797
796
return returnvalue;
798
797
}
@@ -997,7 +996,8 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
997
996
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
998
997
QgsDebugMsg ( QString ( " SQL: %1" ).arg ( sql ) );
999
998
OGR_DS_ExecuteSQL ( ogrDataSource, mEncoding ->fromUnicode ( sql ).data (), NULL , NULL );
1000
- featuresCounted = OGR_L_GetFeatureCount ( ogrLayer, TRUE ); // new feature count
999
+
1000
+ recalculateFeatureCount ();
1001
1001
1002
1002
OGR_L_GetExtent ( ogrOrigLayer, ( OGREnvelope * ) extent_, TRUE );
1003
1003
@@ -1860,3 +1860,22 @@ bool QgsOgrProvider::syncToDisc()
1860
1860
1861
1861
return true ;
1862
1862
}
1863
+
1864
+ void QgsOgrProvider::recalculateFeatureCount ()
1865
+ {
1866
+ OGRGeometryH filter = OGR_L_GetSpatialFilter ( ogrLayer );
1867
+ if ( filter )
1868
+ {
1869
+ filter = OGR_G_Clone ( filter );
1870
+ OGR_L_SetSpatialFilter ( ogrLayer, 0 );
1871
+ }
1872
+
1873
+ // feature count returns number of features within current spatial filter
1874
+ // so we remove it if there's any and then put it back
1875
+ featuresCounted = OGR_L_GetFeatureCount ( ogrLayer, TRUE );
1876
+
1877
+ if ( filter )
1878
+ {
1879
+ OGR_L_SetSpatialFilter ( ogrLayer, filter );
1880
+ }
1881
+ }
0 commit comments