I have a list of feature ids and I am using layer.SetAttributeFilter for that but the code neither selects those features nor does it raise an error. I took help from these questions:
OGR Filter Vector Layer by a List of Feature ID's
OGR: How to save layer from AttributeFilter to a shapefile?
Here is my code:
inlyr=inds.GetLayer()
m =[]
for a in range (0, len(new_coords1)):
m.append(new_coords1[a])
inlyr.SetAttributeFilter("POINTID IN {}".format(tuple(m)))
drv = ogr.GetDriverByName( 'ESRI Shapefile' )
outds = drv.CreateDataSource( "test2.shp" )
outlyr = outds.CopyLayer(inlyr,'test2')
del inlyr,inds,outlyr,outds
where POINTID is the name of the id field. The output creates a new shapefile with all the features and not the selected features. I also tried executing the SetAttributeFilter separately which gives an output of 5. I don't know what that output means.
m = tuple(m) str_join = ",".join(str(x) for x in m) inlyr.SetAttributeFilter("POINTID IN {}".format(str_join))
– rao Feb 6 at 6:49