-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEOR_city_enrichment.py
72 lines (57 loc) · 3.55 KB
/
EOR_city_enrichment.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
63
64
65
66
67
68
69
70
71
72
import json
import requests
with open("userinfo.txt", "r") as fuser:
username = fuser.readline().strip()
with open('datasets/original_ukrainian_geoname_uri_mappings.json', 'r') as original_ukrainian_cities:
original_geoname_uri_mappings = json.load(original_ukrainian_cities)
with open('datasets/extended-ukrainian-geoname-uri-mappings.json', 'r', encoding='utf-8') as extended_ukrainian_cities:
extended_geoname_uri_mappings = json.load(extended_ukrainian_cities)
original_geoname_uri_mappings.update(extended_geoname_uri_mappings)
geoname_uri_mappings = original_geoname_uri_mappings
with open("datasets/EOR-2023-04-30.geojson") as fjson:
data = json.load(fjson)
# Load existing results from the file
with open("datasets/city_coordinates.json") as fresult:
existing_results = json.load(fresult)
result = [] # Store the city data with coordinates
i = 0
for feature in data['features']:
if feature['geometry'].get('coordinates'):
lng, lat = feature["geometry"]["coordinates"]
new_coordinates = [lng, lat]
# Check if coordinates already exist in the existing results
if any(d["coordinates"] == new_coordinates for d in existing_results):
print('I am already there') # Skip coordinates if already present in existing results
else:
if feature["properties"].get("city"):
city_name = feature['properties']['city']
if city_name not in geoname_uri_mappings:
geonames_url = f'http://api.geonames.org/findNearbyPlaceNameJSON?lat={lat}&lng={lng}&username={username}'
#request = requests.get(geonames_url)
response = requests.get(geonames_url).json()
print(response)
if i < 200:
if 'name' in response['geonames'][0] and 'geonameId' in response['geonames'][0]:
city_name = response['geonames'][0]['name']
geoname_id = response['geonames'][0]['geonameId']
result.append({"city": city_name, "coordinates": [lng, lat], "URI": f'http://sws.geonames.org/{geoname_id}/' })
i += 1
updated_results = existing_results + result
# Save the result to a new JSON file
with open("datasets/city_coordinates.json", "w") as fresult:
json.dump(updated_results, fresult)
if 'city' not in feature['properties']:
geonames_url = f'http://api.geonames.org/findNearbyPlaceNameJSON?lat={lat}&lng={lng}&username={username}'
#request = requests.get(geonames_url)
response = requests.get(geonames_url).json()
print(response)
if i < 200:
if 'name' in response['geonames'][0] and 'geonameId' in response['geonames'][0]:
city_name = response['geonames'][0]['name']
geoname_id = response['geonames'][0]['geonameId']
result.append({"city": city_name, "coordinates": [lng, lat], "URI": f'http://sws.geonames.org/{geoname_id}/' })
i += 1
updated_results = existing_results + result
# Save the result to a new JSON file
with open("datasets/city_coordinates.json", "w") as fresult:
json.dump(updated_results, fresult)