This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BusStops.py
84 lines (59 loc) · 2.62 KB
/
BusStops.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
import osmread
import numpy as np
import itertools
import xml.etree.ElementTree as ET
def getNodeDist(startNode, endNode):
""" Returns the as-the-crow-flies distance between two nodes """
return np.sqrt((startNode.lat - endNode.lat)**2 + (startNode.lon - endNode.lon)**2)
def getNodes(latStart, longStart, latEnd, longEnd, mapData, tolerance):
""" Take the input starting latitude and get a nearby associated node """
it1, it2 = itertools.tee(mapData, 2) # Allows you to iterate over the generator (mapData) several times
entities1 = []
for entity in it1:
entities1.append(entity)
entities2 = []
for entity in it2:
entities2.append(entity)
print("Getting start node...")
shortestDistance = 100
for entity in entities1:
if isinstance(entity, osmread.Node):
distance = np.sqrt((latStart - entity.lat)**2 + (longStart - entity.lon)**2)
if distance < shortestDistance:
shortestDistance = distance
startNode = entity
print("Getting end node...")
shortestDistance = 100
for entity in entities2:
if isinstance(entity, osmread.Node):
distance = np.sqrt((latEnd - entity.lat)**2 + (longEnd - entity.lon)**2)
if distance < shortestDistance:
shortestDistance = distance
endNode = entity
return startNode, endNode
def findNearestBusStop(inputNode):
""" Returns the nearest bus stop to the input node """
busStops = [] # Empty list to store nodes representing bus stops
mapData = osmread.parse_file("trimmed.osm")
for entity in mapData:
if isinstance(entity, osmread.Node) and 'highway' in entity.tags:
if entity.tags["highway"] == "bus_stop":
busStops.append(entity)
shortestDistance = 100
for busStop in busStops:
distance = getNodeDist(inputNode, busStop)
if distance < shortestDistance:
shortestDistance = distance
closestBusStop = busStop
return closestBusStop
def bus_find_wrapper(a,b):
nodea, nodeb = getNodes(a[0],a[1],b[0],b[1],"trimmed.osm",1e-5)
busa = findNearestBusStop(nodea)
busb = findNearestBusStop(nodeb)
dista = getNodeDist(nodea,busa)
distb = getNodeDist(nodeb,busb)
return dista,disb
if __name__ == "__main__":
mapData = osmread.parse_file("trimmed.osm")
startNode, endNode = getNodes(53.798395, -1.547876, 53.790613, -1.546279, mapData, 1e-5)
print(findNearestBusStop(startNode))