# Working with operators

In Map Algebra, operators apply a mathematical operation on input rasters and numbers.

Operators are generally placed between two inputs to perform a mathematical operation (for example, outVar = 3 + 7). In Map Algebra, operators have been overloaded, allowing operators to be used with Raster objects. To utilize an operator with a raster dataset, the dataset name must be cast as a Raster object, indicating that the Spatial Analyst implementation of the operator should be used.

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

+

+

+

Plus

Division

/

/

/, div

Divide

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 And

Boolean Complement

N/A

~

^, not

Boolean Exclusive Or

N/A

^

!, xor

Boolean XOr

Boolean Or

N/A

|

|, or

Boolean Or

Relational

Equal To

==

==

==, eq

Equal To

Greater Than

>

>

>, gt

Greater Than And Equal To

>=

>=

>=, ge

Less Than

<

<

<, lt

Less Than

Less Than And Equal To

<=

<=

<=, le

Not Equal To

!=

!=

^=, <>, ne

Not Equal

Bitwise

Bitwise And

&

N/A

&&

Bitwise And

Bitwise Complement

~

N/A

^^

Bitwise Not

Bitwise Exclusive Or

^

N/A

!!

Bitwise XOr

Bitwise Left Shift

<<

<<

<<

Bitwise Or

|

N/A

||

Bitwise Or

Bitwise Right Shift

>>

>>

>>

Bitwise Right Shift

* 9.x Map Algebra operator refers to the operator symbol you would have used in the Single Output Map Algebra tool or the Raster Calculator in the Spatial Analyst toolbar in ArcGIS versions 9.x and prior.

## Operator rules

• When operators are only used with numbers, then the Python operator is used and acts accordingly. As shown below, the following operation will return a number.
```# outVar will be assigned 10
outVar = 3 + 7 ```
• When applying an operator to rasters, the dataset names must be cast by calling the Raster class constructor to identify that the statement should use the Spatial Analyst operator to process the spatial datasets. As shown below, the operation will return a Raster object.
`outRas = Raster("inraster1") + Raster("inraster2")`
• If a raster is to be used in conjunction with a number, the Spatial Analyst operator will be used.
```# In the following statement 4 is added to each cell value in inraster1
outRas = Raster("inraster1") + 4  ```
• Some Spatial Analyst operators can precede the input dataset.
`outRas = -Raster("inraster")`
• Python constants can be used with operators in Map Algebra statements.

```from arcpy.sa import *
import math
outRas = 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.

Tools and operators can be nested to create complex statements.

Tip:

Spaces are not necessary between operators but are recommended for readability.

## Operator precedence

The precedence value determines the order in which operators are executed. The operator with the higher precedence will be processed first. 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 identifies the precedence value defining the priority order for processing each operator.

Map Algebra operator

Description

Precedence

Arithmetic

- (Subtraction)

Subtraction

11

- (Negate)

Unary minus

14

% (Modulo)

Modulus

13

* (Multiplication)

Multiplication

13

/ (Division)

Division

13

// (Integer Division)

Integer divide

13

12

+ (Unary Plus)

Unary plus

14

** (Power)

Power

16

Bitwise

>> (Bitwise Right Shift)

Bitwise right shift

11

<< (Bitwise Left Shift)

Bitwise left shift

11

Boolean

~ (Boolean Not)

Boolean complement

4

& (Boolean And)

Boolean and

3

^ (Boolean XOr)

Boolean exclusive

2

| (Boolean Or)

Boolean or

2

Relational

< (Less Than)

Less than

7

<= (Less Than Equal)

Less than or equal to

7

> (Greater Than)

Greater than

7

>= (Greater Than Equal)

Greater than or equal to

7

== (Equal To)

Equal to

7

!= (Not Equal)

Not equal to

7

## Related Topics

Published 6/7/2010