<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Route (Analysis Network) in Python generates the same route 3 times for each OD pair in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224132#M61361</link>
    <description>&lt;P&gt;Hello Dan,&lt;/P&gt;&lt;P&gt;thanks for the tip! Please refer to the formatted code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy as ap
import sys

# set environment
ap.env.workspace = "C:/Users/conni/Documents/EsriTraining/Tese_v1"
ap.env.overwriteOutput = "TRUE"
ap.CheckOutExtension("Network")

# set names for files with points and arcs (flows)
flow_feature = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/ODLines4l1krc"
origins = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"
destinations = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"

# set local variables, create geodatabases (?)
out_gdb_path = "C:/Users/conni/Documents/EsriTraining/Tese_v1"
out_gdb_name = "Port_Warehouse_shift_link"
ap.CreateFileGDB_management(out_gdb_path, out_gdb_name)
out_path = out_gdb_path + "/" + out_gdb_name + ".gdb"

out_name = "stops"
geometry_type = "POINT"

# execute CreateFeatureclass
ap.CreateFeatureclass_management(out_path, out_name, geometry_type, spatial_reference = flow_feature)
# create field for LP_ID
ap.AddField_management(out_path + "/" + out_name, "LP_ID", "TEXT")
# assign variable to stops feature
stops = out_path + "/" + out_name

# create feature dataset for routes and edges
ap.CreateFeatureDataset_management(out_path, "routes", origins)
routes_path = out_path + "/" + "routes"
ap.CreateFeatureDataset_management(out_path, "edges", origins)
edges_path = out_path + "/" + "edges"

# a cursor is used to query information from the origins feature and insert rows on stops feature. ID's are assigned.
# "SHAPE@XY" allows to set the coordinate of each point in the feature class
with ap.da.SearchCursor(origins, ["CD_MUN", "SHAPE@XY"]) as cur_a:
    for i,row in enumerate(cur_a,1):
        cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])
        cur_b.insertRow([row[0],row[1]])

with ap.da.SearchCursor(destinations, ["CD_MUN", "SHAPE@XY"]) as cur_a:
    for i,row in enumerate(cur_a,1):
        cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])
        cur_b.insertRow([row[0],row[1]])

inNetworkDataset = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/SantaCatarina/SC_ND"

intermodal_data = {}

ap.AddField_management(flow_feature, "MODE", "TEXT")
ap.AddField_management(flow_feature, "ACCESS", "TEXT")
ap.AddField_management(flow_feature, "EXIT", "TEXT")

