1ARCENGINE 最短路径分析(C#源码)2
Arcgis操作 实验十五:最短路径分析
实验十五:最短路径分析一、实验目的1、掌握各种类型的最短路径分析;2、理解网络分析原理。
二、实验准备数据准备:City.mdb软件准备:ArcGIS Desktop9.x,ArcCatalog三、实验内容根据不同的要求,获得到达指定目的地的最佳路径,并给出路径的长度;找出距景点最近的某设施的路径。
1、在网络中指定一个商业中心,分别求出在不同距离、时间的限制下从家到商业中心的最佳路径;2、给定访问顺序,按要求找出从家出发,逐个经过访问点,最终到达目的地的最佳路径;3、研究阻强的设置对最佳路径选择的影响。
四、实验步骤启动ArcMap ,打开city. mdb ,双击city数据库,加载数据。
对点状要素place符号化:以HOME字段,1值为家,0值为商业中心。
具体步骤见操作视频:最短路径分析.exe图1 无权重参照的最短路径显示(1)无权重最佳路径的生成1)在网络分析工具条上,选择旗标工具,将旗标放在“家”和想要取得“商业中心”点上。
2)选择Analysis/Options命令,打开Analysis Options对话框,确认Weights和Weight Filter 标签项全部是None,这种情况下进行的最短路径分析是完全按照这个网络自身的长短来确定。
3)在Track Task文本框中选择Find path。
单击solve按钮。
显示最短路径(图1),这条路径的总成本显示在状态栏中。
(2)加权最佳路径生成1)在设施网络分析工具条下,点选旗标工具,将旗标分别放在“家”和想去的某个“商业中心”的位置上。
2)选择Analysis/Options命令,打开Analysis Options对话框(图2)进入Weights标签页,在边的权重上,全部选择长度权重属性。
图2 长度权重属性设置3)在Track Task文本枢中选择Find path,单击solve按钮,则以长度为比重的最短路径将显示出来(图3),这条路径的总成本显示在状态栏中。
gis计算最短路径的Dijkstra算法详细讲解
最短路径之Dijkstra算法详细讲解1最短路径算法在日常生活中,我们如果需要常常往返A地区和B 地区之间,我们最希望知道的可能是从A地区到B地区间的众多路径中,那一条路径的路途最短。
最短路径问题是图论研究中的一个经典算法问题,旨在寻找图(由结点和路径组成的)中两结点之间的最短路径。
算法具体的形式包括:(1)确定起点的最短路径问题:即已知起始结点,求最短路径的问题。
(2)确定终点的最短路径问题:与确定起点的问题相反,该问题是已知终结结点,求最短路径的问题。
在无向图中该问题与确定起点的问题完全等同,在有向图中该问题等同于把所有路径方向反转的确定起点的问题。
(3)确定起点终点的最短路径问题:即已知起点和终点,求两结点之间的最短路径。
(4)全局最短路径问题:求图中所有的最短路径。
用于解决最短路径问题的算法被称做“最短路径算法”,有时被简称作“路径算法”。
最常用的路径算法有:Dijkstra算法、A*算法、Bellman-Ford算法、Floyd-Warshall算法、Johnson算法。
本文主要研究Dijkstra算法的单源算法。
2Dijkstra算法2.1 Dijkstra算法Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径。
主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。
Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。
Dijkstra算法是很有代表性的最短路算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。
2.2 Dijkstra算法思想Dijkstra算法思想为:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径, 就将加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U 表示),按最短路径长度的递增次序依次把第二组的顶点加入S中。
最短路径问题的优化算法
最短路径问题的优化算法最短路径问题是图论中的经典问题之一,涉及在给定图中找到两个节点之间的最短路径。
这个问题在实际生活中有广泛的应用,如导航系统中的路线规划、网络通信中数据包的传输等。
为了提高计算效率,许多优化算法被提出和应用于解决最短路径问题。
1. 单源最短路径问题单源最短路径问题是指在给定图中,从一个固定的起始节点到其他所有节点的最短路径问题。
经典的解决方法包括迪杰斯特拉算法和贝尔曼-福特算法。
迪杰斯特拉算法是一种贪婪算法,通过确定与起始节点距离最短的节点来逐步扩展最短路径树。
具体步骤如下:1) 初始化距离数组,将起始节点距离设为0,其他节点距离设为无穷大。
2) 选择当前距离最短的节点,并标记为已访问。
3) 更新与该节点相邻节点的距离,若经过当前节点到相邻节点的距离更短,则更新距离数组。
4) 重复步骤2和步骤3,直到所有节点都被访问过。
最后,距离数组中记录的即为从起始节点到其他所有节点的最短路径。
贝尔曼-福特算法是一种动态规划算法,通过不断地松弛边来逐步得到最短路径。
具体步骤如下:1) 初始化距离数组,将起始节点距离设为0,其他节点距离设为无穷大。
2) 依次对所有边进行松弛操作,即更新边的端点节点的距离。
3) 重复步骤2,直到所有边都被松弛完毕。
4) 判断是否存在负环路,若存在则说明无最短路径;若不存在,则距离数组中记录的即为从起始节点到其他所有节点的最短路径。
2. 全局最短路径问题全局最短路径问题是指在给定图中,找到任意两个节点之间的最短路径问题。
弗洛伊德算法是一种经典的解决方法,通过动态规划的思想逐步求解。
弗洛伊德算法的具体步骤如下:1) 初始化距离矩阵,将所有节点之间的距离设为无穷大。
2) 根据已知的边信息更新距离矩阵,即将已知路径的距离设为对应的实际距离。
3) 对于每一对节点,考虑经过中转节点的路径是否更短,若更短则更新距离矩阵。
4) 重复步骤3,直到距离矩阵不再变化。
最后,距离矩阵中记录的即为任意两个节点之间的最短路径。
最短路径原理
最短路径原理最短路径原理什么是最短路径•最短路径是图论中的一个经典问题,旨在寻找两个顶点之间权值和最小的路径。
Dijkstra算法•Dijkstra算法是最短路径问题中一种常用的解法。
•此算法从起点开始,逐步确定到达其他顶点的最短路径。
Dijkstra算法步骤1.初始化–创建两个集合:一个用于存储已经找到最短路径的顶点,一个用于存储未找到最短路径的顶点。
–将起点加入已找到最短路径集合,其余顶点加入未找到最短路径集合。
–初始化从起点到各顶点的距离为无穷大,起点到自身的距离为0。
2.寻找最短路径–选择未找到最短路径集合中,距离起点最近的顶点,将其加入已找到最短路径集合。
–更新与该顶点相邻的顶点的距离,若通过该顶点到达邻接顶点的路径更短,则更新距离。
3.重复步骤2,直到所有顶点都加入已找到最短路径集合。
示例让我们通过一个简单的示例来说明Dijkstra算法应用于最短路径的原理。
假设有一个无向图,顶点分别为A、B、C、D和E,边的权值分别为:AB(5)、AC(3)、BD(2)、CD(1)、DE(4)。
首先,我们从顶点A开始,初始化距离。
初始时,A到A的距离为0,A到B、C、D和E的距离为无穷大。
经过第一轮计算后,已找到最短路径的集合为{A},未找到最短路径的集合为{B, C, D, E}。
此时,A到C的距离为3,A到B、D和E的距离依然为无穷大。
经过第二轮计算,选择距离A最近的顶点C,将C加入已找到最短路径集合。
更新距离后,A到B的距离为8,A到D的距离为4,A到E的距离为7。
重复以上步骤,直到所有的顶点都加入已找到最短路径集合。
最后得到A到B的最短路径为:A->C->D->B,权值和为7。
总结通过Dijkstra算法,我们可以找到两个顶点之间的最短路径,并计算出最小的权值和。
该算法从起点开始,逐步确定最短路径,直到所有顶点都被加入已找到最短路径集合。
使用这一算法,我们可以在实际应用中解决各种问题,比如路线规划、网络中数据包的传输等。
arcgis 最短路径 原理
ArcGIS 最短路径原理ArcGIS是一款专业的地理信息系统(GIS)软件,最短路径是ArcGIS中的一个重要功能之一。
最短路径是指在一个网络中,从一个起点到达目标点所需经过的路径中,总距离最短的路径。
在地理空间分析中,最短路径可以用于解决很多问题,比如交通规划、物流配送、紧急救援等。
最短路径算法是基于图论的算法,主要包括两个重要的概念:图和路径。
图在最短路径算法中,图是由节点和边组成的数据结构。
节点表示位置或者地点,边表示节点之间的连接关系,也可以表示节点之间的距离或者权重。
在ArcGIS中,图可以通过矢量数据或者栅格数据来表示,比如道路网络、河流网络等。
图中的节点可以是离散的点,也可以是连续的线或面。
每个节点都有一个唯一的标识符,可以是一个ID号或者一个坐标值。
节点之间的边可以是无向边或者有向边,有向边表示只能从一个节点到另一个节点,而无向边表示可以双向通行。
边可以有不同的权重,表示节点之间的距离或者代价。
在最短路径算法中,边的权重通常用于计算路径的总距离或者代价。
路径路径是指从一个起点到达目标点所需经过的一系列节点和边。
路径可以是一条简单路径,即不经过重复节点的路径,也可以是一条环路,即起点和目标点相同的路径。
在最短路径算法中,路径可以用于计算路径的总距离或者代价。
最短路径算法会根据边的权重来选择最短路径,即总距离或者代价最小的路径。
最短路径算法最短路径算法是用于计算最短路径的一种算法。
常用的最短路径算法有Dijkstra算法、Floyd-Warshall算法和A*算法等。
Dijkstra算法Dijkstra算法是一种单源最短路径算法,用于计算从一个起点到其他所有节点的最短路径。
算法的基本思想是通过不断更新起点到其他节点的最短距离来找到最短路径。
具体步骤如下:1.初始化起点到其他节点的距离为无穷大,起点到自身的距离为0。
2.选择一个距离最小的节点作为当前节点,标记该节点为已访问。
3.更新当前节点的邻居节点的距离,如果经过当前节点到达邻居节点的距离小于已知的最短距离,则更新最短距离。
ArcGIS空间分析和最短路径分析实习报告
实验一、矢量数据的空间分析练习1:市区择房分析操作步骤:首先打开ArcMap,打开E:\Chp7\Ex1\city.mxd文件将文件加入到窗口中来,这时就将五个文件全部加入其中来了,如下图所示;(1)主干道噪音缓冲区的建立1)选择交通网络图层(network.shp),打开图层的属性表,在右下角的打开option选项中,在菜单中选择select by attributes,在select by attributes对话框中,左边选择“TYPE”双击将其添加到对话框下面SQL算式表中,点中间“=”,再单击Get unique values 将TYPE的全部属性值加入上面的列表框中,然后选择“ST”属性值,双击添加到SQL算式表中,单击APPL Y按钮,就将市区的主要道路选择出来了2)点击缓冲区按钮对选择的主干道进行缓冲区的建立,首先在缓冲区对象图层选择交通网络图层(network),然后将下面的Use Only the Selected Feature(仅对选择的要素进行分析)选中,单击next;3)确定尺寸单位,选择第一种缓冲区建立方法(At a specified distance),指定缓冲区半径为200米,单击next;4)由于不是分别考虑一个图层的各个不同的要素的目的,所以我们在这里选择的是第一种边界设定类型(Dissolve barriers between),然后指定好缓冲区文件的存放路径和文件名后,单击OK,完成主干道噪音污染缓冲区的建立。
(2)商业中心影响范围建立1)建立大型商业中心的影响范围。
首先点击缓冲区按钮,在缓冲区对象图层选择商业中心分布图层(network),单击next;2)确定尺寸单位,选择第一种缓冲区建立方法,以其属性字段YUZHI为缓冲区半径,单击next;后,单击OK,完成商业中心影响范围缓冲区的建立。
(3)名牌高中的影响范围建立1)点击缓冲区按钮,在缓冲区对象图层选择名牌高中分布图层(school),单击next;2)确定尺寸单位米,选择第一种缓冲区建立方法,指定750米作为半径,设置好后,单击next;后,单机OK,完成名牌高中的覆盖范围缓冲区的建立。
ArcGIS网络分析最短路径分析源代码(VB6.0)
ArcGIS⽹络分析最短路径分析源代码(VB6.0)ArcGIS⽹络分析最短路径分析源代码(VB6.0)12' Copyright 1995-2005 ESRI34' All rights reserved under the copyright laws of the United States.56' You m ay freely redistribute and use this sam ple code, with or without m odifi cation.78' Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS O R IMPLIED9' WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILIT Y AND FITNESS10' FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESR I OR11' CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE CIAL, EXEMPLARY,12' OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROC UREMENT OF13' SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; O R BUSINESS14' INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSE D AND ON ANY15' THEORY OF LIABILITY, WHETHER IN CONT RACT, STRICT LIABILITY, OR TO RT ARISING IN ANY16' WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE P OSSIBILITY OF17' SUCH DAMAGE.1819' For additional information contact: Environmental System s Research Institu te, Inc.2021' Attn: Contracts Dept.2223' 380 New York Street2425' Redlands, California, U.S.A. 923732627' Em ail: contracts@/doc/07098e37f111f18583d05ab5.html2829Option Explicit3031' vb version of the PathFinder object3233' 本地变量34Private m_ipGeometricNetwork As esriGeoDatabase.IGeometricNetwork35Private m_ipMap As esriCarto.IMap36Private m_ipPoints As esriGeometry.IPointCollection37Private m_ipPointToEID As esriNetworkAnalysis.IPointToEID38' 返回结果变量39Private m_dblPathCost As Double40Private m_ipEnumNetEID_Junctions As esriGeoDatabase.IEnumNetEID41Private m_ipEnumNetEID_Edges As esriGeoDatabase.IEnumNetEID42Private m_ipPolyline As esriGeometry.IPolyline434445' Optionally set the Map (e.g. the current m ap in ArcMap),46' otherwise a default m ap will be made (for IPointToEID).48Public Property Set Map(Map As esriCarto.IMap)49Set m_ipMap = Map50End Property5152Public Property Get Map() As esriCarto.IMap53Set Map = m_ipMap54End Property5556' Either OpenAccessNetwork or OpenFeatureDatasetNetwork57' needs to be called.5859Public Sub OpenAccessNetwork(AccessFileName As String, FeatureDatasetNa me As String)6061Dim ipWorkspaceFactory As esriGeoDatabase.IWorkspaceFactory62Dim ipWorkspace As esriGeoDatabase.IWorkspace63Dim ipFeatureWorkspace As esriGeoDatabase.IFeatureWorkspace64Dim ipFeatureDataset As esriGeoDatabase.IFeatureDataset6566' After this Sub exits, we'll have an INetwork interface67' and an IMap interface initialized for the network we'll be using.6869' close down the last one if opened70 CloseWorkspace7172' open the m db73Set ipWorkspaceFactory =New esriDataSourcesGDB.AccessWorkspaceFact ory74Set ipWorkspace = ipWorkspaceFactory.OpenFromFile(AccessFileName, 0)76' get the FeatureWorkspace77Set ipFeatureWorkspace = ipWorkspace7879' open the FeatureDataset80Set ipFeatureDataset = ipFeatureWorkspace.OpenFeatureDataset(FeatureD atasetNam e)8182' initialize Network and Map (m_ipNetwork, m_ipMap)83If Not InitializeNetworkAndMap(ipFeatureDataset) Then Err.Raise 0, "Open AccessNetwork", "Error initializing Network and Map"8485End Sub8687Public Sub OpenFeatureDatasetNetwork(FeatureDataset As esriGeoDatabase. IFeatureDataset)88' close down the last one if opened89 CloseWorkspace9091' we assum e that the caller has passed a valid FeatureDataset9293' initialize Network and Map (m_ipNetwork, m_ipMap)94If Not InitializeNetworkAndMap(FeatureDataset) Then Err.Raise 0, "OpenFe atureDatasetNetwork", "Error initializing Network and Map"9596End Sub9798' The collection of points to travel through must be set.99100Public Property Set StopPoints(Points As esriGeometry.IPointCollection)102End Property103104Public Property Get StopPoints() As esriGeometry.IPointCollection105Set StopPoints = m_ipPoints106End Property107108' Calculate the path109110Public Sub SolvePath(WeightName As String)111112Dim ipNetwork As esriGeoDatabase.INetwork113Dim ipTraceFlowSolver As esriNetworkAnalysis.ITraceFlowSolver114Dim ipNetSolver As esriNetworkAnalysis.INetSolver115Dim ipNetFlag As esriNetworkAnalysis.INetFlag116Dim ipaNetFlag() As esriNetworkAnalysis.IEdgeFlag117Dim ipEdgePoint As esriGeometry.IPoint118Dim ipNetElements As esriGeoDatabase.INetElements119Dim intEdgeUserClassID As Long120Dim intEdgeUserID As Long121Dim intEdgeUserSubID As Long122Dim intEdgeID As Long123Dim ipFoundEdgePoint As esriGeometry.IPoint124Dim dblEdgePercent As Double125Dim ipNetWeight As esriGeoDatabase.INetWeight126Dim ipNetSolverWeights As esriNetworkAnalysis.INetSolverWeights127Dim ipNetSchem a As esriGeoDatabase.INetSchem a128Dim intCount As Long129Dim i As Long130Dim vaRes() As Variant132' m ake sure we are ready133 Debug.Assert Not m_ipPoints Is Nothing134 Debug.Assert Not m_ipGeometricNetwork Is Nothing135136' instantiate a trace flow solver137Set ipTraceFlowSolver = New esriNetworkAnalysis.TraceFlowSolver138139' get the INetSolver interface140Set ipNetSolver = ipTraceFlowSolver141142' set the source network to solve on143Set ipNetwork = m_/doc/07098e37f111f18583d05ab5.html work144Set ipNetSolver.SourceNetwork = ipNetwork145146' m ake edge flags from the points147148' the INetElements interface is needed to get UserID, UserClassID,149' and UserSubID from an element id150Set ipNetElements = ipNetwork151152' get the count153 intCount = m_ipPoints.PointCount154 Debug.Assert intCount > 1155156' dimension our IEdgeFlag array157ReDim ipaNetFlag(intCount)158159For i = 0 To intCount - 1160' m ake a new Edge Flag162Set ipEdgePoint = m_ipPoints.Point(i)163' look up the EID for the current point (this will populate intEdgeID and d blEdgePercent)164 m_ipPointToEID.GetNearestEdge ipEdgePoint, intEdgeID, ipFoundEdgePoi nt, dblEdgePercent165 Debug.Assert intEdgeID > 0 ' else Point (eid) not found166 ipNetElements.QueryIDs intEdgeID, esriETEdge, intEdgeUserClassID, intE dgeUserID, intEdgeUserSubID167 Debug.Assert (intEdgeUserClassID > 0) And (intEdgeUserID > 0) ' else P oint not found168 /doc/07098e37f111f18583d05ab5.html erClassID = intEdgeUserClassID169 /doc/07098e37f111f18583d05ab5.html erID = intEdgeUserID170 /doc/07098e37f111f18583d05ab5.html erSubID = intEdgeUserSubID171Set ipaNetFlag(i) = ipNetFlag172Next173174' add these edge flags175 ipTraceFlowSolver.PutEdgeOrigins intCount, ipaNetFlag(0)176177' set the weight (cost field) to solve on178179' get the INetSchem a interface180Set ipNetSchem a = ipNetwork181Set ipNetWeight = ipNetSchem a.WeightByName(WeightName)182 Debug.Assert Not ipNetWeight Is Nothing183184' set the weight (use the sam e for both directions)185Set ipNetSolverWeights = ipTraceFlowSolver186Set ipNetSolverWeights.From ToEdgeWeight = ipNetWeight188189' initialize array for results to number of segments in result190ReDim vaRes(intCount - 1)191192' solve it193 ipTraceFlowSolver.FindPath esriFMConnected, esriSPObjFnMinSum, m_ipEn umNetEID_Junctions, m_ipEnumNetEID_Edges, intCount - 1, vaRes(0)194195' com pute total cost196 m_dblPathCost = 0197For i =LBound(vaRes) To UBound(vaRes)198 m_dblPathCost = m_dblPathCost + vaRes(i)199Next200201' clear the last polyline result202Set m_ipPolyline =Nothing203204End Sub205206' Property to get the cost207208Public Property Get PathCost() As Double209 PathCost = m_dblPathCost210End Property211212' Property to get the shape213214Public Property Get PathPolyLine() As esriGeometry.IPolyline215217Dim count As Long, i As Long218Dim ipEIDInfo As esriNetworkAnalysis.IEIDInfo219Dim ipEnumEIDInfo As esriNetworkAnalysis.IEnumEIDInfo220Dim ipGeometry As esriGeometry.IGeometry221Dim ipNewGeometryColl As esriGeometry.IGeometryCollection222Dim ipSpatialReference As esriGeometry.ISpatialReference223224' if the line is already computed since the last path, just return it225If Not m_ipPolyline Is Nothing Then226Set PathPolyLine = m_ipPolyline227Exit Property228End If229230Set m_ipPolyline =New esriGeometry.Polyline231Set ipNewGeometryColl = m_ipPolyline232233' a path should be solved first234 Debug.Assert Not m_ipEnumNetEID_Edges Is Nothing235236' m ake an EIDHelper object to translate edges to geom etric features237Set ipEIDHelper =New esriNetworkAnalysis.EIDHelper238Set ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork239Set ipSpatialReference = m_ipMap.SpatialReference240Set ipEIDHelper.OutputSpatialReference = ipSpatialReference241 ipEIDHelper.ReturnGeometries =True242243' get the details using the IEIDHelper classes244Set ipEnumEIDInfo = ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_ Edges)247' set the iterator to beginning248 ipEnumEIDInfo.Reset249250For i = 1 To count251252' get the next EID and a copy of its geom etry (it m akes a Clone)253Set ipEIDInfo = ipEnumEIDInfo.Next254Set ipGeom etry = ipEIDInfo.Geometry255256 ipNewGeometryColl.AddGeometryCollection ipGeometry257258Next' EID259260' return the m erged geometry as a Polyline261Set PathPolyLine = m_ipPolyline262263End Property264265' Private266267Private Sub CloseWorkspace()268' m ake sure we let go of everything and start with new results269Set m_ipGeometricNetwork =Nothing270Set m_ipPoints =Nothing271Set m_ipPointToEID = Nothing272Set m_ipEnumNetEID_Junctions =Nothing273Set m_ipEnumNetEID_Edges =Nothing274Set m_ipPolyline =Nothing277Private Function InitializeNetworkAndMap(FeatureDataset As esriGeoDataba se.IFeatureDataset) As Boolean278279Dim ipNetworkCollection As esriGeoDatabase.INetworkCollection280Dim ipNetwork As esriGeoDatabase.INetwork281Dim count As Long, i As Long282Dim ipFeatureClassContainer As esriGeoDatabase.IFeatureClassContainer283Dim ipFeatureClass As esriGeoDatabase.IFeatureClass284Dim ipGeoDataset As esriGeoDatabase.IGeoDataset285Dim ipLayer As esriCarto.ILayer286Dim ipFeatureLayer As esriCarto.IFeatureLayer287Dim ipEnvelope As esriGeometry.IEnvelope, ipMaxEnvelope As esriGeomet ry.IEnvelope288Dim dblSearchTol As Double289Dim dblWidth As Double, dblHeight As Double290291On Error GoTo Trouble292293' get the networks294Set ipNetworkCollection = FeatureDataset295296' even though a FeatureDataset can have m any networks, we'll just297' assum e the first one (otherwise you would pass the network name in, et c.)298299' get the count of networks300 count = ipNetworkCollection.GeometricNetworkCount301304' get the first Geometric Newtork (0 - based)305Set m_ipGeometricNetwork = ipNetworkCollection.GeometricNetwork(0)306307' get the Network308Set ipNetwork = m_/doc/07098e37f111f18583d05ab5.html work309310' The EID Helper class that converts points to EIDs needs a311' IMap, so we'll need one around with all our layers added.312' This Pathfinder object has an optional Map property than m ay be set313' before opening the Network.314If m_ipMap Is Nothing Then315Set m_ipMap = New esriCarto.Map316317' Add each of the Feature Classes in this Geometric Network as a m ap Lay er318Set ipFeatureClassContainer = m_ipGeometricNetwork319 count = ipFeatureClassContainer.ClassCount320 Debug.Assert count > 0 ' then Exception.Create('No (network) feature cl asses found');321322For i = 0 To count - 1323' get the feature class324Set ipFeatureClass = ipFeatureClassContainer.Class(i)325' m ake a layer326Set ipFeatureLayer = New esriCarto.FeatureLayer327Set ipFeatureLayer.FeatureClass = ipFeatureClass328' add layer to the m ap329 m_ipMap.AddLayer ipFeatureLayer331End If' we needed to m ake a Map332333334' Calculate point snap tolerance as 1/100 of m ap width.335 count = m_/doc/07098e37f111f18583d05ab5.html yerCount336Set ipMaxEnvelope =New esriGeometry.Envelope337For i = 0 To count - 1338Set ipLayer = m_/doc/07098e37f111f18583d05ab5.html yer(i)339Set ipFeatureLayer = ipLayer340' get its dim ensions (for setting search tolerance)341Set ipGeoDataset = ipFeatureLayer342Set ipEnvelope = ipGeoDataset.Extent343' m erge with m ax dim ensions344 ipMaxEnvelope.Union ipEnvelope345Next346347' finally, we can set up the IPointToEID348Set m_ipPointToEID = New esriNetworkAnalysis.PointToEID349Set m_ipPointToEID.SourceMap = m_ipMap350Set m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork351352' set snap tolerance353 dblWidth = ipMaxEnvelope.Width354 dblHeight = ipMaxEnvelope.Height355356If dblWidth > dblHeight Then357 dblSearchTol = dblWidth / 100#358Else359 dblSearchTol = dblHeight / 100#360End If361362 m_ipPointToEID.SnapTolerance = dblSearchTol363364 InitializeNetworkAndMap =True' good to go365Exit Function366367Trouble:368 InitializeNetworkAndMap =False' we had an error369End Function。
GIS软件工程实习报告(最短路径分析)
AE开发之基于几何网络的最短路径分析1、实习目的本次实习目的在于熟练掌握ArcGIS Engine开发工具并能够通过C#语言在VS 2010开发环境中完成查询几何网络的最短路径分析的功能。
2、实习时间2015年5月23日星期六3、实习容3.1实验环境操作系统:Windows 2007二次开发平台:VS 2010开发环境、ArcGIS Desktop10.0、AE开发组件3.2实验任务完成基于几何网络分析的最短路径查询功能,即实现通过在几何网络地图中指定起始点,能够查询经过起始点的最短路线,并能够通过缩放功能在地图窗口居中显示。
3.3实验步骤3.3.1新建项目选择\文件\新建项目,如图选择项目类型中Visual C#,再选择Windows Application,记为“FindShortPath”,点击确定。
3.3.2添加控件3.3.3控件绑定因为添加的控件只是单独存在,但是程序需要各控件间协同工作,因此要进行控件绑定。
3.3.4创建几何网络1.在ArcCataLog中新建个人地理数据库—“甘地的个人地理数据库”,然后新建要素数据集“road”,接着,鼠标右键选择要素数据集“road”,选择“导入要素类”,导入需要创建几何网络的要素类“主要公路”,输出要素类的名字改为“road”,如下图所示:2.鼠标右键选择要素数据集“road”,选择新建“几何网络”,则出现“新建几何网络”对话框,作如下图所示的一系列设置,最终得到几何网络“road_Net”。
至此,得到几何网络“road_Net”,如下图所示:3.3.5代码实现最短路径分析①设置ToolStrip1②添加代码private IActiveView m_ipActiveView;private IMap m_ipMap;//地图控件中地图private IGraphicsContainer pGC;//图形对象private bool clicked = false;int clickedcount = 0;private double m_dblPathCost = 0;private IGeometricNetwork m_ipGeometricNetwork;private IPointCollection m_ipPoints;//输入点集合private IPointToEID m_ipPointToEID;private IEnumNetEID m_ipEnumNetEID_Junctions;private IEnumNetEID m_ipEnumNetEID_Edges;private IPolyline m_ipPolyline;private IMapControl3 mapctrlMainMap = null;private void Form1_Load(object sender, EventArgs e){//对象初始化mapctrlMainMap = (IMapControl3)this.axMapControl1.Object;m_ipActiveView = axMapControl1.ActiveView;m_ipMap = m_ipActiveView.FocusMap;clicked = false;pGC = m_ipMap as IGraphicsContainer;}private void Findpath_Click(object sender, EventArgs e){mapctrlMainMap.CurrentTool = null;//设置鼠标样式axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;if (yerCount == 0){MessageBox.Show("请先加载几何网络数据!");return;}m_ipActiveView = axMapControl1.ActiveView;m_ipMap = m_ipActiveView.FocusMap;clicked = false;pGC = m_ipMap as IGraphicsContainer;ILayer ipLayer = m_ipMap.get_Layer(0);IFeatureLayer ipFeatureLayer = ipLayer as IFeatureLayer;IFeatureDataset ipFDS = ipFeatureLayer.FeatureClass.FeatureDataset;OpenFeatureDatasetNetwork(ipFDS);clicked = true;clickedcount = 0;pGC.DeleteAllElements();}private void SolvePath_Click(object sender, EventArgs e){axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;if (SolvePathGan("Weight"))//先解析路径{IPolyline ipPolyResult = PathPolyLine();//最后返回最短路径clicked = false;if (ipPolyResult.IsEmpty){MessageBox.Show("没有路径可到!!");}else{IRgbColor color = new RgbColorClass();color.Red = 255;color.Blue = 64;color.Green = 128;LineElementClass element = new LineElementClass();ILineSymbol linesymbol = new SimpleLineSymbolClass();linesymbol.Color = color as IColor;linesymbol.Width = 3;element.Geometry = m_ipPolyline;element.Symbol = linesymbol;pGC.AddElement(element, 0);m_ipActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);axMapControl1.FlashShape(element.Geometry);MessageBox.Show("路径经过" + m_ipEnumNetEID_Edges.Count + "条线" + "\r\n" + "经过节点数为" + m_ipEnumNetEID_Junctions.Count + "\r\n" + "路线长度为" + m_ipPolyline.Length.ToString("#######.##") + "\r\n", "几何路径信息");}}}//路径缩放功能private void SuoFang_Click(object sender, EventArgs e){if (m_ipPolyline == null){MessageBox.Show("当前没有执行路径查询!!!请确认!");}else{this.axMapControl1.Extent = m_ipPolyline.Envelope;}}#region 自定义路径查询函数public void OpenFeatureDatasetNetwork(IFeatureDataset FeatureDataset){CloseWorkspace();if (!InitializeNetworkAndMap(FeatureDataset))Console.WriteLine("打开network出错");}//关闭工作空间private void CloseWorkspace(){m_ipGeometricNetwork = null;m_ipPoints = null;m_ipPointToEID = null;m_ipEnumNetEID_Junctions = null;m_ipEnumNetEID_Edges = null;m_ipPolyline = null;}//初始化几何网络和地图private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset){IFeatureClassContainer ipFeatureClassContainer;IFeatureClass ipFeatureClass;IGeoDataset ipGeoDataset;ILayer ipLayer;IFeatureLayer ipFeatureLayer;IEnvelope ipEnvelope, ipMaxEnvelope;double dblSearchTol;INetworkCollection ipNetworkCollection = FeatureDataset as INetworkCollection;int count = ipNetworkCollection.GeometricNetworkCount;//获取第一个几何网络工作空间m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0);INetwork ipNetwork = m_work;if (m_ipMap != null){ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer;count = ipFeatureClassContainer.ClassCount;for (int i = 0; i < count; i++){ipFeatureClass = ipFeatureClassContainer.get_Class(i);ipFeatureLayer = new FeatureLayerClass();ipFeatureLayer.FeatureClass = ipFeatureClass;for (int j = 0; j < m_yerCount; j++){if (m_ipMap.get_Layer(j).Name.ToUpper() == .ToUpper()){continue;}}}}count = m_yerCount;ipMaxEnvelope = new EnvelopeClass();for (int i = 0; i < count; i++){ipLayer = m_ipMap.get_Layer(i);ipFeatureLayer = ipLayer as IFeatureLayer;ipGeoDataset = ipFeatureLayer as IGeoDataset;ipEnvelope = ipGeoDataset.Extent;ipMaxEnvelope.Union(ipEnvelope);}m_ipPointToEID = new PointToEIDClass();m_ipPointToEID.SourceMap = m_ipMap;m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork;double dblWidth = ipMaxEnvelope.Width;double dblHeight = ipMaxEnvelope.Height;if (dblWidth > dblHeight)dblSearchTol = dblWidth / 100;elsedblSearchTol = dblHeight / 100;m_ipPointToEID.SnapTolerance = dblSearchTol;return true;}//返回路径的几何体public IPolyline PathPolyLine(){IEIDInfo ipEIDInfo;IGeometry ipGeometry;if (m_ipPolyline != null) return m_ipPolyline;m_ipPolyline = new PolylineClass();IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGeometryCollection;//引用传递ISpatialReference ipSpatialReference = m_ipMap.SpatialReference;IEIDHelper ipEIDHelper = new EIDHelper();ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork;ipEIDHelper.OutputSpatialReference = ipSpatialReference;ipEIDHelper.ReturnGeometries = true;IEnumEIDInfo ipEnumEIDInfo = ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges);int count = ipEnumEIDInfo.Count;ipEnumEIDInfo.Reset();for (int i = 0; i < count; i++){string[] info;info = new string[count];ipEIDInfo = ipEnumEIDInfo.Next();ipGeometry = ipEIDInfo.Geometry;ipNewGeometryColl.AddGeometryCollection(ipGeometry as IGeometryCollection);info[i] = m_ipPolyline.Length.ToString();}return m_ipPolyline;}public bool SolvePathGan(string WeightName){try{int intJunctionUserClassID;int intJunctionUserID;int intJunctionUserSubID;int intJunctionID;IPoint ipFoundJunctionPoint;ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolver() as ITraceFlowSolverGEN;INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;if (m_ipGeometricNetwork == null){return false;}INetwork ipNetwork = m_work;ipNetSolver.SourceNetwork = ipNetwork;INetElements ipNetElements = ipNetwork as INetElements;if (m_ipPoints == null){ MessageBox.Show("请选择点!!"); return false; }int intCount = m_ipPoints.PointCount;//这里的points 有值吗?////定义一个边线旗数组//*********最近点***************//////////IJunctionFlag[] pJunctionFlagList = new JunctionFlag[intCount];for (int i = 0; i < intCount; i++){INetFlag ipNetFlag = new JunctionFlag() as INetFlag;IPoint ipJunctionPoint = m_ipPoints.get_Point(i);//查找输入点的最近的网络点m_ipPointToEID.GetNearestJunction(ipJunctionPoint, out intJunctionID, out ipFoundJunctionPoint);ipNetElements.QueryIDs(intJunctionID, esriElementType.esriETJunction, out intJunctionUserClassID, out intJunctionUserID, out intJunctionUserSubID);erClassID = intJunctionUserClassID;erID = intJunctionUserID;erSubID = intJunctionUserSubID;IJunctionFlag pTemp = (IJunctionFlag)(ipNetFlag as IJunctionFlag);pJunctionFlagList[i] = pTemp;}ipTraceFlowSolver.PutJunctionOrigins(ref pJunctionFlagList);INetSchema ipNetSchema = ipNetwork as INetSchema;INetWeight ipNetWeight = ipNetSchema.get_WeightByName(WeightName);INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重object[] vaRes = new object[intCount - 1];//通过findpath得到边线和交汇点的集合ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,esriShortestPathObjFn.esriSPObjFnMinSum,outm_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, intCount - 1, ref vaRes);m_dblPathCost = 0;for (int i = 0; i < vaRes.Length; i++){double m_Va = (double)vaRes[i];m_dblPathCost = m_dblPathCost + m_Va;}m_ipPolyline = null;return true;}catch (Exception ex){Console.WriteLine(ex.Message); return false;}#endregion}private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e){if (clicked != true)return;IPoint ipNew;if (m_ipPoints == null){m_ipPoints = new Multipoint();}ipNew = m_ipActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);object o = Type.Missing;m_ipPoints.AddPoint(ipNew, ref o, ref o);IElement element;ITextElement textelement = new TextElementClass();element = textelement as IElement;clickedcount++;textelement.Text = clickedcount.ToString();element.Geometry = m_ipActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);pGC.AddElement(element, 0);m_ipActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}3.4实验结果在得到的窗口中加载“甘地的个人地理数据库”(在压缩包中有)中的几何网络“road_Net”,将几何网络地图加载到地图显示窗口中,如下图所示:点击图标添加起始点,效果如下图:然后点击图标,计算最短路径,效果如下图:最后点击图标得到最短路径的缩放,效果如下图:这样就完成了最短路径的查询分析和显示的功能。
ArcGIS最短路径实验
《GIS在道路工程中的应用》实验报告——ArcGIS最短路径实验ArcGIS最短路径实验一:实验目的本实验是笔者在前段时间学习ArcGIS软件的过程中总结的一些心得,通过这个实验,使我熟悉了ArcGIS栅格数据距离分析、表面分析、成本权重距离、数据重分类、最短路径等空间分析功能,极大地拓展了专业视野。
二:实验数据①等高线文件——等高线地形图.DWG②起点文件——StartPoint.shp③终点文件——EndPoint.shp三:实验步骤1:由等高线图生成TIN,具体步骤如下。
1):将“等高线地形图.DWG”另存为dxf格式,以备ArcMap使用。
2):打开ArcMap,添加“等高线地形图.dxf”数据。
3):打开“3D分析”命令,选择“创建/修改TIN”,再选择“从要素创建TIN”。
在弹出的图层选择框中将后面三个勾上,点击确定。
2:将TIN转化为栅格,并进行重分类,具体步骤如下。
1):打开“3D分析”命令,选择“转换”,再选择“TIN到栅格”,在弹出的对话框中直接点击确定。
2):重分类栅格数据,选择“空间分析”菜单命令,在下拉菜单中选择“重分类”。
3):在弹出的菜单中,点击“分类”命令,将高程栅格数据分成10类,如下所示:3:进行表面坡度、坡向分析,具体步骤如下。
1):选择“空间分析”菜单命令,在下拉菜单中选择“表面分析”,再选择“坡度”选项,在弹出的对话框中直接点击确定。
2):同理,重分类坡度栅格数据,见下图。
3):选择“空间分析”菜单命令,在下拉菜单中选择“表面分析”,再选择“坡向”选项,在弹出的对话框中直接点击确定。
4:创建起终点文件,并编辑,具体步骤如下。
1):打开ArcCatalog,在本次实验的目录新建两个shpfile文件,一个是StartPoint.shp,另个是EndPoint.shp,并选择正确的坐标系,如下图。
2):在ArcMap中分别加载StartPoint.shp和EndPoint.shp文件,并进行编辑,分别绘制出起点和终点。
最短路径算法介绍
最短路径算法介绍最短路径算法是计算两个节点之间最短路径的一组算法。
在计算网络最短路径、交通路线规划、导航系统以及优化其他经济和工业流程等很多领域都有广泛的应用。
最短路径算法的目标是找出网络中连接起始节点与目标节点的最短路径。
在网络中,起始节点和目标节点被称为源节点和目标节点。
网络包含节点(也称为顶点)和连接节点的边。
每一条边上都有一个权重,这个权重表示了通过这条边所需要的代价或距离等值。
最短路径算法是通过这些权重来查找最短路径的。
最短路径算法的核心思想是通过规定一些规则或算法来查找网络上的最短路径。
最常用的最短路径算法是Dijkstra算法和A*算法。
Dijkstra算法是一个基于贪心算法的最短路径算法,它的特点是时间复杂度较低,适用于稠密图。
而A*算法是通过启发式搜索来计算最短路径的,它适用于稀疏图和高维空间搜索(如机器人路径规划)。
Dijkstra算法的基本思想是从源节点开始依次计算到各个节点的最短距离,直到计算出目标节点的最短路径。
Dijkstra算法的优点是保证了每个节点被计算后,所有可能的最短路径都被计算过,从而保证了最终计算出的路径是最短路径。
Dijkstra算法的缺点是需要存储所有节点的距离,因此对于大规模图,存储距离的开销非常大。
A*算法是一种启发式搜索算法。
它是在Dijkstra算法的基础上引入了启发式函数,利用这个函数来评估节点到目标节点的距离,从而优先扩展距离目标节点更近的节点。
A*算法的重点是设计合适的启发函数,这个函数应该尽可能地准确地评估节点到目标节点的距离。
与Dijkstra算法相比,A*算法可以大大减少计算开销,从而提高算法的效率。
最短路径算法在实际的应用中非常重要。
在网络最短路径问题中,最短路径算法可以用于计算网络拓扑的特征,如网络直径、网络中心性等。
在地图导航和交通规划中,最短路径算法可以用于找到最短的路径以及计算交通拥堵等。
在机器人路径规划中,最短路径算法可以用于确定机器人行走的最短路径以及防止机器人撞到障碍物等。
Arcgis操作 实验十五:最短路径分析
实验十五:最短路径分析一、实验目的1、掌握各种类型的最短路径分析;2、理解网络分析原理。
二、实验准备数据准备:City.mdb软件准备:ArcGIS Desktop9.x,ArcCatalog三、实验内容根据不同的要求,获得到达指定目的地的最佳路径,并给出路径的长度;找出距景点最近的某设施的路径。
1、在网络中指定一个商业中心,分别求出在不同距离、时间的限制下从家到商业中心的最佳路径;2、给定访问顺序,按要求找出从家出发,逐个经过访问点,最终到达目的地的最佳路径;3、研究阻强的设置对最佳路径选择的影响。
四、实验步骤启动ArcMap ,打开city. mdb ,双击city数据库,加载数据。
对点状要素place符号化:以HOME字段,1值为家,0值为商业中心。
具体步骤见操作视频:最短路径分析.exe图1 无权重参照的最短路径显示(1)无权重最佳路径的生成1)在网络分析工具条上,选择旗标工具,将旗标放在“家”和想要取得“商业中心”点上。
2)选择Analysis/Options命令,打开Analysis Options对话框,确认Weights和Weight Filter 标签项全部是None,这种情况下进行的最短路径分析是完全按照这个网络自身的长短来确定。
3)在Track Task文本框中选择Find path。
单击solve按钮。
显示最短路径(图1),这条路径的总成本显示在状态栏中。
(2)加权最佳路径生成1)在设施网络分析工具条下,点选旗标工具,将旗标分别放在“家”和想去的某个“商业中心”的位置上。
2)选择Analysis/Options命令,打开Analysis Options对话框(图2)进入Weights标签页,在边的权重上,全部选择长度权重属性。
图2 长度权重属性设置3)在Track Task文本枢中选择Find path,单击solve按钮,则以长度为比重的最短路径将显示出来(图3),这条路径的总成本显示在状态栏中。
在ArcGIS矢量图中搜寻最短路径的实现
在ArcGIS矢量图中搜寻最短路径的实现
在ArcGIS矢量图中搜寻最短路径的实现
最短路径问题是地理网络分析中的重要问题之一,具有重要的应用价值.搜索最短路径的方法很多,在研究了各种方法后,本文提出了在ArcGIS矢量图中搜索最短路径的新方法.首先,提取经过ArcGIS简单处理的矢量图的信息,然后,借助Floyd算法,用MATIAB建模来提取节点间的`最短路径,最后根据模型运算的结果在矢量图中绘出最短路径.试验证明,该方法操作简单,效果良好.
作者:高吉 GAO Ji 作者单位:北京林业大学,北京,100083 刊名:北京测绘英文刊名: BEIJING SURVEYING AND MAPPING 年,卷(期): 2009 ""(2) 分类号: P208 关键词:最短路径算法地理信息系统 MATLAB。
ArcEngine+最短路径分析(C#源码)
using workAnalysis;public class ClsPathFinder{private IGeometricNetwork m_ipGeometricNetwork;private IMap m_ipMap;private IPointCollection m_ipPoints;private IPointToEID m_ipPointToEID;private double m_dblPathCost =0;private IEnumNetEID m_ipEnumNetEID_Junctions;private IEnumNetEID m_ipEnumNetEID_Edges;private IPolyline m_ipPolyline;#region Public Function//返回和设置当前地图public IMap SetOrGetMap{set{ m_ipMap = value;}get{return m_ipMap;}}//打开网络public void OpenFeatureDatasetNetwork(IFeatureDataset FeatureDataset){CloseWorkspace();if (!InitializeNetworkAndMap(FeatureDataset))Console.WriteLine( "打开出错");}//输入点的集合public IPointCollection StopPoints{set{m_ipPoints= value;}get{return m_ipPoints;}}//路径成本public double PathCost{get {return m_dblPathCost;}}//返回路径public IPolyline PathPolyLine(){IEIDInfo ipEIDInfo;IGeometry ipGeometry;if(m_ipPolyline!=null)return m_ipPolyline;m_ipPolyline = new PolylineClass();IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGeometryCollection;ISpatialReference ipSpatialReference = m_ipMap.SpatialReference; IEIDHelper ipEIDHelper = new EIDHelperClass();ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork;ipEIDHelper.OutputSpatialReference = ipSpatialReference;ipEIDHelper.ReturnGeometries = true;IEnumEIDInfo ipEnumEIDInfo =ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges);int count = ipEnumEIDInfo.Count;ipEnumEIDInfo.Reset();for(int i =0;i<count;i++){ipEIDInfo = ipEnumEIDInfo.Next();ipGeometry = ipEIDInfo.Geometry;ipNewGeometryColl.AddGeometryCollection( ipGeometry as IGeometryCollection);}return m_ipPolyline;}//解决路径public void SolvePath(string WeightName){try{int intEdgeUserClassID;int intEdgeUserID;int intEdgeUserSubID;int intEdgeID;IPoint ipFoundEdgePoint;double dblEdgePercent;/*C#中使用*ITraceFlowSolverGEN替代ITraceFlowSolver*/ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;INetwork ipNetwork = m_work;ipNetSolver.SourceNetwork = ipNetwork;INetElements ipNetElements = ipNetwork as INetElements;int intCount = m_ipPoints.PointCount;//定义一个边线旗数组IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];for(int i = 0;i<intCount ;i++){INetFlag ipNetFlag = new EdgeFlagClass()as INetFlag;IPoint ipEdgePoint = m_ipPoints.get_Point(i);//查找输入点的最近的边线m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeID,out ipFoundEdgePoint, out dblEdgePercent);ipNetElements.QueryIDs( intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID,out intEdgeUserSubID);erClassID = intEdgeUserClassID;erID = intEdgeUserID;erSubID = intEdgeUserSubID;IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);pEdgeFlagList=pTemp;}ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);INetSchema ipNetSchema = ipNetwork as INetSchema;INetWeight ipNetWeight =ipNetSchema.get_WeightByName(WeightName);INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重object [] vaRes =new object[intCount-1];//通过findpath得到边线和交汇点的集合ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges, intCount-1, ref vaRes);//计算成本m_dblPathCost = 0;for (int i =0;i<vaRes.Length;i++){double m_Va =(double) vaRes;m_dblPathCost = m_dblPathCost + m_Va;}m_ipPolyline = null;}catch(Exception ex){Console.WriteLine(ex.Message);}}#endregion#region Private Function//初始化private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset) {IFeatureClassContainer ipFeatureClassContainer;IFeatureClass ipFeatureClass ;IGeoDataset ipGeoDataset;ILayer ipLayer ;IFeatureLayer ipFeatureLayer;IEnvelope ipEnvelope, ipMaxEnvelope ;double dblSearchTol;INetworkCollection ipNetworkCollection = FeatureDataset as INetworkCollection;int count = ipNetworkCollection.GeometricNetworkCount;//获取几何网络工作空间m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0); INetwork ipNetwork = m_work;if(m_ipMap!=null){m_ipMap = new MapClass();ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer;count = ipFeatureClassContainer.ClassCount;for(int i =0;i<count;i++){ipFeatureClass = ipFeatureClassContainer.get_Class(i); ipFeatureLayer = new FeatureLayerClass();ipFeatureLayer.FeatureClass = ipFeatureClass;m_ipMap.AddLayer( ipFeatureLayer);}}count = m_yerCount;ipMaxEnvelope = new EnvelopeClass();for(int i =0;i<count;i++){ipLayer = m_ipMap.get_Layer(i);ipFeatureLayer = ipLayer as IFeatureLayer;ipGeoDataset = ipFeatureLayer as IGeoDataset;ipEnvelope = ipGeoDataset.Extent;ipMaxEnvelope.Union( ipEnvelope);}m_ipPointToEID = new PointToEIDClass();m_ipPointToEID.SourceMap = m_ipMap;m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork;double dblWidth = ipMaxEnvelope.Width;double dblHeight = ipMaxEnvelope.Height;if( dblWidth > dblHeight)dblSearchTol = dblWidth / 100;elsedblSearchTol = dblHeight / 100;m_ipPointToEID.SnapTolerance = dblSearchTol;return true ;}private void CloseWorkspace(){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 = new ClsPathFinder();ipMap = this.m_ActiveView.FocusMap;ipLayer = ipMap.get_Layer(0);ipFeatureLayer = ipLayer as IFeatureLayer;ipFDB = ipFeatureLayer.FeatureClass.FeatureDataset;m_ipPathFinder.SetOrGetMap = ipMap;m_ipPathFinder.OpenFeatureDatasetNetwork(ipFDB);}private void ViewMap_OnMouseDown(object sender,ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)//获取鼠标输入的点{IPoint ipNew ;if( m_ipPoints==null){m_ipPoints = new MultipointClass();m_ipPathFinder.StopPoints = m_ipPoints;}ipNew =ViewMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x ,e.y);object o = Type.Missing;m_ipPoints.AddPoint(ipNew,ref o,ref o);}m_ipPathFinder.SolvePath("Weight");//解析路径IPolyline ipPolyResult = m_ipPathFinder.PathPolyLine();//返回最短路径。
基于arcgis最短路径分析
✓ 网络组成要素
结点(Node):网络中任意两条线段的交点,属性如资源数 量等
链(Link):连接两个结点的弧段。供物体运营的通道,链间 的连接关系由弧段-结点拓扑数据结构来表达。属性如资源 流动的时间、速度等
中心(Center):网络中位于结点处,具有沿着链收集和发 放资源能力的设施,如邮局、电站、水库等
要素,如果代表天桥的两条弧段相交于一个
节点(高程为0),那么代表天桥下面的街
道的两条弧段就相交于另一节点(高程为1
)
29
(1)网络数据集的连通性---(Connectivity)
连通性可在参与网络的要素类中定义
也可以在要素类子类(subtype)中定义
可以使用高程字段判断连通性
30
连通组和连通策略
几何网络(Geometric networks)
用于定向网络分析 (如: 水流、电流等) 线 & 点 -> Geometric network ArcMap中使用 Utility Network Analyst 工具条
网络数据集(Network datasets)
用于非定向网络分析 (如:交通问题) 线,点&转弯( turns)-> Network dataset 使用 ArcGIS Network Analyst扩展模块
为了实现供需关系,在网络中必然存在资源的运 输和流动。资源要么由供方送到需方,要么由需 需方到供方索取。
15
关于Allocate的两个例子
Supply-To-Demand的例子:负荷设 计、时间与距离损耗估算
电能从电站产生,并通过电网传送到客户那 里去。在这里,电站就是网络模型中的 “Center”,因为它可以提供电力供应。电能的 客户沿电网的线路(网络模型中的Link)分布, 他们产生了“Demand”。在这种情况下,资源 是通过网络由供方传输到需方来实现资源分配 。可用来分析输电系统是否超载;停电的社会 、经济影响估计等。
AE 最短路径分析
ArcEngine 最短路径分析using System;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.Geodatabase;using workAnalysis;namespace GisEditor{/// <summary>/// 最短路径分析/// </summary>public class ClsPathFinder{private IGeometricNetwork m_ipGeometricNetwork;private IMap m_ipMap;private IPointCollection m_ipPoints;private IPointToEID m_ipPointToEID;private double m_dblPathCost =0;private IEnumNetEID m_ipEnumNetEID_Junctions;private IEnumNetEID m_ipEnumNetEID_Edges;private IPolyline m_ipPolyline;#region Public Function//返回和设置当前地图public IMap SetOrGetMap{set{ m_ipMap = value;}get{return m_ipMap;}}//打开几何数据集的网络工作空间public void OpenFeatureDatasetNetwork(IFeatureDataset FeatureDataset) {CloseWorkspace();if (!InitializeNetworkAndMap(FeatureDataset))Console.WriteLine( "打开network出错");}//输入点的集合public IPointCollection StopPoints{set{m_ipPoints= value;}get{return m_ipPoints;}}//路径成本public double PathCost{get {return m_dblPathCost;}}//返回路径的几何体public IPolyline PathPolyLine(){IEIDInfo ipEIDInfo;IGeometry ipGeometry;if(m_ipPolyline!=null)return m_ipPolyline;m_ipPolyline = new PolylineClass();IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGeometryCollection;ISpatialReference ipSpatialReference = m_ipMap.SpatialReference;IEIDHelper ipEIDHelper = new EIDHelperClass();ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork;ipEIDHelper.OutputSpatialReference = ipSpatialReference;ipEIDHelper.ReturnGeometries = true;IEnumEIDInfo ipEnumEIDInfo = ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges);int count = ipEnumEIDInfo.Count;ipEnumEIDInfo.Reset();for(int i =0;i<count;i++){ipEIDInfo = ipEnumEIDInfo.Next();ipGeometry = ipEIDInfo.Geometry;ipNewGeometryColl.AddGeometryCollection( ipGeometry as IGeometryCollection);}return m_ipPolyline;}//解决路径public void SolvePath(string WeightName){try{int intEdgeUserClassID;int intEdgeUserID;int intEdgeUserSubID;int intEdgeID;IPoint ipFoundEdgePoint;double dblEdgePercent;/*PutEdgeOrigins方法的第二个参数要求是IEdgeFlag类型的数组,* 在VB等其他语言的代码中,只需传人该类型数组的第一个元素即* 可,但C#中的机制有所不同,需要作出如下修改:使用* ITraceFlowSolverGEN替代ITraceFlowSolver*/ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN;INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver;INetwork ipNetwork = m_work;ipNetSolver.SourceNetwork = ipNetwork;INetElements ipNetElements = ipNetwork as INetElements;int intCount = m_ipPoints.PointCount;//定义一个边线旗数组IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount];for(int i = 0;i<intCount ;i++){INetFlag ipNetFlag = new EdgeFlagClass()as INetFlag;IPoint ipEdgePoint = m_ipPoints.get_Point(i);//查找输入点的最近的边线m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeID,out ipFoundEdgePoint, out dblEdgePercent);ipNetElements.QueryIDs( intEdgeID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID,out intEdgeUserSubID);erClassID = intEdgeUserClassID;erID = intEdgeUserID;erSubID = intEdgeUserSubID;IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag);pEdgeFlagList[i]=pTemp;}ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList);INetSchema ipNetSchema = ipNetwork as INetSchema;INetWeight ipNetWeight = ipNetSchema.get_WeightByName(WeightName);INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights;ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始边线的权重ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止边线的权重object [] vaRes =new object[intCount-1];//通过findpath得到边线和交汇点的集合ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnected,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges, intCount-1, ref vaRes);//计算元素成本m_dblPathCost = 0;for (int i =0;i<vaRes.Length;i++){double m_Va =(double) vaRes[i];m_dblPathCost = m_dblPathCost + m_Va;}m_ipPolyline = null;}catch(Exception ex){Console.WriteLine(ex.Message);}}#endregion#region Private Function//初始化几何网络和地图private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset){IFeatureClassContainer ipFeatureClassContainer;IFeatureClass ipFeatureClass ;IGeoDataset ipGeoDataset;ILayer ipLayer ;IFeatureLayer ipFeatureLayer;IEnvelope ipEnvelope, ipMaxEnvelope ;double dblSearchTol;INetworkCollection ipNetworkCollection = FeatureDataset as INetworkCollection; int count = ipNetworkCollection.GeometricNetworkCount;//获取第一个几何网络工作空间m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0); INetwork ipNetwork = m_work;if(m_ipMap!=null){m_ipMap = new MapClass();ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer; count = ipFeatureClassContainer.ClassCount;for(int i =0;i<count;i++){ipFeatureClass = ipFeatureClassContainer.get_Class(i);ipFeatureLayer = new FeatureLayerClass();ipFeatureLayer.FeatureClass = ipFeatureClass;m_ipMap.AddLayer( ipFeatureLayer);}}count = m_yerCount;ipMaxEnvelope = new EnvelopeClass();for(int i =0;i<count;i++){ipLayer = m_ipMap.get_Layer(i);ipFeatureLayer = ipLayer as IFeatureLayer;ipGeoDataset = ipFeatureLayer as IGeoDataset;ipEnvelope = ipGeoDataset.Extent;ipMaxEnvelope.Union( ipEnvelope);}m_ipPointToEID = new PointToEIDClass();m_ipPointToEID.SourceMap = m_ipMap;m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork;double dblWidth = ipMaxEnvelope.Width;double dblHeight = ipMaxEnvelope.Height;if( dblWidth > dblHeight)dblSearchTol = dblWidth / 100;elsedblSearchTol = dblHeight / 100;m_ipPointToEID.SnapTolerance = dblSearchTol;return true ;}//关闭工作空间private void CloseWorkspace(){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 = new ClsPathFinder();ipMap = this.m_ActiveView.FocusMap;ipLayer = ipMap.get_Layer(0);ipFeatureLayer = ipLayer as IFeatureLayer;ipFDB = ipFeatureLayer.FeatureClass.FeatureDataset;m_ipPathFinder.SetOrGetMap = ipMap;m_ipPathFinder.OpenFeatureDatasetNetwork(ipFDB);}private void ViewMap_OnMouseDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)//获取地图上鼠标输入的点{IPoint ipNew ;if( m_ipPoints==null){m_ipPoints = new MultipointClass();m_ipPathFinder.StopPoints = m_ipPoints;}ipNew = ViewMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);object o = Type.Missing;m_ipPoints.AddPoint(ipNew,ref o,ref o);}m_ipPathFinder.SolvePath("Weight");//先解析路径IPolyline ipPolyResult = m_ipPathFinder.PathPolyLine();//最后返回最短路径。
路径规划(最短路径)算法C#实现
路径规划(最短路径)算法C#实现以前空闲的时候⽤C#实现的路径规划算法,今⽇贴它出来,看⼤家有没有更好的实现⽅案。
关于路径规划(最短路径)算法的背景知识,⼤家可以参考《C++算法--图算法》⼀书。
该图算法描述的是这样的场景:图由节点和带有⽅向的边构成,每条边都有相应的权值,路径规划(最短路径)算法就是要找出从节点A到节点B的累积权值最⼩的路径。
⾸先,我们可以将“有向边”抽象为Edge类:public class Edge{public string StartNodeID ;public string EndNodeID ;public double Weight ; //权值,代价}节点则抽象成Node类,⼀个节点上挂着以此节点作为起点的“出边”表。
public class Node{private string iD ;private ArrayList edgeList ;//Edge的集合--出边表public Node(string id ){this.iD = id ;this.edgeList = new ArrayList() ;}property}在计算的过程中,我们需要记录到达每⼀个节点权值最⼩的路径,这个抽象可以⽤PassedPath类来表⽰:///<summary>/// PassedPath ⽤于缓存计算过程中的到达某个节点的权值最⼩的路径///</summary>public class PassedPath{private string curNodeID ;private bool beProcessed ; //是否已被处理private double weight ; //累积的权值private ArrayList passedIDList ; //路径public PassedPath(string ID){this.curNodeID = ID ;this.weight = double.MaxValue ;this.passedIDList = new ArrayList() ;this.beProcessed = false ;}#region propertypublic bool BeProcessed{get{return this.beProcessed ;}set{this.beProcessed = value ;}}public string CurNodeID{get{return this.curNodeID ;}}public double Weight{get{return this.weight ;}set{this.weight = value ;}}public ArrayList PassedIDList{get{return this.passedIDList ;}}#endregion}另外,还需要⼀个表PlanCourse来记录规划的中间结果,即它管理了每⼀个节点的PassedPath。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(m_ipMap!=null) {
m_ipMap = new MapClass(); ipFeatureClassContainer = m_ipGeometricNetwork as IFeatur eClassContainer; count = ipFeatureClassContainer.ClassCount; for(int i =0;i<count;i++) {
IEIDInfo ipEIDInfo; IGeometry ipGeometry; if(m_ipPolyline!=null)return m_ipPolyline;
m_ipPolyline = new PolylineClass(); IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGe ometryCollection;
组, * 在 VB 等其他语言的代码中,只需传人该类型数组的第一个元
素即 * 可,但 C#中的机制有所不同,需要作出如下修改:使用 * ITraceFlowSolverGEN 替代 ITraceFlowSolver */
ITraceFlowSolverGEN ipTraceFlowSolver = new TraceFlow SolverClass() as ITraceFlowSolverGEN;
INetworkCollection ipNetworkCollection = FeatureDataset as
INetworkCollection; int count = ipNetworkCollection.GeometricNetworkCount; //获取第一个几何网络工作空间 m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNet
object [] vaRes =new object[intCount-1]; //通过 findpath 得到边线和交汇点的集合 ipTraceFlowSolver.FindPath(esriFlowMethod.esriFMConnecte d,
esriShortestPathObjFn.esriSPObjFnMinSum, out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges, intCount-1, ref vaRes); //计算元素成本 m_dblPathCost = 0; for (int i =0;i<vaRes.Length;i++) { double m_Va =(double) vaRes[i]; m_dblPathCost = m_dblPathCost + m_Va; } m_ipPolyline = null; } catch(Exception ex) { Console.WriteLine(ex.Message); } } #endregion
#region Private Function //初始化几何网络和地图 private bool InitializeNetworkAndMap(IFeatureDataset FeatureD ataset) {
IFeatureClassContainer ipFeatureClassContainer; IFeatureClass ipFeatureClass ; IGeoDataset ipGeoDataset; ILayer ipLayer ; IFeatureLayer ipFeatureLayer; IEnvelope ipEnvelope, ipMaxEnvelope ; double dblSearchTol;
ence;
ISpatialReference ipSpatialReference = m_ipMap.SpatialRefer
IEIDHelper ipEIDHelper = new EIDHelperClass(); ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork; ipEIDHelper.OutputSpatialReference = ipSpatialReference; ipEIDHelper.ReturnGeometries = true;
//解决路径 public void SolvePath(string WeightName) {
try
{ int intEdgeUserClassID; int intEdgeUserID; int intEdgeUserSubID; int intEdgeID; IPoint ipFoundEdgePoint; double dblEdgePercent; /*PutEdgeOrigins 方法的第二个参数要求是 IEdgeFlag 类型的数
ipFeatureClass = ipFeatureClassContainer.get_Class (i);
ipFeatureLayer = new FeatureLayerClass(); ipFeatureLayer.FeatureClass = ipFeatureClass; m_ipMap.AddLayer( ipFeatureLayer); } } count = m_yerCount; ipMaxEnvelope = new EnvelopeClass(); for(int i =0;i<count;i++) { ipLayer = m_ipMap.get_Layer(i); ipFeatureLayer = ipLayer as IFeatureLayer; ipGeoDataset = ipFeatureLayer as IGeoDataset; ipEnvelope = ipGeoDataset.Extent; ipMaxEnvelope.Union( ipEnvelope); }
INetFlag ipNetFlag = new EdgeFlagClass()as INetFlag; IPoint ipEdgePoint = m_ipPoints.get_Point(i); //查找输入点的最近的边线 m_ipPointToEID.GetNearestEdge(ipEdgePoint, out intEdgeI D,out ipFoundEdgePoint, out dblEdgePercent); ipNetElements.QueryIDs( intEdgeID, esriElementType.esri ETEdge, out intEdgeUserClassID, out intEdgeUserID,out intEdgeUserS ubID); erClassID = intEdgeUserClassID; erID = intEdgeUserID; erSubID = intEdgeUserSubID; IEdgeFlag pTemp = (IEdgeFlag)(ipNetFlag as IEdgeFlag); pEdgeFlagList[i]=pTemp; } ipTraceFlowSolver.PutEdgeOrigins(ref pEdgeFlagList); INetSchema ipNetSchema = ipNetwork as INetSchema; INetWeight ipNetWeight = ipNetSchema.get_WeightByName(Wei
private IPolyline
m_ipPolyline;
#region Public Function
//返回和设置当前地图
public IMap SetOrGetMap
{
set{ m_ipMap = value;}
get{return
m_ipMap;}
}
//打开几何数据集的网络工作空间
public void OpenFeatureDatasetNetwork(IFeatureDataset Feature
INetSolver ipNetSolver = ipTraceFlowSolver as INetSolver; INetwork ipNetwork = m_work; ipNetSolver.SourceNetwork = ipNetwork; INetElements ipNetElements = ipNetwork as INetElements; int intCount = m_ipPoints.PointCount; //定义一个边线旗数组 IEdgeFlag[] pEdgeFlagList = new EdgeFlagClass[intCount]; for(int i = 0;i<intCount ;i++) {
ArcEngine 最短路径分析(源码)
using System; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using workAnalysis;
ghtName);
INetSolverWeights ipNetSolverWeights =olverWeights;
ipNetSolverWeights.FromToEdgeWeight = ipNetWeight;//开始 边线的权重
ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight;//终止 边线的权重
namespace GisEditor
{
/// <summary>
/// 最短路径分析
/// </summary>