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

Hugo hfpmartins at gmail.com
Fri Sep 7 20:06:41 PDT 2012


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.

import os
from osgeo import ogr

path = 'C:\\datasets'
os.chdir(path)

shp = ogr.Open('locais.shp',1)

layer = shp.GetLayer(0)
print layer.GetFeatureCount()
layer.SetAttributeFilter('name ilike "nacional%"')

feat = layer.GetNextFeature()
while feat:
    featId = feat.GetFID()
    layer.DeleteFeature(featId)
    feat = layer.GetNextFeature()

shp.Destroy()

Any suggestions? Thanks in advance.
Regards,

-- 
Hugo Martins
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20120908/b7dccac3/attachment.html>


More information about the gdal-dev mailing list