Initial commit - Trekker V2 source
This commit is contained in:
commit
d71faf19e3
65 files changed
+3615
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,92 @@
|
||||
"""GPX file -> TrekkerRoute, with source app detection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import gpxpy
|
||||
import gpxpy.gpx
|
||||
|
||||
from app.ingest.segmenter import build_segments, compute_stats
|
||||
from app.models.route import TrekkerRoute, TrkPt, Wpt
|
||||
|
||||
|
||||
def detect_source(creator: str | None, raw_xml: str) -> str:
|
||||
"""Detect originating app. Checks creator attribute first, namespaces as fallback."""
|
||||
creator_l = (creator or "").lower()
|
||||
if "gaia" in creator_l:
|
||||
return "gaia"
|
||||
if "onx" in creator_l:
|
||||
return "onx"
|
||||
head = raw_xml[:4096].lower()
|
||||
if "onxmaps.com" in head:
|
||||
return "onx"
|
||||
if "topografix" in head and "gaia" in head:
|
||||
return "gaia"
|
||||
return "manual"
|
||||
|
||||
|
||||
def parse_gpx(raw: bytes | str, filename: str | None = None) -> TrekkerRoute:
|
||||
"""Parse GPX content into a segmented TrekkerRoute."""
|
||||
text = raw.decode("utf-8", errors="replace") if isinstance(raw, bytes) else raw
|
||||
gpx = gpxpy.parse(text)
|
||||
|
||||
points: list[TrkPt] = []
|
||||
for track in gpx.tracks:
|
||||
for seg in track.segments:
|
||||
for p in seg.points:
|
||||
points.append(
|
||||
TrkPt(
|
||||
lat=p.latitude,
|
||||
lon=p.longitude,
|
||||
ele=p.elevation,
|
||||
time=p.time.isoformat() if p.time else None,
|
||||
)
|
||||
)
|
||||
# Some apps export planned routes as <rte> instead of <trk>
|
||||
if not points:
|
||||
for rte in gpx.routes:
|
||||
for p in rte.points:
|
||||
points.append(
|
||||
TrkPt(
|
||||
lat=p.latitude,
|
||||
lon=p.longitude,
|
||||
ele=p.elevation,
|
||||
time=p.time.isoformat() if p.time else None,
|
||||
)
|
||||
)
|
||||
|
||||
waypoints = [
|
||||
Wpt(
|
||||
lat=w.latitude,
|
||||
lon=w.longitude,
|
||||
ele=w.elevation,
|
||||
time=w.time.isoformat() if w.time else None,
|
||||
name=w.name,
|
||||
desc=w.description,
|
||||
sym=w.symbol,
|
||||
type=w.type,
|
||||
)
|
||||
for w in gpx.waypoints
|
||||
]
|
||||
|
||||
name = None
|
||||
if gpx.name:
|
||||
name = gpx.name
|
||||
elif gpx.tracks and gpx.tracks[0].name:
|
||||
name = gpx.tracks[0].name
|
||||
elif filename:
|
||||
name = filename.rsplit("/", 1)[-1].removesuffix(".gpx")
|
||||
|
||||
route = TrekkerRoute(
|
||||
name=name or "Unnamed route",
|
||||
source=detect_source(gpx.creator, text),
|
||||
points=points,
|
||||
waypoints=waypoints,
|
||||
metadata={
|
||||
"creator": gpx.creator,
|
||||
"description": gpx.description,
|
||||
"filename": filename,
|
||||
},
|
||||
)
|
||||
route.segments = build_segments(route)
|
||||
route.stats = compute_stats(route)
|
||||
return route
|
||||
@@ -0,0 +1,147 @@
|
||||
"""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"],
|
||||
)
|
||||
@@ -0,0 +1,78 @@
|
||||
"""Folder watch: ingest .gpx files dropped into the configured directories.
|
||||
|
||||
Internal path uses inotify-backed Observer; remote path (SMB/Nextcloud/USB
|
||||
mounts, where inotify is unreliable) uses PollingObserver per PLAN.md 1e.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from watchdog.events import FileSystemEvent, FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.observers.polling import PollingObserver
|
||||
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.storage.route_store import Store
|
||||
|
||||
log = logging.getLogger("trekker.watchdog")
|
||||
|
||||
|
||||
class GpxHandler(FileSystemEventHandler):
|
||||
def __init__(self, store: Store):
|
||||
self.store = store
|
||||
|
||||
def on_created(self, event: FileSystemEvent) -> None:
|
||||
if event.is_directory or not str(event.src_path).lower().endswith(".gpx"):
|
||||
return
|
||||
self._ingest(Path(str(event.src_path)))
|
||||
|
||||
def on_moved(self, event: FileSystemEvent) -> None:
|
||||
dest = getattr(event, "dest_path", None)
|
||||
if dest and str(dest).lower().endswith(".gpx"):
|
||||
self._ingest(Path(str(dest)))
|
||||
|
||||
def _ingest(self, path: Path) -> None:
|
||||
# Wait for the writer to finish (size stable across two checks).
|
||||
try:
|
||||
last = -1
|
||||
for _ in range(20):
|
||||
size = path.stat().st_size
|
||||
if size == last and size > 0:
|
||||
break
|
||||
last = size
|
||||
time.sleep(0.5)
|
||||
route = parse_gpx(path.read_bytes(), filename=path.name)
|
||||
self.store.save_route(route)
|
||||
log.info("ingested %s as route %s (%s)", path.name, route.id, route.source)
|
||||
except Exception:
|
||||
log.exception("failed to ingest %s", path)
|
||||
|
||||
|
||||
class WatchdogDaemon:
|
||||
def __init__(self, store: Store, internal_path: str | None,
|
||||
remote_path: str | None, poll_interval: int = 30):
|
||||
self.observers = []
|
||||
handler = GpxHandler(store)
|
||||
if internal_path:
|
||||
Path(internal_path).mkdir(parents=True, exist_ok=True)
|
||||
obs = Observer()
|
||||
obs.schedule(handler, internal_path, recursive=True)
|
||||
self.observers.append(obs)
|
||||
if remote_path and Path(remote_path).is_dir():
|
||||
obs = PollingObserver(timeout=poll_interval)
|
||||
obs.schedule(handler, remote_path, recursive=True)
|
||||
self.observers.append(obs)
|
||||
|
||||
def start(self) -> None:
|
||||
for obs in self.observers:
|
||||
obs.start()
|
||||
if self.observers:
|
||||
log.info("watchdog started (%d observers)", len(self.observers))
|
||||
|
||||
def stop(self) -> None:
|
||||
for obs in self.observers:
|
||||
obs.stop()
|
||||
obs.join(timeout=5)
|
||||
Reference in new issue
Block a user