with ap.da.UpdateCursor(flow_feature,["cod_mun_origem", "cod_mun_destino", "volume_total", "MODE", "ACCESS", "EXIT"]) as cur:
    for i,row in enumerate(cur,1):
        try:
            ap.MakeFeatureLayer_management(stops, "stops_lyr")
            where_clause_1 = '"LP_ID"' + " = '" + str(row[0]) + "'"
            temp_select = ap.SelectLayerByAttribute_management('stops_lyr', 'NEW_SELECTION', where_clause_1)
            where_clause_2 = '"LP_ID"' + " = '" + str(row[1]) + "'"
            ap.SelectLayerByAttribute_management('stops_lyr', 'ADD_TO_SELECTION', where_clause_2)
            inStops = temp_select
            route_name = str(row[0]) + "_" + str(row[1])

            result_object = ap.na.MakeRouteAnalysisLayer(inNetworkDataset, route_name, "Multimodal")
            layer_object = result_object.getOutput(0)
            sublayer_names = ap.na.GetNAClassNames(layer_object)
            stops_layer_name = sublayer_names["Stops"]
            routes_layer_name = sublayer_names["Routes"]
            ap.na.AddLocations(layer_object, stops_layer_name, inStops, search_criteria = [["Clip_OutFeatureClass_Ferrovias", "NONE"],
                                                                                            ["Clip_OutFeatureClass_Rodovias", "SHAPE"],
                                                                                            ["SC_TIN_TinEdge", "NONE"],
                                                                                            ["Rodo_OD", "SHAPE"],
                                                                                            ["Rodo_TermFerrExistente", "SHAPE"],
                                                                                            ["Rodo_TermFerrNovo", "SHAPE"],
                                                                                            ["BR_RG_Imediatas_2021_MeanCenter", "SHAPE"],
                                                                                            ["Clip_OutFeatureClass_fc_ferro_terminal_de_carga", "SHAPE"],
                                                                                            ["SC_ND_Junctions", "NONE"]])
            ap.na.Solve(layer_object)
            routes_sublayer = ap.na.GetNASublayer(layer_object, routes_layer_name)
            ap.management.CopyFeatures(routes_sublayer, routes_path + route_name)
            ap.na.CopyTraversedSourceFeatures(layer_object, edges_path, "TraversedEdges_" + route_name, "TraversedJunctions_" + route_name, "TraversedTurns_" + route_name)
            temp_junction = edges_path + "/" + "TraversedEdges_" + route_name
            intermodal_data[route_name] = []

        except Exception:
            e = sys.exc_info()[1]
            print(e.args[0])
            ap.AddError(e.args[0])

        cur.updateRow(row)&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 20 Oct 2022 23:37:16 GMT</pubDate>
    <dc:creator>ConnieSu</dc:creator>
    <dc:date>2022-10-20T23:37:16Z</dc:date>
    <item>
      <title>Route (Analysis Network) in Python generates the same route 3 times for each OD pair</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224025#M61349</link>
      <description>&lt;P&gt;Hello! I´m building a Python code in IDLE to run with ArcGIS Pro 2.9.&lt;/P&gt;&lt;P&gt;I have a list of OD pairs and a multimodal (road and rail) network dataset and I need to calculate the shortest route for each OD pair.&lt;/P&gt;&lt;P&gt;Here is the code that I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy as ap&lt;BR /&gt;import sys&lt;/P&gt;&lt;P&gt;# set environment&lt;BR /&gt;ap.env.workspace = "C:/Users/conni/Documents/EsriTraining/Tese_v1"&lt;BR /&gt;ap.env.overwriteOutput = "TRUE"&lt;BR /&gt;ap.CheckOutExtension("Network")&lt;/P&gt;&lt;P&gt;# set names for files with points and arcs (flows)&lt;BR /&gt;flow_feature = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/ODLines4l1krc"&lt;BR /&gt;origins = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"&lt;BR /&gt;destinations = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"&lt;/P&gt;&lt;P&gt;# set local variables, create geodatabases (?)&lt;BR /&gt;out_gdb_path = "C:/Users/conni/Documents/EsriTraining/Tese_v1"&lt;BR /&gt;out_gdb_name = "Port_Warehouse_shift_link"&lt;BR /&gt;ap.CreateFileGDB_management(out_gdb_path, out_gdb_name)&lt;BR /&gt;out_path = out_gdb_path + "/" + out_gdb_name + ".gdb"&lt;/P&gt;&lt;P&gt;out_name = "stops"&lt;BR /&gt;geometry_type = "POINT"&lt;/P&gt;&lt;P&gt;# execute CreateFeatureclass&lt;BR /&gt;ap.CreateFeatureclass_management(out_path, out_name, geometry_type, spatial_reference = flow_feature)&lt;BR /&gt;# create field for LP_ID&lt;BR /&gt;ap.AddField_management(out_path + "/" + out_name, "LP_ID", "TEXT")&lt;BR /&gt;# assign variable to stops feature&lt;BR /&gt;stops = out_path + "/" + out_name&lt;/P&gt;&lt;P&gt;# create feature dataset for routes and edges&lt;BR /&gt;ap.CreateFeatureDataset_management(out_path, "routes", origins)&lt;BR /&gt;routes_path = out_path + "/" + "routes"&lt;BR /&gt;ap.CreateFeatureDataset_management(out_path, "edges", origins)&lt;BR /&gt;edges_path = out_path + "/" + "edges"&lt;/P&gt;&lt;P&gt;# a cursor is used to query information from the origins feature and insert rows on stops feature. ID's are assigned.&lt;BR /&gt;# "SHAPE@XY" allows to set the coordinate of each point in the feature class&lt;BR /&gt;with ap.da.SearchCursor(origins, ["CD_MUN", "SHAPE@XY"]) as cur_a:&lt;BR /&gt;for i,row in enumerate(cur_a,1):&lt;BR /&gt;cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])&lt;BR /&gt;cur_b.insertRow([row[0],row[1]])&lt;/P&gt;&lt;P&gt;with ap.da.SearchCursor(destinations, ["CD_MUN", "SHAPE@XY"]) as cur_a:&lt;BR /&gt;for i,row in enumerate(cur_a,1):&lt;BR /&gt;cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])&lt;BR /&gt;cur_b.insertRow([row[0],row[1]])&lt;/P&gt;&lt;P&gt;inNetworkDataset = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/SantaCatarina/SC_ND"&lt;/P&gt;&lt;P&gt;intermodal_data = {}&lt;/P&gt;&lt;P&gt;ap.AddField_management(flow_feature, "MODE", "TEXT")&lt;BR /&gt;ap.AddField_management(flow_feature, "ACCESS", "TEXT")&lt;BR /&gt;ap.AddField_management(flow_feature, "EXIT", "TEXT")&lt;/P&gt;&lt;P&gt;with ap.da.UpdateCursor(flow_feature,["cod_mun_origem", "cod_mun_destino", "volume_total", "MODE", "ACCESS", "EXIT"]) as cur:&lt;BR /&gt;for i,row in enumerate(cur,1):&lt;BR /&gt;try:&lt;BR /&gt;ap.MakeFeatureLayer_management(stops, "stops_lyr")&lt;BR /&gt;where_clause_1 = '"LP_ID"' + " = '" + str(row[0]) + "'"&lt;BR /&gt;temp_select = ap.SelectLayerByAttribute_management('stops_lyr', 'NEW_SELECTION', where_clause_1)&lt;BR /&gt;where_clause_2 = '"LP_ID"' + " = '" + str(row[1]) + "'"&lt;BR /&gt;ap.SelectLayerByAttribute_management('stops_lyr', 'ADD_TO_SELECTION', where_clause_2)&lt;BR /&gt;inStops = temp_select&lt;BR /&gt;route_name = str(row[0]) + "_" + str(row[1])&lt;/P&gt;&lt;P&gt;result_object = ap.na.MakeRouteAnalysisLayer(inNetworkDataset, route_name, "Multimodal")&lt;BR /&gt;layer_object = result_object.getOutput(0)&lt;BR /&gt;sublayer_names = ap.na.GetNAClassNames(layer_object)&lt;BR /&gt;stops_layer_name = sublayer_names["Stops"]&lt;BR /&gt;routes_layer_name = sublayer_names["Routes"]&lt;BR /&gt;ap.na.AddLocations(layer_object, stops_layer_name, inStops, search_criteria = [["Clip_OutFeatureClass_Ferrovias", "NONE"],&lt;BR /&gt;["Clip_OutFeatureClass_Rodovias", "SHAPE"],&lt;BR /&gt;["SC_TIN_TinEdge", "NONE"],&lt;BR /&gt;["Rodo_OD", "SHAPE"],&lt;BR /&gt;["Rodo_TermFerrExistente", "SHAPE"],&lt;BR /&gt;["Rodo_TermFerrNovo", "SHAPE"],&lt;BR /&gt;["BR_RG_Imediatas_2021_MeanCenter", "SHAPE"],&lt;BR /&gt;["Clip_OutFeatureClass_fc_ferro_terminal_de_carga", "SHAPE"],&lt;BR /&gt;["SC_ND_Junctions", "NONE"]])&lt;BR /&gt;ap.na.Solve(layer_object)&lt;BR /&gt;routes_sublayer = ap.na.GetNASublayer(layer_object, routes_layer_name)&lt;BR /&gt;ap.management.CopyFeatures(routes_sublayer, routes_path + route_name)&lt;BR /&gt;ap.na.CopyTraversedSourceFeatures(layer_object, edges_path, "TraversedEdges_" + route_name, "TraversedJunctions_" + route_name, "TraversedTurns_" + route_name)&lt;BR /&gt;temp_junction = edges_path + "/" + "TraversedEdges_" + route_name&lt;BR /&gt;intermodal_data[route_name] = []&lt;/P&gt;&lt;P&gt;except Exception:&lt;BR /&gt;e = sys.exc_info()[1]&lt;BR /&gt;print(e.args[0])&lt;BR /&gt;ap.AddError(e.args[0])&lt;/P&gt;&lt;P&gt;cur.updateRow(row)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The program calculates the shortest route for each OD pair, but it calculates 3 times (please refer to the attached pictures). Does anyone know why it calculates 3 times and what can I do so it calculates only 1 time?&lt;/P&gt;&lt;P&gt;Thank you and regards!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 19:53:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224025#M61349</guid>
      <dc:creator>ConnieSu</dc:creator>
      <dc:date>2022-10-20T19:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: Route (Analysis Network) in Python generates the same route 3 times for each OD pair</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224078#M61352</link>
      <description>&lt;P&gt;formatted code, with line numbers would be helpful&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 21:22:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224078#M61352</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-10-20T21:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: Route (Analysis Network) in Python generates the same route 3 times for each OD pair</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224132#M61361</link>
      <description>&lt;P&gt;Hello Dan,&lt;/P&gt;&lt;P&gt;thanks for the tip! Please refer to the formatted code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy as ap
