Select to view content in your preferred language

If empty output then....

2105
1
09-09-2010 05:53 AM
AaronBarkhurst
Emerging Contributor
I am trying to implement an if statement that will allow a model to take one of two directions if the output of a tool is empty (e.g. a feature is selected by it's location within a buffer. there may not be any features inside of that buffer). this is where i need to have the model go to a different tool. I do not know any python, so I wondered if anyone could give me an example, or something to start with?

Gratefully,

Aaron
0 Kudos
1 Reply
ChrisFox3
Frequent Contributor
Hi Aaron,

Here would be your high-level workflow:

1. Write the script to contain the if-then-else logic. Below is a sample python code based on your example of determining if the feature is empty or not:
import arcpy

inputFC = arcpy.GetParameterAsText(0) #Input for the script tool

#Evaluate if input has greater than 0 features
if (arcpy.GetCount_management(inputFC) > 0):
    arcpy.SetParameterAsText(1, "true") #Output Boolean parameter for feature count greater than 0, set to true
    arcpy.SetParameterAsText(2, "false")#Output Boolean parameter for feature count equal to 0, set to false
    arcpy.AddMessage("Feature count greater than 0") 

#If it does not have greater than 0 features it will have 0
else:
    arcpy.SetParameterAsText(1, "false")#Output Boolean parameter for feature count greater than 0, set to false
    arcpy.SetParameterAsText(2, "true")#Output Boolean parameter for feature count equal to 0, set to true
    arcpy.AddMessage("Feature count equal to 0") 


2. In ArcMap or Catalog create a new Toolbox and add a New Script Tool. See the Adding a script tool topic for more information.

3. The Script Tool will have 3 parameters. First will be the input feature class or feature layer, second and third will be for the boolean outputs we created in the python script. Understanding script tool parameters is a good help topic on this process.

4. Add the Script to your model and set up the preconditions based on the boolean output parameters of your script tool.

-Chris
0 Kudos