HOW TO
The Spatial Analyst tools can be executed using the Spatial Analyst ArcPy module to perform raster analysis. When writing a Python script, it is possible to drag and drop a tool into the Python console to generate a line of code. Although the line of code is generated for the Raster Calculator tool, the execution returns an empty output, or fails with the following error message:
Error: AttributeError: module 'arcpy.sa' has no attribute 'RasterCalculator'The Raster Calculator tool is not intended for use in scripting environments and is not available in the standard Spatial Analyst ArcPy module. It is designed for use only in the application as a geoprocessing tool or in ModelBuilder. However, it is possible to deploy the Spatial Analyst Arcpy module for use with Map Algebra operators in Python scripting. Refer to ArcGIS Pro: Working with operators in Map Algebra for more information.
The sample script below represents the workflow to modify pixel values using the multiplication operation on all the rasters in a folder.
import arcpy from arcpy import env from arcpy.sa import *
arcpy.env.workspace = r"<location_path>\<name>.gdb" arcpy.env.scratchWorkspace = r"<location_path>\<name>.gdb"
raster_list = arcpy.ListRasters("*") print (raster_list)
try: if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") print ("Checked out \"Spatial\" Extension") else: raise LicenseError except LicenseError: print "Spatial Analyst license is unavailable" except: print arcpy.GetMessages(2)
for raster in raster_list: ras = Raster(raster) filename = str(raster)[:-3] + '<desired file extension>' outraster = ras * <desired_value> outraster.save(r"<location_path>\<name>.gdb" + "\\" + filename)
Artikel-ID:000022418
Unterstützung durch ArcGIS-Experten anfordern
Esri Support App herunterladen