forked from DLu/ros_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_location.py
53 lines (40 loc) · 1.48 KB
/
add_location.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
from geopy.geocoders import get_geocoder_for_service
import yaml
from location_data import datafiles
def choose(elements, prompt='> '):
try:
while True:
for i, element in enumerate(elements):
print(f'{i}) {element}')
choice = input(prompt)
try:
index = int(choice)
return elements[index]
except (ValueError, IndexError):
pass
except (KeyboardInterrupt, EOFError):
exit(0)
if __name__ == '__main__':
entry = {}
entry['name'] = input('Name> ')
entry['type'] = choose(['school', 'company', 'research institute', 'other'], 'Type> ')
address = input('Address> ')
geolocator = get_geocoder_for_service('photon')()
location = geolocator.geocode(address, timeout=15)
if location is None:
print('Geocoder failed')
exit()
entry['lat'] = location.latitude
entry['long'] = location.longitude
datafile = choose(datafiles, 'Chose a datafile> ')
include_address = choose(['yes', 'no'], 'Include address in entry?> ')
if include_address == 'yes':
entry['address'] = address
link = input('URL Link (optional)> ')
if link:
entry['link'] = link
full_filename = f'data/{datafile}.yaml'
data = yaml.safe_load(open(full_filename))
data.append(entry)
data = sorted(data, key=lambda d: d['name'])
yaml.dump(data, open(full_filename, 'w'), allow_unicode=True, default_flow_style=False)