Summary
Makes a route network analysis layer and sets its analysis properties. A route analysis layer is useful for determining the best route between a set of network locations based on a specified network cost.
Usage
After creating the analysis layer with this tool, you can add network analysis objects to it using the Add Locations tool, solve the analysis using the Solve tool, and save the results on disk using the Save To Layer File tool.
-
When using this tool in geoprocessing models, if the model is run as a tool, the output network analysis layer must be made a model parameter; otherwise, the output layer is not added to the contents of the map.
Syntax
MakeRouteLayer_na (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})
Parameter | 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 to be used as impedance in the analysis. | String |
find_best_order (Optional) |
| Boolean |
ordering_type (Optional) | Specifies the ordering of stops when FIND_BEST_ORDER is used.
| String |
time_windows (Optional) | Specifies whether time windows will be used at the stops.
| Boolean |
accumulate_attribute_name [accumulate_attribute_name,...] (Optional) | List of cost attributes to be accumulated during analysis. These accumulation attributes are purely for reference; 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) | The U-Turn policy at junctions. Allowing U-turns implies 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 dead end. To accommodate, the U-turn policy parameter is implicitly specified by how many edges 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. Also, look at setting the CurbApproach property of your network locations. | String |
restriction_attribute_name [restriction_attribute_name,...] (Optional) | List of restriction attributes to apply during the analysis. | String |
hierarchy (Optional) |
The parameter is not used if a hierarchy attribute is not defined on the network dataset used to perform the analysis. In such cases, use "#" as the parameter value. | Boolean |
hierarchy_settings (Optional) | Network Analyst Hierarchy Settings | |
output_path_shape (Optional) |
Specifies the shape type for the route features that are output by the analysis.
No matter which output shape type is chosen, the best route is always determined by the network impedance, never Euclidean distance. This means only the route shapes are different, not the underlying traversal of the network. | String |
start_date_time (Optional) | Specifies a start date and time for the route. Route start time is mostly used to find routes based on the impedance attribute that varies with the time of the day. For example, a start time of 7 a.m. could be used to find a route that considers the 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, then the current date is used. Instead of using a particular date, a day of the week can be specified using the following dates.
After the solve, the start and end time of the route are populated in the output routes. These start and end times are also used when directions are generated. | Date |
Code sample
MakeRouteLayer example 1 (Python window)
Execute 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)
Execute 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 tool 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
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/data/SanFrancisco.gdb"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = "Transportation/Streets_ND"
outNALayerName = "BestRoute"
impedanceAttribute = "TravelTime"
inAddressLocator = "SanFranciscoLocator"
inAddressTable = "C:/data/StopAddresses.csv"
inAddressFields = "Street Address VISIBLE NONE"
outStops = "GeocodedStops"
outLayerFile = "C:/data/output" + "/" + outNALayerName + ".lyr"
#Create a new Route layer. For this scenario, the default value for all the
#remaining parameters statisfies the analysis requirements
outNALayer = arcpy.na.MakeRouteLayer(inNetworkDataset, outNALayerName,
impedanceAttribute)
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object.
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the route layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
stopsLayerName = subLayerNames["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(inAddressTable, inAddressLocator,
inAddressFields, outStops)
#Load the geocoded address locations as stops mapping the address field from
#geocoded stop features as Name property using field mappings.
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, stopsLayerName)
fieldMappings["Name"].mappedFieldName = "Address"
arcpy.na.AddLocations(outNALayer, stopsLayerName, outStops, fieldMappings,
"", exclude_restricted_elements = "EXCLUDE")
#Solve the route layer, ignore any invalid locations such as those that
#can not be geocoded
arcpy.na.Solve(outNALayer,"SKIP")
#Save the solved route layer as a layer file on disk with relative paths
arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"RELATIVE")
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 datetime
#Import system modules
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/Data/SanFrancisco.gdb"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = "Transportation/Streets_ND"
inStops_Home = "Analysis/Commuters_Home"
inStops_Work = "Analysis/Commuters_Work"
outNALayerName = "Commuters"
outRoutesFC = "Analysis/outRoutes"
impedanceAttribute = "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.
outRouteResultObject = arcpy.na.MakeRouteLayer(inNetworkDataset, outNALayerName,
impedanceAttribute,
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.
outNALayer = outRouteResultObject.getOutput(0)
#Get the names of all the sublayers within the route layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Store the layer names that we will use later
stopsLayerName = subLayerNames["Stops"]
routesLayerName = subLayerNames["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 inStops_Home and inStops_Work will end up in the same
#route.
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, stopsLayerName)
fieldMappings["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(outNALayer, stopsLayerName, inStops_Home,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
arcpy.na.AddLocations(outNALayer, stopsLayerName, inStops_Work,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
#Solve the route layer.
arcpy.na.Solve(outNALayer)
# Get the output Routes sublayer and save it to a feature class
RoutesSubLayer = arcpy.mapping.ListLayers(outNALayer, routesLayerName)[0]
arcpy.management.CopyFeatures(RoutesSubLayer, outRoutesFC)
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)
Environments
Licensing information
- ArcGIS Desktop Basic: Yes
- ArcGIS Desktop Standard: Yes
- ArcGIS Desktop Advanced: Yes