Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following C# code to create a Network Dataset layer that I can bring into ArcMap. However, even though I am able to create a Route from the ND, I am not able to create Barriers of any kind using this ND (even when using the editing t. What attributes do I set, or what should I do to the code that will allow for a Barrier layer in the Route layer that gets created when I click on "Network Analyst" and "New Route"?

    public ILayer CreateNDLayer(string sFeatureWSname, string sShapefileName)

        // Create an empty data element for a buildable network dataset.
        IDENetworkDataset2 deND = new DENetworkDatasetClass();
        deND.Buildable = true;

        // Open the shapefile feature class
        IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(sFeatureWSname, 0);
        IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(sShapefileName + ".shp");
        IGeoDataset geoDataset = (IGeoDataset)featureClass;

        // Copy the feature dataset's extent and spatial reference to the network dataset data element.
        IDEGeoDataset deGeoDataset = (IDEGeoDataset)deND;
        deGeoDataset.Extent = geoDataset.Extent;
        deGeoDataset.SpatialReference = geoDataset.SpatialReference;

        // Specify the name of the network dataset.
        IDataElement dataElement = (IDataElement)deND;
        dataElement.Name = sShapefileName + "_ND";

        // Create an EdgeFeatureSource object and point it to the feature class.
        INetworkSource edgeNetworkSource = new EdgeFeatureSourceClass();
        edgeNetworkSource.Name = sShapefileName;
        edgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;

        // Specify the network dataset's elevation model.
        deND.ElevationModel = esriNetworkElevationModel.esriNEMNone;

        // Set the edge feature source's connectivity settings.
        IEdgeFeatureSource edgeFeatureSource = (IEdgeFeatureSource)edgeNetworkSource;
        edgeFeatureSource.UsesSubtypes = false;
        edgeFeatureSource.ClassConnectivityGroup = 1;
        edgeFeatureSource.ClassConnectivityPolicy = esriNetworkEdgeConnectivityPolicy.esriNECPAnyVertex;

        // Create a StreetNameFields object and populate its settings.
        IStreetNameFields streetNameFields = new StreetNameFieldsClass();
        streetNameFields.Priority = 1;
        streetNameFields.StreetNameFieldName = "nam";

        // Add the StreetNameFields object to a new NetworkSourceDirections object,
        // then add it to the EdgeFeatureSource created earlier.
        INetworkSourceDirections nsDirections = new NetworkSourceDirectionsClass();
        IArray nsdArray = new ArrayClass();
        nsDirections.StreetNameFields = nsdArray;
        edgeNetworkSource.NetworkSourceDirections = nsDirections;

        deND.SupportsTurns = true;

        IArray sourceArray = new ArrayClass();
        deND.Sources = sourceArray;

        // Create a new EvaluatedNetworkAttribute object and populate its settings.
        IEvaluatedNetworkAttribute2 lengthEvaluatedNetworkAttribute = new EvaluatedNetworkAttributeClass();
        INetworkAttribute2 lengthNetworkAttributes2 = (INetworkAttribute2)lengthEvaluatedNetworkAttribute;
        lengthNetworkAttributes2.Name = "Length";
        lengthNetworkAttributes2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;
        lengthNetworkAttributes2.DataType = esriNetworkAttributeDataType.esriNADTDouble;
        lengthNetworkAttributes2.Units = esriNetworkAttributeUnits.esriNAUDecimalDegrees;
        lengthNetworkAttributes2.UseByDefault = true;

        // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.
        INetworkFieldEvaluator lengthNetworkFieldEvaluator = new NetworkFieldEvaluatorClass();
        lengthNetworkFieldEvaluator.SetExpression("[SHAPE]", "");
        INetworkEvaluator ddNetworkEvaluator = (INetworkEvaluator)lengthNetworkFieldEvaluator;
        lengthEvaluatedNetworkAttribute.set_Evaluator(edgeNetworkSource, esriNetworkEdgeDirection.esriNEDAlongDigitized, ddNetworkEvaluator);
        lengthEvaluatedNetworkAttribute.set_Evaluator(edgeNetworkSource, esriNetworkEdgeDirection.esriNEDAgainstDigitized, ddNetworkEvaluator);

        INetworkConstantEvaluator lengthNetworkConstantEvaluator = new NetworkConstantEvaluatorClass();
        lengthNetworkConstantEvaluator.ConstantValue = 0;
        INetworkEvaluator lengthConstantNetworkEvaluator = (INetworkEvaluator)lengthNetworkConstantEvaluator;
          (esriNetworkElementType.esriNETEdge, lengthConstantNetworkEvaluator);
          (esriNetworkElementType.esriNETJunction, lengthConstantNetworkEvaluator);
          (esriNetworkElementType.esriNETTurn, lengthConstantNetworkEvaluator);  

        //array for the network attributes will be created, then each attribute will be created and added to the array
        IArray attributeArray = new ArrayClass();
        deND.Attributes = attributeArray;

        // Create a new UID that references the NetworkDatasetWorkspaceExtension.
        UID ndWorkspaceExtensionUID = new UIDClass();
        ndWorkspaceExtensionUID.Value =

        // Get the workspace extension and create the network dataset based on the data element.
        IWorkspaceExtensionManager workspaceExtensionManager =
        IWorkspaceExtension workspaceExtension =
        IDatasetContainer2 datasetContainer2 = (IDatasetContainer2)workspaceExtension;
        IDEDataset deDataset = (IDEDataset)deND;
        INetworkDataset networkDataset = (INetworkDataset)datasetContainer2.CreateDataset(deDataset);

        // Once the network dataset is created, build it.
        INetworkBuild networkBuild = (INetworkBuild)networkDataset;

        //Create the Network Layer
        INetworkLayer networkLayer = new NetworkLayerClass();
        networkLayer.NetworkDataset = networkDataset;

        IFeatureClass fClass = featureWorkspace.OpenFeatureClass(sShapefileName + "_ND_Junctions.shp");
        IFeatureLayer junctionsLyr = new FeatureLayer();
        junctionsLyr.FeatureClass = fClass;
        junctionsLyr.Name = sShapefileName + "_ND_Junctions";

        //Return the ND layer
        ILayer pNetworkLayer = (ILayer)networkLayer;
        pNetworkLayer.Name = sShapefileName + "_NDlayer";
        return pNetworkLayer;

share|improve this question

After looking further into creating Routes, Barriers, etc. I have changed my approach, and no longer need an answer to this question.
I will be posting another question shortly, regarding how to programmatically create a way that the user can create barriers on a map in the same way that they would indicate the stops for the Route (which I already have implemented).
Thanks, Renee

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.