ArcGIS Network Analysis Shortest Path Analysis source code (VB6.0)

Source: Internet
Author: User
Tags assert exit count imap polyline ipoint

1
2 ' Copyright 1995-2005 ESRI
3
4 ' All rights reserved under the copyright laws of the United States.
5
6 ' may freely redistribute and use this sample code, with or without modification.
7
8 ' disclaimer:the SAMPLE CODE is provided ' as are ' and any EXPRESS OR implied
9 ' warranties, including the implied warranties of merchantability and FITNESS
' for A particular purpose ARE disclaimed. In NO EVENT SHALL ESRI OR
One ' CONTRIBUTORS be liable to any DIRECT, INDIRECT, incidental, SPECIAL, exemplary,
' OR consequential damages (including, BUT not LIMITED to, procurement of
' Substitute goods or SERVICES; LOSS of Use, DATA, OR profits; OR BUSINESS
Interruption) sustained by you OR A Third party, HOWEVER caused and on any
Theory of liability, WHETHER in CONTRACT, STRICT liability, OR TORT arising in
WAY out of the "use of" this SAMPLE CODE, even IF advised of the possibility of
' SUCH DAMAGE.
18
' For additional information contact:environmental Systems in Institute, Inc.
20
' Attn:contracts Dept.
22
380 New York Street
24
Redlands, California, U.S.A. 92373
26
' Email:contracts@esri.com
28
29Option Explicit
30
' VB version of the Pathfinder object
32
33 ' local variable
34Private M_ipgeometricnetwork as Esrigeodatabase.igeometricnetwork
35Private M_ipmap as Esricarto.imap
36Private M_ippoints as Esrigeometry.ipointcollection
37Private M_ippointtoeid as Esrinetworkanalysis.ipointtoeid
38 ' Returns the result variable
39Private M_dblpathcost as Double
40Private M_ipenumneteid_junctions as Esrigeodatabase.ienumneteid
41Private M_ipenumneteid_edges as Esrigeodatabase.ienumneteid
42Private M_ippolyline as Esrigeometry.ipolyline
43
44
Optionally set the map (e.g. the current map in ArcMap),
The ' otherwise a default map would be made (for Ipointtoeid).
47
48Public Property Set Map (map as Esricarto.imap)
Set M_ipmap = Map
50End Property
51
52Public Property Get Map () as Esricarto.imap
The Set Map = M_ipmap
54End Property
55
Either Openaccessnetwork or Openfeaturedatasetnetwork
needs to be called.
58
59Public Sub openaccessnetwork (accessfilename As String, Featuredatasetname as String)
60
Dim Ipworkspacefactory as Esrigeodatabase.iworkspacefactory
The Dim Ipworkspace as Esrigeodatabase.iworkspace
The Dim Ipfeatureworkspace as Esrigeodatabase.ifeatureworkspace
The Dim Ipfeaturedataset as Esrigeodatabase.ifeaturedataset
65
After this Sub exits, we'll have an Inetwork interface
The ' and an ' IMap interface initialized for the network we ' ll be using.
68
"Close down" the last one if opened
Closeworkspace
71
The ' open ' the MDB
The Set ipworkspacefactory = New esridatasourcesgdb.accessworkspacefactory
The Set ipworkspace = ipworkspacefactory.openfromfile (accessfilename, 0)
75
The ' Get the ' featureworkspace
Set Ipfeatureworkspace = Ipworkspace
78
The ' Open ' Featuredataset
The Set Ipfeaturedataset = Ipfeatureworkspace.openfeaturedataset (featuredatasetname)
81
Initialize Network and MAP (M_ipnetwork, M_ipmap)
If not Initializenetworkandmap (ipfeaturedataset) Then err.raise 0, "Openaccessnetwork", "Error initializing Network D Map "
84
85End Sub
86
87Public Sub openfeaturedatasetnetwork (featuredataset as Esrigeodatabase.ifeaturedataset)
"Close down" the last one if opened
Closeworkspace
90
Caller ' We assume that has passed a valid Featuredataset
92
Initialize Network and MAP (M_ipnetwork, M_ipmap)
If not Initializenetworkandmap (featuredataset) Then err.raise 0, "Openfeaturedatasetnetwork", "Error initializing Net Work and Map "
95
96End Sub
97
The collection of points to travel through must is set.
99
100Public Property Set stoppoints (Points as Esrigeometry.ipointcollection)
Set m_ippoints = Points
102End Property
103
104Public Property Get Stoppoints () as Esrigeometry.ipointcollection
The Set stoppoints = m_ippoints
106End Property
107
108 ' Calculate The path
109
110Public Sub Solvepath (weightname as String)
111
112 Dim Ipnetwork as Esrigeodatabase.inetwork
113 Dim Iptraceflowsolver as Esrinetworkanalysis.itraceflowsolver
114 Dim Ipnetsolver as Esrinetworkanalysis.inetsolver
Ipnetflag Dim as Esrinetworkanalysis.inetflag
116 Dim Ipanetflag () as Esrinetworkanalysis.iedgeflag
117 Dim Ipedgepoint as Esrigeometry.ipoint
118 Dim Ipnetelements as Esrigeodatabase.inetelements
119 Dim Intedgeuserclassid as Long
The Dim Intedgeuserid as Long
121 Dim Intedgeusersubid as Long
122 Dim Intedgeid as Long
123 Dim Ipfoundedgepoint as Esrigeometry.ipoint
124 Dim Dbledgepercent as Double
Ipnetweight Dim as Esrigeodatabase.inetweight
126 Dim Ipnetsolverweights as Esrinetworkanalysis.inetsolverweights
127 Dim Ipnetschema as Esrigeodatabase.inetschema
128 Dim intcount as Long
129 Dim I as Long
130 Dim Vares () as Variant
131
132 ' Make sure we are ready
M_ippoints Debug.Assert Not
134 Debug.Assert Not m_ipgeometricnetwork are nothing
135
136 ' Instantiate a trace flow solver
137 Set iptraceflowsolver = New esrinetworkanalysis.traceflowsolver
138
139 ' Get the Inetsolver interface
140 Set Ipnetsolver = Iptraceflowsolver
141
"Set" the source network to solve on
143 Set ipnetwork = m_ipgeometricnetwork.network
144 Set ipnetsolver.sourcenetwork = ipnetwork
145
146 ' Make edge flags from the points
147
148 ' The Inetelements interface is needed to get UserID, Userclassid,
149 ' and usersubid from a element ID
Set ipnetelements = ipnetwork
151
152 ' Get the Count
153 intcount = M_ippoints.pointcount
154 Debug.Assert intcount > 1
155
156 ' Dimension our Iedgeflag array
157 ReDim Ipanetflag (intcount)
158
159 for i = 0 to IntCount-1
160 ' Make a new Edge Flag
161 Set Ipnetflag = New Esrinetworkanalysis.edgeflag
162 Set Ipedgepoint = M_ippoints.point (i)
163 ' Look up to the EID for the "current" (This'll populate Intedgeid and dbledgepercent)
164 M_ippointtoeid.getnearestedge Ipedgepoint, Intedgeid, Ipfoundedgepoint, dbledgepercent
165 Debug.Assert Intedgeid > 0 ' else point (Eid) not found
166 Ipnetelements.queryids Intedgeid, Esrietedge, Intedgeuserclassid, Intedgeuserid, IntEdgeUserSubID
167 Debug.Assert (Intedgeuserclassid > 0) and (Intedgeuserid > 0) ' Else point not ' found
Ipnetflag.userclassid = Intedgeuserclassid
169 Ipnetflag.userid = Intedgeuserid
170 Ipnetflag.usersubid = Intedgeusersubid
171 Set Ipanetflag (i) = Ipnetflag
172 Next
173
174 ' Add these edge flags
175 Iptraceflowsolver.putedgeorigins intcount, Ipanetflag (0)
176
177 ' Set the weight (Cost field) to solve on
178
179 ' Get the Inetschema interface
180 Set Ipnetschema = Ipnetwork
181 Set ipnetweight = Ipnetschema.weightbyname (weightname)
Ipnetweight Debug.Assert Not
183
184 ' Set the weight (use of the same for both directions)
185 Set ipnetsolverweights = Iptraceflowsolver
186 Set Ipnetsolverweights.fromtoedgeweight = ipnetweight
187 Set Ipnetsolverweights.tofromedgeweight = ipnetweight
188
189 ' Initialize array for results to # of segments in
190 ReDim Vares (intCount-1)
191
The ' Solve it
193 Iptraceflowsolver.findpath esrifmconnected, Esrispobjfnminsum, M_ipenumneteid_junctions, M_ipEnumNetEID_Edges, IntCount-1, Vares (0)
194
195 ' compute Total cost
196 M_dblpathcost = 0
197 for i = LBound (Vares) to UBound (Vares)
198 M_dblpathcost = m_dblpathcost + vares (i)
199 Next
200
201 ' Clear the last polyline result
The Set m_ippolyline = Nothing
203
204End Sub
205
206 ' The Cost
207
208Public Property Get Pathcost () as Double
209 Pathcost = M_dblpathcost
210End Property
211
212 ' property to get the shape
213
214Public Property Get Pathpolyline () as Esrigeometry.ipolyline
215
216 Dim Ipeidhelper as Esrinetworkanalysis.ieidhelper
217 Dim count as long, I as long
218 Dim Ipeidinfo as Esrinetworkanalysis.ieidinfo
219 Dim Ipenumeidinfo as Esrinetworkanalysis.ienumeidinfo
Ipgeometry Dim as Esrigeometry.igeometry
221 Dim Ipnewgeometrycoll as Esrigeometry.igeometrycollection
222 Dim Ipspatialreference as Esrigeometry.ispatialreference
223
224 ' If is already computed since the last path, just return it
If not m_ippolyline are nothing Then
The Set pathpolyline = M_ippolyline
The Exit property
228 End If
229
230 Set m_ippolyline = New esrigeometry.polyline
231 Set Ipnewgeometrycoll = m_ippolyline
232
233 ' A path should be solved
234 Debug.Assert not m_ipenumneteid_edges are nothing
235
236 ' Make a Eidhelper object to translate edges to geometric features
237 Set ipeidhelper = New esrinetworkanalysis.eidhelper
238 Set ipeidhelper.geometricnetwork = m_ipgeometricnetwork
239 Set ipspatialreference = m_ipmap.spatialreference
The Set ipeidhelper.outputspatialreference = ipspatialreference
241 Ipeidhelper.returngeometries = True
242
243 ' Get the details using the Ieidhelper classes
244 Set ipenumeidinfo = Ipeidhelper.createenumeidinfo (m_ipenumneteid_edges)
245 count = Ipenumeidinfo.count
246
247 ' Set the iterator to beginning
248 Ipenumeidinfo.reset
249
i = 1 to Count
251
252 ' Get the next EID and a copy of it geometry (it makes a Clone)
253 Set ipeidinfo = Ipenumeidinfo.next
254 Set ipgeometry = ipeidinfo.geometry
255
256 Ipnewgeometrycoll.addgeometrycollection Ipgeometry
257
258 Next ' EID
259
The merged geometry as a polyline
261 Set pathpolyline = M_ippolyline
262
263End Property
264
265 ' Private
266
267Private Sub Closeworkspace ()
268 ' Make sure we have go to everything and start with new results
MB Set m_ipgeometricnetwork = Nothing
270 Set m_ippoints = Nothing
271 Set M_ippointtoeid = Nothing
272 Set m_ipenumneteid_junctions = Nothing
273 Set m_ipenumneteid_edges = Nothing
274 Set M_ippolyline = Nothing
275End Sub
276
277Private Function Initializenetworkandmap (Featuredataset as Esrigeodatabase.ifeaturedataset) as Boolean
278
279 Dim Ipnetworkcollection as Esrigeodatabase.inetworkcollection
280 Dim Ipnetwork as Esrigeodatabase.inetwork
281 Dim count As long, I as long
282 Dim Ipfeatureclasscontainer as Esrigeodatabase.ifeatureclasscontainer
283 Dim Ipfeatureclass as Esrigeodatabase.ifeatureclass
284 Dim Ipgeodataset as Esrigeodatabase.igeodataset
285 Dim IpLayer as Esricarto.ilayer
286 Dim Ipfeaturelayer as Esricarto.ifeaturelayer
287 Dim Ipenvelope as Esrigeometry.ienvelope, Ipmaxenvelope as Esrigeometry.ienvelope
The Dim Dblsearchtol as Double
289 Dim Dblwidth as double, dblheight as double
290
291 on Error GoTo trouble
292
293 ' Get the networks
294 Set ipnetworkcollection = Featuredataset
295
296 ' Even though a featuredataset can have many networks, we ' ll just
297 ' Assume the one (otherwise you would pass the network name in, etc.)
298
299 ' Get the count of networks
Count = Ipnetworkcollection.geometricnetworkcount
301
302 Debug.Assert Count > 0 ' then Exception.create (' No networks found ');
303
304 ' Get the geometric newtork (0-based)
305 Set m_ipgeometricnetwork = ipnetworkcollection.geometricnetwork (0)
306
307 ' Get the network
308 Set ipnetwork = M_ipgeometricnetwork.network
309
310 ' The EID Helper class that converts points to eids needs a
311 ' IMap, so we'll need one around with all our layers added.
312 ' This Pathfinder object has a optional Map property than May is set
313 ' before opening the network.
The M_ipmap If is nothing Then
315 Set M_ipmap = New Esricarto.map
316
317 ' Add Each of the Feature Classes in this geometric network as a map Layer
318 Set Ipfeatureclasscontainer = m_ipgeometricnetwork
319 Count = Ipfeatureclasscontainer.classcount
Debug.Assert Count > 0 ' then exception.create (' No (network) feature classes found ');
321
322 for i = 0 to Count-1
323 ' Get the feature class
324 Set Ipfeatureclass = Ipfeatureclasscontainer.class (i)
325 ' Make a layer
326 Set Ipfeaturelayer = New Esricarto.featurelayer
327 Set Ipfeaturelayer.featureclass = Ipfeatureclass
328 ' Add layer to the map
329 M_ipmap.addlayer Ipfeaturelayer
The Next
331 End If ' We needed to make a Map
332
333
334 ' Calculate Point snap tolerance as 1/100 of map width.
335 count = M_ipmap.layercount
336 Set ipmaxenvelope = New esrigeometry.envelope
337 for i = 0 to Count-1
338 Set IpLayer = M_ipmap.layer (i)
339 Set Ipfeaturelayer = IpLayer
Dimensions (for setting search tolerance)
341 Set Ipgeodataset = Ipfeaturelayer
342 Set Ipenvelope = ipgeodataset.extent
343 ' Merge with Max dimensions
344 ipmaxenvelope.union Ipenvelope
345 Next
346
347 ' Finally, we can set up the Ipointtoeid
348 Set M_ippointtoeid = New Esrinetworkanalysis.pointtoeid
349 Set M_ippointtoeid.sourcemap = M_ipmap
The Set m_ippointtoeid.geometricnetwork = m_ipgeometricnetwork
351
352 ' Set snap tolerance
353 Dblwidth = Ipmaxenvelope.width
354 Dblheight = Ipmaxenvelope.height
355
356 If dblwidth > Dblheight Then
357 Dblsearchtol = dblwidth/100#
358 Else
359 Dblsearchtol = dblheight/100#
360 End If
361
362 M_ippointtoeid.snaptolerance = Dblsearchtol
363
364 Initializenetworkandmap = True ' Good to go
365 Exit Function
366
367Trouble:
368 Initializenetworkandmap = False ' We had an error
369End Function
370

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.