Skip to content

Commit 0e3045f

Browse files
author
wonder
committed
Fix calculation of feature count in OGR provider (FeatureCount function works with current spatial filter)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13345 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 6bb9aa6 commit 0e3045f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/providers/ogr/qgsogrprovider.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL )
271271

272272
// getting the total number of features in the layer
273273
// TODO: This can be expensive, do we really need it!
274-
featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE );
274+
recalculateFeatureCount();
275275

276276
// get the extent_ (envelope) of the layer
277277
QgsDebugMsg( "Starting get extent" );
@@ -771,7 +771,6 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
771771
{
772772
f.setFeatureId( OGR_F_GetFID( feature ) );
773773
}
774-
++featuresCounted;
775774
OGR_F_Destroy( feature );
776775
return returnValue;
777776
}
@@ -792,7 +791,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
792791
{
793792
returnvalue = false;
794793
}
795-
featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE ); //new feature count
794+
recalculateFeatureCount();
796795

797796
return returnvalue;
798797
}
@@ -997,7 +996,8 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
997996
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
998997
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
999998
OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, NULL );
1000-
featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE ); //new feature count
999+
1000+
recalculateFeatureCount();
10011001

10021002
OGR_L_GetExtent( ogrOrigLayer, ( OGREnvelope * ) extent_, TRUE );
10031003

@@ -1860,3 +1860,22 @@ bool QgsOgrProvider::syncToDisc()
18601860

18611861
return true;
18621862
}
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+
}

src/providers/ogr/qgsogrprovider.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
245245
/**Get an attribute associated with a feature*/
246246
void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );
247247

248-
248+
/** find out the number of features of the whole layer */
249+
void recalculateFeatureCount();
249250

250251
private:
251252
unsigned char *getGeometryPointer( OGRFeatureH fet );

0 commit comments

Comments
 (0)