ArcGIS Engine 10 开发手册(11-13)无向网络最短路径分析

本文目录
  • 正文

本文演示一个使用代码实现无向网络最短路径分析的方法。

示例代码

/// <summary>

/// _pFtClass参数为Stops的要素类,_pPointC是用鼠标点的点生成的点的集合,最后 一个参数是捕捉距离

/// </summary>

/// <param name="_pNaContext"></param>

/// <param name="_pFtClass"></param>

/// <param name="_pPointC"></param>

/// <param name="_pDist"></param>

void NASolve (INAContext _pNaContext, IFeatureClass _pFtClass, IPointCollection _pPointC, double _pDist)

{

  INALocator pNAlocator = _pNaContext.Locator;
  for (int i = 0; i < _pPointC.PointCount; i++)

  {

    IFeature pFt = _pFtClass.CreateFeature ();
    IRowSubtypes pRowSubtypes = pFt as IRowSubtypes;
    pRowSubtypes.InitDefaultValues ();

    pFt.Shape = _pPointC.get_Point (i) as IGeometry;

    IPoint pPoint = null;
    INALocation pNalocation = null;

    pNAlocator.QueryLocationByPoint (_pPointC.get_Point (i), ref pNalocation, ref pPoint, ref _pDist);

    INALocationObject pNAobject = pFt as INALocationObject;
    pNAobject.NALocation = pNalocation;

    int pNameFieldIndex = _pFtClass.FindField ("Name");

    pFt.set_Value (pNameFieldIndex, pPoint.X.ToString () + " , " + pPoint.Y.ToString ());

    int pStatusFieldIndex = _pFtClass.FindField ("Status");
    pFt.set_Value (pStatusFieldIndex, esriNAObjectStatus.esriNAObjectStatusOK);

    int pSequenceFieldIndex = _pFtClass.FindField ("Sequence");
    pFt.set_Value (_pFtClass.FindField ("Sequence"), ((ITable) _pFtClass).RowCount (null));

    pFt.Store ();

  }

}

/// <summary>

/// 获取网络数据集

/// </summary>

/// <param name="_pFeatureWs"></param>

/// <param name="_pDatasetName"></param>

/// <param name="_pNetDatasetName"></param>

/// <returns></returns>

INetworkDataset GetNetDataset (IFeatureWorkspace _pFeatureWs, string _pDatasetName, string _pNetDatasetName)

{

  ESRI.ArcGIS.Geodatabase.IDatasetContainer3 pDatasetContainer = null;

  ESRI.ArcGIS.Geodatabase.IFeatureDataset pFeatureDataset = _pFeatureWs.OpenFeatureDataset (_pDatasetName);

  ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer pFeatureDatasetExtensionContainer = pFeatureDataset as ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer; // Dynamic Cast

  ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension pFeatureDatasetExtension = pFeatureDatasetExtensionContainer.FindExtension (ESRI.ArcGIS.Geodatabase.esriDataset Type.esriDTNetworkDataset);

  pDatasetContainer = pFeatureDatasetExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3; // Dynamic Cast

  ESRI.ArcGIS.Geodatabase.IDataset pNetWorkDataset = pDatasetContainer.get_DatasetByName (ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTN etworkDataset, _pNetDatasetName);

  return pNetWorkDataset as ESRI.ArcGIS.Geodatabase.INetworkDataset; //

}

/// <summary>

/// 加载NetworkDataset到Map中

/// </summary>

/// <param name="_pMap"></param>

/// <param name="_pNetworkDataset"></param>

void loadNet (IMap _pMap, INetworkDataset _pNetworkDataset)

{

  INetworkLayer pNetLayer = new NetworkLayerClass ();
  pNetLayer.NetworkDataset = _pNetworkDataset;

  _pMap.AddLayer (pNetLayer as ILayer);

}

/// <summary>

/// 获取网络分析上下文,这个接口是网络分析中很重要的一个

/// </summary>

/// <param name="_pNaSolver"></param>

/// <param name="_pNetworkDataset"></param>

/// <returns></returns>

public INAContext GetSolverContext (INASolver _pNaSolver, INetworkDataset _pNetworkDataset)

{

  //Get the Data Element

  IDatasetComponent pDataComponent = _pNetworkDataset as IDatasetComponent;
  IDEDataset pDeDataset = pDataComponent.DataElement;

  INAContextEdit pContextEdit = _pNaSolver.CreateContext (pDeDataset as IDENetworkDataset, _pNaSolver.Name) as INAContextEdit;
  //Prepare the context for analysis based upon the current network dataset

  pContextEdit.Bind (_pNetworkDataset, new GPMessagesClass ());
  return pContextEdit as INAContext;

}

/// <summary>

/// 获取NALayer

/// </summary>

/// <param name="_pNaSover"></param>

/// <param name="_pNaContext"></param>

/// <returns></returns>

INALayer GetNaLayer (INASolver _pNaSover, INAContext _pNaContext)

{

  return _pNaSover.CreateLayer (_pNaContext);

}

IFeatureClass pftclass = pNaContext.NAClasses.get_ItemByName ("Stops") as IFeatureClass;

NASolve (pNaContext, pftclass, pPointC, 5000);
IGPMessages gpMessages = new GPMessagesClass ();

bool pBool = pNASolveClass.Solve (pNaContext, gpMessages, null);

运行结果

ArcGIS Engine 10 开发手册全集

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

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

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

如您有疑问,可在文末留言,也可在 麻辣GIS•小分队 搜索回答或发帖 ,还可以在QQ群中提问。

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

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

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

发表评论