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


How to access source features referenced by a network dataset (ArcObjects .NET 10.5 SDK)

How to access source features referenced by a network dataset


Summary
This topic describes how to access source features that are referenced as edges and junctions in a network dataset.
For more information on programming with the ArcGIS Network Analyst extension application programming interface (API), see the ArcGIS Network Analyst extension library overview topic.

This method uses a network dataset, an element identification (EID), and a type as shown in the following code. The EID represents the ID for a network element as referenced by the network dataset. The type represents the kind of source feature element.
To get the INetworkDataset interface, see How to open a network dataset.
[C#]
private ESRI.ArcGIS.Geodatabase.IFeature GetSourceFeature
    (ESRI.ArcGIS.Geodatabase.INetworkDataset networkDataset, int eid,
    ESRI.ArcGIS.Geodatabase.esriNetworkElementType elementType)
{
    // The element will be retrieved from the INetworkQuery interface on the network dataset.
    var netQuery=networkDataset as ESRI.ArcGIS.Geodatabase.INetworkQuery;

    // Create an empty element.
    ESRI.ArcGIS.Geodatabase.INetworkElement element=netQuery.CreateNetworkElement
        (elementType);

    // Populate the element base on the EID.
    switch (elementType)
    {
        case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETEdge:
            netQuery.QueryEdge(eid,
                ESRI.ArcGIS.Geodatabase.esriNetworkEdgeDirection.esriNEDAlongDigitized, element as ESRI.ArcGIS.Geodatabase.INetworkEdge2);
            break;
        case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETJunction:
            netQuery.QueryJunction(eid, element as
                ESRI.ArcGIS.Geodatabase.INetworkJunction);
            break;
        case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETTurn:
            netQuery.QueryTurn(eid, element as ESRI.ArcGIS.Geodatabase.INetworkTurn);
            break;
    }

    // Get the source ID and OID from the element.
    int sourceID=element.SourceID;
    int sourceOID=element.OID;

    // Get the network source using the source ID of the element.
    ESRI.ArcGIS.Geodatabase.INetworkSource netSource=networkDataset.get_SourceByID
        (sourceID);

    // Get the network source as a feature class.
    var fClassContainer=networkDataset as
        ESRI.ArcGIS.Geodatabase.IFeatureClassContainer;
    ESRI.ArcGIS.Geodatabase.IFeatureClass sourceFClass =
        fClassContainer.get_ClassByName(netSource.Name);

    // Finally, get the source feature from the source feature class.
    return sourceFClass.GetFeature(sourceOID);
}
[VB.NET]
Private Function GetSourceFeature(ByVal networkDataset As ESRI.ArcGIS.Geodatabase.INetworkDataset, ByVal eid As Integer, ByVal elementType As ESRI.ArcGIS.Geodatabase.esriNetworkElementType) As ESRI.ArcGIS.Geodatabase.IFeature
    ' The element will be retrieved from the INetworkQuery interface on the network dataset.
    Dim netQuery=TryCast(networkDataset, ESRI.ArcGIS.Geodatabase.INetworkQuery)
    
    ' Create an empty element.
    Dim element As ESRI.ArcGIS.Geodatabase.INetworkElement=netQuery.CreateNetworkElement(elementType)
    
    ' Populate the element base on the EID.
    Select Case elementType
        Case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETEdge
            netQuery.QueryEdge(eid, ESRI.ArcGIS.Geodatabase.esriNetworkEdgeDirection.esriNEDAlongDigitized, TryCast(element, ESRI.ArcGIS.Geodatabase.INetworkEdge2))
            Exit Select
        Case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETJunction
            netQuery.QueryJunction(eid, TryCast(element, ESRI.ArcGIS.Geodatabase.INetworkJunction))
            Exit Select
        Case ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETTurn
            netQuery.QueryTurn(eid, TryCast(element, ESRI.ArcGIS.Geodatabase.INetworkTurn))
            Exit Select
    End Select
    
    ' Get the source ID and OID from the element.
    Dim sourceID As Integer=element.SourceID
    Dim sourceOID As Integer=element.OID
    
    ' Get the network source using the source ID of the element.
    Dim netSource As ESRI.ArcGIS.Geodatabase.INetworkSource=networkDataset.get_SourceByID(sourceID)
    
    ' Get the network source as a feature class.
    Dim fClassContainer=TryCast(networkDataset, ESRI.ArcGIS.Geodatabase.IFeatureClassContainer)
    Dim sourceFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=fClassContainer.get_ClassByName(netSource.Name)
    
    ' Finally, get the source feature from the source feature class.
    Return sourceFClass.GetFeature(sourceOID)
End Function


See Also:

What is ArcGIS Network Analyst extension?
What is a network dataset?
NetworkAnalyst
Geodatabase
About the ArcGIS Network Analyst extension Tutorial




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: Network Analyst
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