Select to view content in your preferred language

How to make a loop work?

926
5
08-20-2019 01:35 PM
JessicaRamirez1
Emerging Contributor

Hi, 

I have a survey made from survey 123, I have a point layer representing assets. The goal is to complete the survey for every asset. I have a field in my points layer called CompleteY_N, by default the attribute is 'Not Completed'. I want the attribute to change to 'complete' as a survey it summited and loop through until all assets say complete in the CompleteY_N field. 

I wrote a script that selects by location and then uses UpdateCursor to change the attribute to complete. I have also used field calculate to change the selected features to complete ( not sure which way is better). 

I am not sure how to do a loop like this, or if I have even explained what I am trying to do correctly.  

Here is my script. 

import arcpy

# Input property
inPropertypntfc = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb\propertypnts"
print(inPropertypntfc)

# input survey feature class
inSurveyfc = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb\retailpt2"
print(inSurveyfc)

# Make feature layer of feature classes to allow selection
arcpy.MakeFeatureLayer_management(inPropertypntfc, "Propertypntlyr")
arcpy.MakeFeatureLayer_management(inSurveyfc, "Surveylyr")


# select intersecting points
arcpy.SelectLayerByLocation_management("Propertypntlyr", "INTERSECT", "Surveylyr")
print ("select")

# Update Cursor for point layer
cursor = arcpy.da.UpdateCursor("Propertypntlyr", ["CompleteY_N"])
print (cursor)
for row in cursor:
    cursor.updateRow(["Completed"])
print ("Done")

del row
del cursor
Tags (2)
0 Kudos
5 Replies
DuncanHornby
MVP Notable Contributor

Have you tried searching the internet on how to loop in python, here is one such website?

JessicaRamirez1
Emerging Contributor

Thank you for the info

0 Kudos
JoeBorgione
MVP Emeritus

As Duncan Hornby‌ suggests, for loops are a standard operation in python.  I've always said, give me a list to Ioop through and I can change the world...

However, I'm unclear what it is you need to loop through?  In your current for loop through the cursor object, what is the purpose of passing a list as the value?  (Line 2  below)

for row in cursor:
    cursor.updateRow(["Completed"])
That should just about do it....
0 Kudos
LukeWebb
Frequent Contributor

I wrote this script based on my understanding of your explanation (Which was a little hazy!)

I kept your existing code exactly as it was just moved it into a function, then added one new function, and a Loop.

Hopefully this helps    


import arcpy
import time


def update_database():
    # Input property
    inPropertypntfc = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb\propertypnts"
    print(inPropertypntfc)

    # input survey feature class
    inSurveyfc = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb\retailpt2"
    print(inSurveyfc)

    # Make feature layer of feature classes to allow selection
    arcpy.MakeFeatureLayer_management(inPropertypntfc, "Propertypntlyr")
    arcpy.MakeFeatureLayer_management(inSurveyfc, "Surveylyr")


    # select intersecting points
    arcpy.SelectLayerByLocation_management("Propertypntlyr", "INTERSECT", "Surveylyr")
    print ("select")

    # Update Cursor for point layer
    cursor = arcpy.da.UpdateCursor("Propertypntlyr", ["CompleteY_N"])
    print (cursor)
    for row in cursor:
        cursor.updateRow(["Completed"])
    print ("Done")

    del row
    del cursor
    
    arcpy.Delete_management("Propertypntlyr")
    arcpy.Delete_management("Surveylyr")


def all_surveys_complete():
    inPropertypntfc = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb\propertypnts"

    # Make feature layer of feature classes to allow selection
    arcpy.MakeFeatureLayer_management(inPropertypntfc, "Propertypntlyr")

    # Select features that are not complete
    arcpy.SelectLayerByAttribute_management("Propertypntlyr", 'NEW_SELECTION',
                                             """   "CompleteY_N" <> 'Completed'  """)

    #Check if we got a selection
    desc = arcpy.Describe("Propertypntlyr")
    if desc.FIDSet  == '':
        #If there is no selection, all surveys are complete
        arcpy.Delete_management("Propertypntlyr")
        return True
    else:
        arcpy.Delete_management("Propertypntlyr")
        return False



#Main Script logic


#Carry on forever (until we "break" the loop)
while True:

    #Existing code
    update_database()

    #Check if all are complete
    if all_surveys_complete() == True:

        #Exit the while loop
        break

    #Wait for 10 mins for more surveys to be completed and added to the retailpt2 layer
    time.sleep(600)


print "All surveys are complete!"


JessicaRamirez1
Emerging Contributor

Thank you, I will try this out! 

0 Kudos