4

I have a single raster file and about 40 small polygons shapes. I would like to clip the raster by the extent of all these shapes, and create 40 resulting .img files.

I know I can just do this by right-clicking the clip tool and selecting batch process.... but I would rather have a script because I will need to do this multiple times over the next few months.

I am inexperienced in python and would like some help with my script. I am not using geodatabases, just a simple input and output folder. I was looking at a similar post which has the below code... but the rectangle number option in arcpy.Clip_management requires me to manually enter every single rectangle number which defeats the purpose for me.

How can I modify this to manually clip a single raster using all 40 polygon files?

I am using ArcGIS 10.2.2.

import arcpy

rasterlist = arcpy.ListRasters("your_workspace")
for raster in rasterlist:
for i in range(40):
    arcpy.MakeFeatureLayer_management("your_shapefile", "layer" + str(i), ' "FID" = ' + str(i)) #create a layer with only polygon i
    arcpy.Clip_management(raster, "#", raster[:-4] + "clip" + str(i) +" .img","layer" + str(i), "0", "ClippingGeometry") #clip based on layer, clipping geometry will use the polygon extent only
  • Are the polygon shapes all in one shapefile? Will the number ever change? – Midavalo Jun 3 '16 at 21:32
6

This uses multiple shape features in a single shapefile (based on your code snippet). If you have a single shape feature in multiple shapefiles I have added code for that further down.

The following update on your script will loop through each feature in your polygon shapefile (regardless of number of features), and clip each raster it finds using List Rasters. It is set up to output to a different folder so that on subsequent runs it won't be trying to clip rasters that have already been clipped.

import arcpy, os

arcpy.env.workspace = r"N:\GISSE\ClipRaster" # Where the clip shapefile and input raster are stored
outputWorkspace = r"N:\GISSE\ClipRaster\ClipRasterOutput" # Where the output images will be stored

rasterlist = arcpy.ListRasters() # Get a list of input rasters

clipShapefile = r"ClipRasterPolygon.shp" # Clip shapefile

# Create set of FIDs in the clip shapefile
clipShapes = set()
with arcpy.da.SearchCursor(clipShapefile, ['OID@']) as cursor:
    for row in cursor:
        clipShapes.add(row[0])

# Loop through input rasters, and clip by each shape in the input shapefile
for raster in rasterlist:
    rastername, rasterext = os.path.splitext(raster)
    for i in clipShapes:
        newRaster = "{}_clip_{}.img".format(rastername, i)
        newRasterPath = os.path.join(outputWorkspace, newRaster)
        if arcpy.Exists('clipLayer'): # Remove existing 'clipLayer' feature layer if it still exists for some reason
            arcpy.Delete_management('clipLayer')
        arcpy.MakeFeatureLayer_management(clipShapefile, 'clipLayer', ' "FID" = {}'.format(i)) #create a layer with only polygon i
        arcpy.Clip_management(raster, "#", newRasterPath, 'clipLayer', "0", "ClippingGeometry") #clip based on layer, clipping geometry will use the polygon extent only
        arcpy.Delete_management('clipLayer')

For multiple shapefiles containing clip shape, try this:

import arcpy, os

arcpy.env.workspace = r"N:\GISSE\ClipRaster2" # Where the clip shapefile and input raster are stored
outputWorkspace = r"N:\GISSE\ClipRaster2\ClipRasterOutput" # Where the output images will be stored

raster = r"MyRaster.png"
rastername, rasterext = os.path.splitext(raster)

clipShapes = arcpy.ListFeatureClasses("*.shp")

i = 1

for clipShape in clipShapes:
    newRaster = "{}_clip_{}.img".format(rastername, i)
    newRasterPath = os.path.join(outputWorkspace, newRaster)
    arcpy.Clip_management(raster, "#", newRasterPath, clipShape, "0", "ClippingGeometry")
    i += 1
  • I have updated for multiple shapefiles containing clip shape – Midavalo Jun 3 '16 at 22:32
  • Thanks immensely Midavalo. I truly appreciate the help and your time. I am having trouble running my script at the moment- perhaps because this is my first time running python in arcgis. I am getting some error messages - please see below about how I entered my script: – Alexander Jun 4 '16 at 23:44
  • I think you should accept this answer to acknowledge the assistance given by Midavalo, and if you have additional questions then ask them separately. – PolyGeo Aug 1 '16 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.