Creating a network dataset programmatically

2904
0
12-08-2014 12:37 AM
SmitaSathe
New Contributor

I am running into a problem with creating and building a network dataset programmatically. My feature dataset is hosted in an SDE database and I need to build a network dataset for it. I have version 10.2.1 installed. I have the following code to do it. I get an error at the very end of the function when CreateDataset is called:

Exception from HRESULT: 0x80040220

I got most of this code from the ESRI site and adapted it to my situation. I have not been able to figure out what the problem is. If someone could help me, I would be most obliged.

Thanks in advance....

Smita

 

        public static void CreateAndBuildNetworkDataset(IDatasetName datasetName)

        {

            IFeatureClass featureClass;

            try

            {

                // Create an empty data element for a buildable network dataset.

                IDENetworkDataset2 deNetworkDataset = new DENetworkDatasetClass();

                deNetworkDataset.Buildable = true;

                IDataset dataset = null;

                IFields iFields;

 

 

                OpenTableOrFeatureClass(datasetName, out iFields, out dataset);

                featureClass = dataset as IFeatureClass;

                IGeoDataset geoDataset = (IGeoDataset)dataset;

 

 

                // Copy the feature dataset's extent and spatial reference to the network dataset data element.

                IDEGeoDataset deGeoDataset = (IDEGeoDataset)deNetworkDataset;

                deGeoDataset.Extent = geoDataset.Extent;

                deGeoDataset.SpatialReference = geoDataset.SpatialReference;

 

 

                // Specify the name of the network dataset.

                IDataElement dataElement = (IDataElement)deNetworkDataset;

                dataElement.Name = string.Format("{0}_ND", datasetName.Name);

 

 

                //Specify network sources

                //*****************************************************************

                // Specify the network dataset's elevation model.

                deNetworkDataset.ElevationModel = esriNetworkElevationModel.esriNEMElevationFields;

                deNetworkDataset.NetworkType = esriNetworkDatasetType.esriNDTGeodatabase;

 

 

                // Create an EdgeFeatureSource object and point it to the Streets feature class.

                IEdgeFeatureSource streetsEdgeFeatureSource = new EdgeFeatureSourceClass();

                INetworkSource streetsNetworkSource = (INetworkSource)streetsEdgeFeatureSource;

                streetsNetworkSource.Name = dataset.Name;

                streetsNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;

 

 

                // Set the edge feature source's connectivity settings.

                streetsEdgeFeatureSource.UsesSubtypes = false;

                streetsEdgeFeatureSource.ClassConnectivityGroup = 1;

                streetsEdgeFeatureSource.ClassConnectivityPolicy =

                    esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;

 

 

                streetsEdgeFeatureSource.FromElevationFieldName = "FromElevation";

                streetsEdgeFeatureSource.ToElevationFieldName = "ToElevation";

 

 

                //Specifying directions settings

                // Create a StreetNameFields object and populate its settings for the Streets source.

                IStreetNameFields streetNameFields = new StreetNameFieldsClass();

                streetNameFields.Priority = 1; // Priority 1 indicates the primary street name.

                streetNameFields.StreetNameFieldName = "StreetName";

                streetNameFields.PrefixTypeFieldName = "PrefixType";

 

 

                // Add the StreetNameFields object to a new NetworkSourceDirections object,

                // then add it to the EdgeFeatureSource object created earlier.

                INetworkSourceDirections streetNetworkSourceDirections = new

                    NetworkSourceDirectionsClass();

                IArray streetNameFieldsArray = new ArrayClass();

                streetNameFieldsArray.Add(streetNameFields);

                streetNetworkSourceDirections.StreetNameFields = streetNameFieldsArray;

                streetsNetworkSource.NetworkSourceDirections = streetNetworkSourceDirections;

 

 

                IArray sourceArray = new ArrayClass();

                sourceArray.Add(streetsNetworkSource);

                deNetworkDataset.Sources = sourceArray;

                //*********************************************************************************

 

 

                //global turns

                deNetworkDataset.SupportsTurns = true;

 

 

                //Attributes

                IArray attributeArray = new ArrayClass();

 

 

                // Initialize variables reused when creating attributes.

                IEvaluatedNetworkAttribute evalNetAttr;

                INetworkAttribute2 netAttr2;

                INetworkFieldEvaluator netFieldEval;

                INetworkConstantEvaluator netConstEval;

 

                //Length field

                // Create an EvaluatedNetworkAttribute object and populate its settings.

                evalNetAttr = new EvaluatedNetworkAttributeClass();

                netAttr2 = (INetworkAttribute2)evalNetAttr;

                netAttr2.Name = "Length";

                netAttr2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;

                netAttr2.DataType = esriNetworkAttributeDataType.esriNADTDouble;

                netAttr2.Units = esriNetworkAttributeUnits.esriNAUMeters;

                netAttr2.UseByDefault = true;

 

 

                // Create evaluator objects and set them on the EvaluatedNetworkAttribute object.

                netFieldEval = new NetworkFieldEvaluatorClass();

                netFieldEval.SetExpression("[Shape]", "");

                //netFieldEval.SetExpression("[SpeedLimit]", "");

                evalNetAttr.set_Evaluator(streetsNetworkSource,

                    esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator)netFieldEval);

                evalNetAttr.set_Evaluator(streetsNetworkSource,

                    esriNetworkEdgeDirection.esriNEDAgainstDigitized, (INetworkEvaluator)

                    netFieldEval);

 

 

                netConstEval = new NetworkConstantEvaluatorClass();

                netConstEval.ConstantValue = 0;

                evalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,

                    (INetworkEvaluator)netConstEval);

                evalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,

                    (INetworkEvaluator)netConstEval);

                evalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,

                    (INetworkEvaluator)netConstEval);

 

                // Add the attribute to the array.

                attributeArray.Add(evalNetAttr);

 

                //************************************************************************************************************************

                deNetworkDataset.Attributes = attributeArray;

 

 

                // Create a NetworkDirections object and populate its settings.

                INetworkDirections networkDirections = new NetworkDirectionsClass();

                networkDirections.DefaultOutputLengthUnits = esriNetworkAttributeUnits.esriNAUMiles;

                networkDirections.LengthAttributeName = "Length";

                //networkDirections.TimeAttributeName = "TravelTimeSeconds";

                //networkDirections.RoadClassAttributeName = "RoadClass";

 

 

                // Add the NetworkDirections object to the network dataset data element.

                deNetworkDataset.Directions = networkDirections;

 

 

                // Get the feature dataset extension and create the network dataset based on the data element.

                IFeatureDatasetExtensionContainer fdxContainer = (IFeatureDatasetExtensionContainer)

                    featureClass.FeatureDataset;

                IFeatureDatasetExtension featureDatasetExtension = fdxContainer.FindExtension

                    (esriDatasetType.esriDTNetworkDataset);

                IDatasetContainer2 datasetContainer2 = (IDatasetContainer2)featureDatasetExtension;

                IDEDataset deDataset = (IDEDataset)deNetworkDataset;

                INetworkDataset networkDataset = (INetworkDataset)datasetContainer2.CreateDataset

                    (deDataset);

 

                // Once the network dataset is created, build it.

                INetworkBuild networkBuild = (INetworkBuild)networkDataset;

                networkBuild.BuildNetwork(geoDataset.Extent);

            }

            catch (Exception ex)

            {

                //close

            }

 

 

        }

0 Kudos
0 Replies