-
Notifications
You must be signed in to change notification settings - Fork 5
/
02_get_geocodes.py
35 lines (26 loc) · 860 Bytes
/
02_get_geocodes.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
import json
import requests
def geocode(address):
print(address)
response = requests.get('https://maps.googleapis.com/maps/api/geocode/json', params=dict(address=address))
results = response.json()['results']
if len(results) < 1:
partial = address.rsplit(' ', 1)[0]
if partial == address:
raise ValueError('can not geocode')
return geocode(partial)
location = results[0]['geometry']['location']
return location['lat'], location['lng']
# TODO : normalize address
codes = {}
for line in json.load(open('data/data.json')):
address = line[3]
if address not in codes:
try:
code = geocode(address)
except ValueError as e:
print(e)
code = None
codes[address] = code
print(codes)
json.dump(codes, open('data/geocodes.json', 'w+'))