-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (53 loc) · 2.25 KB
/
main.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
from geopy.geocoders import Nominatim
import os
import json
import time
COUNTRY_KEY = 'country'
COUNTRY_CODE_KEY = 'country_code'
ADDRESS_KEY = 'address'
NAME_KEY = 'name'
def monkey_patch_geopy_base(instance, lang='en'):
instance.headers['Accept-Language'] = lang
def main(db_path='../BetaLibrary/', is_update=True):
# walk all data
areas = next(os.walk(db_path + 'data/zones/'))[1]
country_dict = {}
for area in areas:
time.sleep(1) # rate limiter (as per Geopy's documentation)
# Load zone data
datafile = db_path + 'data/zones/' + area + '/' + area + '.json'
with open(datafile, encoding='utf-8') as data:
area_data = json.load(data)
if is_update and area_data.get(COUNTRY_KEY, ''):
print(f'Skipping update for {area_data[NAME_KEY]}')
continue
geolocator = Nominatim(user_agent='[email protected]')
# Patch geopy to return countries in English
monkey_patch_geopy_base(geolocator, lang='en')
location = geolocator.reverse(f"{area_data['latitude']}, {area_data['longitude']}")
country = location.raw[ADDRESS_KEY][COUNTRY_CODE_KEY]
# print(location.raw)
print(area_data[NAME_KEY], country)
# print(location.address)
area_data[COUNTRY_KEY] = country
with open(datafile, 'w', encoding='utf-8') as data:
json.dump(area_data, data, indent=4)
def update_single_zone(area, db_path='../BetaLibrary/'):
datafile = db_path + 'data/zones/' + area + '/' + area + '.json'
with open(datafile, encoding='utf-8') as data:
area_data = json.load(data)
geolocator = Nominatim(user_agent='[email protected]')
# Patch geopy to return countries in English
monkey_patch_geopy_base(geolocator, lang='en')
location = geolocator.reverse(f"{area_data['latitude']}, {area_data['longitude']}")
print(location)
country = location.raw[ADDRESS_KEY][COUNTRY_CODE_KEY]
# print(location.raw)
print(area_data[NAME_KEY], country)
# print(location.address)
area_data[COUNTRY_KEY] = country
with open(datafile, 'w', encoding='utf-8') as data:
json.dump(area_data, data, indent=4)
if __name__ == "__main__":
# main(is_update=True)
update_single_zone('ubatuba')