Initial commit - Trekker V2 source
This commit is contained in:
commit
d71faf19e3
65 files changed
+3615
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user