How to generate a street intersections report


This sample will create a text file containing a list of all streets that meet at each intersection in the network. For each junction, it will show how many streets meet at that intersection, and listing these streets by OID and name, and indicating at what azimuth each street leaves the intersection. The sample demonstrates how to query for adjacent elements in the network dataset.

How to use

  1. In ArcCatalog, using either the treeview or content view, select a network dataset.
  2. Paste this code into a VBA module or ThisDocument in ArcCatalog.
  3. Modify the constant for the output text file to be created.
  4. Call the subroutine StreetIntersectionReport().
[VBA]
Option Explicit

'Location where the intersection report text file is to be created
Private Const c_TextFile As String = "C:\Temp\StreetIntersections.txt"

'Collection for looking up feature classes, keyed by SourceID
Private cSourceIDToFC As New Collection

'Collections for looking up street name fields, keyed by FeatureClassID
Private cPreDirLookup As Collection
Private cPreTypeLookup As Collection
Private cStreetNameLookup As Collection
Private cSufTypeLookup As Collection
Private cSufDirLookup As Collection

Public Sub StreetIntersectionReport()
    On Error GoTo ErrorHandler
    
    Dim pApp As IGxApplication
    Dim pGxObj As IGxObject
    Dim pName As IName
    Dim pDS As IDataset
    Dim pND As INetworkDataset
    Dim pSource As INetworkSource
    Dim pNSD As INetworkSourceDirections
    Dim pSNF As IStreetNameFields
    Dim pFCC As IFeatureClassContainer
    Dim pFC As IFeatureClass
    Dim fileNum As Integer
    Dim pNetQuery As INetworkQuery
    Dim pEnum As IEnumNetworkElement
    Dim pJunction As INetworkJunction
    Dim pEdge As INetworkEdge
    Dim lCount As Long, i As Long
    
    'Open the selected item in ArcCatalog and check that it is a network dataset
    Set pApp = Application
    Set pGxObj = pApp.SelectedObject
    Set pName = pGxObj.InternalObjectName
    Set pDS = pName.Open
    If pDS.Type <> esriDTNetworkDataset Then
        MsgBox "The selected item is not a network dataset."
        Set pDS = Nothing
        Exit Sub
    End If
    Set pND = pDS
    
    'Populate the collections for feature class and street name lookup
    Set cSourceIDToFC = New Collection
    Set cPreDirLookup = New Collection
    Set cPreTypeLookup = New Collection
    Set cStreetNameLookup = New Collection
    Set cSufTypeLookup = New Collection
    Set cSufDirLookup = New Collection
    Dim sFCID As String
    Set pFCC = pND
    lCount = pND.SourceCount
    For i = 0 To lCount - 1
        Set pSource = pND.Source(i)
        If pSource.SourceType = esriNSTEdgeFeature Then
            Set pFC = pFCC.ClassByName(pSource.Name)
            cSourceIDToFC.Add pFC, CStr(pSource.ID)
            sFCID = CStr(pFC.FeatureClassID)
            Set pNSD = pSource.NetworkSourceDirections
            If pNSD Is Nothing Then 'There are no street name settings for directions
                cPreDirLookup.Add -1, sFCID
                cPreTypeLookup.Add -1, sFCID
                cStreetNameLookup.Add -1, sFCID
                cSufTypeLookup.Add -1, sFCID
                cSufDirLookup.Add -1, sFCID
            Else
                Set pSNF = pNSD.StreetNameFields.Element(0)
                cPreDirLookup.Add pFC.FindField(pSNF.PrefixDirectionFieldName), sFCID
                cPreTypeLookup.Add pFC.FindField(pSNF.PrefixTypeFieldName), sFCID
                cStreetNameLookup.Add pFC.FindField(pSNF.StreetNameFieldName), sFCID
                cSufTypeLookup.Add pFC.FindField(pSNF.SuffixTypeFieldName), sFCID
                cSufDirLookup.Add pFC.FindField(pSNF.SuffixDirectionFieldName), sFCID
            End If
        End If
    Next i
    
    'Open the output text file
    On Error GoTo closeTxtFileDueToError
    fileNum = FreeFile
    Open c_TextFile For Output As fileNum
    
    'Query all intersections in the network write them to the file
    Set pNetQuery = pND
    Set pEdge = pNetQuery.CreateNetworkElement(esriNETEdge)
    Set pEnum = pNetQuery.Elements(esriNETJunction)
    pEnum.Reset
    Set pJunction = pEnum.Next
    Do Until pJunction Is Nothing
        lCount = pJunction.EdgeCount
        Print #fileNum, "Junction EID " & pJunction.EID & ", Valence " & lCount
        For i = 0 To lCount - 1
            pJunction.QueryEdge i, True, pEdge
            Print #fileNum, CInt(pEdge.FromAzimuth) & Chr(176) & ", OID " & pEdge.OID & ", " & GetStreetName(pEdge)
        Next i
        Print #fileNum, "" 'make a blank line
        Set pJunction = pEnum.Next
    Loop
    Close fileNum
    
    Exit Sub
closeTxtFileDueToError:
    Close fileNum
ErrorHandler:
    Debug.Print Err.Number, Err.Description
End Sub

Private Function GetStreetName(pEdge As INetworkEdge) As String
    Dim pFC As IFeatureClass
    Dim sFCID As String
    Dim pFeature As IFeature
    
    Set pFC = cSourceIDToFC(CStr(pEdge.SourceID))
    sFCID = CStr(pFC.FeatureClassID)
    Set pFeature = pFC.GetFeature(pEdge.OID)
    
    If cStreetNameLookup(sFCID) = -1 Then
        GetStreetName = "" 'No street name given
        Exit Function
    End If
    
    GetStreetName = pFeature.Value(cStreetNameLookup(sFCID))
    If cPreTypeLookup(sFCID) <> -1 Then GetStreetName = pFeature.Value(cPreTypeLookup(sFCID)) & " " & GetStreetName
    If cSufTypeLookup(sFCID) <> -1 Then GetStreetName = GetStreetName & " " & pFeature.Value(cSufTypeLookup(sFCID))
    If cPreDirLookup(sFCID) <> -1 Then GetStreetName = pFeature.Value(cPreDirLookup(sFCID)) & " " & GetStreetName
    If cSufDirLookup(sFCID) <> -1 Then GetStreetName = GetStreetName & " " & pFeature.Value(cSufDirLookup(sFCID))
End Function