Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.