Directions (Network Analyst)

Summary

Generates turn-by-turn directions from a network analysis layer with routes. The directions can be written to a file in text, XML, or HTML format. If you provide an appropriate style sheet, the directions can be written to any other file format.

Usage

  • The tool solves the network analysis layer if it does not already have a valid result.

Parameters

LabelExplanationData Type
Input Network Analysis Layer

The network analysis layer for which directions will be generated. Directions can be generated only for route, closest facility, and vehicle routing problem network analysis layers.

Caution:
This tool does not support last mile delivery analysis layers even though this layer type supports directions.

Network Analyst Layer
Output File Type

Specifies the format that will be used for the output directions file. This parameter is ignored if the style sheet parameter has a value.

  • XMLThe output directions file will be generated as an XML file. Apart from direction strings and the length and time information for the routes, the file will also contain information about the maneuver type and the turn angle for each direction.
  • TextThe output directions file will be generated as a simple TXT file containing the direction strings, the length and, optionally, the time information for the routes.
  • HTMLThe output directions file will be generated as an HTML file containing the direction strings, the length and, optionally, the time information for the routes.
String
Output Directions File

The full path to the directions file that will be written.

If you provide a style sheet for the Stylesheet parameter, ensure that the file suffix for Output Directions File matches the file type the style sheet produces.

File
Report Length in These Units

Specifies the linear units that will be used in the directions file. For example, even if the impedance attribute has units of meters, you can show directions in miles.

  • Nautical milesThe linear units will be nautical miles
  • FeetThe linear units will be feet.
  • YardsThe linear units will be yards.
  • MilesThe linear units will be miles.
  • MetersThe linear units will be meters.
  • KilometersThe linear units will be kilometers.
String
Report Travel Time
(Optional)

Specifies whether travel time will be reported in the directions file.

  • Checked—Travel time will be reported in the directions file. This is the default.
  • Unchecked—Travel time will not be reported in the directions file.
Boolean
Time Attribute
(Optional)

The time-based cost attribute that will be used to provide travel times in the directions. The cost attribute must exist on the network dataset used by the input network analysis layer.

String
Language
(Optional)

The language that will be used for driving directions.

Use a two- or five-character language code representing one of the available languages for directions generation for this parameter value. In Python, you can retrieve a list of available language codes using the ListDirectionsLanguages function.

String
Style Name
(Optional)

Specifies the formatting style that will be used for directions.

  • Printable driving directionsPrintable turn-by-turn directions will be used.
  • Driving directions for a navigation deviceTurn-by-turn directions designed for an in-vehicle navigation device will be used.
  • Walking directionsTurn-by-turn walking directions, which are designed for pedestrian routes, will be used.
String
Stylesheet
(Optional)

The style sheet that will be used for generating a formatted output file type (such as a PDF, Word, or HTML). The suffix of the file in the output directions file parameter must match the file type that the style sheet generates. The tool overrides the output file type parameter if this parameter contains a value.

Tip:

To create custom HTML and text style sheets, copy and edit the style sheets from Network Analyst. The style sheets are available in the <ArcGIS installation directory>\Pro\Resources\NetworkAnalyst\Directions\Styles directory. The HTML style sheet is Dir2PHTML.xsl, and the text style sheet is Dir2PlainText.xsl.

File

Derived Output

LabelExplanationData Type
Network Analysis Layer

The updated network analysis layer with routes.

Network Analyst Layer

arcpy.management.Directions(in_network_analysis_layer, file_type, out_directions_file, report_units, {report_time}, {time_attribute}, {language}, {style_name}, {stylesheet})
NameExplanationData Type
in_network_analysis_layer

The network analysis layer for which directions will be generated. Directions can be generated only for route, closest facility, and vehicle routing problem network analysis layers.

Caution:
This tool does not support last mile delivery analysis layers even though this layer type supports directions.

Network Analyst Layer
file_type

Specifies the format that will be used for the output directions file. This parameter is ignored if the style sheet parameter has a value.

  • XMLThe output directions file will be generated as an XML file. Apart from direction strings and the length and time information for the routes, the file will also contain information about the maneuver type and the turn angle for each direction.
  • TEXTThe output directions file will be generated as a simple TXT file containing the direction strings, the length and, optionally, the time information for the routes.
  • HTMLThe output directions file will be generated as an HTML file containing the direction strings, the length and, optionally, the time information for the routes.
String
out_directions_file

If you provide a style sheet for the stylesheet parameter, ensure that the file suffix for out_directions_file matches the file type the style sheet produces.

File
report_units

