Trouble Understanding Get Count

1612
5
Jump to solution
11-22-2016 08:07 AM
ChrisHolmes
Occasional Contributor III

Good day everyone,

I think I'm having some trouble understanding how to make use of Get Count. I have the following:

What I want to see happen is if Row Count = 0 > End the model. If Row Count > 0 continue the model. What I see is when Row Count > 0 the model continues and when Row Count = 0 it just seems to spin it's wheels for a bit (30 seconds) then end. I don't know if maybe it has something to do with Select Layer By Location using AddressSelect as it's Selecting Features? Or maybe I'm just using Get Count completely wrong and there's a different way I should be approaching this.

Thanks for the help,

Chris

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

The output of the Get Count tool is of type integer, which when it has the value of zero is interpreted as false when linked to the next tool in the chain as a precondition.  This can cause problems when you want the tool to continue when the count is zero. In situations like that you are better off running the Get Count inside a Python function (arcpy.GetCount_management(lyr)) in the Calculate Value tool (that is, hiding the results from the Model Builder) and returning true or false as Boolean based on the Get Count result.

View solution in original post

5 Replies
ChrisHolmes
Occasional Contributor III

Upon further investigation, it looks like maybe it is working. When Row Count = 0 it continues going through all tools/scripts it just doesn't execute them, due to either "precondition is false" or "inputs are not current". The following is what is displayed. Now that I think it's working, would there be a way to add in a arcpy.AddWarning("Incorrect address, try again") in addition to or in lieu of all of the precondition is false... inputs are not current...

Thanks!

Executing: FinalModel22 "7 windermure rd sw" M-4

Start Time: Tue Nov 22 13:01:22 2016

Executing (GetLocNo): GetLocNo

Start Time: Tue Nov 22 13:01:23 2016

Running script GetLocNo...

Completed script GetLocNo...

Succeeded at Tue Nov 22 13:01:25 2016 (Elapsed Time: 2.50 seconds)

Executing (Select): Select "Construction Layers\CALGIS.ADDRESS_POSSE" in_memory\AddressSelect "ADDRESS =UPPER('7 windermure rd sw')"

Start Time: Tue Nov 22 13:01:25 2016

WARNING 000117: Warning empty output generated.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.73 seconds)

Executing (Get Count): GetCount in_memory\AddressSelect

Start Time: Tue Nov 22 13:01:26 2016

Row Count = 0

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Select Layer By Location): SelectLayerByLocation "Construction Layers\OPF - Ownership Parcel Fabric\CALGIS.OPF_PARCEL" INTERSECT in_memory\AddressSelect # NEW_SELECTION

Start Time: Tue Nov 22 13:01:26 2016

The process did not execute because the precondition is false.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Zoom To Selected Feature): ZoomToSelected

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Append): Append 'Construction Layers\OPF - Ownership Parcel Fabric\CALGIS.OPF_PARCEL' site_polygon NO_TEST # #

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Append (2)): Append 'Construction Layers\OPF - Ownership Parcel Fabric\CALGIS.OPF_PARCEL' "PROPOSED - proposed_LUD_polygon - OUTLINE" NO_TEST # #

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Update Proposed Lud Polygon Info): UpdateProposedLudPolygonInfo LOC2017-0016 M-4

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Update Site Polygon Info): UpdateSitePolygonInfo LOC2017-0016

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Set Scale): SetScale400

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Executing (Create Site Location): CreateSiteLocation #

Start Time: Tue Nov 22 13:01:26 2016

All the inputs are not current.

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 0.00 seconds)

Succeeded at Tue Nov 22 13:01:26 2016 (Elapsed Time: 3.44 seconds)

0 Kudos
curtvprice
MVP Esteemed Contributor

The output of the Get Count tool is of type integer, which when it has the value of zero is interpreted as false when linked to the next tool in the chain as a precondition.  This can cause problems when you want the tool to continue when the count is zero. In situations like that you are better off running the Get Count inside a Python function (arcpy.GetCount_management(lyr)) in the Calculate Value tool (that is, hiding the results from the Model Builder) and returning true or false as Boolean based on the Get Count result.

curtvprice
MVP Esteemed Contributor

In response to this question:

Could there be a way to add in a arcpy.AddWarning("Incorrect address, try again") in addition to or in lieu of all of the precondition is false... inputs are not current...

Without building a python script to print the messages, you can do this in the Calculate Value tool. I have discovered you cannot embed "\n" in the Calculate value tool; I use chr(12) instead to print newlines.

Expression

test(r"%AddressSelect%")‍‍

Code Block:

def test(lyr):  
  if arcpy.GetCount_management(lyr).getOutput(0) == "0":    
    return "{0}{0}Incorrect address{0}{0}".format(chr(12))‍‍‍‍‍‍

Data Type: String

This will print (in the middle of your messages):



Incorrect Address

‍‍‍‍‍‍‍‍

But I think no matter what you do you'll see all those warning messages about inputs not current. If you really want full control of messages, you need to start thinking about Python scripting. Model Builder is awesome, but it has its limits.

0 Kudos
ChrisHolmes
Occasional Contributor III

Super. Thanks for your time Curtis.

Sent from my iPhone

0 Kudos
ChrisHolmes
Occasional Contributor III

Quick update. I changed over to the following python script:

import arcpy
locno = arcpy.GetParameterAsText(0)
year = locno[3:7]
workspace = "O:/plan/luam/1p2007/" + year + "/" + locno + "/data_for_item.gdb"
arcpy.env.workspace = workspace
inFc = 'in_memory\AddressSelect'
numRecords = int(arcpy.GetCount_management(inFc).getOutput(0))
if numRecords == 0:
    arcpy.AddError('Incorrect address. Please try again.')
    result = False
else:
    result = True
arcpy.SetParameterAsText(1, result)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I tried it both using AddWarning and AddError.

AddWarning gives the following to the output then continues to run through all tools/scripts providing either the message 'precondition is false' or 'inputs are not current':

AddError provides the following, and seems to be a bit 'cleaner' and I think easier for the end user to know that something is not right:

Something I find interesting is the script with AddWarning does display as Completed at the top left of the form while the AddError script shows Executing Test For Records... is that because in the case of AddWarning, the model did complete and with AddError it did not?