-
Notifications
You must be signed in to change notification settings - Fork 20
/
route.py
150 lines (134 loc) · 4.55 KB
/
route.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
#----------------------------------------------------------------
# Routing for OSM data
#
#------------------------------------------------------
# Usage as library:
# datastore = loadOsm('transport type')
# router = Router(datastore)
# result, route = router.doRoute(node1, node2)
#
# (where transport is cycle, foot, car, etc...)
#------------------------------------------------------
# Copyright 2007-2008, Oliver White
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------
import sys
import math
from loadOsm import LoadOsm
class Router(object):
def __init__(self, data):
self.data = data
def distance(self,n1,n2):
"""Calculate distance between two nodes"""
lat1 = self.data.rnodes[n1][0]
lon1 = self.data.rnodes[n1][1]
lat2 = self.data.rnodes[n2][0]
lon2 = self.data.rnodes[n2][1]
# TODO: projection issues
dlat = lat2 - lat1
dlon = lon2 - lon1
dist2 = dlat * dlat + dlon * dlon
dist = math.sqrt(dist2)
return(dist)
def doRoute(self,start,end):
"""Do the routing"""
self.searchEnd = end
closed = [start]
self.queue = []
# Start by queueing all outbound links from the start node
blankQueueItem = {'end':-1,'distance':0,'nodes':str(start)}
try:
for i, weight in self.data.routing[start].items():
self.addToQueue(start,i, blankQueueItem, weight)
except KeyError:
return('no_such_node',[])
# Limit for how long it will search
count = 0
while count < 1000000:
count = count + 1
try:
nextItem = self.queue.pop(0)
except IndexError:
# Queue is empty: failed
# TODO: return partial route?
return('no_route',[])
x = nextItem['end']
if x in closed:
continue
if x == end:
# Found the end node - success
routeNodes = [int(i) for i in nextItem['nodes'].split(",")]
return('success', routeNodes)
closed.append(x)
try:
for i, weight in self.data.routing[x].items():
if not i in closed:
self.addToQueue(x,i,nextItem, weight)
except KeyError:
pass
else:
return('gave_up',[])
def addToQueue(self,start,end, queueSoFar, weight = 1):
"""Add another potential route to the queue"""
# getArea() checks that map data is available around the end-point,
# and downloads it if necessary
#
# TODO: we could reduce downloads even more by only getting data around
# the tip of the route, rather than around all nodes linked from the tip
end_pos = self.data.rnodes[end]
self.data.getArea(end_pos[0], end_pos[1])
# If already in queue, ignore
for test in self.queue:
if test['end'] == end:
return
distance = self.distance(start, end)
if(weight == 0):
return
distance = distance / weight
# Create a hash for all the route's attributes
distanceSoFar = queueSoFar['distance']
queueItem = { \
'distance': distanceSoFar + distance,
'maxdistance': distanceSoFar + self.distance(end, self.searchEnd),
'nodes': queueSoFar['nodes'] + "," + str(end),
'end': end}
# Try to insert, keeping the queue ordered by decreasing worst-case distance
count = 0
for test in self.queue:
if test['maxdistance'] > queueItem['maxdistance']:
self.queue.insert(count,queueItem)
break
count = count + 1
else:
self.queue.append(queueItem)
if __name__ == "__main__":
# Test suite - do a little bit of easy routing in birmingham
data = LoadOsm("cycle")
node1 = data.findNode(52.552394,-1.818763)
node2 = data.findNode(52.563368,-1.818291)
print(node1)
print(node2)
router = Router(data)
result, route = router.doRoute(node1, node2)
if result == 'success':
# list the nodes
print(route)
# list the lat/long
for i in route:
node = data.rnodes[i]
print("%d: %f,%f" % (i,node[0],node[1]))
else:
print("Failed (%s)" % result)