How to create a network dataset on a shapefile


This sample will show you how to programmatically create a Shapefile network dataset within ArcCatalog. The sample will demonstrate creating a network dataset using a Data Element. It is designed to work with the Streets shapefile that is located in the Samples\data\NetworkAnalyst folder. To run the sample, copy the shapefile to another directory, select the it in ArcCatalog and run the code.

How to use

  1. In ArcCatalog, copy the shapefile to another directory.
  2. Using either the treeview or content view, select the Streets shapefile
  3. Paste this code into a VBA module or ThisDocument in ArcCatalog
  4. Call the function CreateNetworkDataset_Example()
[VBA]
Option Explicit

'To use this macro: Select the shapefile on which to build the network in ArcCatalog.

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
    
    '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 = pDS.Name
    pDENDS.SupportsTurns = False
    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
    
    'Create a new EdgeFeatureSource
    Set pNDSource = New EdgeFeatureSource
    'Name of the Network Source, use the dataset name
    pNDSource.Name = pDS.Name
    
    'Set the some of the EdgeSourceSpecific parameters
    Set pEFS = pNDSource
    'Define the Connectivity Settings (Group and Policy at the class level)
    pEFS.UsesSubtypes = False
    pEFS.ClassConnectivityGroup = 1
    pEFS.ClassConnectivityPolicy = esriNECPEndVertex
    
    pEFS.FromElevationFieldName = "F_ZLEV"
    pEFS.ToElevationFieldName = "T_ZLEV"
    
    'Specify Directions on the Network Source
    Dim pNetSrcDirections As INetworkSourceDirections
    Dim pStreetNameFields As IStreetNameFields
    Dim pDirectionsArray As IArray
    
    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 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
    
    '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 "[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
    
    'Add the array to the network dataset
    Set pDENDS.Attributes = pArray
    
    '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
    
    '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
    
    '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
    
    '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 pWorExtCont As IWorkspaceExtensionManager
    Dim pWorExt As IWorkspaceExtension
    Dim pDSContainter2 As IDatasetContainer2
    Dim pUID As UID
    Dim pNDS As INetworkDataset
    
    'Set the value of the UID in order to use it to get a reference to the extension
    Set pUID = New UID
    pUID.Value = "esriGeoDatabase.NetworkDatasetWorkspaceExtension"
    'Get a reference to the extension
    Set pWorExtCont = pDS.workspace
    Set pWorExt = pWorExtCont.FindExtension(pUID)
    Set pDSContainter2 = pWorExt
    
    'Create the network dataset
    Set pNDS = pDSContainter2.createDataset(pDENDS)
    
    pGxObj.Parent.Refresh
    
    Exit Sub
ErrorHandler:
    Debug.Print Err.Number, Err.Description
End Sub