Activity detail: layout refactor + GPS-derived speed for map coloring

Layout: map + charts stacked left, stats panel (2-col) on the right.
Cadence moved to last stat. Charts sit directly below the map.

Speed coloring: most FIT files don't record per-second speed, leaving
timeseries speed_kmh all-null and the hover link dead. Fix: derive speed
from consecutive GPS coordinates (haversine + 5-pt moving average) when
the device didn't record it. Add --backfill-speed render flag to retrofit
existing timeseries files.
This commit is contained in:
Davide Scaini
2026-05-16 23:24:29 +02:00
parent 0dc450ba30
commit 14a4a0b994
3 changed files with 122 additions and 38 deletions
+39
View File
@@ -2,11 +2,45 @@
the BAS timeseries object (parallel arrays)."""
from datetime import datetime
from math import atan2, cos, radians, sin, sqrt
from typing import Optional
from bincio.extract.models import DataPoint
def _gps_speed_kmh(
lat_vals: list[Optional[float]],
lon_vals: list[Optional[float]],
ts_vals: list[int],
) -> list[Optional[float]]:
"""Compute speed (km/h) from consecutive GPS coordinates via haversine.
Applies a 5-point centred moving-average to reduce GPS noise.
"""
n = len(ts_vals)
raw: list[Optional[float]] = [None] * n
for i in range(1, n):
la0, lo0 = lat_vals[i - 1], lon_vals[i - 1]
la1, lo1 = lat_vals[i], lon_vals[i]
dt = ts_vals[i] - ts_vals[i - 1]
if la0 is None or lo0 is None or la1 is None or lo1 is None or dt <= 0:
continue
dlat = radians(la1 - la0)
dlon = radians(lo1 - lo0)
a = sin(dlat / 2) ** 2 + cos(radians(la0)) * cos(radians(la1)) * sin(dlon / 2) ** 2
d_km = 2 * 6371.0 * atan2(sqrt(a), sqrt(1 - a))
raw[i] = d_km / dt * 3600.0
# 5-point centred moving average (skip None anchors)
half = 2
smoothed: list[Optional[float]] = [None] * n
for i in range(n):
vals = [raw[j] for j in range(max(0, i - half), min(n, i + half + 1)) if raw[j] is not None]
if vals:
smoothed[i] = round(sum(vals) / len(vals), 2)
return smoothed
def build_timeseries(
points: list[DataPoint],
started_at: datetime,
@@ -40,6 +74,11 @@ def build_timeseries(
lon_vals = [round(p.lon, 7) if p.lon is not None else None for p in sampled] if include_gps else None
ele_vals = [round(p.elevation_m, 1) if p.elevation_m is not None else None for p in sampled]
spd_vals = [round(p.speed_kmh, 2) if p.speed_kmh is not None else None for p in sampled]
# Derive speed from GPS when the device didn't record per-second speed.
if include_gps and lat_vals and lon_vals and all(v is None for v in spd_vals):
spd_vals = _gps_speed_kmh(lat_vals, lon_vals, ts_vals)
hr_vals = [p.hr_bpm for p in sampled]
cad_vals = [p.cadence_rpm for p in sampled]
pwr_vals = [p.power_w for p in sampled]