Select to view content in your preferred language

How to get a count zero for arcpy.GetCount_management

970
4
Jump to solution
11-09-2022 12:22 AM
Aнастасия88
Occasional Contributor

Hello, I am just wondering if anyone tells me the way to get a count zero to exit from the while loop that uses arcpy.GetCount_management.

I set up for loop to go through a selection of intersection between polygon (ssLyr) layer and point geometry (cenPnt). Then, while the selected polygon count is greater than zero, a second for loop is carried out. But I would like to exit the while loop in case a specific value (neigbID == 9999) is returned during the second loop.

I tried a couple of ways; arcpy.management.SelectLayerByAttribute(ssLyr, "CLEAR_SELECTION"), del cursor1, arcpy.Delete_management(ssLyr) to get zero count from int(arcpy.GetCount_management(ssLyr).getOutput(0))  but none of them worked. Thank you in advance for any python coding help!

My code is here

 

for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    while int(arcpy.GetCount_management(ssLyr).getOutput(0)) > 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    #Would like to get count zero here
                    #arcpy.management.SelectLayerByAttribute(ssLyr, "CLEAR_SELECTION")
                    #del cursor1
                    #arcpy.Delete_management(ssLyr)
                    print("Check count: ", int(arcpy.GetCount_management(ssLyr).getOutput(0))) 

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Store the count in a variable. If you encounter your break condition, change that variable to 0 before breaking out of the for loop.

for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    ssLyrCount = int(arcpy.GetCount_management(ssLyr).getOutput(0))

    while ssLyrCount > 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    ssLyrCount = 0
                    break

 

I don't know the rest of your script, but the snippet you posted will result in an infinite while loop if you select at least 1 feature and have no features with neigbID=9999 in the selection. Any reason to use while over if?


Have a great day!
Johannes

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

The first getcount_management needs to return a value needs to return a value when the script ends.

 


... sort of retired...
0 Kudos
Aнастасия88
Occasional Contributor

Hi Dan, thanks for your reply!

What I wanted is actually just keep the while loop continues in the special case.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Store the count in a variable. If you encounter your break condition, change that variable to 0 before breaking out of the for loop.

for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    ssLyrCount = int(arcpy.GetCount_management(ssLyr).getOutput(0))

    while ssLyrCount > 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    ssLyrCount = 0
                    break

 

I don't know the rest of your script, but the snippet you posted will result in an infinite while loop if you select at least 1 feature and have no features with neigbID=9999 in the selection. Any reason to use while over if?


Have a great day!
Johannes
Aнастасия88
Occasional Contributor

Hi Johannes, thanks a lot for your follow up and inquiry!

The rest of scripts continues with elif to deal with a case that biorID is not equal to neighbID. This continues while the output count is one but once it gets zero, then other functions are to be triggered. I don't know if this is the best way but I wrote a code like below.

elif biorID != neighbID:

    Expression2 = "BID = {0}".format(neighbID)
    arcpy.management.SelectLayerByAttribute(ssLyr, 'NEW_SELECTION', Expression2)
    print('Expression2 is: ', Expression2)

    if ssLyrCount == 0:
        ##Other functions##
   
    else:
        pass

 

0 Kudos