# Import arcpy module import arcpy # Local variables: Shapefile = r'E:\folder\folder\folder\folder\shapefile1.shp' Raster_Dataset = r'E:\folder\folder\folder\raster1.tif' Output_Raster_Dataset = r'E:\folder\test.tif' # Process: Clip arcpy.Clip_management(Raster_Dataset, "", Output_Raster_Dataset, Shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
Even though your suggestion was posted 10 years ago, this worked great for me! I was pulling my hair out trying to get a sub-model with an iterator to work within a model that also had an iterator, but your suggestion was so much easier to implement. Brilliant workaround.
import arcpy, os raster_ws = r'E:\path_to_raster_fold' shp_ws = r'E:\path_to_shp_fold' # output workspace out_ws = r'E:\path_to_output_fold' # get lists arcpy.env.workspace = raster_ws rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()] arcpy.env.workspace = shp_ws shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()] # get dictionary {raster1 : shapefile1.shp, ...} clip_dict = dict(zip(rasters, shapes)) # clip all reasters for raster, shapefile in clip_dict.iteritems(): out_raster = os.path.join(out_ws, os.path.basename(raster)) arcpy.Clip_management(raster, "", out_raster, shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT") print 'Clipped {0}'.format(os.path.basename(raster))
import arcpy, os, sys from arcpy import env from arcpy.sa import * arcpy.env.overwriteOutput = True raster_ws = r'E:\Test_awd\Criteria_SP_2mm_v2' shp_ws = r'E:\Test_awd\Season1' # output workspace out_ws = r'E:\Test_awd' # get lists arcpy.env.workspace = raster_ws rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()] arcpy.env.workspace = shp_ws shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()] # get dictionary {raster1 : shapefile1.shp, ...} clip_dict = dict(zip(rasters, shapes)) # clip all rasters outputList = [] for raster, shapefile in clip_dict.iteritems(): out_raster = os.path.join(out_ws, os.path.basename(raster)) arcpy.Clip_management(raster, "", out_raster, shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT") print 'Clipped {0}'.format(os.path.basename(raster)) outputList.append(out_raster) for raster in outputList(): sumRaster = CellStatistics(["outputList"], "SUM", "DATA") sumRaster.save(r'E:\Test_awd')
import arcpy, os, sys from arcpy import env from arcpy.sa import * arcpy.env.overwriteOutput = True arcpy.CheckOutExtension('Spatial') raster_ws = r'E:\Test_awd\Criteria_SP_2mm_v2' shp_ws = r'E:\Test_awd\Season1' # output workspace out_ws = r'E:\Test_awd' # get lists arcpy.env.workspace = raster_ws rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()] arcpy.env.workspace = shp_ws shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()] # get dictionary {raster1 : shapefile1.shp, ...} clip_dict = dict(zip(rasters, shapes)) # clip all rasters outputList = [] for raster, shapefile in clip_dict.iteritems(): out_raster = os.path.join(out_ws, os.path.basename(raster)) arcpy.Clip_management(raster, "", out_raster, shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT") print 'Clipped {0}'.format(os.path.basename(raster)) outputList.append(out_raster) # output list is already a list, so no need to wrap it in [ ] sumRaster = CellStatistics(outputList, "SUM", "DATA") sumRaster.save(r'E:\Test_awd') print "Created Sum Raster" arcpy.CheckInExtension('Spatial')
import arcpy, os, sys from arcpy import env from arcpy.sa import * arcpy.env.overwriteOutput = True arcpy.CheckInExtension("Spatial") raster_ws = r'E:\Test_awd\Criteria_SP_2mm_v2' shp_ws = r'E:\Test_awd\Season1' # output workspace out_ws = r'E:\Test_awd' # get lists arcpy.env.workspace = raster_ws rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()] arcpy.env.workspace = shp_ws shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()] # get dictionary {raster1 : shapefile1.shp, ...} clip_dict = dict(zip(rasters, shapes)) # clip all rasters outputList = [] for raster, shapefile in clip_dict.iteritems(): out_raster = os.path.join(out_ws, os.path.basename(raster)) arcpy.Clip_management(raster, "", out_raster, shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT") print 'Clipped {0}'.format(os.path.basename(raster)) outputList.append(out_raster) for raster in outputList: sumRaster = CellStatistics(outputList, "SUM", "DATA") sumRaster.save(r'E:\Test_awd\test.tif') print "Created Sum Raster"
import arcpy from arcpy import env from arcpy.sa import * env.workspace = r'E:\Test_awd\output' Raster01 = "RE_suitable_dek001.tif" Raster02 = "RE_suitable_dek002.tif" Raster03 = "RE_suitable_dek003.tif" Raster04 = "RE_suitable_dek004.tif" Raster05 = "RE_suitable_dek005.tif" arcpy.CheckOutExtension("Spatial") outCellStatistics = CellStatistics([Raster01, Raster02, Raster03, Raster04, Raster05], "SUM", "DATA") outCellStatistics.save(r'E:\Test_awd\output.tif')