ArcGIS Engine 10 开发手册(11-12)使用代码创建 Networkdataset

本文目录
  • 正文

本文演示一个使用代码实现创建 Networkdataset的过程。

示例代码

/// <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 ("[METERS]", "");

  //方向设置 
  pEvalNetAttr.set_Evaluator (pEdgeNetworkSource, esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator) pNetFieldEval);

  pEvalNetAttr.set_Evaluator (pEdgeNetworkSource, sriNetworkEdgeDirection.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) DatasetContainer2.CreateDataset (pDENetDataset);

  return pNetworkDataset;

}

ArcGIS Engine 10 开发手册全集

ArcGIS Engine 10 开发手册全集: ArcGIS Engine 10 开发手册

如果本文对你有所帮助,欢迎对我们团队进行打赏捐助,让我们在传播3S的路上可以走得更远。
赞赏支持
微信捐助麻辣GIS 支付宝捐助麻辣GIS

麻辣GIS-Sailor
作者:
GIS爱好者,学GIS,更爱玩GIS。

如您有疑问,可在文末留言,或到QQ群提问。

本站QQ群(一):291616564 麻辣GIS

本站QQ群(二):166408035 麻辣GIS(二)

微信公众号:malagis,扫描右边二维码直接关注。

发表评论