-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-map.py
86 lines (68 loc) · 2.45 KB
/
generate-map.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import json
import sqlite3
import sys
from geopy.geocoders import Nominatim
data = []
geolocator = Nominatim(timeout=10, user_agent='Opencast map generator')
def get_user_info(database):
query = '''
select organisation_name, department_name,
country, postal_code, city, street, street_no
from adopter
where organisation_name != ''
'''
cur = sqlite3.connect(database).cursor()
cur.execute(query)
for adopter_data in cur.fetchall():
yield get_geo_info(*adopter_data)
def get_geo_info(organization, department, country, zipcode, city, street,
street_no):
print(f'Requesting: {organization}, {department}'.strip(' ,'))
addresses = [
f'{organization}, {street} {street_no} {country} {zipcode} {city}',
f'{street} {street_no}, {country} {zipcode} {city}',
f'{country} {zipcode} {city}',
f'{organization}'
]
for address in addresses:
address = address.strip(' ,')
if not address:
continue
print(f' Address: {address}')
location = geolocator.geocode(address, addressdetails=True)
print(f' Location: {location}')
if location:
return {'country': country, 'city': city,
'organization': organization,
'department': department,
'latitude': location.latitude,
'longitude': location.longitude}
def convert_geo_json(addresses):
features = []
for address in addresses:
if address:
feature = {
'type': 'Feature',
'properties': {
'institution': address['organization']
},
'geometry': {
'type': 'Point',
'coordinates': [address['longitude'], address['latitude']]
}}
if address['department']:
feature['properties']['department'] = address['department']
features.append(feature)
return {'type': 'FeatureCollection', 'features': features}
def main():
if len(sys.argv) != 3:
print('%s DATABASE OUTPUT' % sys.argv[0])
sys.exit(1)
database = sys.argv[1]
outfile = sys.argv[2]
geo_data = convert_geo_json(get_user_info(database))
with open(outfile, 'w') as f:
f.write(json.dumps(geo_data))
if __name__ == '__main__':
main()