import sys

# set environment
ap.env.workspace = "C:/Users/conni/Documents/EsriTraining/Tese_v1"
ap.env.overwriteOutput = "TRUE"
ap.CheckOutExtension("Network")

# set names for files with points and arcs (flows)
flow_feature = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/ODLines4l1krc"
origins = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"
destinations = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/BR_Municipios_2021_MeanCenter"

# set local variables, create geodatabases (?)
out_gdb_path = "C:/Users/conni/Documents/EsriTraining/Tese_v1"
out_gdb_name = "Port_Warehouse_shift_link"
ap.CreateFileGDB_management(out_gdb_path, out_gdb_name)
out_path = out_gdb_path + "/" + out_gdb_name + ".gdb"

out_name = "stops"
geometry_type = "POINT"

# execute CreateFeatureclass
ap.CreateFeatureclass_management(out_path, out_name, geometry_type, spatial_reference = flow_feature)
# create field for LP_ID
ap.AddField_management(out_path + "/" + out_name, "LP_ID", "TEXT")
# assign variable to stops feature
stops = out_path + "/" + out_name

# create feature dataset for routes and edges
ap.CreateFeatureDataset_management(out_path, "routes", origins)
routes_path = out_path + "/" + "routes"
ap.CreateFeatureDataset_management(out_path, "edges", origins)
edges_path = out_path + "/" + "edges"

