-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter_cities.py
61 lines (45 loc) · 1.89 KB
/
filter_cities.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
import json, os
# Filtering worldwide cities to get relevant NE Scotland ones
# List from here http://bulk.openweathermap.org/sample/
# So that we can run this api.openweathermap.org/data/2.5/weather?id=2172797 to get local weather
file_path = "reference_data/"
target_file = "city.list.json"
def load_data():
base_folder = os.path.dirname(__file__)
file_p = os.path.join(base_folder, 'data', 'reference_data', 'city.list.json')
with open(file_p, 'r') as fp:
city_dict = json.load(fp)
return city_dict
def filter_NE_only(city_list):
short_list = []
for city in city_list:
GB = False
NS = False
EW = False
no_walton = False
for key, val in city.items(): #outer dictionary
if key == "country" and val == "GB":
GB = True # we are only interested in GB places (no point testing lat long of whole world)
if key=="name" and val != "Walton":
no_walton = True # needed because there is a fictional town "Walton" in the data
if key == "coord":
for k, v in val.items(): #inner dictionary
# equivalent of our bounding box
if k == "lon" and float(v) > -2.78633 and float(v) < 1.9112:
EW = True
if k == "lat" and float(v) > 56.955 and float(v) < 57.6875:
NS = True
if GB and NS and EW and no_walton:
short_list.append(city)
return short_list
def write_out(in_list):
base_folder = os.path.dirname(__file__)
out_p = os.path.join(base_folder, 'data', 'reference_data', 'ne_towns.json')
with open(out_p, 'w') as json_file:
json.dump(in_list, json_file, indent = 4)
def main():
city_data = load_data()
ne_locations = filter_NE_only(city_data)
write_out(ne_locations)
if __name__ == '__main__':
main()