66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
"""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)
|