Building complex statements
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 processing speeds may increase. Complex expressions are not limited to Spatial Analyst functionality they may include tools from any other toolboxes. There are specific interaction rules when creating complex statements.
Complex statement rules
- In complex statements, only the output to the left of the equal sign becomes a raster object.
- Each statement in a series of individual statements is delimited by carriage return.
addRas = Raster("inraster1") + Raster("inraster2") outRas = Slope("inraster3") - Precedence values can be overridden using parentheses. The expression within the parentheses is processed first, regardless of the precedence value of the operators.
outRas = Raster("inraster1") / (Raster("inraster2") + Raster("inraster3"))In the statement above, inraster2 is added to inraster3 and the result is divided into inraster1.
- Parentheses can be nested. The expression within the innermost parentheses will be processed first.
outRas = (Raster("inraster1") + (Raster("inraster2") - (Raster("inraster3") >> Raster("inraster4")))) / Raster("inraster5")In the statement above, a right bitwise shift (>>) is performed first.
- Scalar variables and numbers can be used in the creation of a complex statement composed of operators.
scalarVar = 10 outRas = Raster("inraster1") + 2 * scalarVarThe order of precedence is still dependent on the precedence value assigned to the operator.
- Operators, variables, numbers, and tools can all be used in complex statements.
outRas = Sin("inraster1") + Raster("inraster2") + 8 - All rules that apply to parentheses for statements created with operators apply for those statements created with tools and operators. The tool or operator that is within the deepest nested parentheses will be processed first.
scalarVar = 10 outRas = Raster("inraster1") * (ZonalStatistics((Raster("inraster2") \ + Raster("inraster3")), "Value", "valueraster", "MAXIMUM") - scalarVar / 8)In the statement above, inraster2 is first added to inraster3.
- In a series of statements, the output from a preceding statement can be used as input in a subsequent statement.
outadd = Raster("inraster1") + Raster("inraster2") outRas = FocalStatistics(outadd, NbrCircle(5, "Map"), "MEAN")In the example above, outadd is a raster object created from the addition of inraster1 and inraster2. Since outadd is a variable, no quotation marks are required when it is used as input to the subsequent Focal Statistics tool.
- Map Algebra statements outputting feature data, tables, or files are generally specified in the geoprocessing tool format with the output identified in the tool within the parenthesis. The output dataset is written to disk and can be used in subsequent statements.
ContourList("elevation", "C:/outputs/outcontour", [1500]) # outcontour is feature data in the current workspace Outdistance = EucDistance("C:/outputs/outcontour") - Any tool that has output on the right hand side can be embedded into another tool.
outdistance = EucDistance(ContourList("elevation", "#", [1500]))dist = EucDistance(arcpy.Select_analysis("schools", "#", "Pop>2000"))