Overview of the rules for Map Algebra

Like all languages, whether for speech, computer programming, or other means of communication, Map Algebra has specific rules for combining the language components.

The general structure of a Map Algebra statement is an assignment operator (=), which is used to separate the action to its right from the name of the output (a raster object) to its left. A statement takes on the following algebraic form:

Output_Name = Action

The Map Algebra components

Map Algebra expressions are made up of five components.

Tools and functions

Geoprocessing tools and functions (such as Sin, Slope, and Reclassify) are used in Map Algebra expressions.

For example, to calculate the aspect for each cell in a raster, use the Aspect tool:

outRas = Aspect("C:/Datat/inraster")

Operators

Operators are symbols used to represent mathematical operations to be applied to the input.

For example, the following is used to add two rasters together:

outRas = Raster("inraster1") + Raster("inraster2")

Many operators are used in both Map Algebra and Python; the operators are overloaded. When applying operators to raster datasets, you must cast the dataset using Raster, as shown above. Casting the raster indicates that the operation should be applied to rasters, thus calling the Spatial Analyst implementation of the operator, which results in an output raster object.

If only numbers are entered into a statement, the Python implementation of the operator will be used, producing an output number. For example:

# In the following statement outVar will be set to 15
outVar = 6 + 9

Input

The input elements, such as rasters, features, numbers, constants, objects, and variables, are the components on which the statement is applied.

For example, use the following to apply the sine calculation to each cell in an input raster:

outRas = Sin("inraster")

Tool parameters

Tool parameters define how the tool is to be executed. Each tool has its own unique set of parameters. Some parameters are required; they must have values for the tool to execute. Other parameters are optional, meaning you can leave them blank and/or accept their default values.

For example, to calculate the slope of a raster, set the optional output measurement parameter with the PERCENT_RISE keyword:

outRas = Slope("inraster", "PERCENT_RISE")

Output data

The output from a Map Algebra expression will be a Raster object referencing temporary raster data.

For example, the following will store the results from the slope calculations as a Raster object named outRas:

outRas = Slope("inraster")
NoteNote:

As with all geoprocessing tools, Map Algebra statements honor the environment settings and the selected set.

Consistent with Python, commas are required between each input parameter; spaces are optional between arguments but are recommended for readability. Comments can be added to your script by starting a line with the pound sign (#):

# The following statement takes the slope of the input elevation
outRas = Slope("elevation")

Related Topics

3/7/2014