Building expressions in Raster Calculator

When building expressions in the Raster Calculator tool, clicking and double-clicking on the various layers, variables, buttons, and tool names available in the dialog box will help you to avoid syntax errors that may otherwise be made while typing.

Since the expression entered in the Raster Calculator tool will be executed in Python, and Python is case sensitive, it is important to use the correct capitalization of the tool name. For example, to use the IsNull tool, be sure to type it as IsNull, instead of Isnull or isnull.

When multiple Relational or Boolean operators are used in a sequence, the expression requires parentheses. For more information, refer to the usage tips listed in the Raster Calculator tool help.

Example expressions in the Raster Calculator tool dialog box

While Map Algebra is used both in the Raster Calculator and directly in Python, there are some differences in the syntax that you should be aware of.

Raster Calculator tool dialog box examples

Following are several examples of Map Algebra expressions that can be executed in the Raster Calculator tool. In these expressions, the raster layer names are contained within quotes, for example "dist".

  • ("pop" > 150) & ("dist" > 10)
  • (("reclass_rd_dist" * 3) + ("reclass_landuse" * 2) + "reclass_elev") /6.0
  • ("Band4" - "Band3") / Float("Band4" + "Band3")
  • Con("elev" <= 3000, 1, 0)
  • Con(IsNull("elev"),0, "elev")
  • Con(("landuse1" == 1) & ("landuse2" == 5), "landuse1" + "landuse2", 99)

Example Raster Calculator expression in ModelBuilder

When using the Raster Calculator tool in ModelBuilder, the only difference is that variables can be used as input as well as layers. For more information regarding the syntax, what types of variable can be used, and the connectivity behavior of the Raster Calculator tool in ModelBuilder, see the Raster Calculator tool help.

How variables and layers appear in the expression

The example below shows the Map Algebra expression syntax of a Raster Calculator tool being used in a model.

  • In this example, Raster Dataset Variable (Model Variable), a Long Variable (Model Variable), and a Raster Layer (Layer) are being used.

    Notice that the different variables have slightly different syntax:

    Variable or Layer Name

    Variable Type

    How syntax first appears in the expression

    Reclass Road

    Raster Dataset Variable"%Reclass Road%"

    Rd weight

    Long Variable%Rd weight%

    Reclass Landuse

    Raster Layer"Reclass Landuse"

    To have inputs be added with the correct syntax

    1. Place the cursor at the appropriate location in the expression box.
    2. Double-click the appropriate Layer or Variable name.

    Raster Calculator tool with expression using variables and layers
    Raster Calculator tool with expression using variables and layers
  • After clicking OK to close the Raster Calculator tool dialog box, a variable representing the "Reclass Landuse" layer will be added to your model as a model variable.

  • When the Raster Calculator tool dialog box is reopened, notice that:

    1. The Reclass Landuse variable is added to the list of Layers and Variables
    2. The syntax in the expression has been updated from the layer syntax ("Reclass Landuse") to the variable syntax ("%Reclass Landuse%").
    Raster Calculator tool with expression using variables
    Example of Raster Calculator tool with expression using variables

Related Topics


6/29/2011