Select to view content in your preferred language

Batch process to Clip Raster by Polygon

4984
23
08-24-2018 06:37 AM
JimFritz
Regular 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

It's a feature class in the File Geodatabase (Data.gdb).  I avoid shapefiles at all costs.

0 Kudos
JimFritz
Regular Contributor

Thanks for the sample data.  Here's the error message and script using the sample data.  I created a mosaic dataset under Data.gdb for the clipped rasters.  Not sure if that is OK.

Creating Feature Layer for 1

Clipping raster

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 000732: Input Raster: Dataset C:\temp\clips\SampleData\Data.gdb\DEM.tif does not exist or is not supported

import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"C:\temp\clips\SampleData\Data.gdb\Linebuf_geo"
raster = r"C:\temp\clips\SampleData\Data.gdb\DEM.tif"

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] + "'")
        desc = arcpy.Describe("fLayer")
        extent = str(desc.extent.XMin) + " " + str(desc.extent.YMin) + " " + str(desc.extent.XMax) + " " + str(desc.extent.YMax)
        print("Clipping raster")
        arcpy.Clip_management(raster, extent, r"C:\temp\clips\SampleData\Data.gdb\LineNum_Clips\DEM_" + str(row[0]), "fLayer", "", "ClippingGeometry", "MAINTAIN_EXTENT")
del cursor
0 Kudos
JakeSkinner
Esri Esteemed Contributor

You will not want to create a mosaic dataset to get this code to work.  Change the following:

raster = r"C:\temp\clips\SampleData\Data.gdb\DEM.tif"

to:

raster = r"C:\temp\clips\SampleData\DEM.tif"
JimFritz
Regular Contributor

Thanks for all your help, Jake!

0 Kudos