I'm using ArcGIS Desktop 10.5.1. and I'm a beginner with Python. I'm trying to clip a raster layer with a polygon layer (shapefile) that has multiple features. The tool should

  1. clip the raster according to those multiple polygon features, and then
  2. name the new output rasters according to a field name (FID) in the polygon layer.

    • Example input raster name: RasterFile.tif
    • Example output raster name: RasterFile_FID0.tif (this would be the first raster)

I have a code (from @Midavalo) that I have used to do the first part (code below). This works just fine, but I can't figure it out how to modify the code so that it would do the second part.

import arcpy, os
arcpy.env.workspace = r"E:\Folder\Subfolder"
outputWorkspace = r"E:\Folder\Subfolder\Outputs"
rasterlist = arcpy.ListRasters()
clipShapefile = r"Grid.shp"
clipShapes = set()
with arcpy.da.SearchCursor(clipShapefile, ['OID@']) as cursor:
    for row in cursor:

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'):
        arcpy.MakeFeatureLayer_management(clipShapefile, 'clipLayer', ' "FID" = {}'.format(i)) 
        arcpy.Clip_management(raster, "#", newRasterPath, 'clipLayer', "0", "ClippingGeometry") 

1 Answer 1


This line sets your new filename.

newRaster = "{}_clip_{}.img".format(rastername, i)

The output will look something like RasterFilename_clip_1.img where RasterFilename is the name of your raster, and 1 is the FID.

You just need to change this line to your new filename structure. Since you want something like RasterFilename_FID1.tif, you just need to remove _clip and add FID (and change the raster output extension). Something like this should produce what you want:

newRaster = "{}_FID{}.tif".format(rastername, i)
  • Of course, so simple! I will try it out with my data and when I confirm it works I'll tag your answer as the right one. Thank you!
    – Sn0W
    Jan 20, 2018 at 17:17
  • I ran the code with the suggested changes and it's giving me an error (ERROR 999998) relating to the MakeFeatureLayer, line 18: arcpy.MakeFeatureLayer_management(clipShapefile, 'clipLayer', ' "FID" = {}'.format(i)). What did I miss?
    – Sn0W
    Jan 22, 2018 at 17:18
  • @FiveO Does it tell you any more in the error message?
    – Midavalo
    Jan 22, 2018 at 17:36
  • I rebooted everything and now it works. Thank you!
    – Sn0W
    Jan 22, 2018 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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