A quick tour of using map algebra in Image Analyst

Available with Image Analyst license.

Available with Spatial Analyst license.

Map algebra allows you access to operators, functions, and classes through algebra. In its most basic form, an output raster is specified before an equal sign (=), and the tools, operators, and their parameters are after it. The following is an example:

from arcpy.ia import *
elevationPlus100 = Plus("inelevation", 100)

The statement above adds 100 units to an elevation dataset, and creates a Raster object named elevationPlus100 to store the results.

Map algebra can run simple statements, but the power of the language is realized when creating complex statements and models. As map algebra has been integrated in Python, all the functionality of Python and ArcPy and its extensions (modules, classes, functions, and properties) are available to you, the modeler.

As your needs grow, you can explore many of the facets of map algebra. The following quick tour will give you the essentials to get started.

Map algebra basics

There are three ways to use map algebra:

  • The Raster Calculator tool
  • The Python window
  • A Python integrated development environment (IDE)

Raster Calculator

The Raster Calculator tool runs map algebra expressions. The tool has a calculator interface from which most map algebra statements can be created by clicking buttons. This tool can be used as a stand-alone tool, and it can also be used in ModelBuilder. As a result, the tool allows map algebra to be integrated into ModelBuilder.

Raster Calculator tool user interface

In the expression above, three rasters are combined by multiplying the second and third rasters, and adding that result to the first. Operators follow a defined order of precedence.

The Raster Calculator tool is not intended to replace other Image Analyst or Spatial Analyst tools. Continue to use the other tools for the appropriate calculations. For example, use the Weighted Sum tool to overlay several weighted rasters. The Raster Calculator tool is designed to run single-line algebraic statements.

As mentioned above, the Raster Calculator tool can be integrated into ModelBuilder. See the following topics for more information:

Python window

Using the Python window interactive console, you can run Python code directly from ArcGIS Pro through a Python interpreter, without requiring a script file. The Python code you run from this window can range from a single line to complex, multiline blocks of code. The following video provides an overview of the Python window.

To open the Python window, click the Python button Show Python window in the Geoprocessing group on the Analysis tab, or from the Windows group on the View tab.

Python window example

In the sequence of statements above, the ArcPy site package, geoprocessing environments, and Image Analyst modules are imported, the workspace is set, and the Classify Raster tool is run. Once a carriage return is entered at the end of a statement, that statement is immediately run.

Some features of the Python window include built-in line autocompletion, use of variables, and access to Python and ArcPy functionality.

Python integrated development environment

Even though there is no limit to the number of statements that can be entered in the Python window in ArcGIS Pro, it may be cumbersome to create complex models. The Image Analyst modules' tools, operators, functions, and classes can also be accessed from an IDE. Start an IDE and enter the desired statements.

In the following script, ArcPy, the geoprocessing environments, and the Image Analyst module are imported; variables are set; the extension is checked out; the Classify Raster tool is run; and the output is saved.

# Name: Image Classification
# Description: 
# Requirements: Image Analyst Extension


# Import system modules
import arcpy
from arcpy import env
from arcpy.ia import *

# Check out the ArcGIS Image Analyst extension license
arcpy.CheckOutExtension("ImageAnalyst")

# Set environment settings
env.workspace = "C:/data"

# Set local variables
inRaster = "Landsat8_Redlands.tif"
classification_file = "LandCover.ecd"

# Run Classify Raster
outLandCover = ClassifyRaster(inRaster, classification_file)

# Save the output
outLandCover.save("C:/data/Landcover.tif")

As with the Python window, an IDE will provide access to all available Python and ArcPy functionality.

Operators

Map algebra supports a series of operators (for example, +, -, and *). These same operators also exist in Python but are modified for map algebra to handle Raster objects differently. For example, the following adds two numbers together into a variable:

# set outVar to 14 using the Python + operator
outVar = 5 + 9

To distinguish that the statement should work on rasters (that is, to use the map algebra operator), you must cast the dataset as a Raster object. The following example uses the map algebra + operator to add two rasters together:

outRas = Raster("inras1.tif") + Raster("inras2.tif")

Operators can accept a mixture of rasters and numbers. For example, the following adds a constant value of 8 to all the cells in the input raster:

outRas = Raster("inras1.tif") + 8

Complex expressions

Tools and operators can be strung together in a single statement. The following example runs several tools and operators in each expression:

outRas = Slope("indem" * 2) / 57
outdist = EucDistance(ExtractByAttributes("inras", "Value > 105"))

Parentheses can be used to control the order of processing. Consider the following two examples, which use the same operators but yield different results due to the use of parentheses:

outRas1 = (Raster("inras1") + Raster("inras2")) / Raster("inras3")

and

outRas2 = Raster("inras1") + Raster("inras2") / Raster("inras3")

In the first statement, inras1 is added to inras2, and the result is divided by inras3. Without the parentheses, as in the second statement, inras2 would be divided by inras3, and the result would be added to inras1.

Suggestions for map algebra statements

In all the map algebra examples shown below, the output is a Raster object. The Raster object points to a temporary raster dataset that, unless it is explicitly saved, will be removed when the ArcGIS session ends. To permanently save the temporary dataset, the save method is called on the Raster object (see the two examples below).

The following example demonstrates the workspace environment:

import arcpy 
from arcpy import env 
from arcpy.ia import *

env.workspace = "C:/data" 

outLandCover = ClassifyRaster("Landsat8_Redlands", "LandCover.ecd")

outLandCover.save("RedlandsLandcover")

In the statement above, the workspace is set, and RedlandsLandcover will be saved in C:/data.

Additional information

To learn more about ArcPy, explore these topics:

For more information about geoprocessing in Python, see the following topics:

Related topics