Is there a means in ogr2ogr to "clip out" one shapefile from another?

I know ogr2ogr -clipsrc B.shp out.shp A.shp will remove any part of A.shp that is NOT in B.shp.

Is there an ogr2ogr command to remove any part of A.shp that intersects with B.shp?

I have seen this called "reverse clip", "clip out", "erase" (ArcMap), or "difference".

Note: this question is about ogr2ogr, the command line tool, NOT the OGR library, which requires programming.

  • Is your copy of GDAL compiled with sqlite/spatialite support?
    – SaultDon
    Commented Apr 1, 2016 at 18:57
  • I don't believe so.
    – Tom West
    Commented Apr 2, 2016 at 20:28

3 Answers 3


Try this,

ogr2ogr -f "ESRI Shapefile" out.shp road.shp
-dialect sqlite -sql "
select ST_Difference(road.geometry, seg_buff.geometry)
from road, 'seg_buff.shp'.seg_buff seg_buff

Putting an extra seg_buff at the end makes the query work.

This extra word is needed because ogr2ogr only automatically generates SQL table names for files passed to it directly. When specifying an additional file from within the SQL statement you must supply an arbitrary table name1 to refer to it elsewhere in the SQL statement. In this instance:

 The shapefile
 |             The table within the shapefile
 |             |        The name we're going to use to refer to the
 |             |        table elsewhere in the SQL statement
 |             |        |
 ------------  -------- --------
'seg_buff.shp'.seg_buff seg_buff 

1 The supplied table name doesn't have to match the filename (or its contained table). For example, the following works as well:

ogr2ogr -f "ESRI Shapefile" out.shp road.shp
-dialect sqlite -sql "
select ST_Diffrence(road.geometry, foo.geometry)
from road, 'seg_buff.shp'.seg_buff foo

In either example, out.shp will contain only the parts of road.shp that are not contained within seg_buff.shp.

  • 4
    This answer could be improved by explaining why this might work or by providing citations.
    – Kingfisher
    Commented Aug 21, 2019 at 12:42
  • I submitted github.com/OSGeo/gdal/issues/10266 in hope that users will no longer need to figure this out. I cannot. Commented Jun 22 at 10:31

Using GDAL >= 1.10.0 compiled with SQLite and SpatiaLite:

ogr2ogr difference.shp a.shp -dialect SQLite -sql "SELECT ST_Difference(a.geometry, b.geometry) AS geometry FROM a, 'b.shp'.b"
  • 3
    It doesn't work in my case ogr2ogr -f "ESRI Shapefile" out.shp road.shp -dialect sqlite -sql "select ST_Diffrence(road.geometry, seg_buff.geometry) from road, 'seg_buff.shp'.seg_buff and this gives me no such column: seg_buff.geometry' I do not understand why this gives me that error
    – Pil Kwon
    Commented Oct 14, 2018 at 13:30
  • @PilKwon See my edit to Shadab's answer Commented Nov 18, 2019 at 16:42
  • Any way to edit this to not go off the screen? Commented Jun 22 at 9:48

Try using this command:

ogr2ogr -clipsrc B.shp out.shp A.shp -clipsrclayer B
  • 1
    Could you please provide some extra explanations, citations or other useful information to make this answer better?
    – Piskr
    Commented Aug 21, 2019 at 12:53
  • Apologies, I have misunderstood the question, Commented Aug 25, 2019 at 12:39
  • The answer for the question is to do a "Difference" operation which can be done in QGIS Python API, PostGIS. Also as explained by Shadab. Commented Aug 25, 2019 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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