Rectangle for Clip_management tool in arcpy

6734
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
ChrisSnyder
Regular Contributor III
Try this:

import arcpy
arcpy.env.workspace = r"C:\Temp"
raster = "some_raster"
outraster = r"C:\output_raster"
extentFC = r"C:\GIS_Data\test_extent2.shp"
dsc = arcpy.describe(extent)
arcpy.Clip_management (raster, dsc.extent, outraster)
###or if you want to mask it using extentFC as the outline:
##arcpy.Clip_management (raster, dsc.extent, outraster, extentFC, "", "CLIPPINGGEOMETRY")
0 Kudos
RichNauman
New Contributor III
Thanks but I still get the "The value is not a Envelope." 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 15, in <module>
    arcpy.Clip_management (raster, dsc.extent, 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).
0 Kudos
ChrisSnyder
Regular Contributor III
Hmm... Maybe a bug in v10?

Here's an excerpt of some v9.3 code that works for me:

#Process: Creates a blank GDB
fgdbName = "oesf_score_gnn_raster"
fgdbPath = root + "\\" + fgdbName + ".gdb"
gp.CreateFileGDB_management(root, fgdbName); showGpMessage()

#Process: Extract the OESF
hcpUnitFC = r"\\Snarf\am\div_lm\ds\gis\tools\sde_connections_read\ropa_gis_layer_user.sde\ROPA.HCPUNIT"
oesfFC = fgdbPath + "\\oesf"
gp.Select_analysis(hcpUnitFC, oesfFC, "HCPUNIT_NM = 'OESF'"); showGpMessage()

#Process: Buffer the oesf
oesfBufferUnits = "FEET"
oesfBufferDistance = "35000" #NOTE: This MUST be the same buffer distance as in 'nso_make_path_distance_grids_v93.py'
oesfBufferFC = fgdbPath + "\\oesf_buff_" + str(oesfBufferDistance) + oesfBufferUnits.lower()
gp.Buffer_analysis(oesfFC, oesfBufferFC, str(oesfBufferDistance) + " " + oesfBufferUnits.upper(), "FULL", "ROUND", "NONE", ""); showGpMessage()

#Process: Clip the GNN data to our area of interest
gnnReadGrd = r"\\snarf\am\div_lm\ds\gis\tools\sde_connections_read\raster_gis_layer_user.sde\SHARED_LM.GNN_20100303"
gp.extent = oesfBufferFC
gp.extent = snapExtentToRaster(gp.extent, gnnReadGrd)
gp.mask = oesfBufferFC
gp.cellsize = gnnReadGrd
gnnClipGrd = fgdbPath + "\\gnn_clip"
gp.Clip_management(gnnReadGrd, gp.extent, gnnClipGrd, oesfBufferFC, "", "CLIPPINGGEOMETRY")
0 Kudos
DavidWynne
Esri Contributor
Hi Richard,
You won't be able to use a Extent object directly as input to Clip, but since you can use geometry objects, Polygon, etc.  So, really it just a matter of converting that extent into a polygon.

Working from your original code, something like below should do it.

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

raster = "some_raster"
outraster = r"C:\output_raster"
desc = arcpy.Describe(r"C:\GIS_Data\test_extent2.shp")
extent = desc.extent

rectangle = arcpy.Polygon(arcpy.Array([extent.lowerLeft, extent.upperLeft, extent.upperRight, extent.lowerRight, extent.lowerLeft]),
                          desc.spatialReference)

print rectangle

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

print "Success!"


-Dave
0 Kudos
ChrisSnyder
Regular Contributor III
Looks like I will be rewriting a lot of v9.3 code to make it arcpy compatible...
0 Kudos
DavidWynne
Esri Contributor
Looks like I will be rewriting a lot of v9.3 code to make it arcpy compatible...


Sorry, I was overthinking that example way too much.  It could also be done by converting the extent to a string like so:

desc = arcpy.Describe(r"C:\GIS_Data\test_extent2.shp")
extent = desc.extent

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


Hopefully, that is more agreeable
-Dave
ChrisSnyder
Regular Contributor III
I like that better!
0 Kudos
RichNauman
New Contributor III
Thanks for the help but I am still struggling.

This script works:

import arcpy
raster = r"Y:\GIS_Data\raster1"
outraster = r"Y:\GIS_Data\delete_me"

desc = arcpy.Describe(r"Y:\GIS_Data\test_extent2.shp")
frame = str(desc.extent)
frame = str(frame [:-16])
frame = str('"' + frame + '"')
print frame

arcpy.Clip_management (raster, "-121.677618762735 42.7497214280392 -119.35205017646 45.3659860875993", outraster)
print "Done!"



But this one does not:

import arcpy
raster = r"Y:\GIS_Data\raster1"
outraster = r"Y:\GIS_Data\delete_me"

desc = arcpy.Describe(r"Y:\GIS_Data\test_extent2.shp")
frame = str(desc.extent)
frame = str(frame [:-16])
frame = str('"' + frame + '"')
print frame

arcpy.Clip_management (raster,frame, outraster)
print "Done!"


I copied and posted the output from the "print frame" statement into the clip statement to get it to work.

Any ideas?
0 Kudos
DavidWynne
Esri Contributor

desc = arcpy.Describe(r"Y:\GIS_Data\test_extent2.shp")
frame = str(desc.extent)
frame = str(frame [:-16])
frame = str('"' + frame + '"')



Don't bother with the extra quotes in this last line, I'm pretty sure this is the cause of your error. 

Instead, just do this (leaving out your last frame = line)...
frame = str(desc.extent)
frame = frame[:-16]

or, if your intention is to strip out all the NaN's at the end, you could also do this...
frame = str(desc.extent).strip(" NaN")
0 Kudos