-
Notifications
You must be signed in to change notification settings - Fork 2
/
routing.py
298 lines (246 loc) · 11.8 KB
/
routing.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import requests
import googlemaps
from datetime import datetime
from json.decoder import JSONDecodeError
import json
from random import *
class routing:
def __init__(self):
self.lat = 0
self.lat = 0
self.possible = []
response = requests.post(url="https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
for key in response.json():
if key == "location":
for key2 in response.json()[key]:
if key2 == "lat":
self.lat = response.json()[key][key2]
if key2 == "lng":
self.long = response.json()[key][key2]
self.loc = (self.lat, self.long)
def possible_places(self):
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=cafe&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
cafe = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in cafe:
if key1 == "results":
# print(key1)
for item in cafe[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=restaurant&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
restaurants = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in restaurants:
if key1 == "results":
# print(key1)
for item in restaurants[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=bakery&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
bakery = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in bakery:
if key1 == "results":
# print(key1)
for item in bakery[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=bar&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
bar = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in bar:
if key1 == "results":
# print(key1)
for item in bar[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=parking&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
parking = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in parking:
if key1 == "results":
# print(key1)
for item in parking[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=taxi_stand&radius=500&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
taxi_stand = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in taxi_stand:
if key1 == "results":
# print(key1)
for item in taxi_stand[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=gas_station&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
gas_station = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in gas_station:
if key1 == "results":
# print(key1)
for item in gas_station[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=rv_park&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
rv_park = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in rv_park:
if key1 == "results":
# print(key1)
for item in rv_park[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=lodging&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
lodging = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in lodging:
if key1 == "results":
# print(key1)
for item in lodging[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=mosque&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
mosque = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in mosque:
if key1 == "results":
# print(key1)
for item in mosque[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=hindu_temple&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
hindu_temple = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in hindu_temple:
if key1 == "results":
# print(key1)
for item in hindu_temple[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
response = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + str(self.lat) +","+ str(self.long) +"&type=church&radius=1000&key=AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE")
# print(response.status_code)
# print("")
# print("")
church = response.json()
# print(json.dumps(response.json(), sort_keys=True, indent=4))
for key1 in church:
if key1 == "results":
# print(key1)
for item in church[key1]:
# print(isinstance(item, dict))
for key in item:
if key == "name":
self.possible.append(item[key])
def chooser(self):
ra_idx = randint(0, len(self.possible))
return self.possible[ra_idx]
def routes(self, origin, destination):
gmaps = googlemaps.Client(key='AIzaSyD8np92Z7kxjUMm9jSFE2Y97SzB0QCToUE')
# 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((self.lat, self.long))
# Request directions via public transit
now = datetime.now()
directions_result = gmaps.directions(origin,
destination,
mode="driving",
departure_time=now)
# print(json.dumps(directions_result, sort_keys=True, indent=4))
for item in directions_result:
for key in item:
if key == "legs":
for item1 in item[key]:
for key2 in item1:
if key2 == "steps":
for item2 in item1[key2]:
for key3 in item2:
if key3 == "html_instructions":
print(item2[key3])
""" if __name__ == "__main__":
#test = routing()
#test.possible_places()
#type(test.possible_places())
#start = "1426 Bishop Street, Montreal, Canada"
#end = "Bahen Center for Information Technology, Toronto, Canada"
print("--------------------------------------------------1----------------------------------------------------------")
print("------------------------------------------------------------------------------------------------------------")
#test.routes(start, end)
route = routing()
places = route.possible_places()
choice = route.chooser()
print(choice)
start = route.loc
print(route.loc)
end = choice
print("---------------------------------------------------2---------------------------------------------------------")
print("------------------------------------------------------------------------------------------------------------")
instructions = route.routes(start, end) """