I am trying to automate the process of clipping rasters of scanned maps by following the recommendation found here: How To: Clip a raster using the Clipping Geometry option. The rasters are being clipped to the envelope, but not the shape as pixels outside of the polygon are still persisting. The rasters and the feature class are in the same coordinate system.
My footprints are all stored in same feature class with a field 'RasterFile' storing the raster name (e.g. 'raster01.tif'). I have tried creating a feature layer with a query and using Select analysis to select out the footprint for a single raster (see code below). Everything processes, but the shape clip is not honored.
When I just run the Raster Clip tool manually, the clip honors the shape. Any ideas for what I may be missing?
# Paths have been generalized for example purposes
import os
import arcpy
inputFolder = r'..\RasterFolder'
footprints = r'..\GDB.gbb\ScanFootprints' # Polygon feature class
# Create a list of rasters; Output will look like: ['raster01.tif', 'raster02.tif', ...]
arcpy.env.workspace = inputFolder
processRasters = arcpy.ListRasters()
# Iterate each raster to clip
for raster in processRasters:
inputRaster = os.path.join(inputFolder, raster)
# Feature layer method
arcpy.MakeFeatureLayer_management(footprints, "footprint", "RasterFile = '" + raster + "'")
arcpy.Clip_management(inputRaster, '#', outputRaster, "footprint", '#', 'ClippingGeometry')
# Select Analysis method
arcpy.Select_analysis(footprints, footprints + '_temp', "RasterFile = '" + raster + "'")
arcpy.Clip_management(inputRaster, '#', outputRaster, footprints + '_temp', '#', 'ClippingGeometry')
Solved! Go to Solution.
Sorry to revive an old question, but in case anyone else is having this same issue and stumbles on this thread (like I just did), here's the solution: NoData has to be defined as "0" instead of "#". Using the code above, it should be:
arcpy.Clip_management(inputRaster, '#', outputRaster, footprints + '_temp', '0', 'ClippingGeometry')
That will allow the tool to clip to the polygon shape instead of the bounding rectangle.
I can find anything obviously wrong (with the exception of your outputRaster variable is not defined, but I'm guessing you left it out in the copy/paste.) and cannot duplicate your error. I have a script tool that uses this and it has no issues. The only difference is that I used clipping_geometry = 'ClippingGeometry' for my 6th parameter, but that shouldn't make any difference.
Sorry to revive an old question, but in case anyone else is having this same issue and stumbles on this thread (like I just did), here's the solution: NoData has to be defined as "0" instead of "#". Using the code above, it should be:
arcpy.Clip_management(inputRaster, '#', outputRaster, footprints + '_temp', '0', 'ClippingGeometry')
That will allow the tool to clip to the polygon shape instead of the bounding rectangle.