Using If-Then-Else logic for branching
If-then-else logic is a simple yet powerful concept for performing different actions based on different conditions. If-then-else logic can be explained as such: IF some condition is true, perform an action; ELSE the condition is false, perform a different action.
Using if-then-else logic in ModelBuilder
In ModelBuilder, if-then-else logic can be implemented by writing a script tool that tests some condition, then outputs two Boolean variables that describe the true and false condition and incorporating this script tool in a model. As an alternative to writing a script tool, you can also use the Calculate Value tool to test the condition and output a Boolean.
The model below incorporates a script tool called Check Coordinate System to use branching logic. 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 model defines a projection 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.
Examples of if-then-else logic
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 if (true) condition and the other representing the else (false) condition.
The example checks if input data is in StatePlane, has no PRJ, or one other than StatePlane.
# Import modules import arcpy import sys import traceback # Set local variables prj = "" indata = "C:/ToolData/well.shp" dsc = arcpy.Describe(indata) sr = dsc.spatialReference prj = sr.name.lower() try: # check if indata is in StatePlane, has no PRJ, or one other than StatePlane if prj.find("_stateplane_") > -1: # Set the Is Unknown parameter to FALSE, and the Is StatePlane parameter to TRUE arcpy.SetParameterAsText(1,"false") #The first parameter refers to the "Is Unknown" variable arcpy.SetParameterAsText(2,"true") #The second parameter refers to the "Is StatePlane" variable arcpy.AddMessage("Coordinate system is StatePlane") elif prj == "unknown": # Set the Is Unknown parameter to TRUE, and the Is StatePlane parameter to FALSE arcpy.SetParameterAsText(1,"true") arcpy.SetParameterAsText(2,"false") arcpy.AddMessage("To continue, first define a coordinate system!") else: # Set the Is Unknown parameter to FALSE, and the Is StatePlane parameter to FALSE arcpy.SetParameterAsText(1,"false") arcpy.SetParameterAsText(2,"false") arcpy.AddMessage("Coordinate system is not StatePlane or Unknown") except: tb = sys.exc_info() tbinfo = traceback.format_tb(tb) pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value) arcpy.AddError("Python Messages: " + pymsg + " GP Messages: " + arcpy.GetMessages(2))