部分参照esri的官方例子,理解下各个参数,对照自己的NetWorkDatase创建方式(在arcmap中),多试试代码就调好了。
/// <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; string sNDPath = Functions.g_WorkSpacePath + Functions.g_ROAD + "_ND.nd\"; if (System.IO.Directory.Exists(sNDPath)) { try { string[] strTemp = System.IO.Directory.GetFiles(sNDPath); //要先删除其下的所有子文件,然后删除目录,否则报错:System.IO.IOException: 目录不是空的 foreach (string str in strTemp) { System.IO.File.Delete(str); } System.IO.Directory.Delete(sNDPath); } catch (System.Exception ex) { log.WriteLog(sNDPath + "已存在,删除失败!"); return null; } } // Open the shapefile and cast to the IGeoDataset interface. IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactoryClass() asIWorkspaceFactory2; IWorkspace workspace = workspaceFactory.OpenFromFile(Functions.g_WorkSpacePath, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(Functions.g_ROAD); IGeoDataset geoDataset = (IGeoDataset)featureClass; deNetworkDataset.NetworkType = esriNetworkDatasetType.esriNDTShapefile; // 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"; // 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 = 1; 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", "。。。。。。。。。。。"); //表达式不能出错 evalNetAttr.set_Evaluator(edgeNetworkSource,esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator)netFieldEval); netFieldEval = new NetworkFieldEvaluatorClass(); netFieldEval.SetExpression("a", "。。。。。。。。。"); 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; //// 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)workspace; IWorkspaceExtension workspaceExtension = workspaceExtensionManager.FindExtension(ndWorkspaceExtensionUID); IDatasetContainer3 datasetContainer2 = (IDatasetContainer3)workspaceExtension; IDEDataset deDataset = (IDEDataset)deNetworkDataset; IDataset ds = datasetContainer2.CreateDataset(deDataset); INetworkDataset networkDataset = (INetworkDataset)ds; 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; }