Is there a way to clip many rasters to the boundaries of hundreds of square polygons in a feature class? I am open to either Python or ArcGIS Pro solutions.
Here is what I am trying to do. I have a polygon feature class with about 700 squares. These squares are located at buildings throughout a large region. I have probably 200 rasters that I cannot mosiac together because it would take forever. I want to iterate through each of the polygons and clip whatever raster(s) happen to be located underneath that polygon. I want export each of those clips to its own individual raster.
In Python the process of iterating through the polygons and clipping from one raster seems easy to me. The issue is that there are hundreds of rasters and would not know for each polygon what raster or rasters would be associated with them.
Solved! Go to Solution.
Thanks. That makes sense, but I also realized I had the issue where a polygon might overlap the boundaries of one or more rasters. The solution I found was to create a Mosaic dataset in a file geodatabase and then import all of the hundreds of rasters. Then, in my python script, I simply referenced the Mosaic Dataset and it was treated as one raster when clipping.
import arcpy
import os
sr = arcpy.SpatialReference(6539)
arcpy.env.outputCoordinateSystem = sr
rasterImage = r''
polyFC = r''
out_folder = r'C'
arcpy.Delete_management('polyFC_lyr')
arcpy.MakeFeatureLayer_management(polyFC,'polyFC_lyr')
for row in arcpy.da.SearchCursor(polyFC,["SHAPE@","BLDG"]):
whereClause = "BLDG = " + "'" + row[1] + "'"
arcpy.SelectLayerByAttribute_management('polyFC_lyr','NEW_SELECTION', whereClause)
extent = row[0].extent
extentFormated = str(extent.XMin) + ' ' + str(extent.YMin) + ' ' + str(extent.XMax) + ' ' + str(extent.YMax)
print (extentFormated)
raster_name = row[1] + '.jp2'
print (raster_name)
out_raster = os.path.join(out_folder,raster_name)
print (out_raster)
arcpy.Clip_management(rasterImage,extentFormated,out_raster,'polyFC_lyr','1','ClippingGeometry','MAINTAIN_EXTENT')
Using Python, read your rasters in a loop into Raster objects: Raster—ArcPy classes | ArcGIS Desktop
Compare the raster extent to your polygon. If they overlap, then clip.
Thanks. That makes sense, but I also realized I had the issue where a polygon might overlap the boundaries of one or more rasters. The solution I found was to create a Mosaic dataset in a file geodatabase and then import all of the hundreds of rasters. Then, in my python script, I simply referenced the Mosaic Dataset and it was treated as one raster when clipping.
import arcpy
import os
sr = arcpy.SpatialReference(6539)
arcpy.env.outputCoordinateSystem = sr
rasterImage = r''
polyFC = r''
out_folder = r'C'
arcpy.Delete_management('polyFC_lyr')
arcpy.MakeFeatureLayer_management(polyFC,'polyFC_lyr')
for row in arcpy.da.SearchCursor(polyFC,["SHAPE@","BLDG"]):
whereClause = "BLDG = " + "'" + row[1] + "'"
arcpy.SelectLayerByAttribute_management('polyFC_lyr','NEW_SELECTION', whereClause)
extent = row[0].extent
extentFormated = str(extent.XMin) + ' ' + str(extent.YMin) + ' ' + str(extent.XMax) + ' ' + str(extent.YMax)
print (extentFormated)
raster_name = row[1] + '.jp2'
print (raster_name)
out_raster = os.path.join(out_folder,raster_name)
print (out_raster)
arcpy.Clip_management(rasterImage,extentFormated,out_raster,'polyFC_lyr','1','ClippingGeometry','MAINTAIN_EXTENT')