Batch process to Clip Raster by Polygon

3276
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
JimFritz
Occasional Contributor

OK thanks but still getting an error (for some reason I can't post in Python format):

>>> 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, extent, 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

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 000622: Failed to execute (Clip). Parameters are not valid. ERROR 000800: The value is not a member of ClippingGeometry | NONE.

>>>

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I believe I had some parameters incorrect, and we should be getting the extent of the shapefile, not the raster.  Try:

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\DEM_" + str(row[0]), "fLayer", "", "ClippingGeometry", "MAINTAIN_EXTENT")
del cursor
0 Kudos
JimFritz
Occasional Contributor

Re-running right now and no issues so far.  I'll keep you posted.

Many thanks, Jake and Dan!

0 Kudos
JimFritz
Occasional Contributor

Hi Jake,

I just wanted to thank you again for the script. However, I am still waiting for the first raster to be clipped and was wondering if you had any idea on why it might be taking so long. When I do the processing interactively (on just one query) the clip takes less than a couple minutes to process.

Any ideas?

Thanks,

Jim Fritz, GISP

Xcel Energy | Responsible By Nature

Sr. Geospatial Analyst

414 Nicollet Mall, 6th Floor, Minneapolis, MN 55401

P: 612.330.6956 C: 781.588.5829 F: 612.330.6590

E: james.w.fritz@xcelenergy.com<mailto:james.w.fritz@xcelenergy.com>

0 Kudos
JimFritz
Occasional Contributor

This could be a projection issue, bad me.  I'm going to project the polygon layer to geographic to match the DEM.  I'll try running the script again after the fix.

JimFritz
Occasional Contributor

After running the script over the weekend no clipped rasters were created.  The script showed each polygon being created (selected?) and a clip created.  Unfortunately, when I checked the grid folder there were none there.

Any ideas??

Thanks,

Jim

import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\STLM_NSP.gdb\LineBuf_geo"
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] + "'")
        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"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\clips\DEM_" + str(row[0]), "fLayer", "", "ClippingGeometry", "MAINTAIN_EXTENT")
del cursor1‍23456789101112131415
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try changing the output type to a TIF to see if that changes anything:

import arcpy
arcpy.env.overwriteOutput = 1

shapefile = r"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\STLM_NSP.gdb\LineBuf_geo"
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] + "'")
        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"S:\General-Offices-GO-Trans\SLR-Mapping\GIS_Projects_2018\Smart_T_Line_Model\geodata\clips\DEM_" + str(row[0]) + ".tif", "fLayer", "", "ClippingGeometry", "MAINTAIN_EXTENT")
del cursor
0 Kudos
JimFritz
Occasional Contributor

Hi Jake,

I tried re-running with outputs to tif format. Unfortunately, the script seems to run fine but no tif files are appearing in the “clips” subfolder.

Kind of strange.

Here’s what a file should look like spatially, created interactively.

Thanks for helping out.

Jim Fritz, GISP

Xcel Energy | Responsible By Nature

Sr. Geospatial Analyst

414 Nicollet Mall, 6th Floor, Minneapolis, MN 55401

P: 612.330.6956 C: 781.588.5829 F: 612.330.6590

E: james.w.fritz@xcelenergy.com<mailto:james.w.fritz@xcelenergy.com>

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Attached is some sample data you can test with.  I was able to get this to work with the script you posted previously.  One other thing to try is to write everything to your local C drive.

0 Kudos
JimFritz
Occasional Contributor

Hi Jake,

The zip file did not have a clipping polygon shapefile. Did you intend to send that?

Thanks,

Jim

0 Kudos