ArcEngine最短路径分析.docx
《ArcEngine最短路径分析.docx》由会员分享,可在线阅读,更多相关《ArcEngine最短路径分析.docx(7页珍藏版)》请在冰豆网上搜索。
ArcEngine最短路径分析
publicclassForm1:
System.Windows.Forms.Form
{
IMappMap;
IActiveViewpActiveView;
……
Form1Load
privatevoidForm1_Load(objectsender,System.EventArgse)
{
pMap=axMapControl1.Map;//
pActiveView=pMapasIActiveView;//QI
pActiveView.Extent=axMapControl1.FullExtent;//
pActiveView.Refresh();//
}
axMapControl1MouseDown
privatevoidaxMapControl1_OnMouseDown(objectsender,
ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvente)
{
axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerCrosshair;
Crosshair
IEnvelopepEnv;
pEnv=axMapControl1.TrackRectangle();//TrackRectangle
pActiveView.Extent=pEnv;//pEnv
pActiveView.Refresh();//
}
ArcEngine 最短路径分析(源码)
usingSystem;
usingESRI.ArcGIS.Carto;
usingESRI.ArcGIS.Geometry;
usingESRI.ArcGIS.Geodatabase;
usingESRI.ArcGIS.NetworkAnalysis;
namespaceGisEditor
{
///
///最短路径分析
///
publicclassClsPathFinder
{
privateIGeometricNetworkm_ipGeometricNetwork;
privateIMapm_ipMap;
privateIPointCollectionm_ipPoints;
privateIPointToEIDm_ipPointToEID;
privatedoublem_dblPathCost=0;
privateIEnumNetEIDm_ipEnumNetEID_Junctions;
privateIEnumNetEIDm_ipEnumNetEID_Edges;
privateIPolyline m_ipPolyline;
#regionPublicFunction
//返回和设置当前地图
publicIMapSetOrGetMap
{
set{m_ipMap=value;}
get{return m_ipMap;}
}
//打开几何数据集的网络工作空间
publicvoidOpenFeatureDatasetNetwork(IFeatureDatasetFeatureDataset)
{
CloseWorkspace();
if(!
InitializeNetworkAndMap(FeatureDataset))
Console.WriteLine("打开network出错");
}
//输入点的集合
publicIPointCollectionStopPoints
{
set{m_ipPoints=value;}
get{return m_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;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
*/
ITraceFlowSolverGEN ipTraceFlowSolver=newTraceFlowSolverClass()asITraceFlowSolverGEN;
INetSolveripNetSolver=ipTraceFlowSolverasINetSolver;
INetworkipNetwork=m_ipGeometricNetwork.Network;
ipNetSolver.SourceNetwork=ipNetwork;
INetElementsipNetElements=ipNetworkasINetElements;
intintCount=m_ipPoints.PointCount;
//定义一个边线旗数组
IEdgeFlag[]pEdgeFlagList=newEdgeFlagClass[intCount];
for(inti=0;i {
INetFlagipNetFlag=newEdgeFlagClass()asINetFlag;
IPoint ipEdgePoint=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;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;i {
ipFeatureClass=ipFeatureClassContainer.get_Class(i);
ipFeatureLayer=newFeatureLayerClass();
ipFeatureLayer.FeatureClass=ipFeatureClass;
m_ipMap.AddLayer(ipFeatureLayer);
}
}
count=m_ipMap.LayerCount;
ipMaxEnvelope=newEnvelopeClass();
for(inti=0;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(dblWidth>dblHeight)
dblSearchTol=dblWidth/100;
else
dblSearchTol=dblHeight/100;
m_ipPointToEID.SnapTolerance=dblSearchTol;
returntrue ;
}
//关闭工作空间
privatevoidCloseWorkspace()
{
m_ipGeometricNetwork=null;
m_ipPoints=null;
m_ipPointToEID=null;
m_ipEnumNetEID_Junctions=null;
m_ipEnumNetEID_Edges=null;
m_ipPolyline=null;
}
#endregion
}
}
备注:
在调用该类时的次序:
ClsPathFinder m_ipPathFinder;
if(m_ipPathFinder==null)//打开几何网络工作空间
{
m_ipPathFinder=newClsPathFinder();
ipMap=this.m_ActiveView.FocusMap;
ipLayer=ipMap.get_Layer(0);
ipFeatureLayer=ipLayerasIFeatureLayer;
ipFDB=ipFeatureLayer.FeatureClass.FeatureDataset;
m_ipPathFinder.SetOrGetMap=ipMap;
m_ipPathFinder.OpenFeatureDatasetNetwork(ipFDB);
}
privatevoidViewMap_OnMouseDown(objectsender,ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvente)//获取地图上鼠标输入的点
{
IPointipNew;
if(m_ipPoints==null)
{
m_ipPoints=newMultipointClass();
m_ipPathFinder.StopPoints=m_ipPoints;
}
ipNew=ViewMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);
objecto=Type.Missing;
m_ipPoints.AddPoint(ipNew,refo,refo);
}
m_ipPathFinder.SolvePath("Weight");//先解析路径
IPolylineipPolyResult=m_ipPathFinder.PathPolyLine();//最后返回最短路径