3

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**

subprocess.call(["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", clipping_shp, output_shp, input_shp])
4
  • Hi Stefan, Welcome to GIS.se :) 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 StackOverflow.com. 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: {...do stuff..}; else: print 'cant find %s!' % input_shp Jun 3, 2013 at 20:03
  • how would i go about running the module directly'
    – stefan
    Jun 3, 2013 at 20:29
  • I am having trouble finding any documentation or examples of ogr's clip method
    – stefan
    Jun 3, 2013 at 20:49
  • computer spits out --> ‘ogr2ogr’ is not recognized as an internal or external command, operable program or batch file.
    – stefan
    Jun 3, 2013 at 20:58

1 Answer 1

2

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
subprocess.call(["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", clipping_shp, output_shp, input_shp], shell=True)
21
  • i tried this and got "WindowsError: [Error 2] The system cannot find the file specified"
    – stefan
    Jun 3, 2013 at 17:15
  • Here is the error :
    – stefan
    Jun 3, 2013 at 17:25
  • Traceback (most recent call last): File "C:\Users\Stefan\Desktop\CLIPPING\clip_shp_to_shp.py", line 26, in <module> clip_shp_to_shp(directory, shpclip) File "C:\Users\Stefan\Desktop\CLIPPING\clip_shp_to_shp.py",
    – stefan
    Jun 3, 2013 at 17:25
  • line 20, in clip_shp_to_shp subprocess.call(["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", shpclip, os.path.basename(source) + "_clip.shp", source]) File "C:\Python27\lib\subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 711, in init errread, errwrite) File "C:\Python27\lib\subprocess.py", line 948, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
    – stefan
    Jun 3, 2013 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, 2013 at 17:40

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.