How to create a network dataset


This sample will show you how to programmatically create a Geodatabase network dataset within ArcCatalog. The sample will demonstrate creating a network dataset using a Data Element. It is designed to work with the NetworkCity.mdb that is located in the Samples\data\NetworkAnalyst folder. The sample can also be run on SDE by copying the data to SDE. To run the sample, select the feature dataset in ArcCatalog and run the code.

How to use

  1. In ArcCatalog, select the FDS feature dataset in the NetworkCity.mdb file
  2. Paste this code into a VBA module or ThisDocument in ArcCatalog
  3. Call the function CreateNetworkDataset_Example()
[VBA]
Option Explicit

'To use this macro: Select the feature dataset in which to build the network in ArcCatalog.
'This macro is designed to work with the NetworkCity.mdb

Public Sub CreateNetworkDataset_Example()
    On Error GoTo ErrorHandler
    
    Dim pApp As IGxApplication
    Dim pGxObj As IGxObject
    Dim pName As IName
    Dim pDS As IDataset
    Dim pFCC As IFeatureClassContainer
    
    Set pApp = Application
    Set pGxObj = pApp.SelectedObject
    Set pName = pGxObj.InternalObjectName
    Set pDS = pName.Open
    Set pFCC = pDS
    
    'Get the Dataset and the Spatial Reference
    Dim pSR As ISpatialReference
    Dim pEnumDS As IEnumDataset
    Dim pGeoDS As IGeoDataset
    
    Set pGeoDS = pDS
    Set pSR = pGeoDS.SpatialReference
    
    'Create the DataElement and set the Spatial Reference
    Dim pDENDS As IDENetworkDataset
    Dim pDE As IDataElement
    Dim pDEGeoDS As IDEGeoDataset
    
    Set pDENDS = New DENetworkDataset
    Set pDE = pDENDS
    Set pDEGeoDS = pDENDS
    
    'Set the network dataset parameters on the DataElement and the DENetworkDataset
    pDE.Name = "MyNetwork"
    pDENDS.SupportsTurns = True
    pDENDS.Buildable = True
    pDENDS.ConfigurationKeyword = ""
    Set pDEGeoDS.SpatialReference = pSR
    
    Dim pFC As IFeatureClass
    Dim pNDSource As INetworkSource
    Dim pEFS As IEdgeFeatureSource
    Dim pDSFC As IDataset
    Dim pDSName As IDatasetName
    Dim pNetSrcDirections As INetworkSourceDirections
    Dim pStreetNameFields As IStreetNameFields
    Dim pDirectionsArray As IArray
    
    'Get the IDatasetName of the feature class to to add to the network
    Set pFC = pFCC.ClassByName("Street")
    'Create a new EdgeFeatureSource
    Set pNDSource = New EdgeFeatureSource
    'Name of the Network Source, it must be the same as the feature class.
    pNDSource.Name = "Street"
    
    'Set the some of the EdgeSourceSpecific parameters
    Set pEFS = pNDSource
    'Define the Connectivity Settings (Group and Policy at the Subtype level)
    pEFS.UsesSubtypes = True
    pEFS.SubtypeConnectivityGroup(0) = 2
    pEFS.SubtypeConnectivityPolicy(0) = esriNECPEndVertex
    pEFS.SubtypeConnectivityGroup(1) = 3
    pEFS.SubtypeConnectivityPolicy(1) = esriNECPEndVertex
    pEFS.SubtypeConnectivityGroup(2) = 1
    pEFS.SubtypeConnectivityPolicy(2) = esriNECPEndVertex
    
    'Set the Elevation Fields for the Streets Source
    pEFS.FromElevationFieldName = "F_ZLEV"
    pEFS.ToElevationFieldName = "T_ZLEV"
    
    'Specify Directions on the Network Source
    Set pNetSrcDirections = New NetworkSourceDirections
    Set pStreetNameFields = New StreetNameFields
    Set pDirectionsArray = New esriSystem.Array
    
    'Assign the street field names
    pStreetNameFields.Priority = 1
    pStreetNameFields.StreetNameFieldName = "Full_Name"
    pStreetNameFields.PrefixDirectionFieldName = "Prefix"
    pStreetNameFields.PrefixTypeFieldName = "Pre_Type"
    pStreetNameFields.SuffixTypeFieldName = "Type"
    pStreetNameFields.SuffixDirectionFieldName = "Suffix"
    
    pDirectionsArray.Add pStreetNameFields
    
    Set pNetSrcDirections.StreetNameFields = pDirectionsArray
    Set pNDSource.NetworkSourceDirections = pNetSrcDirections
    
    'Add the source to the Array
    Dim pArray As IArray
    Set pArray = New esriSystem.Array
    pArray.Add pNDSource
    
    'Add the second Source to the network
    Set pFC = pFCC.ClassByName("Rail")
    Set pNDSource = New EdgeFeatureSource
    pNDSource.Name = "Rail"
    
    Set pEFS = pNDSource
    pEFS.UsesSubtypes = False
    pEFS.ClassConnectivityGroup = 1
    pEFS.ClassConnectivityPolicy = esriNECPEndVertex
    
    pEFS.FromElevationFieldName = "F_ZLEV"
    pEFS.ToElevationFieldName = "T_ZLEV"
    
    Set pNetSrcDirections = New NetworkSourceDirections
    Set pStreetNameFields = New StreetNameFields
    Set pDirectionsArray = New esriSystem.Array
    
    'Assign the street field names. Only specify one of the field names
    pStreetNameFields.Priority = 1
    pStreetNameFields.StreetNameFieldName = "Full_Name"
    
    pDirectionsArray.Add pStreetNameFields
    
    Set pNetSrcDirections.StreetNameFields = pDirectionsArray
    Set pNDSource.NetworkSourceDirections = pNetSrcDirections
    
    pArray.Add pNDSource
    
    'Add the third source to the network, a Junction source
    'Add the second Source to the network
    Dim pJFS As IJunctionFeatureSource
    Set pFC = pFCC.ClassByName("RailStations")
    Set pNDSource = New JunctionFeatureSource
    pNDSource.Name = "RailStations"
    
    Set pJFS = pNDSource
    pJFS.UsesSubtypes = False
    pJFS.AddClassConnectivityGroup 1
    pJFS.AddClassConnectivityGroup 2
    pJFS.ClassConnectivityPolicy = esriNJCPOverride
    
    pArray.Add pNDSource
    
    'Add the fourth source to the network, the Turns source
    Dim pTFS As ITurnFeatureSource
    Set pFC = pFCC.ClassByName("Turns")
    Set pDSFC = pFC
    Set pDSName = pDSFC.FullName
    
    Set pNDSource = New TurnFeatureSource
    pNDSource.Name = "Turns"
    
    pArray.Add pNDSource
    
    'Add the Array to the DENetworkDataset
    Set pDENDS.Sources = pArray
    
    'Add an attribute to the network
    Dim pNDSAttribute As IEvaluatedNetworkAttribute
    Dim pSource As INetworkSource
    
    Set pNDSAttribute = New EvaluatedNetworkAttribute
    
    'Set the Name, Data and Usage type
    pNDSAttribute.Name = "Time"
    pNDSAttribute.DataType = esriNADTDouble
    pNDSAttribute.UsageType = esriNAUTCost
    pNDSAttribute.Units = esriNAUMinutes
    
    Dim pNetworkConstantEval As INetworkConstantEvaluator
    Dim pNetworkFieldEval As INetworkFieldEvaluator
    
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETEdge) = pNetworkConstantEval
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 1
    Set pNDSAttribute.DefaultEvaluator(esriNETJunction) = pNetworkConstantEval
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 2
    Set pNDSAttribute.DefaultEvaluator(esriNETTurn) = pNetworkConstantEval
    
    'Get the Streets source and set the Evaluator in the From-To and To-From directions
    Set pSource = pDENDS.Sources.Element(0)
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[FT_MINUTES]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAlongDigitized) = pNetworkFieldEval
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[TF_MINUTES]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAgainstDigitized) = pNetworkFieldEval
    
    'Get the Rails source and set the Evaluator in the From-To and To-From directions
    Set pSource = pDENDS.Sources.Element(1)
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[FT_MINUTES]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAlongDigitized) = pNetworkFieldEval
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[TF_MINUTES]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAgainstDigitized) = pNetworkFieldEval
    
    'Create a new Array and add the Network Attribute to the Array
    Set pArray = New esriSystem.Array
    pArray.Add pNDSAttribute
    
    'Create a new Evaluated Network Attribute
    Set pNDSAttribute = New EvaluatedNetworkAttribute
    
    'Set the Name, Data and Usage type
    pNDSAttribute.Name = "Meters"
    pNDSAttribute.DataType = esriNADTDouble
    pNDSAttribute.UsageType = esriNAUTCost
    pNDSAttribute.Units = esriNAUMeters
    
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETEdge) = pNetworkConstantEval
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETJunction) = pNetworkConstantEval
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETTurn) = pNetworkConstantEval
    
    'Get the source and set the Evaluator in the From-To and To-From directions
    Set pSource = pDENDS.Sources.Element(0)
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[Meters]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAlongDigitized) = pNetworkFieldEval
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[Meters]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAgainstDigitized) = pNetworkFieldEval
    
    'Get the Rails source and set the Evaluator in the From-To and To-From directions
    Set pSource = pDENDS.Sources.Element(1)
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[Meters]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAlongDigitized) = pNetworkFieldEval
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[Meters]", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAgainstDigitized) = pNetworkFieldEval
    
    'Add the Network Attribute to the Array
    pArray.Add pNDSAttribute
    
    'Add the array to the network dataset
    Set pDENDS.Attributes = pArray
    
    'Create a new Attribute, of type Restriction
    Set pNDSAttribute = New EvaluatedNetworkAttribute
    pNDSAttribute.Name = "OneWay"
    pNDSAttribute.DataType = esriNADTBoolean
    pNDSAttribute.UsageType = esriNAUTRestriction
    pNDSAttribute.Units = esriNAUUnknown
    
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETEdge) = pNetworkConstantEval
    Set pNetworkConstantEval = New NetworkConstantEvaluator
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETJunction) = pNetworkConstantEval
    pNetworkConstantEval.ConstantValue = 0
    Set pNDSAttribute.DefaultEvaluator(esriNETTurn) = pNetworkConstantEval
    
    'Get the Streets source and set the Evaluator in the From-To and To-From directions
    Set pSource = pDENDS.Sources.Element(0)
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[ONEWAY] = ""TF""", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAlongDigitized) = pNetworkFieldEval
    Set pNetworkFieldEval = New NetworkFieldEvaluator
    pNetworkFieldEval.SetExpression "[ONEWAY] = ""FT""", ""
    Set pNDSAttribute.Evaluator(pSource, esriNEDAgainstDigitized) = pNetworkFieldEval
    
    'Add the Network Attribute to the Array
    pArray.Add pNDSAttribute
    
    'Add the array to the network dataset
    Set pDENDS.Attributes = pArray
    
    'Specify the Directions for the network dataset
    Dim pNetworkDirections As INetworkDirections
    Set pNetworkDirections = New NetworkDirections
    pNetworkDirections.DefaultOutputLengthUnits = esriNAUMeters
    pNetworkDirections.TimeAttributeName = "Time"
    pNetworkDirections.LengthAttributeName = "Meters"
    
    Set pDENDS.Directions = pNetworkDirections
    
    'Create the network dataset
    Dim pFDSExtCont As IFeatureDatasetExtensionContainer
    Dim pNDS As INetworkDataset
    Dim pDSContainter2 As IDatasetContainer2
    
    'Get a reference to the network dataset extension
    Set pFDSExtCont = pDS
    Set pDSContainter2 = pFDSExtCont.FindExtension(esriDTNetworkDataset)
    
    Set pNDS = pDSContainter2.createDataset(pDENDS)
    
    pGxObj.Refresh
    
    Exit Sub
ErrorHandler:
    Debug.Print Err.Number, Err.Description
End Sub