[gdal-dev] OGR, GetFeatureCount after feature removal in Shape driver

Even Rouault even.rouault at mines-paris.org
Thu Jun 2 06:02:47 EDT 2011


Rui,

> Hello everyone,
> 
> the ESRI shape driver allows feature removal for layers with update
> access OGRLayer::DeleteFeature.
> 
> After successful removal of a feature and sync to disk, the method
> OGRLayer::GetFeatureCount still returns the same number, as before the
> deletion.
> 
> Actually, the number of records is returned, as opposed to the number
> of valid features. This is confirmed when reopening the shapefile and
> verifying that GetFeatureCount still returns the original number.

Yes, the GetFeatureCount() implementation of OGRShapeLayer returns the number 
of records, both the valid and invalid ones. To get the number of features (= 
valid records), it would have to iterate over each record, which is a 
potentially lengthy operation.

> 
> I realise that OGRShapeLayer::Repack may do the job of removing empty
> records and thus reset the feature count.

Yes that it's aim.

> However this class is not
> available in the distributed headers.
> 
> What would you propose to circumvent this issue? Is there a way to get
> an accurate feature count without having to iterate through all the
> valid features?

Indeed, you cannot directly call OGRShapeLayer::Repack(), but you can run it 
by issuing a ds->ExecuteSQL("REPACK the_layer_name"), as suggested by 
http://www.gdal.org/ogr/drv_shapefile.html

Best regards,

Even


More information about the gdal-dev mailing list