arcpy.na.MakeRouteLayer(in_network_dataset, out_network_analysis_layer, impedance_attribute, {find_best_order}, {ordering_type}, {time_windows}, {accumulate_attribute_name}, {UTurn_policy}, {restriction_attribute_name}, {hierarchy}, {hierarchy_settings}, {output_path_shape}, {start_date_time})
|
Name
|
Explanation
|
Data type
|
|
in_network_dataset
|
The network dataset on which the route analysis will be performed.
|
Network Dataset Layer
|
|
out_network_analysis_layer
|
Name of the route network analysis layer to create.
|
String
|
|
impedance_attribute
|
The cost attribute that will be used as impedance in the analysis.
|
String
|
|
find_best_order
(Optional)
|
Specifies whether the input stops must be visited in a particular order when calculating the optimal route. This option changes the route analysis from a shortest-path problem to a traveling salesperson problem (TSP).
FIND_BEST_ORDER—The stops will be reordered to find the optimal route. This option changes the route analysis from a shortest-path problem to a traveling salesperson problem (TSP).
USE_INPUT_ORDER—The stops will be visited in the input order. This is the default.
|
Boolean
|
|
ordering_type
(Optional)
|
Specifies the ordering of stops when FIND_BEST_ORDER is used.
PRESERVE_BOTH—The first and last stops by input order will be preserved as the first and last stops in the route.
PRESERVE_FIRST—The first stop by input order will be preserved as the first stop in the route, but the last stop can be reordered.
PRESERVE_LAST—The last stop by input order will be preserved as the last stop in the route, but the first stop can be reordered.
PRESERVE_NONE—The first and last stops will not be preserved and can be reordered.
|
String
|
|
time_windows
(Optional)
|
Specifies whether time windows will be used at the stops.
USE_TIMEWINDOWS—The route will consider time windows on the stops. If a stop is arrived at before its time window, there will be wait time until the time window starts. If a stop is arrived at after its time window, there will be a time window violation. Total time window violation is balanced against minimum impedance when computing the route. This is a valid option only when the impedance is in time units.
NO_TIMEWINDOWS—The route will ignore time windows on the stops. This is the default.
|
Boolean
|
|
accumulate_attribute_name
[accumulate_attribute_name,...]
(Optional)
|
A list of cost attributes to be accumulated during analysis. These accumulation attributes are for reference only; the solver only uses the cost attribute specified by the Impedance Attribute parameter to calculate the route.
For each cost attribute that is accumulated, a Total_ [Impedance] property is added to the routes that are output by the solver.
|
String
|
|
UTurn_policy
(Optional)
|
Specifies the U-turn policy that will be used at junctions. Allowing U-turns implies that the solver can turn around at a junction and double back on the same street. Given that junctions represent street intersections and dead ends, different vehicles may be able to turn around at some junctions but not at others—it depends on whether the junction represents an intersection or a dead end. To accommodate this, the U-turn policy parameter is implicitly specified by the number of edges that connect to the junction, which is known as junction valency. The acceptable values for this parameter are listed below; each is followed by a description of its meaning in terms of junction valency.
If you need a more precisely defined U-turn policy, consider adding a global turn delay evaluator to a network cost attribute or adjusting its settings if one exists, and pay particular attention to the configuration of reverse turns. You can also set the CurbApproach property of your network locations.
ALLOW_UTURNS—U-turns are permitted at junctions with any number of connected edges. This is the default value.
NO_UTURNS—U-turns are prohibited at all junctions, regardless of junction valency. However, U-turns are still permitted at network locations even when this option is specified, but you can set the individual network location's CurbApproach property to prohibit U-turns there as well.
ALLOW_DEAD_ENDS_ONLY—U-turns are prohibited at all junctions, except those that have only one adjacent edge (a dead end).
ALLOW_DEAD_ENDS_AND_INTERSECTIONS_ONLY—U-turns are prohibited at junctions where exactly two adjacent edges meet but are permitted at intersections (junctions with three or more adjacent edges) and dead ends (junctions with exactly one adjacent edge). Often, networks have extraneous junctions in the middle of road segments. This option prevents vehicles from making U-turns at these locations.
|
String
|
|
restriction_attribute_name
[restriction_attribute_name,...]
(Optional)
|
A list of restriction attributes that will be applied during the analysis.
|
String
|
|
hierarchy
(Optional)
|
The parameter is not used if no hierarchy attribute is defined on the network dataset used to perform the analysis.
USE_HIERARCHY—The hierarchy attribute will be used for the analysis. Using a hierarchy results in the solver preferring higher-order edges to lower-order edges. Hierarchical solves are faster, and they can be used to simulate the preference of a driver who chooses to travel on freeways rather than local roads when possible—even if that means a longer trip. This option is valid only if the input network dataset has a hierarchy attribute.
NO_HIERARCHY—The hierarchy attribute will not be used for the analysis, and the result will be an exact route for the network dataset.
|
Boolean
|
|
hierarchy_settings
(Optional)
|
Legacy:
Prior to version 10, this parameter allowed you to change the hierarchy ranges for the analysis from the default hierarchy ranges established in the network dataset. At version 10, this parameter is no longer supported and should be specified as an empty string. To change the hierarchy ranges for the analysis, update the default hierarchy ranges in the network dataset.
|
Network Analyst Hierarchy Settings
|
|
output_path_shape
(Optional)
|
Specifies the shape type that will be used for the route features that are output by the analysis.
Regardless of the output shape type specified, the best route is always determined by the network impedance, not Euclidean distance. This means that only the route shapes are different, not the underlying traversal of the network.
TRUE_LINES_WITH_MEASURES—The output routes will have the exact shape of the underlying network sources. The output includes route measurements for linear referencing. The measurements increase from the first stop and record the cumulative impedance to reach a given position.
TRUE_LINES_WITHOUT_MEASURES—The output routes will have the exact shape of the underlying network sources.
STRAIGHT_LINES—The output route shape will be a single straight line between the stops.
NO_LINES—No shape will be generated for the output routes.
|
String
|
|
start_date_time
(Optional)
|
The start date and time for the route. Route start time is typically used to find routes based on the impedance attribute that varies with the time of the day. For example, a start time of 7:00 a.m. can be used to find a route that considers rush hour traffic. The default value for this parameter is 8:00 a.m. A date and time can be specified as 10/21/05 10:30 AM. If the route spans multiple days and only the start time is specified, the current date is used.
Configure the analysis to use one of the following special dates to model a day of the week or the current date instead of a specific, static date:
Today—12/30/1899
Sunday—12/31/1899
Monday—1/1/1900
Tuesday—1/2/1900
Wednesday—1/3/1900
Thursday—1/4/1900
Friday—1/5/1900
Saturday—1/6/1900
For example, to specify that travel should begin at 5:00 p.m. on Tuesday, specify the parameter value as 1/2/1900 5:00 PM.
After the solve, the start and end times of the route are populated in the output routes. These start and end times are also used when directions are generated.
Learn more about how dates and times are used and interpreted in a network analysis
|
Date
|
Derived output
|
Name
|
Explanation
|
Data type
|
|
output_layer
|
The newly created network analysis layer.
|
Network Analyst Layer
|
Code sample
MakeRouteLayer example 1 (Python window)
Run the tool using only the required parameters.
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteLayer(network, "WorkRoute", "TravelTime")
MakeRouteLayer example 2 (Python window)
Run the tool using all parameters.
network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeRouteLayer(network, "InspectionRoute", "TravelTime",
"FIND_BEST_ORDER", "PRESERVE_BOTH", "USE_TIMEWINDOWS",
["Meters", "TravelTime"],
"ALLOW_DEAD_ENDS_AND_INTERSECTIONS_ONLY", ["Oneway"],
"USE_HIERARCHY", "", "TRUE_LINES_WITH_MEASURES",
"1/1/1900 9:00 AM")
MakeRouteLayer example 3 (workflow)
The following stand-alone Python script demonstrates how the MakeRouteLayer function can be used to find a best route to visit the geocoded stop locations.
# Name: MakeRouteLayer_Workflow.py
# Description: Find a best route to visit the stop locations and save the
# route to a layer file. The stop locations are geocoded from a
# text file containing the addresses.
# Requirements: Network Analyst extension
#Import system modules
import arcpy
from arcpy import env
import os
try:
#Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
env.workspace = os.path.join(output_dir, "Output.gdb")
env.overwriteOutput = True
#Set local variables
input_gdb = "C:/Data/SanFrancisco.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
layer_name = "BestRoute"
impedance = "TravelTime"
address_locator = os.path.join(input_gdb, "SanFranciscoLocator")
address_table = "C:/Data/StopAddresses.csv"
address_fields = "Street Address;City City;State State;ZIP <None>"
out_stops = "GeocodedStops"
output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
#Create a new Route layer. For this scenario, the default values for all the
#remaining parameters statisfy the analysis requirements
result_object = arcpy.na.MakeRouteLayer(network, layer_name, impedance)
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object.
layer_object = result_object.getOutput(0)
#Get the names of all the sublayers within the route layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
stops_layer_name = sublayer_names["Stops"]
#Geocode the stop locations from a csv file containing the addresses.
#The Geocode Addresses tool can use a text or csv file as input table
#as long as the first line in the file contains the field names.
arcpy.geocoding.GeocodeAddresses(address_table, address_locator,
address_fields, out_stops)
#Load the geocoded address locations as stops mapping the address field from
#geocoded stop features as Name property using field mappings.
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
stops_layer_name)
field_mappings["Name"].mappedFieldName = "Address"
arcpy.na.AddLocations(layer_object, stops_layer_name, out_stops,
field_mappings, "",
exclude_restricted_elements="EXCLUDE")
#Solve the route layer, ignoring any invalid locations such as those that
#cannot be geocoded
arcpy.na.Solve(layer_object, "SKIP")
#Save the solved route layer as a layer file on disk
layer_object.saveACopy(output_layer_file)
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print("An error occurred on line %i" % tb.tb_lineno)
print(str(e))
MakeRouteLayer example 4 (workflow)
This example creates multiple routes in a single solve, which is often used to calculate distances or drive times between origin-destination pairs.
# Name: MakeRouteLayer_MultiRouteWorkflow.py
# Description: Calculate the home-work commutes for a set of people and save
# the output to a feature class
# Requirements: Network Analyst extension
#Import system modules
import arcpy
from arcpy import env
import datetime
import os
try:
#Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
env.workspace = os.path.join(output_dir, "Output.gdb")
env.overwriteOutput = True
#Set local variables
input_gdb = "C:/data/SanFrancisco.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
stops_home = os.path.join(input_gdb, "Analysis", "Commuters_Home")
stops_work = os.path.join(input_gdb, "Analysis", "Commuters_Work")
layer_name = "Commuters"
out_routes_featureclass = "Commuter_Routes"
impedance = "TravelTime"
#Set the time of day for the analysis to 8AM on a generic Monday.
start_time = datetime.datetime(1900, 1, 1, 8, 0, 0)
#Create a new Route layer. Optimize on TravelTime, but compute the
#distance traveled by accumulating the Meters attribute.
result_object = arcpy.na.MakeRouteLayer(network, layer_name, impedance,
accumulate_attribute_name=["Meters"],
hierarchy="NO_HIERARCHY",
start_date_time=start_time)
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object.
layer_object = result_object.getOutput(0)
#Get the names of all the sublayers within the route layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
stops_layer_name = sublayer_names["Stops"]
routes_layer_name = sublayer_names["Routes"]
#Before loading the commuters' home and work locations as route stops, set
#up field mapping. Map the "Commuter_Name" field from the input data to
#the RouteName property in the Stops sublayer, which ensures that each
#unique Commuter_Name will be placed in a separate route. Matching
#Commuter_Names from stops_home and stops_work will end up in the same
#route.
field_mappings = arcpy.na.NAClassFieldMappings(layer_object, stops_layer_name)
field_mappings["RouteName"].mappedFieldName = "Commuter_Name"
#Add the commuters' home and work locations as Stops. The same field mapping
#works for both input feature classes because they both have a field called
#"Commuter_Name"
arcpy.na.AddLocations(layer_object, stops_layer_name, stops_home,
field_mappings, "",
exclude_restricted_elements = "EXCLUDE")
arcpy.na.AddLocations(layer_object, stops_layer_name, stops_work,
field_mappings, "", append="APPEND",
exclude_restricted_elements = "EXCLUDE")
#Solve the route layer.
arcpy.na.Solve(layer_object)
# Get the output Routes sublayer and save it to a feature class
routes_sublayer = layer_object.listLayers(routes_layer_name)[0]
arcpy.management.CopyFeatures(routes_sublayer, out_routes_featureclass)
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print("An error occurred on line %i" % tb.tb_lineno)
print(str(e))