This is related to the link attached below and in my case, it is not working properly.

Can ogr2ogr "reverse clip" (or "clip out" or "erase" or "difference") one shapefile from another?

From road.shp, I want to erase out where the road.shp intersects with seg.shp

ogr2ogr difference.shp road.shp -dialect sqlite -sql "Select ST_Diffrence(road.geometry, seg_buff.geometry) as geometry from road, 'seg_buff.shp'.seg_buff"

I get this error:

ERROR 1: In ExecuteSQL(): sqlite3_prepare_v2(Select ST_Diffrence(road.geometry, seg_buff.geometry) as geometry from road, "_OGR_1"): no such column: seg_buff.geometry

This is the road.shp screenshot and seg.shp screenshot together

road.shp enter image description here

seg.shp enter image description here

  • 1
    In your SQL you refer to seg_buff.shp and in your screenshots seg.shp - does seg_buff.shp exist?
    – RoperMaps
    Oct 14 '18 at 15:28
  • As @RoperMaps suggested, can you try ogr2ogr difference.shp road.shp -dialect sqlite -sql "Select ST_Diffrence(road.geometry, seg.geometry) as geometry from road, 'seg.shp'.seg". What does it say?
    – Taras
    Oct 14 '18 at 19:42
  • @Taras, RoperMaps, sorry there was a typo. Seg_buff.shp and seg.shp is basically same file. and of course, it gives me a same error message
    – Pil Kwon
    Oct 15 '18 at 1:32

If you play with layers and not with features you can use GDAL ogr layer algebra. See https://trac.osgeo.org/gdal/wiki/rfc39_ogr_layer_algebra and https://trac.osgeo.org/gdal/wiki/LayerAlgebra.

You can find some Python examples from https://trac.osgeo.org/gdal/attachment/wiki/rfc39_ogr_layer_algebra/layer_algebra.2.py.


I edited the script a little bit as below and it worked just fine.

ogr2ogr -overwrite out.shp input.shp -dialect SQLite -sql "select st_difference(a.geometry, b.geometry) as geometry from input as a, 'overlay.shp'.overlay as b"

this, however, is not working with same geometry type (i.e., difference line and polygon did not work with)

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.