Batch process to Clip Raster by Polygon

1895
23
08-24-2018 06:37 AM
JimFritz
Occasional Contributor

I'm not a python script writer thus the question. What's needed is a script that will do the following:

  • Run a definition query for each record in a polygon shapefile called "LineBuf.shp" on an attribute field named "LINENUM". (There are 441 unique records).
  • For each unique polygon, run a raster clip on "MN_DEM3second", the raster dataset. The option to "Use Input Features for Clipping Geometry" should be selected.
  • The file name of the resultant raster layer (in ESRI grid format) for each record should be the value of "LINENUM" (from the original definition query).
  • The script should create 441 raster layers.
0 Kudos
23 Replies
JakeSkinner
Esri Esteemed Contributor

Try the following:

import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"C:\temp\LineBuf.shp"
raster = r"C:\temp\MN_DEM3second"

with arcpy.da.SearchCursor(shapefile, "LINENUM") as cursor:
    for row in cursor:
        print("Creating Feature Layer for " + str(row[0]))
        arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LINENUM = " + row[0])
        print("Clipping raster")
        arcpy.Clip_management(raster, "", r"C:\temp\DEM_" + str(row[0]), "", "fLayer", "MAINTAIN_EXTENT")

del cursor
JimFritz
Occasional Contributor

Jake,

Many thanks for script but I get the following error message.  Note that I changed the folder paths to the shapefile and raster inputs.  The field "LineNum" is a text field if that is an issue.

>>> import arcpy

arcpy.env.overwriteOutput = 1

 

shapefile = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\LineBuf.shp"

raster = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\MN_DEM3second"

 

with arcpy.da.SearchCursor(shapefile, "LineNum") as cursor:

for row in cursor:

print("Creating Feature Layer for " + str(row[0]))

arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LineNum = " + row[0])

print("Clipping raster")

arcpy.Clip_management(raster, "", r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\DEM_" + str(row[0]), "", "fLayer", "MAINTAIN_EXTENT")

 

del cursor

Creating Feature Layer for 0517

Runtime error Traceback (most recent call last): File "<string>", line 10, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 6520, in MakeFeatureLayer raise e ExecuteError: ERROR 000358: Invalid expression LineNum = 0517 Failed to execute (MakeFeatureLayer).

>>>

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

exp = "LineNum = {}".format(roww[0])

arcpy.MakeFeatureLayer_management(shapefile, "fLayer", exp)

maybe?

For reference

/blogs/dan_patterson/2016/08/14/script-formatting 

since you have no line numbers

0 Kudos
JakeSkinner
Esri Esteemed Contributor

You can replace the following line:

arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LINENUM = " + row[0])

with

arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LINENUM = '" + row[0] + "'")

Or Dan Patterson's approach (he was missing single quotes around the {}:

exp = "LineNum = '{}'".format(row[0])

arcpy.MakeFeatureLayer_management(shapefile, "fLayer", exp)

JimFritz
Occasional Contributor

Thanks, Jake and Dan but now I'm getting this message:

Runtime error Traceback (most recent call last): File "<string>", line 12, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 13594, in Clip raise e ExecuteError: ERROR 000622: Failed to execute (Clip). Parameters are not valid. ERROR 000800: The value is not a member of ClippingGeometry | NONE.

>>>

Probably something easy with the ClippingGeometry arguments?

Jim

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Looks like the raster extent is needed.  Try the following:

import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"C:\temp\LineBuf.shp"
raster = r"C:\temp\MN_DEM3second"
desc = arcpy.Describe(raster)
extent = str(desc.extent.XMin) + " " + str(desc.extent.YMin) + " " + str(desc.extent.XMax) + " " + str(desc.extent.YMax)

with arcpy.da.SearchCursor(shapefile, "LINENUM") as cursor:
    for row in cursor:
        print("Creating Feature Layer for " + str(row[0]))
        arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LINENUM = '" + row[0] + "'")
        print("Clipping raster")
        arcpy.Clip_management(raster, extent, r"C:\temp\DEM_" + str(row[0]), "", "fLayer", "MAINTAIN_EXTENT")

del cursor
0 Kudos
JimFritz
Occasional Contributor

Getting this message now:

Runtime error Traceback (most recent call last): File "<string>", line 14, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 13594, in Clip raise e ExecuteError: ERROR 000622: Failed to execute (Clip). Parameters are not valid. ERROR 000800: The value is not a member of ClippingGeometry | NONE.

0 Kudos
JimFritz
Occasional Contributor
import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\LineBuf.shp"
raster = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\MN_DEM3second"
desc = arcpy.Describe(raster)
extent = str(desc.extent.XMin) + " " + str(desc.extent.YMin) + " " + str(desc.extent.XMax) + " " + str(desc.extent.YMax)

with arcpy.da.SearchCursor(shapefile, "LineNum") as cursor:
    for row in cursor:
        print("Creating Feature Layer for " + str(row[0]))
        arcpy.MakeFeatureLayer_management(shapefile, "fLayer", "LINENUM = '" + row[0] + "'")
        print("Clipping raster")
        arcpy.Clip_management(raster, "", r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\DEM_" + str(row[0]), "", "fLayer", "MAINTAIN_EXTENT")

del cursor
0 Kudos
JakeSkinner
Esri Esteemed Contributor

You're missing the 'extent' variable in the arcpy.Clip_management function.  You should have:

arcpy.Clip_management(raster, extent, r"S:\Gen......
0 Kudos