# 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")
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')
I tried revising the code this morning and run it again, but the process is stuck on sumRaster = CellStatistics(ouputList, "SUM", "DATA"). Is this an indication that we cannot use list of raster values in Cellstatistics function?
[in_raster_or_constant,...] A list of input rasters for which a statistic will be calculated for each cell within the Analysis window.
# output list is already a list, so no need to wrap it in [ ] sumRaster = CellStatistics(outputList, "SUM", "DATA") sumRaster.save(r'E:\Test_awd')
# you do not want to have this in a loop, remove the for loop
for raster in outputList: # take this out
sumRaster = CellStatistics(outputList, "SUM", "DATA")
sumRaster.save(r'E:\Test_awd\test.tif')
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "E:\Python_scripts\Clip\rasterClip_with_cellstats.py", line 32, in <module>
sumRaster = CellStatistics(outputList, "SUM", "DATA")
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 2931, in CellStatistics
ignore_nodata)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 2927, in Wrapper
[function] + Utils.flattenLists(in_rasters_or_constants))
RuntimeError: ERROR 000824: The tool is not licensed.
import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckInExtension("Spatial")
arcpy.CheckOutExtension("Spatial")