Working with operators in Map Algebra
In Map Algebra, operators apply a mathematical operation on input rasters and numbers.
Operators are generally placed between two inputs (operands) to perform a mathematical operation (for example, outVar = 3 + 7). In Map Algebra, operands can be rasters or numbers. To use an operator with a raster, the raster must be a Raster object.
The table below provides a quick reference indicating how current Map Algebra operators have been implemented in relation to Python operators and previous 9.x Map Algebra operators.
Operation | Python operator |
Map Algebra operator |
9.x Map Algebra operator * |
Spatial Analyst GP tool |
---|---|---|---|---|
Arithmetic | ||||
Addition | + | + | + | |
Division | / |
/ |
/, div | |
Integer Division | // |
// |
N/A |
N/A |
Modulo | % | % | Mod | |
Multiplication | * |
* |
* | |
Power | ** |
** |
N/A | |
Subtraction | - |
- |
- | |
Unary Minus | - |
- |
- | |
Unary Plus | + |
+ |
N/A |
N/A |
Boolean | ||||
Boolean And | N/A |
& |
&, and | |
Boolean Complement | N/A |
~ |
^, not | |
Boolean Exclusive Or | N/A |
^ |
!, xor | |
Boolean Or | N/A |
| |
|, or | |
Relational | ||||
Equal To | == | == | ==, eq | |
Greater Than | > | > | >, gt | |
Greater Than And Equal To | >= |
>= |
>=, ge | |
Less Than | < | < | <, lt | |
Less Than And Equal To | <= |
<= |
<=, le | |
Not Equal To | != |
!= |
^=, <>, ne | |
Bitwise | ||||
Bitwise And | & | N/A | && | |
Bitwise Complement | ~ | N/A | ^^ | |
Bitwise Exclusive Or | ^ | N/A | !! | |
Bitwise Left Shift | << | << | << | |
Bitwise Or | | | N/A | || | |
Bitwise Right Shift | >> | >> | >> |
Operator rules
- When only numbers are used with operators, the result will be a number.
# outVar will be assigned 10 outVar = 3 + 7
- When using operators with rasters, the raster must be a Raster object.
outRas = Raster("inraster1") + Raster("inraster2")
- When a raster operand is used, then the result is a Raster object.
# In the following statement, 4 is added to each cell value in inraster1 outRas = Raster("inraster1") + 4 outRas2 = Raster("inraster") + math.pi
In the above statement, pi is used from the Python math module. The math module also includes the base of the natural logarithm, math.e, which can also be used in a Map Algebra statement.
- Some operators can precede a Raster object or a number.
outRas = -Raster("inraster")
- The Boolean (~, &, ^, |) operators will perform a Boolean operation when one or more input (operand) is a raster. If both inputs (operands) are numbers, then these operators will perform Bitwise operations.
Spaces are not necessary between operators but are recommended for readability.
Tools and operators can be nested to create complex statements.
Operator precedence
The precedence value determines the order in which operators are executed. The operator with the higher precedence will be processed first. If two operators have the same precedence value, then they will be processed in a left to right order in an expression.
You can use parentheses to override the precedence priority, with the operation in the deepest parentheses being processed first no matter what operator is specified.
The following table lists all the Map Algebra operators in order from lowest precedence to highest precedence. Each of the operators that are listed in the same row have the same precedence.
Map Algebra operator |
Reference |
---|---|
<, <=, >, >=, ==, != |
Less Than, Less Than Equal, Greater Than, Greater Than Equal, Equal To, Not Equal |
| |
|
^ |
|
& |
|
<<, >> |
|
+, - |
|
*, /, //, % |
|
+, -, ~ |
|
** |