-
Notifications
You must be signed in to change notification settings - Fork 1
/
openseamap.py
184 lines (149 loc) · 7.47 KB
/
openseamap.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import argparse
import json
from typing import List, Tuple
import folium
import gpxpy
import gpxpy.gpx
import jinja2
import matplotlib.pyplot as plt
from jinja2 import Environment, FileSystemLoader
from utils import track_serializer
def parse_arguments():
parser = argparse.ArgumentParser(description="Animate GPX tracks on an OpenSeaMap.")
parser.add_argument('--files', nargs='+', required=True, help='GPX files to process')
parser.add_argument('--names', '-n', nargs='+', help='Names of the participants')
parser.add_argument('--max-speed', '-ms', type=float, default=12, help='Maximum speed in knots (default: 12)')
parser.add_argument('--title', '-t', help='The title of the page')
return parser.parse_args()
def parse_gpx(file_path: str) -> List[dict]:
with open(file_path, 'r') as gpx_file:
gpx = gpxpy.parse(gpx_file)
all_tracks = []
for track in gpx.tracks:
points = [
{'lat': point.latitude, 'lon': point.longitude, 'time': point.time}
for segment in track.segments
for point in segment.points
]
all_tracks.append({
'name': track.name,
'description': track.description,
'points': points,
})
return all_tracks
def calculate_speeds(points: List[dict], max_speed: float) -> List[float]:
"""
Calculates the speed of each point in the list of points.
The `max_speed` is used to control the dirty data: if the speed is larger
than some reasonable value (max_speed), then usually this means zero division,
that's we simply nullify the speed.
"""
speeds = []
for i in range(1, len(points)):
lat1, lon1, time1 = points[i - 1].values()
lat2, lon2, time2 = points[i].values()
distance = gpxpy.geo.haversine_distance(lat1, lon1, lat2, lon2)
time_diff = (time2 - time1).total_seconds()
if time_diff > 0:
speed = (distance / time_diff) * 1.94384 # Convert m/s to knots
if speed > max_speed:
print(f"Warning: speed {speed:.2f} exceeds {max_speed} kn at time {time1} (dt = {time_diff:.2f}s)")
speed = 0
speeds.append(speed)
else:
speeds.append(0)
return speeds
def speed_to_color(speed: float, max_speed: float) -> str:
norm_speed = min(speed / max_speed, 1.0)
color = plt.cm.RdYlGn(norm_speed)
return f"#{int(color[0] * 255):x}{int(color[1] * 255):x}{int(color[2] * 255):x}"
def create_map(gpx_files: List[str], names: List[str], max_speed: float) -> Tuple[folium.Map, List[List], float, str]:
folium_map = folium.Map(location=[0, 0], zoom_start=12, control_scale=True, attributionControl=False, tiles=None)
map_id = folium_map.get_name()
folium.TileLayer('openstreetmap', control=False).add_to(folium_map)
# Add OpenSeaMap layer directly to the map, not to the layer control
folium.TileLayer(
tiles='https://tiles.openseamap.org/seamark/{z}/{x}/{y}.png',
attr='OpenSeaMap',
overlay=False,
control=False, # Set control to `False` to exclude from layer control
).add_to(folium_map)
all_tracks = []
original_names = []
for gpx_file in gpx_files:
for track in parse_gpx(gpx_file):
points = track['points']
speeds = calculate_speeds(points, max_speed)
all_tracks.append(list(zip(points, speeds)))
original_names.append(track['name'])
max_speed = max(s for track in all_tracks for _, s in track)
# Calculate map bounds
latitudes = [p['lat'] for track in all_tracks for p, _ in track]
longitudes = [p['lon'] for track in all_tracks for p, _ in track]
folium_map.fit_bounds([[min(latitudes), min(longitudes)], [max(latitudes), max(longitudes)]])
track_layers = []
colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown', 'pink', 'yellow', 'cyan', 'magenta']
for i, track in enumerate(all_tracks):
color = colors[i % len(colors)]
lat_lon = [(p['lat'], p['lon']) for p, _ in track]
speeds = [s for _, s in track]
times = [p['time'].strftime('%Y-%m-%d %H:%M:%S') for p, _ in track]
name = names[i] if names and i < len(names) else original_names[i]
track_layer = folium.FeatureGroup(name=f"<span style='color:{color};'>●</span> {name}", show=True)
for j in range(len(lat_lon) - 1):
color = speed_to_color(speeds[j], max_speed)
tooltip_content = f"Name: {name}<br>Time: {times[j]} UTC<br>Speed: {speeds[j]:.2f} knots"
folium.PolyLine(
lat_lon[j:j + 2],
color=color,
weight=2.5,
opacity=1,
tooltip=folium.Tooltip(tooltip_content)
).add_to(track_layer)
track_layers.append(track_layer)
folium_map.add_child(track_layer)
folium.LayerControl(collapsed=False).add_to(folium_map)
return folium_map, all_tracks, max_speed, map_id
def add_animation(folium_map: folium.Map,
all_tracks: List[List],
jinja_env: jinja2.Environment,
title: str, map_id:str) -> None:
gpx_points_data = [[point[0] for point in track] for track in all_tracks]
gpx_timestamps = sorted(set(point[0]['time'] for track in all_tracks for point in track))
min_time, max_time = min(gpx_timestamps), max(gpx_timestamps)
time_range = (max_time - min_time).total_seconds()
animation_script = f"""
<script>
var gpx_points_data = {json.dumps(gpx_points_data, default=track_serializer)};
var gpx_timestamps = {json.dumps([t for t in gpx_timestamps], default=track_serializer)};
var min_time = new Date('{min_time.strftime('%Y-%m-%dT%H:%M:%S%z')}').getTime();
var max_time = new Date('{max_time.strftime('%Y-%m-%dT%H:%M:%S%z')}').getTime();
var time_range = {time_range};
var map_id = "{map_id}";
document.title = "{title if title else 'GPX Player'}";
</script>
<script>
{open('animate_tracks.js', encoding='UTF-8').read()}
</script>
"""
folium_map.get_root().html.add_child(folium.Element(animation_script))
if title:
template = jinja_env.get_template('header_template.html')
header_html = template.render(title=title)
folium_map.get_root().html.add_child(folium.Element(header_html))
def add_legend(folium_map: folium.Map, max_speed: float, jinja_env: jinja2.Environment) -> None:
template = jinja_env.get_template('speed_legend_template.html')
legend_html = template.render(max_speed=max_speed)
folium_map.get_root().html.add_child(folium.Element(legend_html))
def main():
args = parse_arguments()
gpx_files = args.files
names = args.names
env = Environment(loader=FileSystemLoader('.'))
folium_map, all_tracks, max_speed, map_id = create_map(gpx_files, names, args.max_speed)
add_animation(folium_map, all_tracks, env, args.title, map_id)
add_legend(folium_map, max_speed, env)
folium_map.save('boat_tracks.html')
print('Map has been saved to boat_tracks.html')
if __name__ == "__main__":
main()