Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix calculation of feature count in OGR provider (FeatureCount functi…
…on works with current spatial filter)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13345 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Apr 22, 2010
1 parent 6bb9aa6 commit 0e3045f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -271,7 +271,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL )

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

// get the extent_ (envelope) of the layer
QgsDebugMsg( "Starting get extent" );
Expand Down Expand Up @@ -771,7 +771,6 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
{
f.setFeatureId( OGR_F_GetFID( feature ) );
}
++featuresCounted;
OGR_F_Destroy( feature );
return returnValue;
}
Expand All @@ -792,7 +791,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
{
returnvalue = false;
}
featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE ); //new feature count
recalculateFeatureCount();

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

recalculateFeatureCount();

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

Expand Down Expand Up @@ -1860,3 +1860,22 @@ bool QgsOgrProvider::syncToDisc()

return true;
}

void QgsOgrProvider::recalculateFeatureCount()
{
OGRGeometryH filter = OGR_L_GetSpatialFilter( ogrLayer );
if ( filter )
{
filter = OGR_G_Clone( filter );
OGR_L_SetSpatialFilter( ogrLayer, 0 );
}

// feature count returns number of features within current spatial filter
// so we remove it if there's any and then put it back
featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE );

if ( filter )
{
OGR_L_SetSpatialFilter( ogrLayer, filter );
}
}
3 changes: 2 additions & 1 deletion src/providers/ogr/qgsogrprovider.h
Expand Up @@ -245,7 +245,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
/**Get an attribute associated with a feature*/
void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );


/** find out the number of features of the whole layer */
void recalculateFeatureCount();

private:
unsigned char *getGeometryPointer( OGRFeatureH fet );
Expand Down

0 comments on commit 0e3045f

Please sign in to comment.