Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to the gdal/ogr library for python and I was wondering how to clip a shp file by another shp file. I think I am having trouble with the syntax. Thanks - Stefan

I keep getting a windows error message: "WindowsError: [Error 2] The system cannot find the file specified"

import subprocess

##The features used to clip the input features.**

clipping_shp = "US_States_STATE_NAME__Oregon.shp"

##The feature class to be created.**

output_shp = "output1.shp"  

##The features to be clipped.**

input_shp = "broadcast_national_ALL.shp"

##Clipping process**["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", clipping_shp, output_shp, input_shp])
share|improve this question
Hi Stefan, Welcome to :) Thanks for adding the specific error message. It reveals that your problem is likely not about GIS exactly, but basic python and operating system interaction and thus better suited for That said, using subprocess like this is ill-advised. You'd be better to use the python module directly, one example or at least test if the files exist, if exist input_shp: { stuff..}; else: print 'cant find %s!' % input_shp – matt wilkie Jun 3 '13 at 20:03
how would i go about running the module directly' – stefan Jun 3 '13 at 20:29
I am having trouble finding any documentation or examples of ogr's clip method – stefan Jun 3 '13 at 20:49
computer spits out --> ‘ogr2ogr’ is not recognized as an internal or external command, operable program or batch file. – stefan Jun 3 '13 at 20:58

One possible solution is to call ogr2ogr. It is also possible to use OGR directly in Python but this is a little bit more difficult.

Here the solution if you call ogr2ogr as a subprocess:

import subprocess

# The features used to clip the input features.
clipping_shp = "US_States_STATE_NAME__Oregon.shp"
# The feature class to be created.
output_shp = "output1.shp"  
# The features to be clipped.
input_shp = "broadcast_national_ALL.shp"

# Clipping process["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", clipping_shp, output_shp, input_shp], shell=True)
share|improve this answer
i tried this and got "WindowsError: [Error 2] The system cannot find the file specified" – stefan Jun 3 '13 at 17:15
Here is the error : – stefan Jun 3 '13 at 17:25
Traceback (most recent call last): File "C:\Users\Stefan\Desktop\CLIPPING\", line 26, in <module> clip_shp_to_shp(directory, shpclip) File "C:\Users\Stefan\Desktop\CLIPPING\", – stefan Jun 3 '13 at 17:25
line 20, in clip_shp_to_shp["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", shpclip, os.path.basename(source) + "_clip.shp", source]) File "C:\Python27\lib\", line 524, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\", line 711, in init errread, errwrite) File "C:\Python27\lib\", line 948, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified – stefan Jun 3 '13 at 17:26
I simplified the code a little bit. Can you please give it a second try. You have to save the code either in the directory where your shapefiles are or paste the path into the code. – ustroetz Jun 3 '13 at 17:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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