-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_directions_api.py
139 lines (105 loc) · 3.9 KB
/
google_directions_api.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
import googlemaps
import os
# Get environment variables
api_key = os.getenv('GOOGLE_API')
# with open('secret.txt', 'r') as file:
# api_key = file.read()
gmaps = googlemaps.Client(key=api_key)
# # Geocoding an address
# geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
#
# # Look up an address with reverse geocoding
# reverse_geocode_result = gmaps.reverse_geocode((40.714224, -73.961452))
def do_the_thing(origin, destination, big_dic):
directions_result = gmaps.distance_matrix(origin, destination, mode="driving")
for idx, thing in enumerate(directions_result['rows']):
for idx2, d in enumerate(thing['elements']):
# print(origin[idx], '->', destination[idx2], d['distance']['value'])
big_dic[origin[idx] + '|' + destination[idx2]] = d['distance']['value']
# print('-' * 50)
return big_dic
# Request directions via public transit
lis = ["Hewlett, NY", "Lynbrook, NY", "Long Beach, NY", 'Valley Stream, NY', "Lawrence, Ny", 'Island park, ny',
'far rockaway, ny', 'Inwood, NY', 'Woodmere, Ny', 'oceanside, ny', 'Rockville center, ny', 'atlantic beach, ny']
# now = datetime.now()
def make_distance_matrix(lis):
if len(lis) > 20:
print('Too many locations')
return {}
elif len(lis) > 10:
big_dic = {}
# quad 1
big_dic = do_the_thing(lis[:10], lis[:10], big_dic)
big_dic = do_the_thing(lis[:10], lis[10:], big_dic)
big_dic = do_the_thing(lis[10:], lis[:10], big_dic)
big_dic = do_the_thing(lis[10:], lis[10:], big_dic)
else:
# directions_result = gmaps.distance_matrix(lis,lis,mode="driving")
big_dic = {}
# quad 1
big_dic = do_the_thing(lis, lis, big_dic)
# print(len(big_dic))
return big_dic
# departure_time=now)
# print(directions_result)
# print('+'*50)
# print(big_dic)
# big_dic = parse_results(directions_result, {})
# big_dic = (directions_result, {})
import math
def get_route(master_arr, list):
if master_arr == {}:
return []
start = list[0]
master_unvisited = list.copy()
best_dist = -1
# try from every start
for i in range(len(master_unvisited)):
unvisited = master_unvisited.copy()
visited = []
u = unvisited.pop(i)
visited.append(u)
while len(unvisited) != 0:
min_distance = -1
for idx, cord in enumerate(unvisited):
key = u + '|' + unvisited[idx]
dist = master_arr[key]
if min_distance == -1:
min_distance = dist
best_node = idx
elif min_distance > dist:
min_distance = dist
best_node = idx
u = unvisited.pop(best_node)
visited.append(u)
# calculate total distance
tot_dist = 0
for idx in range(len(visited) - 1):
key = visited[idx] + '|' + visited[idx + 1]
dist = master_arr[key]
tot_dist += dist
# way back like james (way back home counts
key = visited[0] + "|" + visited[-1]
dist = master_arr[key]
tot_dist += dist
# finish tot_dist calc
#print(visited, tot_dist)
if best_dist > tot_dist or best_dist == -1:
best_dist = tot_dist
best_visited = visited.copy()
s_idx = best_visited.index(start)
out_route = best_visited[s_idx:] + best_visited[:s_idx]
out_route = out_route[::-1]
#print(out_route, best_dist)
# print(master_arr.items())
return out_route
def one_shot_url(list_of_addys):
big_dic = make_distance_matrix(list_of_addys)
addy_list = get_route(big_dic, list_of_addys)
return addy_list
# base_url = 'https://www.google.com/maps/dir/'
# url = ''
# for addy in addy_list:
# url += "+".join(addy.split(' ')) + '/'
# return base_url + url
#print(one_shot_url(lis))