quick question on something that has to be simple, but I'm getting stuck on. I want to use an Attribute Filter and a Spatial Filter simultaneously.

I have two vector files (an overlaying grid and a stream). I want to iteratively select road segments that fall within each grid square and that have a specific attribute value for length.

I've been using -


roads.SetAttributeFilter("Length = 'xxx'")

However, regardless of which filter I use first, applying the second filter resets the process (so I either get the attribute subset or the spatial subset, not both).

  • Can you try roads = roads.SetSpatialFilter(grid_geometry) and then roads = roads.SetAttributeFilter("Length = 'xxx'")? I haven't checked but it is probably making copies rather than modifying roads in-place. – alphabetasoup Mar 1 '16 at 4:37
  • Thanks for that! Unfortunately not a programmer by trade! – Josh K Mar 11 '16 at 6:05

As stated in my comment, you need to do this instead:

roads = roads.SetSpatialFilter(grid_geometry)
roads = roads.SetAttributeFilter("Length = 'xxx'")

GDAL's SetSpatialFilter returns the Layer instance the method is called on, rather than modifying it in-place. Therefore you need to capture the returned value. It may as well be the roads variable overriding roads.

Your Answer

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

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