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: 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.tif")
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.tif") + Raster("inraster2.tif")
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.
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.tif")
Tool parameters define how the tool is to be run. Each tool has a unique set of parameters. Some parameters are required; they must have values for the tool to run. 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.tif", "PERCENT_RISE")
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.tif")
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 in degrees. outRas = Slope("elevation.tif", "DEGREE", 0.3043)