0

I am making a network analyst (Closest Facility) analysis. I will be driving from "Incidents" to "Facilities". My "Facilities" is the population center in a municipality. In "Facilities" I have a column "POP" that says how many residents who are in the population center. First, the Closest Facility analyses will go to the nearest "Facilities" and count how many residents that there are in the column "POP". So it should go to the second-nearest "Facilities" and count how many residents that are in the column "POP" there. For every "Facilities" it will count how many residents there are in the column "POP" and accumulate the result from the column "POP" to a column called cumulative.

I've made it so far. But my problem is when the accumulation has reached the nearest sum over 5000 in the column accumulated I want the Closest Facility analyzes to stop and not go to all the "Facilities" in my dataset. I want the analysis to stop when it has reached the nearest sum over 5000 in order to save time and computer power.

Is it possible to get Closest Facility to stop the way I want, or must it go through all "Facilities" that I have?

import arcpy, os, sys


# Check out any necessary licenses
arcpy.CheckOutExtension("Network")

arcpy.env.overwriteOutput = True

# Local variables:
WS = "C:\Tom\Oppdrag_2015\KMD_NY"
ELVEG_Nettverk_ND_2013 =   "C:\\Tom\\Krd_2013\\ELVEG_Nettverk_2013\\ELVEG_Nettverk_2013.gdb\\ELVEG_Nettverk \\ELVEG_Nettverk_ND_2013"
Til = r"C:\\Tom\\Oppdrag_2015\\KMD_NY\\04_Hedmark_Ut.gdb\\Til"
Fra = r"C:\Tom\Oppdrag_2015\KMD_NY\04_Hedmark_Ut.gdb\Fra"
Ruter = r"C:\Tom\Oppdrag_2015\KMD_NY\Ruter.shp"

Routes = "Closest Facility\\Routes"
ODLines_Layer = "ODLines_Layer"
NY = "TEST"
Closest_Facility = "Closest Facility"

arcpy.MakeFeatureLayer_management(Fra, "FraLyr")

arcpy.MakeFeatureLayer_management(Til, "TilLyr")

# Process: Add Field (2)
arcpy.AddField_management("TilLyr", "Innbyggere_NY", "LONG", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")

arcpy.AddField_management("FraLyr", "FraGkr", "LONG", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (3)
arcpy.CalculateField_management("FraLyr", "FraGkr", "[GRUNNKRETS]", "VB", "")

rows = arcpy.da.SearchCursor("FraLyr", "TEST")


for row in rows:
    Gkr = str(row[0])
    where = " \"TEST\" = " + "" + str(Gkr) + ""
    arcpy.SelectLayerByAttribute_management("FraLyr", "NEW_SELECTION",     where)


    closestBTP = "ClosestBTP"
    # Process: Make Closest Facility Layer
    outNALayer = arcpy.MakeClosestFacilityLayer_na(ELVEG_Nettverk_ND_2013,     closestBTP, "Meters", "TRAVEL_TO", "", "50", "", "ALLOW_UTURNS", "Oneway", "USE_HIERARCHY", "", "TRUE_LINES_WITH_MEASURES", "", "NOT_USED")



    arcpy.AddFieldToAnalysisLayer_na (closestBTP,"Routes", "Gkr_nr", "TEXT", "", "", "50")

    # Process: Add Field (3)
    arcpy.AddField_management(closestBTP + "\\Routes", "Akkumulert", "LONG", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")

    arcpy.AddField_management(closestBTP + "\\Routes", "Grunnkrets", "LONG", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")



    arcpy.AddLocations_na(closestBTP, "Incidents", "FraLyr", "Name GRUNNKRETS #", "5000 Meters", "", "ELVEG_Vegnett_2013 SHAPE;ELVEG_Nettverk_ND_2013_Junctions NONE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "ELVEG_Vegnett_2013 #;ELVEG_Nettverk_ND_2013_Junctions #")




    arcpy.AddLocations_na(closestBTP, "Facilities", "TilLyr", "Name GRUNNKRETS #", "5000 Meters", "", "ELVEG_Vegnett_2013 SHAPE;ELVEG_Nettverk_ND_2013_Junctions NONE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "ELVEG_Vegnett_2013 #;ELVEG_Nettverk_ND_2013_Junctions #")



# Process: Solve
    arcpy.Solve_na(closestBTP, "SKIP", "TERMINATE", "")



# Process: Calculate Field
    arcpy.CalculateField_management(closestBTP + "\\Routes", "Gkr_nr", "[Name]", "VB", "")



# Process: Calculate Field (4)
    arcpy.CalculateField_management(closestBTP + "\\Routes", "Grunnkrets", "[Gkr_nr]", "VB", "")

# Process: Calculate Field (2)
    arcpy.CalculateField_management(closestBTP + "\\Routes", "Gkr_nr", "Right( [Gkr_nr],8  )", "VB", "")

        # Process: Join Field
    arcpy.JoinField_management(closestBTP + "\\Routes", "Gkr_nr", "TilLyr", "GRUNNKRETS", "Innbyggere_NY")

 # Process: Calculate Field (5)
    arcpy.CalculateField_management(closestBTP + "\\Routes", "Akkumulert", "cumsum( !Innbyggere_NY! )", "PYTHON_9.3", "total = 0\\ndef cumsum(inc):\\n global total\\n total+=inc\\n return total\\n")



    arcpy.SaveToLayerFile_management(closestBTP, "Grunnkrets" + "_" + Gkr, "RELATIVE")
1

I did similar works in the past and I think using NA function as you have done here (recursively adding/removing new set of target and destinations) pretty inefficient from processing/resource-use aspects, that's why you are looking for an answer to stop processing once the process reaches a certain threshold (5000 people). Instead I strongly advise you to use OD matrix for this purpose. I have not tested the workflow below but the logic should be apparent:

  • Generate OD matrix and join your population values to Facilities;
  • Export your table to Excel;
  • By using Pivot Table of Excel sum up your population values (you can even do more)

By this way you would be able to calculate overall population and then you can employ thresholds that you want to apply (e.g., Less than 5000, More than 5000, etc.)

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.