[gdal-dev] Can't delete feature from shapefile in python

Even Rouault even.rouault at mines-paris.org
Sun Sep 16 05:40:57 PDT 2012


Le samedi 08 septembre 2012 05:06:41, Hugo a écrit :
> Hello all,
> 
> I'm trying to delete a number of records from shapefile based on an
> attribute. The strange thing that is happening is that feature count never
> changes but if i make the same filter a second time the features are no
> longer there. The code i'm using is below. I should be doing something
> wrong for sure.

I don't think you are doing something wrong. This is just a limitation of the 
implementation of GetFeatureCount() in the shapefile driver. When there are no 
attribute or spatial filter set, it returns the total number of records in the 
DBF file. When you delete a feature, it marks the corresponding record as 
invalid, but doesn't actually remove it from the file, so the total number of 
records remains unchanged. You can "repack" the .shp, .shx and .dbf files by 
running shp.ExecuteSQL("REPACK locais") (see 
http://gdal.org/ogr/drv_shapefile.html)


More information about the gdal-dev mailing list