# Map Algebra Formula created in 9.3.1 Malfunctions in 10.0

110
7
06-02-2011 01:53 PM
New Contributor
I have a Map Algebra formula that works perfectly in the old Spatial Analyst Raster Calculator in 9.3.1 that has a Python error (Python syntax error: Parsing error : invalid syntax) in 10.0
Not sure if the syntax needs revision or what. I also have multiple rasters being created by the script and wasn't sure how the changes in the new Raster Calculator would handle them.  Here is a sample of the formula:

[nbgrs] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 100 AND [caca2fmc] < 120 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
[nbgsh] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 120 AND [caca2fmc] < 140 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
[nbshb] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 140 AND [caca2fmc] < 160 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
[nbtbu] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 160 AND [caca2fmc] < 180 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
[nbtbl] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 180 AND [caca2fmc] < 200 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
[nbslb] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 200 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )
outnonbrn = ( [nbgrs] + [nbgsh] + [nbshb] + [nbtbu] + [nbtbl] + [nbslb] )

Any help would be definitely appreciated!  😄

😞 I've read about why the changes to Raster Calculator were applied to 10.0 which I found troubling (especially when it was stated that most of the user community has accepted and has adapted to the new Raster Calculator).  I work in a multi-agency federal government environment with several hundred users and we definitely have not adapted!!  This leads me to the fact that this decision was not thought out well and did not have a majority of users in support! :confused:
Tags (2)
7 Replies
MVP Esteemed Contributor
Check the syntax in this protracted thread, square brackets have been replaced by double quotes etc, you should be able to piece together a working solution quickly
New Contributor
Hi Dan,
Thanks for your reply and your assistance.  I think I have a clearer picture on how the new syntax works in Raster Calculator.  Do you have any suggestions on this formula?

nbgrs = Con(( �??caca2fm40�?� < 100 ) & ( �??caca2fmc�?� > 100 & �??caca2fmc�?� < 120 ), Abs( �??caca2fm40�?� �?? �??caca2fmc�?� ), 0 )

I still get a "Pythox syntax error: Parsing error" message.

What I'm trying to do is find the grid cell values that occupy the same location in 2 raster data layers ("caca3fm40" and "caca2fmc") that match the stated criteria and then perform an Abosolute math process on them (subtraction) and then save the results in a raster named "nbgrs".
Thanks again!
Best regards,
- Craig
MVP Esteemed Contributor
Esri Contributor
Hello Craig,

The Raster Calculator geoprocessing tool in ArcGIS 10 is indeed not the same as the Raster Calculator toolbar tool in ArcGIs 9.x. A more detailed explanation is avaialable in the Raster Calculator tool help as well as a blog post introducing the New Raster Calculator tool.

Aside from that, a couple of key points to get you started here are:

1) The Map Algebra in the new Raster Calculator tool uses Python, whereas the syntax of the old Raster Calculator was based off of Workstation Grid commands.)
2) In the new Raster Calculator tool, you specify the expression and the output raster seperately in the dialog (in a way, similar to the old Single Output Map Algebra tool), whereas for the the old Raster Calculator, the expression and output were specified in a single line.

Some things to know about Python:

1) Python is case sensitive. Each tool name must always start with a capital letter, with other letters of the tool name cased appropriately. For example, type Con, not con; And, not and; SetNull not setnull, etcetera.
2) Strings, such as dataset names, must be in quotes (for example, "caca2fm40", or 'caca2fm40').
3) The And operator in Python performs a Bitwise And, not a Boolean And. When working with rasters in an expression, use the & operator instead.
4) When using multiple boolean and relational operations in a row in an expression, parentheses must be used to indicate the order of operation. For example, type ("caca2fm40" > 100) & ("caca2fmc" < 120) instead of "caca2fm40" > 100 & "caca2fmc" < 120.

Taking one of the formulas in your post as an example:

[nbgrs] = CON(( [caca2fm40] < 100 ) AND ( [caca2fmc] > 100 AND [caca2fmc] < 120 ), ABS( [caca2fm40] - [caca2fmc] ), 0 )

In the new Raster Calculator tool, the Map Algebra expression would be:

Con((("caca2fm40" < 100) & (("caca2fmc">100) & ("caca2fmc" < 120))),Abs("caca2fm40" - "caca2fmc"),0)

The second parameter, Output raster, is where you supply the output path and name. ("ngbrs", for this example).

You can perform your analysis using multiple Raster Calculator expressions and then combine the results as follows:

"nbgrs" + "nbgsh" + "nbshb" + "nbtbu" + "nbtbl" + "nbslb"

Another solution is to write a small Python script, which I have attached. Just add your layers to the table of contents, right click in the Python Window and load the script, then press enter (modify the output path if necessary, currently the output path is set to C:/temp).
MVP Esteemed Contributor
Bill may be on to something...with the caveat that square brackets are replaced with double quotes and, "AND" is replaced with &  if using version 10
Regular Contributor III
Like you said Dan, I too think Bill is on to something.

The thought of converting my existing v9.x Python SA code (mostly expresions for the SOMA and MOMA tools) to v10 makes me = 🙂 * -1000000.

What added(?) functionality does the new raster algebra syntax allow for? That's not really clear to me...
MVP Esteemed Contributor
The only thing that I can think of that is on the plus side is that they seem to be sticking with Python, which...according to Guido...won't change significantly for at least 3 years...and we aren't even ported to 2.7 yet and the up-porting to 3.x Python isn't too onerous.  My feeling is, the language-de-jour days are getting a bit stale.  Since the GIS community didn't stick with Fortran ( 🙂 ) at least open source languages offer a salvation for code-porting....and Python seems to be it.