AnsweredAssumed Answered

how to enter 'rectangle' argument in raster Clip

Question asked by dwilcoxars on Sep 7, 2016
Latest reply on Sep 8, 2016 by Dan_Patterson

I have a raster that I want to clip to a county boundary (no data outside county).  It is a stretched NDVI tif, so the values of the raster are 0-255.  I tried using the Con function, but it produced an output raster with values from 6 to 66500-ish.  I determined that what would work is the raster Clip tool and checking the 'Use Input Features for Clipping Geometry' box.  The results are fine -- no data outside the county, correct values inside the county -- EXCEPT that the extent of the output raster is the extent of the county polygon, and I want it to be a different, larger extent (so as to be the same size as other raster datasets).

 

The Python Snippet comes out like this:

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the

# layer/table view within the script.
# The following inputs are layers or table views: "...a.tif", "...b.shp"
arcpy.Clip_management(in_raster="...a.tif", rectangle="677439.999999999 3721040 678360.000000007 3722960", out_raster="...c.tif", in_template_dataset="b.shp", nodata_value="256", clipping_geometry="ClippingGeometry", maintain_clipping_extent="NO_MAINTAIN_EXTENT")

 

I can replace all the dataset arguments with variables:

theInRaster   = ...a.tif

theCntyShp   = ...b.shp

theOutRaster = ...c.tif

arcpy.Clip_management(in_raster= theInRaster",

                                        rectangle="677439.999999999 3721040 678360.000000007 3722960",

                                        out_raster=theOutRaster,

                                        in_template_dataset=theCntyShp,

                                        nodata_value="256", clipping_geometry="ClippingGeometry",

                                        maintain_clipping_extent="NO_MAINTAIN_EXTENT")

 

Now, I want the rectangle argument to be equal to the extent of clip.tif.  I tried setting the arcpy.env.extent to clip.tif and leaving the rectangle argument blank (""), but it went to the smaller extent of the polygon.  So, I need to actually put 'something' in the rectangle coordinates.  I tried setting theExtent to several things:

 

theExtent = arcpy.env.extent  and then rectangle = theExtent    #crashed ArcGIS completely

 

theExtentRaster = ...clip.tif and then rectangle = theExtentRaster  #crashed ArcGIS completely

 

theDesc = arcpy.Describe(theExtentRaster)

theExtent = theDesc.extent and then rectangle = theExtent

 

or

 

theDesc = arcpy.Describe(theExtentRaster)

theExtent = theDesc.extent and then rectangle = theExtent

theExtentString = str(theExtent.XMin) + ' ' + str(theExtent.YMin) + ' ' + str(theExtent.XMax) + ' ' + str(theExtent.YMax) and then rectangle = theExtentString

 

and also trying adding either single quotes or double quotes to theExtentString 

 

all result in this error:

 

ExecuteError: ERROR 000622: Failed to execute (Clip). Parameters are not valid. ERROR 000628: Cannot set input into parameter rectangle.

 

So, clearly, there is SOME OTHER WAY to define the rectangle.  I'm hoping someone out there can help....  Thanks.

 

 

 

Outcomes