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?
<SPAN class="comment token"># Paths have been generalized for example purposes</SPAN>
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> arcpy
inputFolder <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'..\RasterFolder'</SPAN>
footprints <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'..\GDB.gbb\ScanFootprints'</SPAN> <SPAN class="comment token"># Polygon feature class</SPAN>
<SPAN class="comment token"># Create a list of rasters; Output will look like: ['raster01.tif', 'raster02.tif', ...]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> inputFolder
processRasters <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Iterate each raster to clip</SPAN>
<SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> processRasters<SPAN class="punctuation token">:</SPAN>
inputRaster <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>inputFolder<SPAN class="punctuation token">,</SPAN> raster<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Feature layer method</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>footprints<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"footprint"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"RasterFile = '"</SPAN> <SPAN class="operator token">+</SPAN> raster <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Clip_management<SPAN class="punctuation token">(</SPAN>inputRaster<SPAN class="punctuation token">,</SPAN> '<SPAN class="comment token">#', outputRaster, "footprint", '#', 'ClippingGeometry')</SPAN>
<SPAN class="comment token"># Select Analysis method</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Select_analysis<SPAN class="punctuation token">(</SPAN>footprints<SPAN class="punctuation token">,</SPAN> footprints <SPAN class="operator token">+</SPAN> <SPAN class="string token">'_temp'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"RasterFile = '"</SPAN> <SPAN class="operator token">+</SPAN> raster <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Clip_management<SPAN class="punctuation token">(</SPAN>inputRaster<SPAN class="punctuation token">,</SPAN> '<SPAN class="comment token">#', outputRaster, footprints + '_temp', '#', 'ClippingGeometry')</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>