# a cursor is used to query information from the origins feature and insert rows on stops feature. ID's are assigned.
# "SHAPE@XY" allows to set the coordinate of each point in the feature class
with ap.da.SearchCursor(origins, ["CD_MUN", "SHAPE@XY"]) as cur_a:
    for i,row in enumerate(cur_a,1):
        cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])
        cur_b.insertRow([row[0],row[1]])

with ap.da.SearchCursor(destinations, ["CD_MUN", "SHAPE@XY"]) as cur_a:
    for i,row in enumerate(cur_a,1):
        cur_b = ap.da.InsertCursor(stops, ["LP_ID", "SHAPE@XY"])
        cur_b.insertRow([row[0],row[1]])

inNetworkDataset = "C:/Users/conni/Documents/EsriTraining/Tese_v1/Tese_v1.gdb/SantaCatarina/SC_ND"

intermodal_data = {}

ap.AddField_management(flow_feature, "MODE", "TEXT")
ap.AddField_management(flow_feature, "ACCESS", "TEXT")
ap.AddField_management(flow_feature, "EXIT", "TEXT")

with ap.da.UpdateCursor(flow_feature,["cod_mun_origem", "cod_mun_destino", "volume_total", "MODE", "ACCESS", "EXIT"]) as cur:
    for i,row in enumerate(cur,1):
        try:
            ap.MakeFeatureLayer_management(stops, "stops_lyr")
            where_clause_1 = '"LP_ID"' + " = '" + str(row[0]) + "'"
            temp_select = ap.SelectLayerByAttribute_management('stops_lyr', 'NEW_SELECTION', where_clause_1)
            where_clause_2 = '"LP_ID"' + " = '" + str(row[1]) + "'"
            ap.SelectLayerByAttribute_management('stops_lyr', 'ADD_TO_SELECTION', where_clause_2)
            inStops = temp_select
            route_name = str(row[0]) + "_" + str(row[1])

            result_object = ap.na.MakeRouteAnalysisLayer(inNetworkDataset, route_name, "Multimodal")
            layer_object = result_object.getOutput(0)
            sublayer_names = ap.na.GetNAClassNames(layer_object)
            stops_layer_name = sublayer_names["Stops"]
            routes_layer_name = sublayer_names["Routes"]
            ap.na.AddLocations(layer_object, stops_layer_name, inStops, search_criteria = [["Clip_OutFeatureClass_Ferrovias", "NONE"],
                                                                                            ["Clip_OutFeatureClass_Rodovias", "SHAPE"],
                                                                                            ["SC_TIN_TinEdge", "NONE"],
                                                                                            ["Rodo_OD", "SHAPE"],
                                                                                            ["Rodo_TermFerrExistente", "SHAPE"],
                                                                                            ["Rodo_TermFerrNovo", "SHAPE"],
                                                                                            ["BR_RG_Imediatas_2021_MeanCenter", "SHAPE"],
                                                                                            ["Clip_OutFeatureClass_fc_ferro_terminal_de_carga", "SHAPE"],
                                                                                            ["SC_ND_Junctions", "NONE"]])
            ap.na.Solve(layer_object)
            routes_sublayer = ap.na.GetNASublayer(layer_object, routes_layer_name)
            ap.management.CopyFeatures(routes_sublayer, routes_path + route_name)
            ap.na.CopyTraversedSourceFeatures(layer_object, edges_path, "TraversedEdges_" + route_name, "TraversedJunctions_" + route_name, "TraversedTurns_" + route_name)
            temp_junction = edges_path + "/" + "TraversedEdges_" + route_name
            intermodal_data[route_name] = []

        except Exception:
            e = sys.exc_info()[1]
            print(e.args[0])
            ap.AddError(e.args[0])

        cur.updateRow(row)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 20 Oct 2022 23:37:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/route-analysis-network-in-python-generates-the/m-p/1224132#M61361</guid>
      <dc:creator>ConnieSu</dc:creator>
      <dc:date>2022-10-20T23:37:16Z</dc:date>
    </item>
  </channel>
</rss>

