Rectangle for Clip_management tool in arcpy

6666
11
08-02-2010 08:57 AM
RichNauman
New Contributor III
Hello,
I have been struggling to get this part of a script to work. If I hard code the four coordinate pairs: like this:

rectangle = "-121.677618762735 42.7497214280392 -119.35205017646 45.3659860875993"

it works but we have to remember to change this every time we change study areas. When I run the script pasted below I get the following error:

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\Administrator\Desktop\clip_test.py", line 13, in <module>
    arcpy.Clip_management (raster, rectangle, outraster)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 7692, in Clip
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Envelope.
Failed to execute (Clip).

Here is my code:

import arcpy
arcpy.env.workspace = r"C:\Temp"

raster = "some_raster"
outraster = r"C:\output_raster"
extent = r"C:\GIS_Data\test_extent2.shp"

desc = arcpy.Describe(extent)
rectangle = desc.extent

print rectangle

# Clip_management (in_raster, rectangle, out_raster, {in_template_dataset}, {nodata_value}, {clipping_geometry})
arcpy.Clip_management (raster, rectangle, outraster)

print "Success!"

What am I missing?
Thanks!
0 Kudos
11 Replies
RichNauman
New Contributor III
Thanks for the help!!!

I'm not sure where I went off track but the following works:

import arcpy
raster = r"c:\GIS_Data\some_raster"
outraster = r"c:\GIS_Data\delete_me"

desc = arcpy.Describe(r"c:\GIS_Data\test_extent2.shp")
frame = str(desc.extent)

arcpy.Clip_management (raster, frame, outraster)
print "Done!"
0 Kudos
JenniferCorcoran
New Contributor
Have you tried applying this code to several rasters from different resolution source imagery? Do you get the same extents in the long run?  I am having major trouble clipping my rasters to specific extents and having them come out the EXACT same. I keep getting some of them the same as I am trying to use and some with extra decimals (ex. Raster Extent Interested: 524002.75 5167611.25 542557.75 5188001.25; Raster Extent Result: 524002.74999979 5167611.25012111 542557.74999979 5188001.25012111).

Thoughts?
0 Kudos