Raster Calculator Python

13024
4
11-28-2011 03:11 AM
JohanL
by
New Contributor
Hi Everyone,

I am trying to implement a simple raster calculator within my python script as the following
arcpy.gp.RasterCalculator_sa("\"%Temp4%\"/2", Temp2)


But I am receiving the following error in python

"Warning (from warnings module):
  File "<string>", line 1
SyntaxWarning: import * only allowed at module level

Traceback (most recent call last):
  File "F:\test.py", line 75, in <module>
    arcpy.gp.RasterCalculator_sa("\"%Temp4%\"/2", Temp2)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
    return lambda *args: val(*gp_fixargs(args))
ExecuteError: ERROR 000539: Error running expression: rcexec() <type 'exceptions.TypeError'>: unsupported operand type(s) for /: 'str' and 'int'
Failed to execute (RasterCalculator)."

I am using arcpy.CheckOutExtension("spatial") so im not sure what the first point of the error is implying!

This is really frustrating... any help would be appreciated!

Bjebn
Tags (2)
0 Kudos
4 Replies
RaphaelR
Occasional Contributor II
according to the help, the raster calculator is for use inside arcmap only, for calculations in python you should use mapalgebra:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z000000z7000000.htm
0 Kudos
JohanL
by
New Contributor
according to the help, the raster calculator is for use inside arcmap only, for calculations in python you should use mapalgebra:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z000000z7000000.htm


Ah of course... i did the mistake of exporting the script from the modelbuilder and tried to modify it -  what a frustrating mistake!
0 Kudos
JohanL
by
New Contributor
I am still having problems with map algebra... its probably an obvious mistake but I cant identify it!

Here is a part of the code im having trouble with...


import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("spatial")

arcpy.env.workspace = r'O:\DATA\test'
InputFolder = arcpy.env.workspace

Temp4 = InputFolder + '\\' + temp4

RasterList = arcpy.ListRasters()
    for RasterImage in RasterList:
    Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
    print ('Focal Statistics')
.....        


I receive the following error
"Traceback (most recent call last):
  File "O:\test.py", line 67, in <module>
    Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 4796, in FocalStatistics
    ignore_nodata)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 4790, in wrapper
    ignore_nodata)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
    return lambda *args: val(*gp_fixargs(args))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster: RasterImage does not exist.Failed to execute (FocalStatistics)."

It does not seem to recognize my for in loop of the RasterImage - any ideas?
0 Kudos
RaphaelR
Occasional Contributor II
try removing the ' before and after RasterImage in the Temp4 = FocalStatistics()... line. It´s a variable, i think they way you´ve got it in  there it´s looking for a string
0 Kudos