3

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).
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 19, 2012 at 5:58
9
  • Use proper case for True/False, that should probably fix it :) Commented Jun 19, 2012 at 7:00
  • Actually use lower-case, quoted values (strings), this is apparently what the model expects for preconditions. Commented 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! Commented 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? Commented 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. Commented Feb 12, 2014 at 19:20

3 Answers 3

5

Use lower-case, quoted values (strings), this is apparently what the model expects for preconditions.

e.g. return "true" or return "false"

answered Jun 19, 2012 at 10:14
0
0

If your using 10 or above simply use the GET COUNT function in ModelBuilder and use rowCount as a precondition for your intersection.

answered Oct 17, 2013 at 14:29
0
answered Mar 5, 2014 at 23:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.