Initial commit - Trekker V2 source
This commit is contained in:
commit
d71faf19e3
65 files changed
+3615
No files matched your search
@@ -0,0 +1,11 @@
|
||||
from app.generators.base import BaseGenerator, GeneratorError
|
||||
from app.generators.elevation_map import ElevationMapGenerator
|
||||
from app.generators.trail_card import TrailCardGenerator
|
||||
from app.generators.weather_overlay import WeatherOverlayGenerator
|
||||
|
||||
GENERATORS: dict[str, type[BaseGenerator]] = {
|
||||
g.name: g
|
||||
for g in (TrailCardGenerator, ElevationMapGenerator, WeatherOverlayGenerator)
|
||||
}
|
||||
|
||||
__all__ = ["BaseGenerator", "GeneratorError", "GENERATORS"]
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Pluggable asset generator interface (PLAN.md Phase 5)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
from app.models.assets import Asset
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
class GeneratorError(Exception):
|
||||
"""Missing prerequisite or generation failure; message is user-facing."""
|
||||
|
||||
|
||||
class BaseGenerator(ABC):
|
||||
name: str = "base"
|
||||
description: str = ""
|
||||
requires: list[str] = [] # subset of {"trail_guide", "weather"}
|
||||
|
||||
def check_requirements(self, route: TrekkerRoute,
|
||||
trail_guide: Optional[TrailGuide],
|
||||
weather: Optional[dict]) -> None:
|
||||
if "trail_guide" in self.requires and trail_guide is None:
|
||||
raise GeneratorError(
|
||||
f"'{self.name}' needs a linked trail guide — link one to this route first"
|
||||
)
|
||||
if "weather" in self.requires and weather is None:
|
||||
raise GeneratorError(
|
||||
f"'{self.name}' needs cached weather — refresh weather for this route first"
|
||||
)
|
||||
|
||||
@abstractmethod
|
||||
def generate(self, route: TrekkerRoute, trail_guide: Optional[TrailGuide],
|
||||
weather: Optional[dict]) -> Asset:
|
||||
...
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Elevation Heatmap: per-segment grade colors feeding the map layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.generators.base import BaseGenerator
|
||||
from app.models.assets import Asset
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
def grade_color(grade_pct: float) -> str:
|
||||
"""green -> yellow -> red gradient by absolute grade %."""
|
||||
g = abs(grade_pct)
|
||||
if g < 5:
|
||||
return "#2ecc71"
|
||||
if g < 10:
|
||||
return "#f1c40f"
|
||||
if g < 18:
|
||||
return "#e67e22"
|
||||
return "#e74c3c"
|
||||
|
||||
|
||||
class ElevationMapGenerator(BaseGenerator):
|
||||
name = "elevation_map"
|
||||
description = "Grade-colored segments and elevation callouts for the map layer"
|
||||
requires: list[str] = []
|
||||
|
||||
def generate(self, route: TrekkerRoute, trail_guide: Optional[TrailGuide],
|
||||
weather: Optional[dict]) -> Asset:
|
||||
segments = [
|
||||
{
|
||||
"index": seg.index,
|
||||
"name": seg.name,
|
||||
"grade_pct_avg": seg.grade_pct_avg,
|
||||
"grade_pct_max": seg.grade_pct_max,
|
||||
"color": grade_color(seg.grade_pct_max),
|
||||
"coordinates": [
|
||||
[p.lon, p.lat] for p in route.points[seg.start_idx : seg.end_idx + 1]
|
||||
],
|
||||
}
|
||||
for seg in route.segments
|
||||
]
|
||||
callouts = []
|
||||
eles = [(i, p) for i, p in enumerate(route.points) if p.ele is not None]
|
||||
if eles:
|
||||
hi = max(eles, key=lambda ip: ip[1].ele)
|
||||
lo = min(eles, key=lambda ip: ip[1].ele)
|
||||
callouts = [
|
||||
{"type": "summit", "lat": hi[1].lat, "lon": hi[1].lon, "ele_m": hi[1].ele},
|
||||
{"type": "low_point", "lat": lo[1].lat, "lon": lo[1].lon, "ele_m": lo[1].ele},
|
||||
]
|
||||
return Asset(route_id=route.id, generator=self.name,
|
||||
data={"segments": segments, "callouts": callouts})
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Trail Card: per-segment readable summary combining route, guide, weather."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.generators.base import BaseGenerator
|
||||
from app.models.assets import Asset
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
def _difficulty_for_grade(grade_pct_max: float) -> str:
|
||||
if grade_pct_max < 8:
|
||||
return "easy"
|
||||
if grade_pct_max < 15:
|
||||
return "moderate"
|
||||
if grade_pct_max < 25:
|
||||
return "difficult"
|
||||
return "extreme"
|
||||
|
||||
|
||||
class TrailCardGenerator(BaseGenerator):
|
||||
name = "trail_card"
|
||||
description = "Per-segment trail card with difficulty, obstacles, and weather notes"
|
||||
requires: list[str] = [] # degrades gracefully without guide/weather
|
||||
|
||||
def generate(self, route: TrekkerRoute, trail_guide: Optional[TrailGuide],
|
||||
weather: Optional[dict]) -> Asset:
|
||||
self.check_requirements(route, trail_guide, weather)
|
||||
seg_weather = (weather or {}).get("segments", {})
|
||||
segments = []
|
||||
for seg in route.segments:
|
||||
daily = seg_weather.get(str(seg.index), {}).get("daily", [])
|
||||
today = daily[0] if daily else None
|
||||
segments.append({
|
||||
"name": seg.name,
|
||||
"distance_mi": round(seg.distance_m / 1609.344, 2),
|
||||
"elevation_gain_ft": round(seg.elevation_gain_m * 3.28084),
|
||||
"elevation_loss_ft": round(seg.elevation_loss_m * 3.28084),
|
||||
"grade_pct_max": seg.grade_pct_max,
|
||||
"difficulty": (trail_guide.difficulty if trail_guide and trail_guide.difficulty
|
||||
else _difficulty_for_grade(seg.grade_pct_max)),
|
||||
"obstacles": trail_guide.obstacle_types if trail_guide else [],
|
||||
"weather_today": today,
|
||||
})
|
||||
data = {
|
||||
"route_name": route.name,
|
||||
"source": route.source,
|
||||
"total_distance_mi": round(route.stats.distance_m / 1609.344, 2),
|
||||
"total_gain_ft": round(route.stats.elevation_gain_m * 3.28084),
|
||||
"guide": None,
|
||||
"segments": segments,
|
||||
}
|
||||
if trail_guide:
|
||||
data["guide"] = {
|
||||
"name": trail_guide.name,
|
||||
"difficulty": trail_guide.difficulty,
|
||||
"water_crossings": [wc.model_dump() for wc in trail_guide.water_crossings],
|
||||
"gate_codes": trail_guide.gate_codes,
|
||||
"vehicle_recommendations": trail_guide.vehicle_recommendations,
|
||||
"closures": trail_guide.closures,
|
||||
"warnings": trail_guide.warnings,
|
||||
}
|
||||
return Asset(route_id=route.id, generator=self.name, data=data)
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Weather Overlay: per-segment advisory data feeding the weather map layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.generators.base import BaseGenerator
|
||||
from app.models.assets import Asset
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
def precip_color(prob: float | None) -> str:
|
||||
"""blue gradient by precipitation probability."""
|
||||
p = prob or 0
|
||||
if p < 20:
|
||||
return "#d6eaf8"
|
||||
if p < 50:
|
||||
return "#5dade2"
|
||||
if p < 80:
|
||||
return "#2e86c1"
|
||||
return "#1b4f72"
|
||||
|
||||
|
||||
class WeatherOverlayGenerator(BaseGenerator):
|
||||
name = "weather_overlay"
|
||||
description = "Per-segment precipitation/wind/lightning advisories"
|
||||
requires = ["weather"]
|
||||
|
||||
def generate(self, route: TrekkerRoute, trail_guide: Optional[TrailGuide],
|
||||
weather: Optional[dict]) -> Asset:
|
||||
self.check_requirements(route, trail_guide, weather)
|
||||
seg_weather = weather.get("segments", {})
|
||||
segments = []
|
||||
for seg in route.segments:
|
||||
tiers = seg_weather.get(str(seg.index), {})
|
||||
daily = tiers.get("daily", [])
|
||||
today = daily[0] if daily else {}
|
||||
segments.append({
|
||||
"index": seg.index,
|
||||
"name": seg.name,
|
||||
"precip_prob_max": today.get("precip_prob_max"),
|
||||
"color": precip_color(today.get("precip_prob_max")),
|
||||
"wind_max_kmh": today.get("wind_max_kmh"),
|
||||
"wind_advisory": (today.get("wind_max_kmh") or 0) >= 40,
|
||||
"lightning_risk": bool(today.get("lightning_risk")),
|
||||
"coordinates": [
|
||||
[p.lon, p.lat] for p in route.points[seg.start_idx : seg.end_idx + 1]
|
||||
],
|
||||
})
|
||||
return Asset(route_id=route.id, generator=self.name, data={"segments": segments})
|
||||
Reference in new issue
Block a user