-
Notifications
You must be signed in to change notification settings - Fork 0
/
suburbs.py
55 lines (45 loc) · 2.17 KB
/
suburbs.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
import googlemaps
import json
from datetime import datetime
def get_suburbs(source, destination):
gmaps = googlemaps.Client(key='AIzaSyB4ZJWZtLdjMPZtkUlCWbWGBNFvfuWq4dY')
# Request directions via public transit
now = datetime.now()
directions_result = gmaps.directions(source,
destination,
mode="driving",
alternatives=True,
departure_time=now)
all_routes = []
for d in directions_result:
suburb_result = []
for step in d["legs"][0]["steps"]:
start_lat = step["start_location"]["lat"]
start_lng = step["start_location"]["lng"]
end_lat = step["end_location"]["lat"]
end_lng = step["end_location"]["lat"]
reverse_start = gmaps.reverse_geocode((start_lat, start_lng))
res_street = list(filter(lambda a: "route" in a["types"], reverse_start[0]["address_components"]))
res_suburb = list(filter(lambda a: "locality" in a["types"], reverse_start[0]["address_components"]))
if res_street and res_suburb:
suburb_result.append(
{
"suburb" : res_suburb[0]["long_name"],
"street" : res_street[0]["long_name"]
}
)
reverse_end = gmaps.reverse_geocode((end_lat, end_lng))
res_street1 = list(filter(lambda a: "route" in a["types"], reverse_end[0]["address_components"]))
res_suburb1 = list(filter(lambda a: "locality" in a["types"], reverse_end[0]["address_components"]))
if res_street1 and res_suburb1:
suburb_result.append(
{
"suburb" : res_suburb1[0]["long_name"],
"street" : res_street1[0]["long_name"]
}
)
all_routes.append(suburb_result)
return all_routes
# get_suburbs("Chatswood", "Alex Ave, Schofields")
if __name__ == '__main__':
print(json.dumps(get_suburbs("Chatswood", "Alex Ave, Schofields")))