how to enter 'rectangle' argument in raster Clip

3541
6
Jump to solution
09-07-2016 03:30 PM
DarleneWilcox
New Contributor III

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.

0 Kudos
1 Solution

Accepted Solutions
NeilAyres
MVP Alum

Try this :

ExtStr = "{} {} {} {}".format(XMin, YMin, XMax, YMax)

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

use the extent object of the desired raster directly as shown in the example script here Raster—Help | ArcGIS for Desktop you can also examine the extent object if your wish as well, Extent—Help | ArcGIS for Desktop 

0 Kudos
DarleneWilcox
New Contributor III

Dan, I know I need to use the extent -- I just can't get the arcpy.Clip_management command to accept the extent in the rectangle argument!  Of course I can enter it manually -- I need to do it within a script.  It is a formatting thing -- how to format the XMin YMin XMax YMax so as not to either crash ArcGIS completely, as occurs when I tried using

arcpy.env.extent = ...clip.tif

theExtent = arcpy.env.extent

or 

theExtent = clip.tif

And if I try building the string of coordinates after using Describe to access the information, i.e.:

theExtentRaster = clip.tif

theDesc = arcpy.Describe(theExtentRaster)

theExtent = theDesc.extent 

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

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

I also tried adding single quotes or double quotes to theExtentString.

So, again, it isn't accessing the information, (I even tried the code snippet in the Extent page you referenced), it is FORMATTING the four coordinates in a way the arcpy.Clip_management command will accept.

Any ideas?  Thanks, Darlene

0 Kudos
NeilAyres
MVP Alum

Try this :

ExtStr = "{} {} {} {}".format(XMin, YMin, XMax, YMax)
DarleneWilcox
New Contributor III

Neil Ayres ... you rock!  That worked -- yeah!

Do you know where (other than right here!) this is documented? -- no joke when I say I spent all day yesterday trying every thing I could think of and searching all over the forums and stack exchange and so on.

Again -- THANK YOU!!!!!

0 Kudos
NeilAyres
MVP Alum

It's just that adding strings to other strings with other strings and all the extra quotes etc, sometimes get very confusing.

the "{Var1} {Var2} {Var3}".format(x, y, z) syntax is much clearer.

Glad it worked for you.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Darlene

I have written a couple of blogs on formating in python and numpy... here is one of several

/blogs/dan_patterson/2016/03/01/basic-fancy-formats 

0 Kudos