Files

148 lines
4.9 KiB
Python

"""Waypoint-driven segmentation and per-segment grade/elevation analysis."""
from __future__ import annotations
import math
from datetime import datetime
from app.models.route import RouteStats, Segment, TrekkerRoute, TrkPt, Wpt
EARTH_RADIUS_M = 6371000.0
# Minimum run between points used for grade math, to keep GPS jitter from
# producing absurd instantaneous grades.
MIN_GRADE_RUN_M = 10.0
# Waypoints farther than this from the track don't define segment boundaries.
MAX_WPT_SNAP_M = 200.0
def haversine_m(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlmb = math.radians(lon2 - lon1)
a = math.sin(dphi / 2) ** 2 + math.cos(phi1) * math.cos(phi2) * math.sin(dlmb / 2) ** 2
return 2 * EARTH_RADIUS_M * math.asin(math.sqrt(a))
def _nearest_point_idx(points: list[TrkPt], wpt: Wpt) -> tuple[int, float]:
best_i, best_d = 0, float("inf")
for i, p in enumerate(points):
d = haversine_m(p.lat, p.lon, wpt.lat, wpt.lon)
if d < best_d:
best_i, best_d = i, d
return best_i, best_d
def _parse_time(t: str | None) -> datetime | None:
if not t:
return None
try:
return datetime.fromisoformat(t)
except ValueError:
return None
def _analyze(points: list[TrkPt], start: int, end: int) -> dict:
"""Distance, gain/loss, grades and duration over points[start..end]."""
dist = gain = loss = 0.0
max_grade = 0.0
# accumulate run since last grade sample to smooth jitter
run_acc = 0.0
ele_anchor: float | None = None
for i in range(start, end):
a, b = points[i], points[i + 1]
d = haversine_m(a.lat, a.lon, b.lat, b.lon)
dist += d
if a.ele is not None and b.ele is not None:
de = b.ele - a.ele
if de >= 0:
gain += de
else:
loss -= de
if ele_anchor is None:
ele_anchor = a.ele
run_acc += d
if run_acc >= MIN_GRADE_RUN_M:
grade = abs(b.ele - ele_anchor) / run_acc * 100.0
max_grade = max(max_grade, grade)
run_acc = 0.0
ele_anchor = b.ele
eles = [p.ele for p in points[start : end + 1] if p.ele is not None]
avg_grade = 0.0
if dist > 0 and len(eles) >= 2:
avg_grade = (eles[-1] - eles[0]) / dist * 100.0
t0 = _parse_time(points[start].time)
t1 = _parse_time(points[end].time)
duration = (t1 - t0).total_seconds() if t0 and t1 and t1 > t0 else None
return {
"distance_m": round(dist, 1),
"elevation_gain_m": round(gain, 1),
"elevation_loss_m": round(loss, 1),
"grade_pct_avg": round(avg_grade, 2),
"grade_pct_max": round(max_grade, 2),
"duration_s": duration,
}
def build_segments(route: TrekkerRoute) -> list[Segment]:
"""Named waypoints define segment boundaries. No waypoints -> one segment."""
points = route.points
if len(points) < 2:
return []
# Snap named waypoints onto the track, keep those reasonably close to it.
boundaries: list[tuple[int, str]] = []
for w in route.waypoints:
if not w.name:
continue
idx, dist = _nearest_point_idx(points, w)
if dist <= MAX_WPT_SNAP_M:
boundaries.append((idx, w.name))
boundaries.sort(key=lambda b: b[0])
# Build cut indices: start, interior waypoint snaps, end (deduplicated).
cuts: list[tuple[int, str | None]] = [(0, None)]
for idx, name in boundaries:
if 0 < idx < len(points) - 1 and idx != cuts[-1][0]:
cuts.append((idx, name))
elif idx == 0:
cuts[0] = (0, name)
end_name = next((n for i, n in boundaries if i == len(points) - 1), None)
cuts.append((len(points) - 1, end_name))
segments: list[Segment] = []
for si in range(len(cuts) - 1):
(s_idx, s_name), (e_idx, e_name) = cuts[si], cuts[si + 1]
stats = _analyze(points, s_idx, e_idx)
label = f"{s_name or 'Start'} → {e_name or 'End'}" if len(cuts) > 2 else route.name
segments.append(
Segment(
index=si,
name=label,
start_wpt=s_name,
end_wpt=e_name,
start_idx=s_idx,
end_idx=e_idx,
**stats,
)
)
return segments
def compute_stats(route: TrekkerRoute) -> RouteStats:
points = route.points
if len(points) < 2:
return RouteStats()
whole = _analyze(points, 0, len(points) - 1)
eles = [p.ele for p in points if p.ele is not None]
return RouteStats(
distance_m=whole["distance_m"],
elevation_gain_m=whole["elevation_gain_m"],
elevation_loss_m=whole["elevation_loss_m"],
elevation_min_m=min(eles) if eles else None,
elevation_max_m=max(eles) if eles else None,
duration_s=whole["duration_s"],
)