Available with Spatial Analyst license.
Like all languages, 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
Map Algebra components
Map Algebra expressions are made up of five components. Those are: tools and functions, operators, inputs, tool parameters, and output data.
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:/Data/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. See the following example:
# In the following statement outVar will be set to 15
outVar = 6 + 9
See An overview of the Map Algebra Operators for a full list of operators available for Spatial Analyst.
Input elements
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 a 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 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")
Note:
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. You can add comments to your script by starting a line with the pound sign (#):
# The following statement takes the slope of the input elevation
outRas = Slope("elevation")