This document is archived and information here might be outdated.  Recommended version.


How to generate directions (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Spatial query and analysis > Network analysis > Programming with the ArcGIS Network Analyst extension > How to generate directions (ArcObjects .NET 10.6 SDK)

How to generate directions


Summary
This topic describes how to programmatically generate and access the directions that accompany routes across a network dataset.
To automate your workflow, use the ArcGIS Network Analyst extension Geoprocessing tools. For this topic, use the Directions tool.

About generating directions

This method takes the INAContext interface as an input parameter as shown in the following code. To learn the usage of a Context, see the ArcGIS Network Analyst extension library overview topic, NetworkAnalyst.
[C#]
public void GenerateAccessAndOutputDirections(ESRI.ArcGIS.NetworkAnalyst.INAContext
    context, string outputFile)
{
    if (context == null || context.Result == null || !context.Result.HasValidResult)
        throw new System.Exception("Context results are not valid");

    // The solver's result type must be an NATraversalResult to generate directions.
    var traversalResult=context.Result as
        ESRI.ArcGIS.NetworkAnalyst.INATraversalResult;

    //To generate directions from the results of the NAVRPSolver, you must use the agent from the NAContext, 
    // which is accessed through INAVRPResult::InternalRouteContext.
    var vrpResult=context.Result as ESRI.ArcGIS.NetworkAnalyst.INAVRPResult;
    if (vrpResult != null)
        traversalResult=vrpResult.InternalRouteContext.Result as
            ESRI.ArcGIS.NetworkAnalyst.INATraversalResult;

    // Some solvers do not produce this type of result and, therefore, cannot produce directions.
    if (traversalResult == null)
        throw new System.Exception("Invalid result type");

    // Get all the generated routes, and store them in a set.
    ESRI.ArcGIS.NetworkAnalyst.INamedSet naClasses=context.NAClasses;
    var routesFClass=(ESRI.ArcGIS.Geodatabase.IFeatureClass)
        naClasses.get_ItemByName("Routes");
    ESRI.ArcGIS.esriSystem.ISet routesSet=new ESRI.ArcGIS.esriSystem.SetClass();
    ESRI.ArcGIS.Geodatabase.IFeatureCursor cursor=routesFClass.Search(null, false);
    ESRI.ArcGIS.Geodatabase.IFeature routeFeature=cursor.NextFeature();
    while (routeFeature != null)
    {
        routesSet.Add(routeFeature);
        routeFeature=cursor.NextFeature();
    }

    // Generate the directions.
    var streetDirectionsAgent=(ESRI.ArcGIS.NetworkAnalyst.INAStreetDirectionsAgent)
        context.Agents.get_ItemByName("StreetDirectionsAgent");
    streetDirectionsAgent.Execute(routesSet, null);

    // Iterate over the directions from each route passed in the route set.
    ESRI.ArcGIS.NetworkAnalyst.INAStreetDirectionsContainer directionsContainer =
        streetDirectionsAgent.DirectionsContainer;
    for (int directionsSetIndex=0; directionsSetIndex <
        directionsContainer.DirectionsCount; directionsSetIndex++)
    {
        // Iterate over individual direction items in each set of directions.
        ESRI.ArcGIS.NetworkAnalyst.INAStreetDirections streetDirections =
            directionsContainer.get_Directions(directionsSetIndex);
        for (int directionsIndex=0; directionsIndex <
            streetDirections.DirectionCount; directionsIndex++)
        {
            ESRI.ArcGIS.NetworkAnalyst.INAStreetDirection direction =
                streetDirections.get_Direction(directionsIndex);
            // To get tokenized strings for the direction, iterate over the string count.
            for (int tokenizedStringIndex=0; tokenizedStringIndex <
                direction.StringCount; tokenizedStringIndex++)
            {
                string tokenizedString=direction.get_String(tokenizedStringIndex);
            }

            // To get the network element associated with each of the directions, iterate over the result objects.
            for (int resultObjectIndex=0; resultObjectIndex <
                direction.ResultObjectCount; resultObjectIndex++)
            {
                ESRI.ArcGIS.NetworkAnalyst.INATraversalResultElement
                    traversalResultElement=direction.get_ResultElement
                    (resultObjectIndex);
            }
        }
    }

    //These driving directions can be saved in a .xml file by calling INAStreetDirectionsContainer.SaveAsXML(<XMLfilename>).
    directionsContainer.SaveAsXML(outputFile);
}
[VB.NET]
Public Sub GenerateAccessAndOutputDirections(ByVal context As ESRI.ArcGIS.NetworkAnalyst.INAContext, ByVal outputFile As String)
    If context Is Nothing OrElse context.Result Is Nothing OrElse Not context.Result.HasValidResult Then
        Throw New System.Exception("Context results are not valid")
    End If
    
    ' The solver's result type must be an NATraversalResult to generate directions.
    Dim traversalResult=TryCast(context.Result, ESRI.ArcGIS.NetworkAnalyst.INATraversalResult)
    
    'To generate directions from the results of the NAVRPSolver, you must use the agent from the NAContext,
    ' which is accessed through INAVRPResult::InternalRouteContext.
    Dim vrpResult=TryCast(context.Result, ESRI.ArcGIS.NetworkAnalyst.INAVRPResult)
    If vrpResult IsNot Nothing Then
        traversalResult=TryCast(vrpResult.InternalRouteContext.Result, ESRI.ArcGIS.NetworkAnalyst.INATraversalResult)
    End If
    
    ' Some solvers do not produce this type of result and, therefore, cannot produce directions.
    If traversalResult Is Nothing Then
        Throw New System.Exception("Invalid result type")
    End If
    
    ' Get all the generated routes, and store them in a set.
    Dim naClasses As ESRI.ArcGIS.NetworkAnalyst.INamedSet=context.NAClasses
    Dim routesFClass=DirectCast(naClasses.get_ItemByName("Routes"), ESRI.ArcGIS.Geodatabase.IFeatureClass)
    Dim routesSet As ESRI.ArcGIS.esriSystem.ISet=New ESRI.ArcGIS.esriSystem.SetClass()
    Dim cursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor=routesFClass.Search(Nothing, False)
    Dim routeFeature As ESRI.ArcGIS.Geodatabase.IFeature=cursor.NextFeature()
    While routeFeature IsNot Nothing
        routesSet.Add(routeFeature)
        routeFeature=cursor.NextFeature()
    End While
    
    ' Generate the directions.
    Dim streetDirectionsAgent=DirectCast(context.Agents.get_ItemByName("StreetDirectionsAgent"), ESRI.ArcGIS.NetworkAnalyst.INAStreetDirectionsAgent)
    streetDirectionsAgent.Execute(routesSet, Nothing)
    
    ' Iterate over the directions from each route passed in the route set.
    Dim directionsContainer As ESRI.ArcGIS.NetworkAnalyst.INAStreetDirectionsContainer=streetDirectionsAgent.DirectionsContainer
    For directionsSetIndex As Integer=0 To directionsContainer.DirectionsCount - 1
        ' Iterate over individual direction items in each set of directions.
        Dim streetDirections As ESRI.ArcGIS.NetworkAnalyst.INAStreetDirections=directionsContainer.get_Directions(directionsSetIndex)
        For directionsIndex As Integer=0 To streetDirections.DirectionCount - 1
            Dim direction As ESRI.ArcGIS.NetworkAnalyst.INAStreetDirection=streetDirections.get_Direction(directionsIndex)
            ' To get tokenized strings for the direction, iterate over the string count.
            For tokenizedStringIndex As Integer=0 To direction.StringCount - 1
                Dim tokenizedString As String=direction.get_String(tokenizedStringIndex)
            Next
            
            ' To get the network element associated with each of the directions, iterate over the result objects.
            For resultObjectIndex As Integer=0 To direction.ResultObjectCount - 1
                Dim traversalResultElement As ESRI.ArcGIS.NetworkAnalyst.INATraversalResultElement=direction.get_ResultElement(resultObjectIndex)
            Next
        Next
    Next
    
    'These driving directions can be saved in a .xml file by calling INAStreetDirectionsContainer.SaveAsXML(<XMLfilename>).
    directionsContainer.SaveAsXML(outputFile)
End Sub


See Also:

What is ArcGIS Network Analyst extension?
About the ArcGIS Network Analyst extension Tutorial
Setting directions




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS Desktop Advanced: Network Analyst ArcGIS Desktop Advanced: Network Analyst
ArcGIS Desktop Standard: Network Analyst ArcGIS Desktop Standard: Network Analyst
ArcGIS Desktop Basic: Network Analyst ArcGIS Desktop Basic: Network Analyst