创建NetWorkDataset---FileGDB篇

/// <summary>

        /// 创建NetWorkDataset

        /// </summary>

        /// <returns>INetworkDataset.</returns>

        public INetworkDataset CreateNetWorkDataset()

        {

            log.WriteLog("开始创建NetWorkDataset...");

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

            IDENetworkDataset2 deNetworkDataset = new DENetworkDatasetClass();

            deNetworkDataset.Buildable = true;

            // Open the feature dataset and cast to the IGeoDataset interface.

            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");

            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

            IWorkspace workspace = workspaceFactory.OpenFromFile(@"E:\TEST.gdb", );

            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

            IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("HOSP");

            IGeoDataset geoDataset = (IGeoDataset)featureDataset;

            deNetworkDataset.NetworkType = esriNetworkDatasetType.esriNDTGeodatabase;

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

            IDEGeoDataset deGeoDataset = (IDEGeoDataset)deNetworkDataset;

            deGeoDataset.Extent = geoDataset.Extent;

            deGeoDataset.SpatialReference = geoDataset.SpatialReference;

            IDataElement dataElement = (IDataElement)deNetworkDataset;  // Specify the name of the network dataset.

            dataElement.Name = Functions.g_ROAD + "_ND";

            //删除已存在的dataset,否则再创建会报错

            IEnumDataset enumFeatureDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);

            enumFeatureDataset.Reset();

            IDataset featureDataset2 = null;

            IEnumDataset enumDataset = null;

            while ((featureDataset2 = enumFeatureDataset.Next()) != null)

            {

                enumDataset = featureDataset.Subsets;

                enumDataset.Reset();

                IDataset dataset = null;

                while ((dataset = enumDataset.Next()) != null)

                {

                    if (dataset is INetworkDataset && dataset.Name == dataElement.Name && dataset.CanDelete())

                    {

                        dataset.Delete();

                        break;

                    }

                }

            }

            // Specify the network dataset's elevation model.

            deNetworkDataset.ElevationModel = esriNetworkElevationModel.esriNEMNone;

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

            INetworkSource edgeNetworkSource = new EdgeFeatureSourceClass();

            edgeNetworkSource.Name = Functions.g_ROAD;

            edgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;

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

            IEdgeFeatureSource edgeFeatureSource = (IEdgeFeatureSource)edgeNetworkSource;

            edgeFeatureSource.UsesSubtypes = false;

            edgeFeatureSource.ClassConnectivityGroup = ;

            edgeFeatureSource.ClassConnectivityPolicy = esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;

            IArray sourceArray = new ArrayClass();

            sourceArray.Add(edgeNetworkSource);

            deNetworkDataset.Sources = sourceArray;

            IArray attributeArray = new ArrayClass();

            IEvaluatedNetworkAttribute evalNetAttr;

            INetworkAttribute2 netAttr2;

            INetworkFieldEvaluator netFieldEval;

            INetworkConstantEvaluator netConstEval;

            // Create an EvaluatedNetworkAttribute object and populate its settings.

            evalNetAttr = new EvaluatedNetworkAttributeClass();

            netAttr2 = (INetworkAttribute2)evalNetAttr;

            netAttr2.Name = "Time";

            netAttr2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;

            netAttr2.DataType = esriNetworkAttributeDataType.esriNADTDouble;

            netAttr2.Units = esriNetworkAttributeUnits.esriNAUSeconds;

            netAttr2.UseByDefault = false;

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

            netFieldEval = new NetworkFieldEvaluatorClass();

            netFieldEval.SetExpression("a", "aaaaaaaaaa"); //Shape_Length

            evalNetAttr.set_Evaluator(edgeNetworkSource, esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator)netFieldEval);

            netFieldEval = new NetworkFieldEvaluatorClass();

            netFieldEval.SetExpression("a", "aaaaaaaaaaaaaa");

            evalNetAttr.set_Evaluator(edgeNetworkSource, esriNetworkEdgeDirection.esriNEDAgainstDigitized, (INetworkEvaluator)netFieldEval);

            netConstEval = new NetworkConstantEvaluatorClass();

            netConstEval.ConstantValue = false;

            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;

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

            IFeatureDatasetExtensionContainer fdxContainer = (IFeatureDatasetExtensionContainer)featureDataset;

            IFeatureDatasetExtension fdExtension = fdxContainer.FindExtension(esriDatasetType.esriDTNetworkDataset);

            IDatasetContainer2 datasetContainer2 = (IDatasetContainer2)fdExtension;

            IDEDataset deDataset = (IDEDataset)deNetworkDataset;

            INetworkDataset networkDataset = (INetworkDataset)datasetContainer2.CreateDataset(deDataset);

            log.WriteLog("NetWorkDataset创建完成,Building Network...");

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

            INetworkBuild networkBuild = (INetworkBuild)networkDataset;

            networkBuild.BuildNetwork(geoDataset.Extent);

            log.WriteLog("BuildNetwork完成!");

            return networkDataset;

        }
上一篇:Mysql的JDBC


下一篇:linux 系统管理 使用技巧