0

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.

  • I would try to convert the tuple(m) to a comma separated string, for SetAttributeFilter. If Pointid is a string field, you probably need to quote the values. – klewis Feb 5 at 19:06
  • i tried that but the output remains the same @klewis. The code was this: 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
  • Also the POINTID is an int field.So didn't quote the values. – rao Feb 6 at 6:56
  • I think IN operator needs its own parenthesis, "POINTID IN ({})", like this POINTID IN (1,2,3) – klewis Feb 6 at 16:31
  • I tried that as well .. but there is no change in the output.@klewis. – rao Feb 23 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.