I have a shape with some features (let's say 100) and a second shape with some of this features (50) buffered (5 meters inside buffer). I want to select and write in a new shape every feature of the first shape which doesn't contain the buffered feature of the second shape. I tried two different approaches with no success:

1) Select by location. Select the features of shape 1 which doesn't intersect with shape2 (ST_difference):

ogr2ogr diff.shp parcels1.shp -dialect SQLite -sql "select st_difference(a.geometry, b.geometry) as geometry from parcels1 as a, """parcels1_buffer5m.shp""".parcels1_buffer5m as b"

No error, but every feature is selected.

2) Select ID (in my case DN_OID) which is not included in the list of DN_OID of second shape:

import fiona 
with fiona.open("parcels1.shp") as input: 
    meta = input.meta
    with fiona.open('diff.shp', 'w',**meta) as output:
        for feature in input:
             if feature['properties']['DN_OID'] not in ("SELECT DN_OID FROM parcels1_buffer5m.geom"):

Any advice is welcomed.

  • why is your second approach not working? can one buffered polygon fall inside more than the original one? – ThingumaBob Jun 17 at 20:09
  • An error appears in this line: if feature['properties']['DN_OID'] not in ("SELECT DN_OID FROM parcels1_buffer5m.geom"): TypeError: 'in <string>' requires string as left operand, not float – Ion Sola Jun 18 at 14:55
  • naturally, that query is nothing but a string here. but I was really trying to figure out if a filter on DN_OID is all that is needed, since you seem to try to select all parcels that don't have the same id as the buffered? or is a spatial filter necessary (e.g. when a parcel can contain multiple buffered parcels?) – ThingumaBob Jun 18 at 21:15

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.