If Then Else in model builder

4536
4
08-14-2013 01:49 AM
DanielHall_Ballester
New Contributor III
Hi all,
I have a model which iterates through feature classes in a geodatabase (image attached)
For each feature class, it runs the check Geometry tool.
I then use the "Get Count" tool to count the number of rows in the output table.
I then want to add an expression where if the number of rows is greater than 0 it should keep running the rest of the model, otherwise it should go on to the next feature class.

I thought this could be achieved by using the Calculate Value tool using the following code.

Expression
CountMe(%Row Count%)


Code Block
def CountMe(n):
 import arcpy
 if n > 0:
  return "true"
 else:
  return "false"


Where %Row Count% is the output from the "Get Count" tool.

This returns True and False values, and I have the output from this set as a precondition to the next steps in the model.

However, I assumed that only "true" data would go through, but all the data seems to go through i.e. the model will push through an empty table from the Check Geometry tool all the way to the end.

I've had a read online and I think that using the Calculate Value tool is the wrong way to go about this.  Am I right in assuming that what I need is a python script that will give me 2 outputs (True and False) and then I can connect the "True" as a precondition to the rest of the model, and leave the "False" disconnected, so the model stops and the iterator goes on to the next feature class?

If so, would anybody be able to give me some help with this?

Many thanks

Dan
Tags (2)
4 Replies
StacyRendall1
Occasional Contributor III
I don't know for sure, but a quick thing to try might be using actual True and False Python values, i.e.:

Code Block
def CountMe(n):
 if n > 0:
  return True
 else:
  return False


Also, you do not need to "import arcpy" within the code, this is done automatically.
0 Kudos
AmyKlug
Occasional Contributor III
I just did something similar to this yesterday. I have a test model and a code that might be a good reference. I just created 2 tables with different row counts to test and used a model parameters as input :

import arcpy
rowcount = int(arcpy.GetParameterAsText(0))

if rowcount >= 1:
    arcpy.SetParameterAsText(1, "True")
    arcpy.SetParameterAsText(2, "False")
    arcpy.AddMessage("Rows Exist, updating table")
elif rowcount == 0:
    arcpy.SetParameterAsText(1, "False")
    arcpy.SetParameterAsText(2, "True")
    arcpy.AddMessage("0 rows, updating table")
else:
    arcpy.AddMessage("Code Didn't Work")
del rowcount
0 Kudos
RhettZufelt
MVP Frequent Contributor
Could also just check to see if file exists, and size:


import os, stat

if os.path.isfile(infile):          # checks if exists, otherwise get error if checking size on non-exist file
 os.stat(infile)[stat.ST_SIZE]   ##  returns size of file  0L if empty
else:
 print "file not exist"



This is done pretty much instantly..

R_
0 Kudos
AlisterFenix
New Contributor
I just did something similar to this yesterday. I have a test model and a code that might be a good reference. I just created 2 tables with different row counts to test and used a model parameters as input :

import arcpy
rowcount = int(arcpy.GetParameterAsText(0))

if rowcount >= 1:
    arcpy.SetParameterAsText(1, "True")
    arcpy.SetParameterAsText(2, "False")
    arcpy.AddMessage("Rows Exist, updating table")
elif rowcount == 0:
    arcpy.SetParameterAsText(1, "False")
    arcpy.SetParameterAsText(2, "True")
    arcpy.AddMessage("0 rows, updating table")
else:
    arcpy.AddMessage("Code Didn't Work")
del rowcount


This is close to the solution that I'm looking for.  I'm "simply" attempting to iterate through my outputs of a process, and when there is an empty geography created it, to delete it.  How did you get the outputs from this script to then link back to the geoprocessing tools? i.e. I have embedded this revised script into modelbuilder, but now am stuck on how to connect the Delete tool to the process when the rowcount == 0.  Thanks in advance for any assistance.
0 Kudos