Created
April 27, 2017 19:10
-
-
Save AlexArcPy/e109a8ac587986adc10e0072a90df061 to your computer and use it in GitHub Desktop.
Network Analyst and ArcPy: finding k-alternate path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''Python add-in that will find multiple k-alternate paths between a given number of stops. | |
| This code is supposed to be run in ArcMap map document with a Route Network Analysis layer | |
| named Route with at least two stops loaded. The impedance attribute name is read from a | |
| Route solver properties object.''' | |
| import os | |
| import arcpy | |
| import arcpy.mapping as mp | |
| import pythonaddins | |
| class ComboBoxClass1(object): | |
| """Implementation for kAlternativeRoutes_addin.combobox (ComboBox)""" | |
| def __init__(self): | |
| self.items = ["1", "2", "3", "4", "5"] | |
| self.editable = True | |
| self.enabled = True | |
| self.dropdownWidth = 'WW' | |
| self.width = 'WW' | |
| def onSelChange(self, selection): | |
| pass | |
| def onEditChange(self, text): | |
| pass | |
| def onFocus(self, focused): | |
| pass | |
| def onEnter(self): | |
| k = int(self.value) | |
| mxd = mp.MapDocument('current') | |
| df = mp.ListDataFrames(mxd)[0] | |
| route_NAlayer = arcpy.mapping.Layer("Route") | |
| barriers_NAlayer = arcpy.mapping.Layer("Route\Line Barriers") | |
| #cleaning all existing barriers from previous runs | |
| arcpy.DeleteFeatures_management(barriers_NAlayer) | |
| out_k_routes = r'in_memory\k_route' | |
| #solve Route | |
| arcpy.Solve_na(in_network_analysis_layer="Route") | |
| routes_solved = arcpy.mapping.ListLayers(route_NAlayer, "Routes")[0] | |
| #create the first route and keep in memory | |
| arcpy.CopyFeatures_management(routes_solved, out_k_routes) | |
| arcpy.AddMessage('done original solve') | |
| for i in xrange(k-1): | |
| arcpy.AddMessage('doing K {}'.format(i+1)) | |
| impedance = arcpy.na.GetSolverProperties(route_NAlayer).impedance | |
| field_mappings = arcpy.na.NAClassFieldMappings(route_NAlayer, 'Line Barriers') | |
| field_mappings["BarrierType"].defaultValue = 1 #Scaled Cost type | |
| #factor by which attr_impedance of road links will be multiplied | |
| field_mappings["Attr_{0}".format(impedance)].defaultValue = 2.5 | |
| #cleaning up all barriers | |
| arcpy.DeleteFeatures_management(barriers_NAlayer) | |
| #loading previously solve route(s) as barriers | |
| arcpy.AddLocations_na(in_network_analysis_layer=route_NAlayer, | |
| sub_layer='Line Barriers', in_table=out_k_routes, | |
| field_mappings=field_mappings) | |
| #solve with barrier(s) | |
| barrier_count = arcpy.GetCount_management(barriers_NAlayer) | |
| arcpy.AddMessage('Number of barriers before solve is: {}'.format(barrier_count)) | |
| arcpy.Solve_na(in_network_analysis_layer="Route") | |
| routes_solved = arcpy.mapping.ListLayers(route_NAlayer, "Routes")[0] | |
| #append to the output all routes which will serve as barriers layer | |
| arcpy.Append_management(routes_solved, out_k_routes, 'NO_TEST') | |
| #generate output routes alternatives in a feature class | |
| arcpy.CopyFeatures_management(out_k_routes, os.path.join(arcpy.env.scratchGDB, | |
| 'AlternativeRoutes')) | |
| arcpy.mapping.RemoveLayer(df, mp.Layer('k_route')) | |
| arcpy.Delete_management(out_k_routes) | |
| def refresh(self): | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment