-1

I'm using Python in ArcGIS Pro to run through a couple of point layers to find the Closest Facility between them. I think I'm really close to getting what I want in an output, but I'm getting more records than expected. The code attached creates a new layer for each Object ID and then uses that to run the Network Analyst tool. Somewhere in the nested for loop, it's outputting a duplicate record(s) for each new layer. For instance, it only writes one record for the first layer, then two duplicate records for the second, and three duplicate records for the third one. I know I'm missing something obvious here, but I don't want to continue to rerun this tool since it burns credits.

arcpy.DeleteRows_management("Routes_Append")
arcpy.DeleteRows_management("Routes")
incidents.definitionQuery = None
import arcpy
import time
import sys

p  = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Map")[0]

#Add the geoprocessing service as a toolbox. Use an alias when importing
arcpy.ImportToolbox(cf_service, "agol")

facilities = m.listLayers("Delivery_Sites_No_Admin")[0]
incidents = m.listLayers("VHA_Facilities_No_Other")[0]

listx = [2, 3, 4]
obid = "OBJECTID = {}"

for oid in listx:

    incidents.definitionQuery = obid.format(oid)
    arcpy.SelectLayerByLocation_management(facilities, 'WITHIN_A_DISTANCE', incidents, "150 Miles", 'NEW_SELECTION', 'NOT_INVERT')
    arcpy.CopyFeatures_management(facilities,".../MyProject6.gdb/ObjectID{}".format(oid))

    output_routes = ".../MyProject6.gdb/Routes"
    fcs = arcpy.ListFeatureClasses("Object*")

    for fc in fcs:

        #Call the tool
        result = arcpy.agol.FindClosestFacilities(incidents, ".../MyProject6.gdb/ObjectID{}".format(oid), "Minutes", "", 1)

        #Check the status of the result object every 0.5 seconds 
        #until it has a value of 4(succeeded) or greater 
        while result.status < 4:
            time.sleep(0.5)

        #print any warning or error messages returned from the tool
        result_severity = result.maxSeverity
        if result_severity == 2:
            print("An error occured when running the tool")
            print(result.getMessages(2))
            sys.exit(2)
        elif result_severity == 1:
            print("Warnings were returned when running the tool")
            print(result.getMessages(1))

        #Get the output routes  and save to a local geodatabase
        result.getOutput(0).save(output_routes)
        arcpy.Append_management("Routes", "Routes_Append", "TEST")
1

I needed to add a delete statement to remove the temp file so that it doesn't get called again. Seems to have done the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.