Simple Python Question (Raster Clip)

706
7
03-05-2020 12:24 PM
JonCicarelli
New Contributor

Hello everyone,

I am relatively new to scripting. I am going through Paul Zanderbergen’s Python book at the moment. I use Pro daily, but this book was written for ArcMap so I am using the ArcMap for these exercises.

Here is my question:

I am writing a script to extract land cover calcs from an input. I need to do  a raster clip to extract a raster file, but only using the rectangle extents and not a full on clip. Naturally I can get the tool in ArcMap  GUI to run just fine, but using  the arcpy.Clip_management module I cannot, for the life of me, get it to run.

Here is the code I am trying

arcpy.Clip_management(“LULC","InputFile","LULCOutput.tif","InputFile","","ClippingGeometry","NO_MAINTAIN_EXTENT")

I basically need a raster rectangle output of my input feature class to use in later parts of my script.

Can anyone help me with my syntax to get this tool to run in python? I have the rest of my  script ready to go, I just cannot get this tool to run in python.

The image attached is how my file structure is set up (C:\LandCoverScript\GDB.gdb)

Thanks!

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

Clip Raster—Data Management toolbox | Documentation 

look at the code example from there

arcpy.env.workspace = "C:/Workspace"
arcpy.Clip_management(
    "image.tif","1952602.23 294196.279 1953546.23 296176.279",
    "clip.gdb/clip", "#", "#", "NONE")

The environment workspace (aka folder) containing the image is specified and the rectangular extent is specified by bounding coordinates in the same coordinate system as those of the tif.

What does your script look like? 

is there a workspace set up?

Is everything in the same projection?

do you have a bounding extent specified?

A few thoughts

0 Kudos
JonCicarelli
New Contributor

All is in the same coordinate system and I have set up my work space.  I am just trying to get the syntax right for arcpy.Clip_management()

I am working in the Python window in ArcMap and planing on later moving it into a IDLE. 

Question on the rectangle extents however, I would need that to be the extents of my InputFile (basically a project area). Shouldn't those parameters be automatically populated if I specify "InputFile" for my rectangle? I shouldn't have to type out four extent coordiantes each time I run the script because those are going to change each time the parameter changes. 

I appreciate your the help. I am a Python n00b and am trying to write a few scripts for practice. 

Thanks,

0 Kudos
DanPatterson_Retired
MVP Emeritus

If the input is some other layer, you have to extract the "extent" from the featureclass (it is a geometry property) which can be derived from polygons and/or raster data. the "extent" can be converted to a string representation of the lower-left and upper-right coordinates.  For testing, grab your mouse and find those two coordinates, and make a string like in the example I posted, If you can progress from that, then the next step will be to move on to coding that

some homework

Extent—ArcPy classes | Documentation 

0 Kudos
JonCicarelli
New Contributor

So I was able to manually get the extents as a string the clip worked. Is there another tool (or Python operation) I can use that can get to extract the extent so this tool can be automated? 

When I use the tool in ArcMap I just specify that my extents will be that of the InputFile and the four coordinates populate automatically. 

Edit: Sorry I missed your link. I will dive into that and see what I can do! 

0 Kudos
DanPatterson_Retired
MVP Emeritus

of course

using some of my data as an example and showing it stepwise.  in_fc7 would be a featureclass input parameter to your script.

import arcpy
desc = arcpy.da.Describe(in_fc7)  # ---- a polygon featureclass's "describe" object
ext = desc['extent']              # ---- get the featureclass 'extent' property
L, B, R, T = ext.XMin, ext.YMin, ext.XMax, ext.YMax   # ---- help file reading
e_st = "{}, {}, {}, {}".format(L, B, R, T)  # ---- python format string stuff, more reading

e_st

'300000.0, 5000000.0, 300012.1075999997, 5000012.774999999'

# ----- your extent
0 Kudos
JonCicarelli
New Contributor

So another quick n00b question here. As I understood using the Python in the ArcMap interface, the syntax is kind of laid out for you. This how I have been learning party, following the prompts after each coma. How come the prompts are giving me the option to put in my FC in the rectangle parameter and not asking for a string of coordinates? Without manually copying and pasting the extents could you not run this tool in the ArcMap python window? 

0 Kudos
DanPatterson_Retired
MVP Emeritus

When you use the tool... that is what happens... which is what modelbuilder is for, combing arctoolbox tools into a nice workflow without having to worry about what goes on behind the scenes.  

When it comes time to code you own tools, there are several avenues you can take

  • have an "extent" parameter, which will allow you to define the clip extent from a featureclass
  • have a featureclass parameter used to define the extent.

Confused?  Good.  The first option means you are going directly to the "extent" property and if you have ever used a tool that wants you to specify one, you can actually specify the extent from a dataset or manually key in the numbers yourself.  If you create a "custom toolbox tool" you can specify these parameters through a dialog and associate them with a GetParameterAsText "thing".  But you are getting ahead of yourself.  The tool does the "magic" behind the scenes, I was just showing you have to get at that property that defines the extent rectangle.

0 Kudos