Select to view content in your preferred language

Multiple rasters added together using python

7652
9
04-08-2011 12:12 AM
Heatherna
Emerging Contributor
Hi,
I am working on adding several rasters together then dividing them by the number of rasters added (to find the average)

Is it possible to call the raster calculator in a stand alone script? Is it even possible to do this outside of ArcMap (works fine in a model calling the raster, but the snippet of python code doesn't work when placed into a script that is run from IDLE or PythonWIN), or do I have to add two together at a time using Plus repeatedly.

I keep getting this error:

return _wrapLocalFunctionRaster(u"Plus_sa", ["Plus", in_raster_or_constant1, in_raster_or_constant2])
RuntimeError: ERROR 000824: The tool is not licensed.

I am using ArcGIS 10 and python 2.6.

Below is my code

import os, sys, arcpy
from arcpy import env as e
from arcpy.sa import *

a1 = Raster(r"D:\raster1.tif")
a2 = Raster(r"D:\raster2.tif")
a3 = Raster(r"D:\raster3.tif")
a4 = Raster(r"D:\raster4.tif")

outras = (a1+a2+a3+a4)/4
outras.save(r"D:\")

Am I missing an extension?

If I use:

outras = arcpy.RasterCalculator_sa(a1+a2+a3+a4)

I get the error:
AttributeError: 'module' object has no attribute 'RasterCalculator_sa'

Very frustrating.
Thanks in advance for any suggestions

Mac
Tags (2)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus
see the standalone script example in the help file, you need to checkout the extension
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z00000096000000.htm
0 Kudos
Heatherna
Emerging Contributor
Thanks, I tried that and I still get an error as follows:

File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run
    exec cmd in globals, locals
  File "D:\average3.py", line 70, in <module>
    outras = arcpy.RasterCalculator_sa(a1+a2+a3+a4)
AttributeError: 'module' object has no attribute 'RasterCalculator_sa'


Any other thoughts? I keep poking at it but its still not working
0 Kudos
Heatherna
Emerging Contributor
Resolved. I ended up ditching the raster calcuator and used the weightedsum tool instead
0 Kudos
DanPatterson_Retired
MVP Emeritus
why not use the plus syntax

from arcpy.sa import *
outPlus = Plus(inRaster1, inRaster2)

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z00000096000000.htm
for further information
0 Kudos
DarrenWiens2
MVP Alum
I haven't tested this out, but the Raster Calculator help says the syntax is RasterCalculator(...) not RasterCalculator_sa (which is tricky - bad ESRI).
0 Kudos
Heatherna
Emerging Contributor
why not use the plus syntax

from arcpy.sa import *
outPlus = Plus(inRaster1, inRaster2)

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z00000096000000.htm
for further information


I needed to use more than two rasters, the plus tool only allows for two inputs.
0 Kudos
DanPatterson_Retired
MVP Emeritus
0 Kudos
RyanDeBruyn
Esri Contributor
Please note:

The help for the RasterCalculater in 10.0 states: 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. 

The Raster Calculator is not supported in scripting because in ArcGIS 10 Map Algebra can be accessed directly when using the geoprocessing ArcPy site-package.  This seamless integration of Map Algebra into Python extends the capabilities of Map Algebra by taking advantage of Python and third party Python modules and libraries; making Map Algebra far more powerful than it has been in the past.

Reference:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z000000z7000000.htm
http://blogs.esri.com/dev/blogs/geoprocessing/archive/2010/04/28/the-new-and-improved-raster-calcula...


The solution to your problem:
1) You will need to checkout the Spatial Analyst License to use operators or any Spatial Aanlayst tools with rasters. 
arcpy.CheckOutExtension('Spatial')


To obtain your average use one of the following.

A)
outras = (a1+a2+a3+a4)/4
outras.save("averaster')


or

B)
outras = arpcy.sa.CellStatisitics([a1,a2,a3,a4], "MEAN")
outras.save("averaster")


Good luck,
-Ryan
0 Kudos
Hannes_I_Reuter
Emerging Contributor
Are you checking out the spatial analyst extension ?

Running code below!

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.env.scratchWorkspace = "yourscratch"
arcpy.env.workspace = "yourworkspace"
arcpy.CheckOutExtension("Spatial")

final = (Raster('Raster_a_withpath' + Raster('Raster_b_with_Path' + ...) / 4
final.save(youroutrastername)

HTH
P.S.:keep in mind, there are strong differences between Single python and buildin python in AG10 syntax..
P.S.: type dir(arcpy.sa)  helps to identify the avaiable commands
P.S.P.S: help(Raster) helps to identify the syntax for a specific command

PS.PS: Don"t use the "FROM" keyword for example in Reclass Table as it is a Keyword in Python!
0 Kudos