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).
-
Use proper case for True/False, that should probably fix it :)blah238– blah2382012年06月19日 07:00:15 +00:00Commented Jun 19, 2012 at 7:00
-
Actually use lower-case, quoted values (strings), this is apparently what the model expects for preconditions.blah238– blah2382012年06月19日 07:24:22 +00:00Commented Jun 19, 2012 at 7:24
-
@blah238 that worked! Now the other branch of my model is failing, but I think that's because I got overexcited with my preconditions, I'll relax some of them now. Could you please add your comment as an answer so that I can accept it? Thanks!Cindy Jayakumar– Cindy Jayakumar2012年06月19日 09:49:36 +00:00Commented Jun 19, 2012 at 9:49
-
I have the same issue as the original question. Currently my true/false values in the calculate value tool are all-caps and when I change them to lower case and hit OK, they revert back to all caps. Anyone know why this is?Steve– Steve2014年02月12日 17:56:03 +00:00Commented Feb 12, 2014 at 17:56
-
I was able to change the true/false values to lower case but still getting "The process did no execute because the precondition is false." and "All the inputs are not current." errors. The second message comes on the export to cad tool.Steve– Steve2014年02月12日 19:20:25 +00:00Commented Feb 12, 2014 at 19:20
3 Answers 3
Use lower-case, quoted values (strings), this is apparently what the model expects for preconditions.
e.g. return "true"
or return "false"
If your using 10 or above simply use the GET COUNT function in ModelBuilder and use rowCount as a precondition for your intersection.
This was both handy and detailed:
http://blogs.esri.com/esri/arcgis/2011/06/06/modelbuilderifthenelse1/
Explore related questions
See similar questions with these tags.