创建网络数据集
/// <summary>
/// 创建自定义网络数据集
/// </summary>
/// <param name="_pWsName"></param>
/// <param name="_pDatasetName"></param>
/// <param name="_pNetName"></param>
/// <param name="_pFtName"></param>
void CreateNetworkDataset(string _pWsName, string _pDatasetName,string _pNetName, string _pFtName)
{
IDENetworkDataset pDENetworkDataset = new DENetworkDatasetClass();
pDENetworkDataset.Buildable = true;
IWorkspace pWs = GetWorkspace(_pWsName);
IFeatureWorkspace pFtWs = pWs as IFeatureWorkspace;
IFeatureDataset pFtDataset = pFtWs.OpenFeatureDataset(_pDatasetName);
// 定义空间参考,负责会出错
IDEGeoDataset pDEGeoDataset = (IDEGeoDataset)pDENetworkDataset;
IGeoDataset pGeoDataset = pFtDataset as IGeoDataset;
pDEGeoDataset.Extent = pGeoDataset.Extent;
pDEGeoDataset.SpatialReference = pGeoDataset.SpatialReference;
// 网络数据集的名称
IDataElement pDataElement = (IDataElement)pDENetworkDataset;
pDataElement.Name = _pNetName;
// 参加建立网络数据集的要素类
INetworkSource pEdgeNetworkSource = new EdgeFeatureSourceClass();
pEdgeNetworkSource.Name = _pFtName;
pEdgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
// 要素类的连通性
IEdgeFeatureSource pEdgeFeatureSource = (IEdgeFeatureSource)pEdgeNetworkSource;
pEdgeFeatureSource.UsesSubtypes = false;
pEdgeFeatureSource.ClassConnectivityGroup = 1;
pEdgeFeatureSource.ClassConnectivityPolicy =esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;
//不用转弯数据
pDENetworkDataset.SupportsTurns = false;
IArray pSourceArray = new ArrayClass();
pSourceArray.Add(pEdgeNetworkSource);
pDENetworkDataset.Sources = pSourceArray;
//网络数据集的属性设置
IArray pAttributeArray = new ArrayClass();
// Initialize variables reused when creating attributes:
IEvaluatedNetworkAttribute pEvalNetAttr;
INetworkAttribute2 pNetAttr2;
INetworkFieldEvaluator pNetFieldEval;
INetworkConstantEvaluator pNetConstEval;
pEvalNetAttr = new EvaluatedNetworkAttributeClass();
pNetAttr2 = (INetworkAttribute2)pEvalNetAttr;
pNetAttr2.Name ="Meters";
pNetAttr2.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;
pNetAttr2.DataType = esriNetworkAttributeDataType.esriNADTDouble;
pNetAttr2.Units = esriNetworkAttributeUnits.esriNAUMeters;
pNetAttr2.UseByDefault = false;
pNetFieldEval = new NetworkFieldEvaluatorClass();
pNetFieldEval.Setexpression_r("[METERS]", "");
//方向设置
pEvalNetAttr.set_Evaluator(pEdgeNetworkSource,esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator)pNetFieldEval);
pEvalNetAttr.set_Evaluator(pEdgeNetworkSource,esriNetworkEdgeDirection.esriNEDAgainstDigitized, (INetworkEvaluator)pNetFieldEval);
pNetConstEval = new NetworkConstantEvaluatorClass();
pNetConstEval.ConstantValue = 0;
pEvalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge,(INetworkEvaluator)pNetConstEval);
pEvalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction,(INetworkEvaluator)pNetConstEval);
pEvalNetAttr.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn,(INetworkEvaluator)pNetConstEval);
// 一个网络数据集可以有多个属性,我只添加了一个
pAttributeArray.Add(pEvalNetAttr);
pDENetworkDataset.Attributes = pAttributeArray;
// 创建网络数据集,注意在创建几何网络的时候会锁定相应的要素类,因此不要用ArcMap或者catalog等打开参相应的数据
INetworkDataset pNetworkDataset = Create(pFtDataset, pDENetworkDataset);
//建立网络
INetworkBuild pNetworkBuild = (INetworkBuild)pNetworkDataset;
pNetworkBuild.BuildNetwork(pGeoDataset.Extent);
}
/// <summary>
/// 创建无向网络
/// </summary>
/// <param name="_pFeatureDataset"></param>
/// <param name="_pDENetDataset"></param>
/// <returns></returns>
public INetworkDataset Create(IFeatureDataset _pFeatureDataset, IDENetworkDataset2 _pDENetDataset)
{
IFeatureDatasetExtensionContainer pFeatureDatasetExtensionContainer = (IFeatureDatasetExtensionContainer) _pFeatureDataset;
IFeatureDatasetExtension pFeatureDatasetExtension = pFeatureDatasetExtensionContainer.FindExtension (esriDatasetType.esriDTNetworkDataset);
IDatasetContainer2 pDatasetContainer2 = (IDatasetContainer2)pFeatureDatasetExtension;
IDEDataset pDENetDataset = (IDEDataset)_pDENetDataset;
INetworkDataset pNetworkDataset = (INetworkDataset)pDatasetContainer2.CreateDataset(pDENetDataset);
return pNetworkDataset
}