HELP ME PLEASE! Script to clip and ROI using a shape file

363
3
03-24-2014 01:08 PM
SamanthaSmyth
New Contributor
Hi! Please help me! I created a script a few weeks ago which would create an ROI using the clipping geometry tool from a shapefile. The script had originally worked and now will only error out. I cannot for the life of me figure out what went wrong. I'm very new to coding and non of my professors can help me to determine what is happening. Here is a copy of my script:

import arcpy
arcpy.env.workspace= r'D:\rmnp\SamSOrgNDVIData\ND 1989 - 1999\nd1990'
out_raster= r'D:\rmnp\SamSOrgNDVIData\Test1990\\'
rastList= arcpy.ListRasters()
for rast in rastList:
    arcpy.Clip_management(rast, "#", out_raster + rast[:-4] + 'Clipped.tif', 'D:\\Shape File Buffer\\RMNPBuff1km.shp', '#', "ClippingGeometry")


Every time I try to run it I get the error: Traceback (most recent call last):
  File "D:/fack.py", line 6, in <module>
    arcpy.Clip_management(rast, "#", out_raster + rast[:-4] + 'Clipped.tif', 'D:\\Shape File Buffer\\RMNPBuff1km.shp', '#', "ClippingGeometry")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 12021, in Clip
    raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (Clip).
Tags (2)
0 Kudos
3 Replies
ClintDow
Occasional Contributor
That "ExecuteError: ERROR 999999: Error executing function." usually means something isn't right with the input parameters for the geoprocessing tool in question. I would guess its probably the parameter: out_raster + rast[:-4] + 'Clipped.tif'

The rast[:-4] will only work if rast has an extension, if its a grid format raster it won't have a file extension. Perhaps try out_raster + rast.split('.')[0] +'Clipped.tif' instead. This will ensure you only get the name of the raster regardless of the file extension or lack thereof.


Also in regards to your screen name, don't get discouraged! Its a bit of a learning curve and its great to come ask questions if you find yourself stuck rather than giving up 🙂
0 Kudos
SamanthaSmyth
New Contributor
Thanks, clint.dow!!! I made some adjustments and actually got the image to clip, however it is not clipping to the polygon feature (like I need), it is clipping to the square of the polygon feature, even though I specified the "ClippingGeometry" tool. If you or anyone has any insight into how to now get that to work I would really appreciate it! Here is my current script:

## Clip_management (in_raster, rectangle, out_raster, {in_template_dataset}, {nodata_value}, {clipping_geometry})

import arcpy
arcpy.env.workspace= r'D:\rmnp\SamSOrgNDVIData\ND 1989 - 1999\nd1996'
ClippingGeometry= "ClippingGeometry"
out_raster= r'D:\rmnp\SamSOrgNDVIData\Test1996\\'
rastList= arcpy.ListRasters()
for rast in rastList:
    arcpy.Clip_management(rast, "#", 'out_raster' + rast[:-4] + '_Clipped.tif', 'D:\Shape File Buffer\RMNPBuff1km.shp', '#', "ClippingGeometry")
0 Kudos
ClintDow
Occasional Contributor
Hi again,

In the line
arcpy.Clip_management(rast, "#", 'out_raster' + rast[:-4] + '_Clipped.tif', 'D:\Shape File Buffer\RMNPBuff1km.shp', '#', "ClippingGeometry")


You should use a raw string instead of 'D:\Shape File Buffer\RMNPBuff1km.shp' -- that is, r'D:\Shape File Buffer\RMNPBuff1km.shp' Right now without a raw string it probably can't find the shapefile.

The r before the string tells it not to pay attention to the backslashes, which will behave differently if the string is not raw.

See here for more info on that concept: http://docs.python.org/release/2.5.2/ref/strings.html
0 Kudos