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,91 @@
|
||||
"""Enriched GPX export: TrekkerRoute -> GPX with trekker:* extensions.
|
||||
|
||||
Per-trackpoint extensions carry grade_pct; segment metadata and trail guide
|
||||
enrichment (surface, water crossing depth) ride on track <extensions>.
|
||||
Waypoints from the original import are preserved.
|
||||
|
||||
Namespaces are written as literal xmlns attributes rather than via
|
||||
ET.register_namespace, which mutates global state across writers.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import Optional
|
||||
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
GPX_NS = "http://www.topografix.com/GPX/1/1"
|
||||
TREKKER_NS = "https://trekker.dev/xmlschemas/v1"
|
||||
|
||||
|
||||
def _point_grades(route: TrekkerRoute) -> dict[int, float]:
|
||||
"""Map point index -> avg grade of its segment (UI/export enrichment)."""
|
||||
grades: dict[int, float] = {}
|
||||
for seg in route.segments:
|
||||
for i in range(seg.start_idx, seg.end_idx + 1):
|
||||
grades[i] = seg.grade_pct_avg
|
||||
return grades
|
||||
|
||||
|
||||
def write_gpx(route: TrekkerRoute, trail_guide: Optional[TrailGuide] = None) -> str:
|
||||
gpx = ET.Element("gpx", {
|
||||
"version": "1.1",
|
||||
"creator": "Trekker",
|
||||
"xmlns": GPX_NS,
|
||||
"xmlns:trekker": TREKKER_NS,
|
||||
})
|
||||
|
||||
meta = ET.SubElement(gpx, "metadata")
|
||||
ET.SubElement(meta, "name").text = route.name
|
||||
|
||||
for w in route.waypoints:
|
||||
wpt = ET.SubElement(gpx, "wpt", {"lat": f"{w.lat:.7f}", "lon": f"{w.lon:.7f}"})
|
||||
if w.ele is not None:
|
||||
ET.SubElement(wpt, "ele").text = f"{w.ele:.1f}"
|
||||
if w.time:
|
||||
ET.SubElement(wpt, "time").text = w.time
|
||||
if w.name:
|
||||
ET.SubElement(wpt, "name").text = w.name
|
||||
if w.desc:
|
||||
ET.SubElement(wpt, "desc").text = w.desc
|
||||
if w.sym:
|
||||
ET.SubElement(wpt, "sym").text = w.sym
|
||||
|
||||
trk = ET.SubElement(gpx, "trk")
|
||||
ET.SubElement(trk, "name").text = route.name
|
||||
|
||||
trk_ext = ET.SubElement(trk, "extensions")
|
||||
ET.SubElement(trk_ext, "trekker:source").text = route.source
|
||||
if trail_guide:
|
||||
if trail_guide.difficulty:
|
||||
ET.SubElement(trk_ext, "trekker:difficulty").text = trail_guide.difficulty
|
||||
for wc in trail_guide.water_crossings:
|
||||
el = ET.SubElement(trk_ext, "trekker:water_crossing")
|
||||
if wc.name:
|
||||
el.set("name", wc.name)
|
||||
if wc.depth_ft is not None:
|
||||
el.set("depth_ft", str(wc.depth_ft))
|
||||
for seg in route.segments:
|
||||
el = ET.SubElement(trk_ext, "trekker:segment")
|
||||
el.set("name", seg.name)
|
||||
el.set("distance_m", str(seg.distance_m))
|
||||
el.set("grade_pct_avg", str(seg.grade_pct_avg))
|
||||
el.set("grade_pct_max", str(seg.grade_pct_max))
|
||||
|
||||
grades = _point_grades(route)
|
||||
surface = (route.metadata or {}).get("surface_type")
|
||||
trkseg = ET.SubElement(trk, "trkseg")
|
||||
for i, p in enumerate(route.points):
|
||||
pt = ET.SubElement(trkseg, "trkpt", {"lat": f"{p.lat:.7f}", "lon": f"{p.lon:.7f}"})
|
||||
if p.ele is not None:
|
||||
ET.SubElement(pt, "ele").text = f"{p.ele:.1f}"
|
||||
if p.time:
|
||||
ET.SubElement(pt, "time").text = p.time
|
||||
ext = ET.SubElement(pt, "extensions")
|
||||
ET.SubElement(ext, "trekker:grade_pct").text = str(grades.get(i, 0.0))
|
||||
if surface:
|
||||
ET.SubElement(ext, "trekker:surface_type").text = str(surface)
|
||||
|
||||
return ET.tostring(gpx, encoding="unicode", xml_declaration=True)
|
||||
@@ -0,0 +1,58 @@
|
||||
"""KML export for onX compatibility: track LineString + waypoint Placemarks."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import Optional
|
||||
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
KML_NS = "http://www.opengis.net/kml/2.2"
|
||||
|
||||
|
||||
def _desc_for_route(route: TrekkerRoute, guide: Optional[TrailGuide]) -> str:
|
||||
mi = route.stats.distance_m / 1609.344
|
||||
gain = route.stats.elevation_gain_m * 3.28084
|
||||
lines = [f"Distance: {mi:.1f} mi", f"Elevation gain: {gain:.0f} ft",
|
||||
f"Source: {route.source}"]
|
||||
for seg in route.segments:
|
||||
lines.append(f"Segment {seg.name}: max grade {seg.grade_pct_max:.1f}%")
|
||||
if guide:
|
||||
if guide.difficulty:
|
||||
lines.append(f"Difficulty: {guide.difficulty}")
|
||||
for wc in guide.water_crossings:
|
||||
depth = f" ({wc.depth_ft} ft)" if wc.depth_ft is not None else ""
|
||||
lines.append(f"Water crossing: {wc.name or 'unnamed'}{depth}")
|
||||
lines.extend(f"Warning: {w}" for w in guide.warnings)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def write_kml(route: TrekkerRoute, trail_guide: Optional[TrailGuide] = None) -> str:
|
||||
kml = ET.Element("kml", {"xmlns": KML_NS})
|
||||
doc = ET.SubElement(kml, "Document")
|
||||
ET.SubElement(doc, "name").text = route.name
|
||||
|
||||
track = ET.SubElement(doc, "Placemark")
|
||||
ET.SubElement(track, "name").text = route.name
|
||||
ET.SubElement(track, "description").text = _desc_for_route(route, trail_guide)
|
||||
line = ET.SubElement(track, "LineString")
|
||||
ET.SubElement(line, "tessellate").text = "1"
|
||||
coords = " ".join(
|
||||
f"{p.lon:.7f},{p.lat:.7f},{p.ele if p.ele is not None else 0:.1f}"
|
||||
for p in route.points
|
||||
)
|
||||
ET.SubElement(line, "coordinates").text = coords
|
||||
|
||||
for w in route.waypoints:
|
||||
pm = ET.SubElement(doc, "Placemark")
|
||||
if w.name:
|
||||
ET.SubElement(pm, "name").text = w.name
|
||||
if w.desc:
|
||||
ET.SubElement(pm, "description").text = w.desc
|
||||
point = ET.SubElement(pm, "Point")
|
||||
ET.SubElement(point, "coordinates").text = (
|
||||
f"{w.lon:.7f},{w.lat:.7f},{w.ele if w.ele is not None else 0:.1f}"
|
||||
)
|
||||
|
||||
return ET.tostring(kml, encoding="unicode", xml_declaration=True)
|
||||
Reference in new issue
Block a user