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])
Ian Turton
  • 81,417
  • 6
  • 84
  • 185
stefan
  • 61
  • 5
  • 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 – matt wilkie Jun 03 '13 at 20:03
  • how would i go about running the module directly' – stefan Jun 03 '13 at 20:29
  • I am having trouble finding any documentation or examples of ogr's clip method – stefan Jun 03 '13 at 20:49
  • computer spits out --> ‘ogr2ogr’ is not recognized as an internal or external command, operable program or batch file. – stefan Jun 03 '13 at 20:58

1 Answers1

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)
ustroetz
  • 7,994
  • 10
  • 72
  • 118
  • i tried this and got "WindowsError: [Error 2] The system cannot find the file specified" – stefan Jun 03 '13 at 17:15
  • Here is the error : – stefan Jun 03 '13 at 17:25
  • Traceback (most recent call last): File "C:\Users\Stefan\Desktop\CLIPPING\clip_shp_to_shp.py", line 26, in clip_shp_to_shp(directory, shpclip) File "C:\Users\Stefan\Desktop\CLIPPING\clip_shp_to_shp.py", – stefan Jun 03 '13 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 03 '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 03 '13 at 17:40
  • do I have to make the out put shp file ahead of time or will it do it for me? Sorry I am used to arcpy. – stefan Jun 03 '13 at 17:54
  • I am getting the same error as well – stefan Jun 03 '13 at 17:54
  • It will create it for you. You just have to enter a name in the code (like here "output1.shp"). – ustroetz Jun 03 '13 at 17:55
  • Can you post the error again. It should be different than the previous one. – ustroetz Jun 03 '13 at 17:56
  • thats what I thought. I continue to get that "windows cannot find file" error – stefan Jun 03 '13 at 17:59
  • Did you save the Python file in the same folder as your shapefiles? – ustroetz Jun 03 '13 at 18:00
  • clipping_shp = "C:\Users\Stefan\Desktop\CLIPPING\US_States_STATE_NAME__Oregon.shp" output_shp = "output1.shp" input_shp = "C:\Users\Stefan\Desktop\CLIPPING\broadcast_national_ALL.shp"

    Clipping process

    subprocess.call(["ogr2ogr", "-f", "ESRI Shapefile", "-clipsrc", clipping_shp, output_shp, input_shp])

    – stefan Jun 03 '13 at 18:01
  • yes and I just wrote the path directly in there – stefan Jun 03 '13 at 18:01
  • I edited the code again with your shapefile names. If you use exactly the code above and save it into your CLIPPING folder it should work. If not than I don't know either. – ustroetz Jun 03 '13 at 18:06
  • I'm thinking its an issue with the subprocess.call itself. What version of python do you have? – stefan Jun 03 '13 at 18:24
  • What does it do it you add shell=True to the subprocess.call (see edited code)? – ustroetz Jun 03 '13 at 18:40
  • OOOOooo No error message...but no saved file – stefan Jun 03 '13 at 18:45
  • Sorry than i don't know. – ustroetz Jun 03 '13 at 19:13
  • oh Don't apologize. You helped me get pretty far! Thanks man! – stefan Jun 03 '13 at 19:15
  • I figured it out! the code works great it was just a user error lol. Thanks! – stefan Jun 03 '13 at 21:32
  • @stefan - I am having the same issue, what exactly was your user error? pls see http://gis.stackexchange.com/questions/71915/clip-and-copy-files-to-same-directory-structure-using-ogr-gdal-with-python – GeorgeC Sep 19 '13 at 12:42