Available with Spatial Analyst license.
One of the most powerful aspects of Map Algebra is the ability to create a statement comprising multiple operators and tools in a single statement. Being able to enter multiple operators and tools in a single statement allows you to more easily model complex interactions, and overall processing time may be reduced. Complex expressions are not limited to the functionality provided by the tools of the ArcGIS Spatial Analyst; they may include tools from any other toolboxes. When building complex statements, there are specific interaction rules that may be required to execute correctly.
Complex statement rules
- In complex statements, only the output to the left of the equals sign becomes a Raster object.
- The order of execution for an expression is determined by parentheses and the precedence level of the operators used. For more information on operator precedence, see Operator Precedence Table.
outRaster = Raster("inras1") + Raster("inras2") / Raster("inras3")
In the statement above, inras2 is divided by inras3 and the result is added to inras1.
- You can use parentheses to control the order of execution. Parentheses can be nested, in which case the expression within the innermost parentheses will be processed first, regardless of the precedence value of the operators.
outRas = Raster("inras1") / (Raster("inras2") + Raster("inras3"))
In the statement above, inras1 is divided by the sum of inras2 and inras3.
- When using multiple Boolean (~, &, ^, |) and/or Relational (<, <=, >, >=, ==, !=) operators consecutively in a single expression, parentheses should be used. For example, parentheses are required in the following expression (a>2) & (a<5).
outRas = (Raster("a") > 2 ) & ( Raster("a") < 5)
- Operators, variables, numbers, and tools can all be used in complex statements.
outRas = Sin("inras1") + Raster("inras2") + 8
const = 10 outRas = Raster("inras1") + 2 * const
- All rules that apply to parentheses for statements created with operators also apply for those statements created with tools and operators. The tool or operator that is within the deepest nested parentheses will be processed first.
num = 10 outRas = (ZonalStatistics((Raster("inras2") + Raster("inras3")), "Value", "valueras", "MAXIMUM") - num ) / 8
In the statement above, the sum of inras2 and inras3 is used as input to the Zonal Statistics tool. The value of num is then subtracted before being divided.
- In a series of statements, the output from a preceding statement can be used as input in a subsequent statement.
outAdd = Raster("inras1") + Raster("inras2") outRas = FocalStatistics(outAdd, NbrCircle(5, "Map"), "MEAN")
In the example above, outAdd is a raster object created from the addition of inras1 and inras2. Since outAdd is a variable, no quotation marks are required when it is used as input to the subsequent Focal Statistics tool.
- Any tool can be embedded into another tool, whether the result is a raster or feature class. The required output from the embedded tool is used as the input to the outer tool. In this example, the output from the Contour List tool is used as input to the Euclidean Distance tool.
outdistance = EucDistance(ContourList("elevation", "#", [1500]))
In the example above, the output from the Analysis toolbox Select tool is used as input to the Euclidean Distance tool.dist = EucDistance(arcpy.Select_analysis("schools", "#", "Pop>2000"))
- To use an optional output from a tool in an expression, the dataset name or variable representing the dataset must be used. In the example above, the optionally output backlink raster is output in the workspace with the name "out_bklink".
costDist = CostDistance("source", "in_cost", 15000, "out_bklink") costOut = CostPath("dest", costDist, "out_bklink")
In the example above, the backlink raster is defined by a variable before the tool is executed. The variable points to the location and name that the output backlink raster will have.bklink = "C:/results/out_bklink" costDist = CostDistance("source", "in_cost", 15000, bklink) costOut = CostPath("dest", costDist, bklink)