Using Raster Calculator in Python Scripts

15467
1
Jump to solution
07-02-2015 04:02 AM
KarolinaKorzeniowska
Occasional Contributor

Hello,

I have a question regarding the using Raster Calculator expressions (+, -, *, /, log, sin) in Python Scripts.

I generated a model in Model Builder and exported them into Python Script. Then I implemented my script as a new tool in a toolbox. However, because in some steps in my model I used Raster calculator the tool does not work. I have an ERROR 000539: Error running expression: rcexec() As I have found in the error link, the problem is that Python Scripts are not using Raster Calculation Expressions.

Does anybody know if there is some way (other than this in the error link) to implement Raster Calculator expressions to Python Scripts?

Below are examples of expressions which I want to use in Python Script:

arcpy.gp.RasterCalculator_sa("(\"%2.tif%\") - (\"%3.tif%\")", v4_tif)

arcpy.gp.RasterCalculator_sa("Cos(\"%5.tif%\")", v6_tif)

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Karonlina,

Take a look at the following link:

A quick tour of using Map Algebra—ArcGIS Help | ArcGIS for Desktop

When using Map Algebra with Python, you will need to perform the raster calculations a little different.  For example, to run a Cos on a raster you would use something similar as below:

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outCos = Cos("degs")
outCos.save("C:/sapyexamples/output/outcos")

View solution in original post

1 Reply
JakeSkinner
Esri Esteemed Contributor

Hi Karonlina,

Take a look at the following link:

A quick tour of using Map Algebra—ArcGIS Help | ArcGIS for Desktop

When using Map Algebra with Python, you will need to perform the raster calculations a little different.  For example, to run a Cos on a raster you would use something similar as below:

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outCos = Cos("degs")
outCos.save("C:/sapyexamples/output/outcos")