Specifies the linear units that will be used in the directions file. For example, even if the impedance attribute has units of meters, you can show directions in miles.

  • FeetThe linear units will be feet.
  • YardsThe linear units will be yards.
  • MilesThe linear units will be miles.
  • MetersThe linear units will be meters.
  • KilometersThe linear units will be kilometers.
  • NauticalMilesThe linear units will be nautical miles
String
report_time
(Optional)

Specifies whether travel time will be reported in the directions file.

  • NO_REPORT_TIMETravel time will not be reported in the directions file.
  • REPORT_TIMETravel time will be reported in the directions file. This is the default.
Boolean
time_attribute
(Optional)

The time-based cost attribute that will be used to provide travel times in the directions. The cost attribute must exist on the network dataset used by the input network analysis layer.

String
language
(Optional)

The language that will be used for driving directions.

Use a two- or five-character language code representing one of the available languages for directions generation for this parameter value. In Python, you can retrieve a list of available language codes using the ListDirectionsLanguages function.

String
style_name
(Optional)

Specifies the formatting style that will be used for directions.

  • NA DesktopPrintable turn-by-turn directions will be used.
  • NA NavigationTurn-by-turn directions designed for an in-vehicle navigation device will be used.
  • NA CampusTurn-by-turn walking directions, which are designed for pedestrian routes, will be used.
String
stylesheet
(Optional)

The style sheet that will be used for generating a formatted output file type (such as a PDF, Word, or HTML). The suffix of the file in the output directions file parameter must match the file type that the style sheet generates. The tool overrides the output file type parameter if this parameter contains a value.

Tip:

To create custom HTML and text style sheets, copy and edit the style sheets from Network Analyst. The style sheets are available in the <ArcGIS installation directory>\Pro\Resources\NetworkAnalyst\Directions\Styles directory. The HTML style sheet is Dir2PHTML.xsl, and the text style sheet is Dir2PlainText.xsl.

File

Derived Output

NameExplanationData Type
output_layer

The updated network analysis layer with routes.

Network Analyst Layer

Code sample

Directions example 1 (Python window)

The following code example demonstrates using the Directions function with all the parameters:

arcpy.na.Directions("Route", "TEXT", "C:/Data/Route_Directions.txt", "Miles",
                    "REPORT_TIME", "Minutes")
Directions example 2 (workflow)

The following stand-alone Python script demonstrates using the Directions function to generate driving directions in an HTML file for a route.

# Name: Directions_Workflow.py
# Description: Generate driving directions in an html file for a route that
#              visits the store locations in the best sequence in order to
#              minimize the total travel time
# 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 inputs and outputs
    input_gdb = "C:/Data/SanFrancisco.gdb"
    network = os.path.join(input_gdb, "Transportation", "Streets_ND")
    layer_name = "StoreRoute"
    impedance = "TravelTime"
    start_location = os.path.join(input_gdb, "Analysis", "DistributionCenter")
    store_locations = os.path.join(input_gdb, "Analysis", "Stores")
    output_directions = os.path.join(output_dir,
                                                layer_name + "_Directions.html")
    output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")

    #Create a new route layer. The route starts at the distribution center and
    #visits the stores in the best order to yield the shortest travel time.
    result_object = arcpy.na.MakeRouteLayer(network, layer_name, impedance,
                                         "FIND_BEST_ORDER", "PRESERVE_FIRST",
                                         "", ['Meters'], "NO_UTURNS",
                                         start_date_time="8 AM")

    #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"]

    #Load the distribution center as the start location using default field
    #mappings and search tolerance
    arcpy.na.AddLocations(layer_object, stops_layer_name, start_location, "",
                            "", exclude_restricted_elements = "EXCLUDE")

    #Load the store locations as stops. Make sure the store locations are
    #appended to the Stops sublayer so the distribution center you just loaded
    #isn't overwritten. Map the ServiceTime field from the input data to the
    #Attr_TravelTime property in the Stops sublayer so that the time it takes to
    #service each store is included in the total travel time for the route.
    field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
                                                            stops_layer_name)
    field_mappings["Name"].mappedFieldName = "Name"
    field_mappings["Attr_" + impedance].mappedFieldName = "ServiceTime"
    arcpy.na.AddLocations(layer_object, stops_layer_name, store_locations,
                          field_mappings, "", append="APPEND",
                          exclude_restricted_elements = "EXCLUDE")

    #Generate driving directions in an HTML file
    arcpy.na.Directions(layer_object, "HTML", output_directions, "Miles",
                        "REPORT_TIME", impedance)

    #Save the solved na 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)))

Environments