-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
275 lines (227 loc) · 10.1 KB
/
app.py
File metadata and controls
275 lines (227 loc) · 10.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from datetime import datetime
from gpx.waypoint import Waypoint
import ipyleaflet
import panel as pn
import param
from param import Event, Parameterized, ListSelector
import plotly.express as px
from gpx.gpx import GPX
from math import sqrt
import logging
import os
logger = logging.getLogger(__name__)
pn.extension('ipywidgets', 'plotly')
class TrackManager(Parameterized):
tracks = ListSelector(objects= {
"First Track": [[0.0, 0.0], [0.0,10.0]],
"Second Track": [[1.0, 1.0], [2.0,2.0], [3.0,1.0]]
}
)
ipyleaflet_map = param.Parameter(None, precedence= -1)
marker: ipyleaflet.Marker = param.Parameter(None, precedence= -1) # Global Marker
@param.depends("tracks")
def create_map(self):
if not self.tracks:
return self.ipyleaflet_map
tracks = {track: self.param.tracks.objects.get(track).tracks[0] for track in self.tracks}
# AwesomeIcon list: https://fontawesome.com/v4/icons/
from ipyleaflet import Marker, AwesomeIcon
ipyleaflet_map = self.ipyleaflet_map.object
ipyleaflet_map.center= (float(tracks[self.tracks[0]].segments[0].points[0].lat), float(tracks[self.tracks[0]].segments[0].points[0].lon)),
# if len(tracks) == 1:
if True:
draw_control: ipyleaflet.DrawControl = ipyleaflet_map.controls[-1]
def update_track(*args, **kwargs):
logger.warning(f"{args=}")
logger.warning(f"{kwargs=}")
track_index = kwargs.get('geo_json', {}).get('track_index')
if track_index is not None:
new_locations = [(lat, lon) for lon, lat in kwargs['geo_json']['geometry']['coordinates']]
for point, new_location in zip(tracks[track_index].segments[0].points, new_locations):
point.lat = new_location[0]
point.lon = new_location[1]
print(self)
draw_control._draw_callbacks.callbacks.clear()
draw_control.on_draw(update_track)
draw_data = []
ipyleaflet_map.layers = ipyleaflet_map.layers[:1]
for index, track in tracks.items():
first_point = track.segments[0].points[0]
last_point = track.segments[0].points[-1]
draw_data.append(
{
'type': 'Feature',
'track_index': index, # Custom value to keep track of the track to update.
'properties':
{
'style':
{
'stroke': True,
'color': '#6bc2e5',
'weight': 8,
'opacity': .8,
'fill': False,
'clickable': True,
}
},
'geometry':
{
'type': 'LineString',
'coordinates':
[[
float(point.lon),
float(point.lat)
] for point in track.segments[0].points]
}
}
)
ipyleaflet_map.add_layer(
Marker(
location= [float(last_point.lat), float(last_point.lon)],
icon= AwesomeIcon(
name="check-circle-o",
marker_color= "red"
),
draggable= False,
)
)
ipyleaflet_map.add_layer(
Marker(
location= [float(first_point.lat), float(first_point.lon)],
icon = AwesomeIcon(
name="check-circle-o",
marker_color= "green"
),
draggable= False,
)
)
draw_control.data = draw_data
# return pn.panel(ipyleaflet_map)
# panel_map = pn.panel(ipyleaflet_map)
# return panel_map
return self.ipyleaflet_map
@param.depends("tracks")
def create_speed_plots(self):
if self.tracks is None:
return None
def compute_speed(current: Waypoint, next_loc: Waypoint):
distance = sqrt((current.lat - next_loc.lat) ** 2 + (current.lon - next_loc.lon) ** 2) * 25000/360
# import pdb; pdb.set_trace()
if isinstance(next_loc.time, datetime) and isinstance(current.time, datetime):
time = next_loc.time - current.time
return distance / ((time.total_seconds() + time.microseconds) / 3600)
def speed_plot_callback(event: Event):
from ipyleaflet import Marker, AwesomeIcon
if event.name == "hover_data":
if event.old:
self.ipyleaflet_map.object.remove_layer(self.marker)
if event.new:
for track in event.new["points"]:
# track_index = track["curveNumber"]
track_index = event.cls.index
segment_index = track["pointIndex"]
selected_segment = self.param.tracks.objects[self.tracks[track_index]].tracks[0].segments[0].points[segment_index]
# pprint(dir(folium_map.object))
self.marker = Marker(
location= [float(selected_segment.lat), float(selected_segment.lon)],
icon= AwesomeIcon(
name= "dot-circle-o"
),
draggable= False,
)
self.ipyleaflet_map.object.add_layer(self.marker)
def create_speed_plot(index, track_index):
track = self.param.tracks.objects.get(track_index).tracks[0]
speeds = [compute_speed(c, n ) for c, n in zip(track.segments[0].points, track.segments[0].points[1:])]
surface = px.line(x= range(len(speeds)), y= speeds)
surface.layout.xaxis["title"] = {"text": "Time"}
surface.layout.yaxis["title"] = {"text": "Speed in km/h"}
plotly_object = pn.pane.Plotly(surface)
plotly_object.width_policy = "max"
plotly_object.index = index
plotly_object.param.watch(speed_plot_callback, ["hover_data"])
return plotly_object
plots = []
for index, track_index in enumerate(self.tracks):
speed_plot = create_speed_plot(index, track_index)
track_plot = pn.Row(
pn.Column(
pn.widgets.StaticText(name='Total distance', value='A string'),
pn.widgets.StaticText(name='Total time', value='A string'),
pn.widgets.StaticText(name='Average speed', value='A string'),
pn.widgets.StaticText(name='Calories burnt', value='Don\'t know'),
),
speed_plot
, width_policy= "max"
)
# plots.append((os.path.basename(track_index), speed_plot))
plots.append((os.path.basename(track_index), track_plot))
tabs = pn.Tabs(*plots)
tabs.width_policy = "max"
return tabs
def create_map(tracks: list[GPX]):
# AwesomeIcon list: https://fontawesome.com/v4/icons/
from ipyleaflet import Map, basemaps, Marker, Polyline, AwesomeIcon, AntPath
from ipywidgets import Layout
ipyleaflet_map = Map(
center= (float(tracks[0].tracks[0].segments[0].points[0].lat), float(tracks[0].tracks[0].segments[0].points[0].lon)),
# center= (0, 0),
zoom=16,
# basemap = basemaps.Stamen.Terrain,
# basemap = basemaps.OpenTopoMap,
# basemap = basemaps.Esri.DeLorme,
basemap = basemaps.OpenStreetMap.HOT,
# basemap = ,
layout = Layout(height= "775px")
)
draw_control = ipyleaflet.DrawControl()
draw_control.polyline = {
"shapeOptions": {
"color": "#6bc2e5",
"weight": 8,
"opacity": 1.0
}
}
ipyleaflet_map.add_control(draw_control)
return pn.panel(ipyleaflet_map)
def main(track_files):
if not track_files:
return pn.pane.Markdown("No track found!")
tracks = {file_path:GPX.from_file(file_path) for file_path in track_files}
folium_map = create_map(list(tracks.values()))
track_manager = TrackManager()
track_manager.param.tracks.objects = tracks
track_manager.ipyleaflet_map = folium_map
save_button = pn.widgets.Button(name="Save")
def save_callback(event):
for file_path, track in tracks.items():
track.to_file(file_path)
save_button.on_click(save_callback)
# template = pn.template.BootstrapTemplate(
template = pn.template.FastListTemplate(
title= "Run Plotter",
main= [pn.Column(*[
# "Hello",
pn.Row(track_manager, save_button),
# folium_map,
track_manager.create_map,
# speed_plots,
track_manager.create_speed_plots,
]),
]
)
# return pn.Column(*template.main)
return template
def parse_args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("track_files", nargs="*", default=[], help= "A list of GPX file to plot")
args = parser.parse_args()
return args.__dict__
if __name__ == "__main__":
from panel.command import main as panel_main
import sys
import glob
sys.argv = ["panel", "serve", __file__, "--args"] + glob.glob("/home/marc/trace_*.gpx")
exit(panel_main())
main(**parse_args()).servable()