-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseStations.py
72 lines (67 loc) · 2.37 KB
/
parseStations.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
__filename__ = "parseStations.py"
__author__ = "Bob Mottram"
__license__ = "GPL3+"
__version__ = "2.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Commandline Interface"
from grid import get_closest_grid_index
def load_station_locations(filename: str, grid: []) -> {}:
"""Loads station locations from inv file
"""
lines = []
try:
with open(filename, 'r', encoding='utf-8') as fp_loc:
lines = fp_loc.readlines()
except OSError:
print('Unable to open ' + filename)
return None
station_locations = {}
for line in lines:
line = line.strip()
if not line:
continue
if len(line) < 38:
continue
sid = line[:10]
latitude = float(line[11:19])
longitude = float(line[20:29])
altitude = float(line[30:37])
name = line[38:].lower().title()
grid_index = get_closest_grid_index(longitude, latitude, grid)
if sid not in grid[grid_index]['station_ids']:
grid[grid_index]['station_ids'].add(sid)
station_locations[sid] = {
'grid_index': grid_index,
'latitude': latitude,
'longitude': longitude,
'altitude': altitude,
'name': name
}
return station_locations
def save_station_locations_as_kml(station_locations: {},
filename: str) -> None:
"""Save station locations in KML format for visualization
"""
kml_str = \
"<?xml version=\"1.0\" encoding='UTF-8'?>\n" + \
"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" + \
"<Document>\n"
for _, item in station_locations.items():
kml_str += \
" <Placemark>\n" + \
" <name>" + str(item['name']) + "</name>\n" + \
" <description>" + str(item['latitude']) + \
' ' + str(item['longitude']) + '</description>\n' + \
" <Point>\n" + \
" <coordinates>" + str(item['longitude']) + "," + \
str(item['latitude']) + "," + str(item['altitude']) + \
"</coordinates>\n" + \
" </Point>\n" + \
" </Placemark>\n"
kml_str += \
"</Document>\n" + \
"</kml>\n"
with open(filename, 'w+', encoding='utf-8') as fp_kml:
fp_kml.write(kml_str)