If-then-else logic is a powerful concept for performing different actions based on different conditions. If-then-else logic can be explained as such:
- IF some condition is true
- THEN perform an action
- ELSE the condition is false
- perform a different action
In ModelBuilder, if-then-else logic cannot be added directly without some help from Python. You can use the Calculate Value tool to test conditions and output Boolean true and false variables. Similarly, you can write a Python script that tests conditions and outputs Boolean true and false variables, make the script into a geoprocessing script tool, and add the script tool to your model.
The model below incorporates branching logic through a script tool called Check Coordinate System. This script tool evaluates an input dataset and tells you if that dataset has a projected State Plane coordinate system or an unknown coordinate system. In the model, if the input dataset is found to have a projected State Plane coordinate system, nothing happens. However, if the input dataset is found to have an unknown coordinate system, the Is Unknown variable is set to True. Since Is Unknown is true, the precondition is true, and the model defines a coordinate system and projects the input data. One of the key steps in using branching logic in ModelBuilder is setting one of the conditional outputs as a precondition to further processing.
The following code sample shows how if-then-else branching is implemented in the Check Coordinate System script tool referenced above. The script outputs two variables: one representing the True condition and the other representing the False condition.
Check coordinate system example
The example checks if input data is in StatePlane projection, unknown coordinate system, or other.
import arcpy # Get the data path and describe its coordinate system # data = arcpy.GetParameterAsText(0) prj = arcpy.Describe(data).spatialReference.name.lower() # Check if indata is in StatePlane, has no PRJ, or something other than StatePlane # Parameter index 1 refers to the "Is Unknown" variable # Parameter index 2 refers to the "Is StatePlane" variable # if prj.find("_stateplane_") > -1: # Set the "Is Unknown" parameter to FALSE, and the "Is StatePlane" parameter to TRUE # arcpy.SetParameter(1, False) arcpy.SetParameter(2, True) arcpy.AddMessage("Excellent. Coordinate system is StatePlane.") elif prj == "unknown": # Set the "Is Unknown" parameter to TRUE, and the "Is StatePlane" parameter to FALSE # arcpy.SetParameter(1, True) arcpy.SetParameter(2, False) arcpy.AddMessage("To continue you must project your data!") else: # Set the "Is Unknown" parameter to FALSE, and the "Is StatePlane" parameter to FALSE # arcpy.SetParameter(1, False) arcpy.SetParameter(2, False) arcpy.AddMessage("Nice try. The coordinate system is neither StatePlane nor unknown.")
There are two logical tools in ModelBuilder that help you control the flow of processing. To add a logical tool, click ModelBuilder > Insert > Logical and select the tool you wish to add.
The Merge Branch tool merges two or more logical branches into a single output.
For the set of input values, iteration will stop if all the input values meet the specified condition of either True or False. It is functionally similar to the While iterator but is useful to stop a model if there is one While iterator in your model and no additional iterators can be added.