[QGIS Commit] r13345 - trunk/qgis/src/providers/ogr

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Apr 22 10:50:11 EDT 2010


Author: wonder
Date: 2010-04-22 10:50:10 -0400 (Thu, 22 Apr 2010)
New Revision: 13345

Modified:
   trunk/qgis/src/providers/ogr/qgsogrprovider.cpp
   trunk/qgis/src/providers/ogr/qgsogrprovider.h
Log:
Fix calculation of feature count in OGR provider (FeatureCount function works with current spatial filter)


Modified: trunk/qgis/src/providers/ogr/qgsogrprovider.cpp
===================================================================
--- trunk/qgis/src/providers/ogr/qgsogrprovider.cpp	2010-04-22 07:37:01 UTC (rev 13344)
+++ trunk/qgis/src/providers/ogr/qgsogrprovider.cpp	2010-04-22 14:50:10 UTC (rev 13345)
@@ -271,7 +271,7 @@
 
   // 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" );
@@ -771,7 +771,6 @@
   {
     f.setFeatureId( OGR_F_GetFID( feature ) );
   }
-  ++featuresCounted;
   OGR_F_Destroy( feature );
   return returnValue;
 }
@@ -792,7 +791,7 @@
   {
     returnvalue = false;
   }
-  featuresCounted = OGR_L_GetFeatureCount( ogrLayer, TRUE ); //new feature count
+  recalculateFeatureCount();
 
   return returnvalue;
 }
@@ -997,8 +996,9 @@
   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 );
 
   return returnvalue;
@@ -1860,3 +1860,22 @@
 
   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 );
+  }
+}

Modified: trunk/qgis/src/providers/ogr/qgsogrprovider.h
===================================================================
--- trunk/qgis/src/providers/ogr/qgsogrprovider.h	2010-04-22 07:37:01 UTC (rev 13344)
+++ trunk/qgis/src/providers/ogr/qgsogrprovider.h	2010-04-22 14:50:10 UTC (rev 13345)
@@ -245,8 +245,9 @@
     /**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 );
     QgsFieldMap mAttributeFields;



More information about the QGIS-commit mailing list