麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

ArcGIS Engine 10 开发手册(11-9)使用代码实现几何网络的最短路径分析

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

示例代码

public void SolvePath (IMap _pMap, IGeometricNetwork _pGeometricNetwork, string _pWeightName, IPointCollection _pPoints, double _pDist, ref IPolyline _pPolyline, ref double _pPathCost)

{

  try

  {
    // 这4个参数其实就是一个定位Element的指标

    int intEdgeUserClassID;
    int intEdgeUserID;

    int intEdgeUserSubID;
    int intEdgeID;

    IPoint pFoundEdgePoint;
    double dblEdgePercent;

    ITraceFlowSolverGEN pTraceFlowSolver = new TraceFlowSolverClass () as ITraceFlowSolverGEN;

    INetSolver pNetSolver = pTraceFlowSolver as INetSolver;

    //操作是针对逻辑网络的,INetwork是逻辑网络 
    INetwork pNetwork = _pGeometricNetwork.Network;
    pNetSolver.SourceNetwork = pNetwork;

    INetElements pNetElements = pNetwork as INetElements;

    int pCount = _pPoints.PointCount;

    //定义一个边线旗数组

    IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[pCount];
    IPointToEID pPointToEID = new PointToEIDClass ();
    pPointToEID.SourceMap = _pMap;
    pPointToEID.GeometricNetwork = _pGeometricNetwork;
    pPointToEID.SnapTolerance = _pDist;

    for (int i = 0; i < pCount; i++)

    {

      INetFlag pNetFlag = new EdgeFlagClass () as INetFlag;

      IPoint pEdgePoint = _pPoints.get_Point (i);

      //查找输入点的最近的边线

      pPointToEID.GetNearestEdge (pEdgePoint, out intEdgeID, out pFoundEdgePoint, out dblEdgePercent);

      pNetElements.QueryIDs (intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID);

      pNetFlag.UserClassID = intEdgeUserClassID;
      pNetFlag.UserID = intEdgeUserID;
      pNetFlag.UserSubID = intEdgeUserSubID;

      IEdgeFlag pTemp = (IEdgeFlag) (pNetFlag as IEdgeFlag);
      pEdgeFlagList[i] = pTemp;

    }

    pTraceFlowSolver.PutEdgeOrigins (ref pEdgeFlagList);
    INetSchema pNetSchema = pNetwork as INetSchema;

    INetWeight pNetWeight = pNetSchema.get_WeightByName (_pWeightName);

    INetSolverWeightsGEN pNetSolverWeights = pTraceFlowSolver as INetSolverWeightsGEN;

    pNetSolverWeights.FromToEdgeWeight = pNetWeight; //开始边线的权重

    pNetSolverWeights.ToFromEdgeWeight = pNetWeight; //终止边线的权重 
    object[] pRes = new object[pCount - 1];

    //通过FindPath得到边线和交汇点的集合 
    IEnumNetEID pEnumNetEID_Junctions;

    IEnumNetEID pEnumNetEID_Edges;

    pTraceFlowSolver.FindPath (esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out pEnumNetEID_Junctions, out pEnumNetEID_Edges, pCount - 1, ref pRes);

    //计算元素成本

    _pPathCost = 0;

    for (int i = 0; i < pRes.Length; i++)

    {

      double m_Va = (double) pRes[i];

      _pPathCost = _pPathCost + m_Va;

    }

    IGeometryCollection pNewGeometryColl = _pPolyline as IGeometryCollection; //QI

    ISpatialReference pSpatialReference = _pMap.SpatialReference;
    IEIDHelper pEIDHelper = new EIDHelperClass ();
    pEIDHelper.GeometricNetwork = _pGeometricNetwork;
    pEIDHelper.OutputSpatialReference = pSpatialReference;
    pEIDHelper.ReturnGeometries = true;

    IEnumEIDInfo pEnumEIDInfo = pEIDHelper.CreateEnumEIDInfo (pEnumNetEID_Edges);

    int Count = pEnumEIDInfo.Count;
    pEnumEIDInfo.Reset ();

    for (int i = 0; i < Count; i++)

    {

      IEIDInfo pEIDInfo = pEnumEIDInfo.Next ();
      IGeometry pGeometry = pEIDInfo.Geometry;

      pNewGeometryColl.AddGeometryCollection (pGeometry as IGeometryCollection);

    }

  } catch (Exception ex)

  {

    Console.WriteLine (ex.Message);

  }

}

运行结果

PS:红色的表示计算结果

ArcGIS Engine 10 开发手册全集

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

相关阅读

麻辣GIS-Sailor

作者:

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

声明

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

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

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

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