-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_maker.py
64 lines (54 loc) · 2.02 KB
/
model_maker.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
import googlemaps
from intersections import find_intersections
key = "AIzaSyCaHJkj5Tzx7gMXDX-M9cUBOw_b3zlgoLw"
def start(file):
intersections = find_intersections(file)
client = googlemaps.Client(key)
ids = []
coords = []
with open('distances.txt', 'w') as f:
f.write('Distance matrix \n')
counter = 1
for id, (x,y) in intersections.items():
print ('Calculating row for: ('+str(counter)+')')
counter += 1
print (id)
ids += [id]
coords += [(x,y)]
row = googlemaps.distance_matrix.distance_matrix(client, [(x,y)], coords)
print ('Received '+str(row))
with open('distances.txt', 'a') as f:
f.write(str(id) + '\t' + str(row))
def resume(file, intersection):
intersections = find_intersections(file)
client = googlemaps.Client(key)
ids = []
coords = []
counter = 1
found = False
for id, (x,y) in intersections.items():
print ('Calculating row for: ('+str(counter)+')')
counter += 1
if not found and id != intersection:
print ('Skipping '+str(counter-1))
continue
elif id == intersection:
print ('Skipping '+str(counter-1))
found = True
continue
print (id)
ids += [id]
coords += [(x,y)]
row = googlemaps.distance_matrix.distance_matrix(client, [(x,y)], coords)
with open('distances.txt', 'a') as f:
f.write(str(id) + '\t' + str(row))
import argparse
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Construct the distance matrix to model distances between bus stops')
parser.add_argument('file', help='The OSM file')
parser.add_argument('--resume', '-r', type=int, help='Resume construction from a certain intersection (input the last id available in the distances.txt file)')
args = parser.parse_args()
if args.resume:
resume(args.file, args.resume)
else :
start(args.file)