您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > ArcEngine+最短路径分析(C源码)
ArcEngine最短路径分析(源码)usingSystem;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS.Geodatabase;usingESRI.ArcGIS.NetworkAnalysis;namespaceGisEditor{///summary///最短路径分析////summarypublicclassClsPathFinder{privateIGeometricNetworkm_ipGeometricNetwork;privateIMapm_ipMap;privateIPointCollectionm_ipPoints;privateIPointToEIDm_ipPointToEID;privatedoublem_dblPathCost=0;privateIEnumNetEIDm_ipEnumNetEID_Junctions;privateIEnumNetEIDm_ipEnumNetEID_Edges;privateIPolylinem_ipPolyline;#regionPublicFunction//返回和设置当前地图publicIMapSetOrGetMap{set{m_ipMap=value;}get{returnm_ipMap;}}//打开几何数据集的网络工作空间publicvoidOpenFeatureDatasetNetwork(IFeatureDatasetFeatureDataset){CloseWorkspace();if(!InitializeNetworkAndMap(FeatureDataset))Console.WriteLine(打开network出错);}//输入点的集合publicIPointCollectionStopPoints{set{m_ipPoints=value;}get{returnm_ipPoints;}}//路径成本publicdoublePathCost{get{returnm_dblPathCost;}}//返回路径的几何体publicIPolylinePathPolyLine(){IEIDInfoipEIDInfo;IGeometryipGeometry;if(m_ipPolyline!=null)returnm_ipPolyline;m_ipPolyline=newPolylineClass();IGeometryCollectionipNewGeometryColl=m_ipPolylineasIGeometryCollection;ISpatialReferenceipSpatialReference=m_ipMap.SpatialReference;IEIDHelperipEIDHelper=newEIDHelperClass();ipEIDHelper.GeometricNetwork=m_ipGeometricNetwork;ipEIDHelper.OutputSpatialReference=ipSpatialReference;ipEIDHelper.ReturnGeometries=true;IEnumEIDInfoipEnumEIDInfo=ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges);intcount=ipEnumEIDInfo.Count;ipEnumEIDInfo.Reset();for(inti=0;icount;i++){ipEIDInfo=ipEnumEIDInfo.Next();ipGeometry=ipEIDInfo.Geometry;ipNewGeometryColl.AddGeometryCollection(ipGeometryasIGeometryCollection);}returnm_ipPolyline;}//解决路径publicvoidSolvePath(stringWeightName){try{intintEdgeUserClassID;intintEdgeUserID;intintEdgeUserSubID;intintEdgeID;IPointipFoundEdgePoint;doubledblEdgePercent;/*PutEdgeOrigins方法的第二个参数要求是IEdgeFlag类型的数组,*在VB等其他语言的代码中,只需传人该类型数组的第一个元素即*可,但C#中的机制有所不同,需要作出如下修改:使用*ITraceFlowSolverGEN替代ITraceFlowSolver*/ITraceFlowSolverGENipTraceFlowSolver=newTraceFlowSolverClass()asITraceFlowSolverGEN;INetSolveripNetSolver=ipTraceFlowSolverasINetSolver;INetworkipNetwork=m_ipGeometricNetwork.Network;ipNetSolver.SourceNetwork=ipNetwork;INetElementsipNetElements=ipNetworkasINetElements;intintCount=m_ipPoints.PointCount;//定义一个边线旗数组IEdgeFlag[]pEdgeFlagList=newEdgeFlagClass[intCount];for(inti=0;iintCount;i++){INetFlagipNetFlag=newEdgeFlagClass()asINetFlag;IPointipEdgePoint=m_ipPoints.get_Point(i);//查找输入点的最近的边线m_ipPointToEID.GetNearestEdge(ipEdgePoint,outintEdgeID,outipFoundEdgePoint,outdblEdgePercent);ipNetElements.QueryIDs(intEdgeID,esriElementType.esriETEdge,outintEdgeUserClassID,outintEdgeUserID,outintEdgeUserSubID);ipNetFlag.UserClassID=intEdgeUserClassID;ipNetFlag.UserID=intEdgeUserID;ipNetFlag.UserSubID=intEdgeUserSubID;IEdgeFlagpTemp=(IEdgeFlag)(ipNetFlagasIEdgeFlag);pEdgeFlagList[i]=pTemp;}ipTraceFlowSolver.PutEdgeOrigins(refpEdgeFlagList);INetSchemaipNetSchema=ipNetworkasINetSchema;INetWeightipNetWeight=ipNetSchema.get_WeightByName(WeightName);INetSolverWeightsipNetSolverWeights=ipTraceFlowSolverasINetSolverWeights;ipNetSolverWeights.FromToEdgeWeight=ipNetWeight;//开始边线的权重ipNetSolverWeights.ToFromEdgeWeight=ipNetWeight;//终止边线的权重object[]vaRes=newobject[intCount-1];//通过findpath得到边线和交汇点的集合ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,esriShortestPathObjFn.esriSPObjFnMinSum,outm_ipEnumNetEID_Junctions,outm_ipEnumNetEID_Edges,intCount-1,refvaRes);//计算元素成本m_dblPathCost=0;for(inti=0;ivaRes.Length;i++){doublem_Va=(double)vaRes[i];m_dblPathCost=m_dblPathCost+m_Va;}m_ipPolyline=null;}catch(Exceptionex){Console.WriteLine(ex.Message);}}#endregion#regionPrivateFunction//初始化几何网络和地图privateboolInitializeNetworkAndMap(IFeatureDatasetFeatureDataset){IFeatureClassContaineripFeatureClassContainer;IFeatureClassipFeatureClass;IGeoDatasetipGeoDataset;ILayeripLayer;IFeatureLayeripFeatureLayer;IEnvelopeipEnvelope,ipMaxEnvelope;doubledblSearchTol;INetworkCollectionipNetworkCollection=FeatureDatasetasINetworkCollection;intcount=ipNetworkCollection.GeometricNetworkCount;//获取第一个几何网络工作空间m_ipGeometricNetwork=ipNetworkCollection.get_GeometricNetwork(0);INetworkipNetwork=m_ipGeometricNetwork.Network;if(m_ipMap!=null){m_ipMap=newMapClass();ipFeatureClassContainer=m_ipGeometricNetworkasIFeatureClassContainer;count=ipFeatureClassContainer.ClassCount;for(inti=0;icount;i++){ipFeatureClass=ipFeatureClassContainer.get_Class(i);ipFeatureLayer=newFeatureLayerClass();ipFeatureLayer.FeatureClass=ipFeatureClass;m_ipMap.AddLayer(ipFeatureLayer);}}count=m_ipMap.LayerCount;ipMaxEnvelope=newEnvelopeClass();for(inti=0;icount;i++){ipLayer=m_ipMap.get_Layer(i);ipFeatureLayer=ipLayerasIFeatureLayer;ipGeoDataset=ipFeatureLayerasIGeoDataset;ipEnvelope=ipGeoDataset.Extent;ipMaxEnvelope.Union(ipEnvelope);}m_ipPointToEID=newPointToEIDClass();m_ipPointToEID.SourceMap=m_ipMap;m_ipPointToEID.GeometricNetwork=m_ipGeometricNetwork;doubledblWidth=ipMaxEnvelope.Width;doubledblHeight=ipMaxEnvelope.Height;if(dblWidthdblHeight)dblSearch
本文标题:ArcEngine+最短路径分析(C源码)
链接地址:https://www.777doc.com/doc-2898276 .html