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();
        nsdArray.Add(streetNameFields);
        nsDirections.StreetNameFields = nsdArray;
        edgeNetworkSource.NetworkSourceDirections = nsDirections;

        deND.SupportsTurns = true;

        IArray sourceArray = new ArrayClass();
        sourceArray.Add(edgeNetworkSource);
        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;
        lengthEvaluatedNetworkAttribute.set_DefaultEvaluator
          (esriNetworkElementType.esriNETEdge, lengthConstantNetworkEvaluator);
        lengthEvaluatedNetworkAttribute.set_DefaultEvaluator
          (esriNetworkElementType.esriNETJunction, lengthConstantNetworkEvaluator);
        lengthEvaluatedNetworkAttribute.set_DefaultEvaluator
          (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();
        attributeArray.Add(lengthEvaluatedNetworkAttribute);
        deND.Attributes = attributeArray;

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

        // Get the workspace extension and create the network dataset based on the data element.
        IWorkspaceExtensionManager workspaceExtensionManager =
          (IWorkspaceExtensionManager)featureWorkspace;
        IWorkspaceExtension workspaceExtension =
          workspaceExtensionManager.FindExtension(ndWorkspaceExtensionUID);
        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;
        networkBuild.BuildNetwork(geoDataset.Extent);

        //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

closed as off-topic by PolyGeo Mar 5 at 11:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the 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).

share|improve this answer

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