5

I run the following code to first select specific attributes using a query and then I need to delete them

from osgeo import gdal, ogr
import os

##shapefile = "D:/New.shp"
##driver = ogr.GetDriverByName("ESRI Shapefile")
##dataSource = driver.Open(shapefile, 0)

shapefile = ogr.Open( "D:/New.shp")

layer = shapefile.GetLayer()
layer.SetAttributeFilter("Area < 5000")

for i in layer:
    print i.GetField("Area")
    layer.DeleteFeature(i)

I get the following error :

layer.DeleteFeature(i)   File
"C:\Python27\ArcGIS10.2\lib\site-packages\osgeo\ogr.py", line 1499, in
DeleteFeature
return _ogr.Layer_DeleteFeature(self, *args) TypeError: not an integer

I changed the last line of my code to ogr.Layer_DeleteFeature(i) to which i get the error

ogr.layer_DeleteFeature(i)
AttributeError: 'module' object has no attribute 'layer_DeleteFeature'

3 Answers 3

7

You're passing a Feature object to layer.DeleteFeature which is expecting an integer value (a feature ID or "FID"), not a Feature object.

Try passing the FID instead:

for feat in layer:
    print feat.GetField("Area")
    layer.DeleteFeature(feat.GetFID())

Note that the OGR layer.DeleteFeature(fid) method doesn't actually delete features, it just marks them as deleted in the .dbf then ignores them. This is mentioned in the shapefile driver doc:

Deleted shapes are marked for deletion in the .dbf file, and then ignored by OGR. To actually remove them permanently (resulting in renumbering of FIDs) invoke the SQL 'REPACK ' via the datasource ExecuteSQL() method.

For more info, see GetFeatureCount gives same result after deleting a feature.

7
  • this runs the code without any error and prints the selected features, but i want it to edit my shapefile which it doesnt Commented Sep 6, 2017 at 11:51
  • 1
    @Basil gis.stackexchange.com/a/220236/2856
    – user2856
    Commented Sep 6, 2017 at 11:59
  • It doesn't work for me, I mean, it doesn't delete the features I previously selected
    – ilFonta
    Commented Dec 6, 2019 at 6:55
  • 1
    @ilFonta have you repacked your dataset? gis.stackexchange.com/a/220236/2856
    – user2856
    Commented Dec 6, 2019 at 7:05
  • I did it. I tried any possible combination. Like this layerVec.SetAttributeFilter("pol = '0'") layerVec.ResetReading() for i in range(0, layerVec.GetFeatureCount()): layerVec.ResetReading() feature = layerVec.GetNextFeature() layerVec.DeleteFeature(feature.GetFID()) fileVec.ExecuteSQL('REPACK ' + layerVec.GetName()) fileVec.ExecuteSQL('RECOMPUTE EXTENT ON ' + layerVec2.GetName())
    – ilFonta
    Commented Dec 6, 2019 at 7:26
2

It works using that code:

shapefile = ogr.Open(file.shp, 1)
layer = shapefile.GetLayer()
layer.SetAttributeFilter("Area < 5000")

for feat in layer:
    print feat.GetField("Area")
    layer.DeleteFeature(feat.GetFID())
    shapefile.ExecuteSQL('REPACK ' + layer.GetName())
0

This worked for me, I'm not sure I properly used all the instructions in the correct way

fileVec = ogr.Open(pathMaskVec, True)
layerVec = fileVec.GetLayer()

layerVec.SetAttributeFilter("pol = '0'")

for i in range(0, layerVec.GetFeatureCount()):
    layerVec.ResetReading()
    feature = layerVec.GetNextFeature()
    layerVec.DeleteFeature(feature.GetFID())
    fileVec.ExecuteSQL('REPACK ' + layerVec.GetName())
    fileVec.ExecuteSQL('RECOMPUTE EXTENT ON ' + layerVec.GetName())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.