I have a set of rasters (in a geodatabase) and I'm trying to do a simple operation with them. Please see code below. I'm indicating with an arrow at the end of the code the operation I'm trying to do:

import arcpy
import os
from arcpy.sa import *

script_folder = sys.path[0]
working_folder = os.path.normpath(os.path.join(sys.path[0], '..'))

DSM_model = os.path.join(working_folder, 'Data4Optimimty\ESRI_UK_Bluesky_DSM\ESRI_UK_Bluesky_DSM.gdb', 'DSM_25cm_2015')

dsm_1 = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_1')
dsm_2 = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_2')
dsm_3 = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_3')
dsm_4 = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_4')

dsm_5 = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_5')
=> dsm_5 = DSM_model + dsm_3 - dsm_4

dsm_1, dsm_2, dsm_3 and dsm_4 are all existing rasters with same characteristics (extent, cell size, etc).

In ESRI help section I can find the following:


RasterCalculator (expression, output_raster)

So I tried with the following line:

arcpy.sa.RasterCalculator (DSM_model + dsm_3 - dsm_4, dsm_5)

But that doesn't work. Error:

Traceback (most recent call last):
  File "D:\ArcGIS_Pro_Projects\Optimity_Alf\Arcpy_Scripts\Updating_Raster.py", line 42, in <module>
    arcpy.sa.RasterCalculator (DSM_model + dsm_3 - dsm_4, dsm_5)
AttributeError: module 'arcpy.sa' has no attribute 'RasterCalculator'
Failed to execute (UpdatingRaster).

Any idea on the way I should be using the RasterCalculator?


From the ArcGIS help:

The Raster Calculator tool is intended for use in the ArcGIS Desktop application only as a GP tool dialog box or in ModelBuilder. It is not intended for use in scripting and is not available in the ArcPy Spatial Analyst module.

You need to use the Spatial Analyst module map algebra syntax instead.

#Raster() class is from arcpy.sa
DSM_model = Raster(os.path.join(working_folder, 'Data4Optimimty', 'ESRI_UK_Bluesky_DSM', 'ESRI_UK_Bluesky_DSM.gdb', 'DSM_25cm_2015'))
dsm_1 = Raster(os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_1'))

dsm_5_path = os.path.join(working_folder, 'Optimity_Temp.gdb', 'dsm_5')
dsm_5_raster = DSM_model + dsm_3 - dsm_4
  • Good pointer--any chance you want to tackle the OP's raster algebra problem? – Aaron Dec 17 '17 at 19:37

Your syntax is incorrect, not the logic but how you call the tool providing inputs. A very simple way of getting it to work and to help you understand the syntax is to run the tool by itself from ArcToolbox then go to the geoprocessing results and right click on the tool and copy snippet. This will copy the code as how you would call it in a scripting environment. As you know the tool ran without error you would then see how to construct the code and call the tool correctly.

  • I still can't manage to get it work this way (using 'RasterCalculator'). The code I got from the tool box is: output_raster = arcpy.sa.RasterCalculator(r' "DSM\DSM_25cm_2015" + "D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Temp.gdb\dsm_3" - "D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Temp.gdb\dsm_4"'); output_raster.save(r"d:\arcgis_pro_projects\optimity_alf\optimity_temp.gdb\dsm_6") – Pitrako Junior Dec 18 '17 at 17:17
  • Not sure how my piece of code should look like: updated_raster = arcpy.sa.RasterCalculator(DSM_model + dsm_3 - dsm_4, updated) [doesn't work] updated_raster = arcpy.sa.RasterCalculator('DSM_model + dsm_3 - dsm_4', updated) [doesn't work] – Pitrako Junior Dec 18 '17 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.