麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

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 开发手册

相关阅读

麻辣GIS-Sailor

作者:

GIS爱好者,学GIS,更爱玩GIS。

声明

1.本文所分享的所有需要用户下载使用的内容(包括但不限于软件、数据、图片)来自于网络或者麻辣GIS粉丝自行分享,版权归该下载资源的合法拥有者所有,如有侵权请第一时间联系本站删除。

2.下载内容仅限个人学习使用,请切勿用作商用等其他用途,否则后果自负。

手机阅读
公众号关注
知识星球
手机阅读
麻辣GIS微信公众号关注
最新GIS干货
关注麻辣GIS知识星球
私享圈子

留言板(小编看到第一时间回复)