-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmaps.py
44 lines (35 loc) · 1.41 KB
/
gmaps.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
import json, requests, os, time
lines = []
with open("pubs.csv", "r") as f:
for l in f.readlines():
lines.append(l.split(",")[0])
pubs = [l.replace("'", "").replace("\"", "").replace(" ", "+") + "+Oxford+UK" for l in lines]
pubDicts = {}
for pub in pubs:
pubDicts[pub] = {}
addresses = set()
most = 5
for row in range(0, len(pubs), most):
for col in range(0, len(pubs), most):
origins = pubs[row:row+most]
dests = pubs[col:col+most]
url = "https://maps.googleapis.com/maps/api/distancematrix/json?"
url += "key=" + os.environ['GMAPS_KEY'] + "&"
url += "mode=walking&"
url += "origins=" + "|".join(origins) + "&"
url += "destinations=" + "|".join(dests)
r = requests.get(url)
j = r.json()
with open("{},{}.json".format(row, col), "w") as f:
f.write(json.dumps(j, sort_keys=True, indent=4))
rows = j["rows"]
for r in range(0, len(rows)):
for c in range(0, len(rows[r]["elements"])):
if "distance" in rows[r]["elements"][c]:
pubDicts[origins[r]][dests[c]] = rows[r]["elements"][c]["distance"]["value"]
else:
print("Error on {},{}".format(pubs[row + r], pubs[col + c]))
time.sleep(1) # API rate limiting
data = json.dumps(pubDicts, sort_keys=True, indent=4)
with open("distances.json", "w") as f:
f.write(data)