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

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

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

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

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

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

本站QQ群(三):627853279 麻辣GIS(三)

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

发表评论