Raster Calculator error using AND / OR

5151
3
03-29-2012 06:46 AM
AnneKull1
New Contributor
Hello!

I have problems using RasterCalculator with ArcGIS10. Raster Calculator give errors when using operators AND and OR. For example when I write
"landuse" == 2 & "slope" < 5
I get an error:
Executing (Raster Calculator): RasterCalculator ""landuse" == 2 & "slope" < 5" C:\Temp\3
Start Time: Thu Mar 29 17:43:04 2012
Raster(r"landuse") == 2 & Raster(r"slope") < 5
ERROR 000539: Error running expression: rcexec() <type 'exceptions.ValueError'>: The truth value of a raster is ambiguous.
Failed to execute (Raster Calculator).
Raster Calculator works fine when I do not use operators AND / OR.
Everything worked fine in ArcGIS 9.3.
Can anyone suggest how to solve the problem? In ArcGIS10 there are not anymore any alternatives to Raster Calculator either.

Best,
Anne
0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
Everything worked fine in ArcGIS 9.3.


In 10.0 there is a new interface to map algebra that leverages Python as a the new interface to the Spatial Analyst tools. This is a pain in the short term, but in the long term this is much better because this new setup replaces no less than four inconsistent interfaces (SOMA, MOMA, Raster Calculator, Tool) to the Spatial Analyst tools in 9.x.

Arc 10 Help:
Working" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html... with Operators
Boolean" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html... And
Comparing" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.ht... Map Algebra between ArcGIS 9.x and 10

As for your syntax, it is not getting parsed right - though perhaps it should be.

Parentheses solve the problem:

("landuse" == 2) & ("slope" < 5)
0 Kudos
AnneKull1
New Contributor
Thank you! Now I can manage. I studied also the material provided by links, but I did not notice the need for using parentheses.

Best,
Anne
0 Kudos
curtvprice
MVP Esteemed Contributor
Thank you! Now I can manage. I studied also the material provided by links, but I did not notice the need for using parentheses.


What tripped you (and has tripped me) up are the Python rules of operator precedence. This must have tripped up a lot of people during testing because the following text is included in the Arc 10 help on Raster Calculator:

When multiple operators are used in an expression, they are not necessarily executed in left-to-right order. The operator with the highest precedence value will be executed first. For more information on operator precedence, see operator precedence table. You can use parentheses to control the execution order.

Boolean (~, &, ^, |) operators have a higher precedence level than Relational (<, <=, >, >=, ==, !=) operators. Therefore, when Boolean operators are used in the same expression as Relational operators, the Boolean operators will be executed first. To change the order of execution, use parentheses.
0 Kudos