The example below shows three raster (i.e. in black) and a single county polygon feature class. How can I clip/extract by mask the rasters by their corresponding county polygons (i.e. Edwards, Clark, Barber) in an automated fashion? For this example, the output should include three separate rasters clipped to the county boundaries. A python or ArcGIS solution suitable for batch processing would be ideal. Thanks for the help.
2 Answers
Presumably since you want to automate this, you'll want each raster to figure out which polygon should be used to clip itself? Pseudo code:
- iterate through the list of list of input rasters
- convert the raster to a polygon
- obtain the polygon's centroid
- determine which county polygon the centroid (and therefore the raster) falls within
- convert that polygon to a County raster, with a value of 1 (and everything else 0 or NULL)
- multiply the county raster with the original raster
After many great suggestions from @Stephen Lead and others, I have a solution:
# Clips rasters with a polygon featureclass
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("3D")
# Set Over write
arcpy.env.overwriteOutput = 1
# Set the workspace
env.workspace = r"Z:\temp.gdb"
Dir = env.workspace
# Local variables:
counties = r"Z:\temp.gdb\boundaries\counties"
counties_lyr = arcpy.MakeFeatureLayer_management(counties,"counties_lyr")
# Get the list of rasters to process
raster_list = arcpy.ListRasters("*_clp")
print raster_list
for raster in raster_list:
# Define name and location for output raster
name = Dir + "\\" + str(raster) + "_clp"
# Process: Raster Domain
arcpy.RasterDomain_3d(raster, "in_memory/temp", "POLYGON")
# Process: Central Feature
arcpy.MeanCenter_stats("in_memory/temp", "in_memory/temp1")
# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(counties_lyr, "intersect", "in_memory/temp1", "", "NEW_SELECTION")
# Clip Raster
arcpy.Clip_management(raster, "#", name,counties_lyr, "#", "ClippingGeometry")
# Delete in_memory
arcpy.Delete_management("in_memory")
print "processing " + raster + " complete..."
print "All processing is now finished"