Thursday 21 July 2016

arcpy - How should the raster calculator be used?


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:


Syntax


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
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?



Answer



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
dsm_5_raster.save(dsm_5_path)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...