I would like to use an if-else
statement in my model (along the lines of this Esri blog). My model works fine (several intersects, dissolves, calculating fields, joins). However, I would like to add a check after the first process runs, which is an Intersect
. If the output has 0 rows (using Get Count
), the model should stop, else continue. Using the following code in the Calculate Value
code code block
def countRows(rowcount):
import arcpy
if %rowcount% == 0:
return false
else:
return true
and expression
countRows(%rowcount%)
The model runs and generates a warning
The process did no execute because the precondition is false.
It then just carries on running the other processes, all which generate the warning
All the inputs are not current.
To summarise: The input is intersected, and the rows in the output are counted. If the number of rows is 0, return false and stop. If true, carry on processing. The Calculate Value
tool is set as a precondition before all the other processes can run. I've looked at the Stop-Continue
tool as well, but I'm not sure how (if?) I should be using it in this instance.
edit Ran the model on an input that I knew would generate some rows after the intersect, when the Calculate Value
ran and returned true
, the model stopped with the error
Error 000539: Error running expression: countRows(3546) <type 'exceptions.NameError'>:
global name 'true' is not defined. Failed to execute (Calculate Value).
Best Answer
Use lower-case, quoted values (strings), this is apparently what the model expects for preconditions.
e.g.
return "true"
orreturn "false"