Initial commit - Trekker V2 source
This commit is contained in:
commit
d71faf19e3
65 files changed
+3615
No files matched your search
+15
@@ -0,0 +1,15 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY app/ app/
|
||||
COPY ui/ ui/
|
||||
|
||||
ENV TREKKER_DATA_DIR=/app/data
|
||||
VOLUME ["/app/data", "/app/import"]
|
||||
|
||||
EXPOSE 8000
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
@@ -0,0 +1,197 @@
|
||||
# Trekker — v0 Implementation Plan (revised)
|
||||
|
||||
> Single canonical source of truth for Trekker product/architecture decisions.
|
||||
> This revision supersedes the original v0 plan; the source tree in this
|
||||
> repository is the implementation of it. Revision notes are at the bottom.
|
||||
|
||||
**Goal:** Build a vehicle-aware overland trip intelligence system that ingests
|
||||
GPX files (manual + folder watch), trail guides (web scrape + PDF with LLM
|
||||
extraction), weather data (Open-Meteo), and offline map areas, then generates
|
||||
structured overland assets and exports enriched GPX/KML files for manual
|
||||
import into onX and Gaia GPS.
|
||||
|
||||
**Architecture:** Dual-host FastAPI backend on home server (always-on,
|
||||
Tailnet) + Neutron truck server (offline-first). GPX ingestion → canonical
|
||||
`TrekkerRoute` model. Pluggable asset generators. Offline map packs provide
|
||||
local basemap/terrain tiles for field use. Export layer → enriched GPX/KML
|
||||
for onX/Gaia.
|
||||
|
||||
**Tech Stack:** FastAPI, gpxpy, openai Python SDK, pypdf, python-watchdog,
|
||||
httpx → Open-Meteo, **MapLibre GL JS** + raster basemap, **AWS Terrain Tiles
|
||||
(Terrarium)** for elevation, SQLite (JSON blobs), local offline tile packs,
|
||||
Docker Compose.
|
||||
|
||||
---
|
||||
|
||||
## Scoped Items (locked with Peyo)
|
||||
|
||||
| # | Item | Decision | Notes |
|
||||
| :-- | :--- | :------- | :---- |
|
||||
| 1 | GPX input | **A + B**: manual upload via web UI + folder watch via `python-watchdog`. `gpxpy` → `TrekkerRoute`. | Handles Gaia and onX metadata. |
|
||||
| 2 | Map / elevation | **MapLibre GL JS** for rendering (no token). **AWS Terrain Tiles (Terrarium encoding)** for elevation. | Powers UI display AND segment grade computation. *(Revised — see note R1.)* |
|
||||
| 2b | Offline map areas | Save map areas for offline use: viewport/rectangle, zoom range → local tile pack served by Trekker. | Per-pack tile cap; tile source configurable. *(See note R2.)* |
|
||||
| 3a | Trail guides — web | `httpx` + BeautifulSoup scrape → LLM extraction. | difficulty, obstacles, water crossings, gate codes, vehicle reqs, closures. |
|
||||
| 3b | Trail guides — PDF | `pypdf` → OpenAI-compatible LLM extraction. | Output requires manual review before saving. |
|
||||
| 3c | LLM interface | OpenAI-compatible API; configurable `base_url`, `model`, `api_key` in `trekker.yml`. | Supports local Ollama, Ollama Cloud, Gemini, Groq, OpenAI. |
|
||||
| 4 | Weather | Open-Meteo — free, no API key, global. | 7/10-day, four granularity tiers, manual refresh. |
|
||||
| 5 | Cell coverage | **Not MVP** — deferred. | — |
|
||||
| 6 | Victron / battery | **Not MVP** — deferred. | — |
|
||||
|
||||
---
|
||||
|
||||
## Implementation status
|
||||
|
||||
All phases below are **built** in this repo at MVP depth and covered by the
|
||||
test suite (`tests/`, 31 tests).
|
||||
|
||||
### Phase 1: GPX Ingestion Engine ✅
|
||||
- `app/models/` — `TrekkerRoute` (TrkPt/Wpt/Segment/RouteStats), `TrailGuide`,
|
||||
`Trip`, `Asset`, `MapPack`. Pydantic v2.
|
||||
- `app/ingest/gpx_parser.py` — gpxpy parse, creator-first source detection
|
||||
(`gaia` / `onx` / `manual`), `<rte>` fallback for planned routes.
|
||||
- `app/ingest/segmenter.py` — waypoint-driven segmentation (named waypoints
|
||||
snapped to track within 200 m define boundaries; no waypoints → one
|
||||
segment). Haversine distance, gain/loss, avg/max grade (10 m smoothing
|
||||
window against GPS jitter), duration.
|
||||
- `app/ingest/watchdog_daemon.py` — dual watch folders; inotify observer for
|
||||
the internal path, **PollingObserver** for the remote mount (SMB/Nextcloud
|
||||
mounts don't deliver inotify events reliably). Size-stable check before
|
||||
ingesting.
|
||||
- `app/storage/` — SQLite, JSON-blob tables (routes, trail_guides, assets,
|
||||
weather_cache, offline_map_packs), WAL mode, FK link route → guide.
|
||||
|
||||
### Phase 2: Web UI Shell & Map ✅
|
||||
- The map is the app: full-screen MapLibre GL JS; collapsible left sidebar
|
||||
(routes, layers, offline packs); slide-out route panel with summary,
|
||||
segments, guide link, trail card, export, delete.
|
||||
- Grade-colored segment toggle (green→yellow→red by max grade) and weather
|
||||
overlay toggle (blue gradient by precip probability) — map layers, not
|
||||
sidebar charts.
|
||||
- `app/services/elevation.py` — Terrarium decode
|
||||
`ele = (R*256 + G + B/256) − 32768`, disk+memory tile cache; fills missing
|
||||
`ele` on upload so grade math works for onX exports that omit elevation.
|
||||
- `app/services/offline_maps.py` + `app/routes/offline_maps.py` — estimate →
|
||||
create → background download → ready; tiles under
|
||||
`data/offline_maps/{pack}/{layer}/{z}/{x}/{y}.png`; served at
|
||||
`/api/offline-maps/tiles/...` which the UI switches to when offline.
|
||||
|
||||
### Phase 3: Trail Guide Module ✅
|
||||
- Draft-first flow: `POST /api/trails/scrape` and `/api/trails/upload-pdf`
|
||||
return **drafts**; nothing is stored until the user reviews/edits and
|
||||
`POST /api/trails` confirms (manual-review gate per 3c).
|
||||
- `app/services/llm.py` — OpenAI-compatible adapter, JSON-mode with fallback,
|
||||
tolerant of code-fenced output from small local models. When no LLM is
|
||||
configured, drafts carry raw extracted text for manual entry.
|
||||
- Max 1 guide per route (`route.trail_guide_id`), user-selectable link in the
|
||||
panel, generators degrade gracefully without one.
|
||||
|
||||
### Phase 4: Weather Integration ✅
|
||||
- `app/services/weather.py` — Open-Meteo, 7-day default / 10-day toggle,
|
||||
per-segment midpoints (deduplicated on a 0.05° grid so short routes make
|
||||
one API call), aggregated to four tiers: hourly / 6 h / 12 h / daily.
|
||||
- Manual refresh only: `POST /api/routes/{id}/refresh-weather`; cached in
|
||||
SQLite; compact forecast badge in the panel corner; wind advisory
|
||||
(≥40 km/h) and lightning risk (WMO codes 95/96/99) flags.
|
||||
|
||||
### Phase 5: Asset Generator Pipeline ✅
|
||||
- `app/generators/base.py` — `BaseGenerator` ABC with `requires[]` checks and
|
||||
user-facing errors (HTTP 409 when prerequisites are missing).
|
||||
- `trail_card` (route + optional guide + optional weather → per-segment JSON
|
||||
card rendered in the panel), `elevation_map` (grade colors + summit/low
|
||||
callouts), `weather_overlay` (per-segment precip/wind/lightning).
|
||||
- Trigger is a user click; output stored as JSON in the assets table.
|
||||
|
||||
### Phase 6: Export & Download ✅
|
||||
- `app/export/gpx_writer.py` — GPX 1.1 with `trekker:` extensions: per-point
|
||||
`grade_pct` (+ `surface_type` when known), per-track segment metadata and
|
||||
guide enrichment (difficulty, water crossing depth). Waypoints preserved —
|
||||
round-trips through the parser losslessly (tested).
|
||||
- `app/export/kml_writer.py` — KML for onX; enrichment in the description.
|
||||
- `GET /api/routes/{id}/export?format=gpx|kml` → file download.
|
||||
|
||||
### Phase 7: Dual-Host Sync & Deployment ✅
|
||||
- Single Docker image; `docker/docker-compose.neutron.yml` override adds the
|
||||
Tailscale sidecar (`TAILSCALE_AUTH_KEY`).
|
||||
- Manual sync: UI button → `POST /api/sync` pulls the peer's bundle, merges,
|
||||
pushes local bundle back. Conflict policy: **newest `updated_at` wins, per
|
||||
record** (see note R3).
|
||||
- UI offline state: `navigator.onLine` → offline badge, sync hidden, weather
|
||||
refresh disabled, basemap source switched to local pack tiles.
|
||||
|
||||
### Phase 8: July 4 Trip — verification gate (next)
|
||||
Deploy home + Neutron instances, run the full workflow with live Windrock
|
||||
routes before the June 27 departure: import GPX → map → link guide →
|
||||
trail card → weather → export → sync → offline mode in the driveway.
|
||||
|
||||
---
|
||||
|
||||
## Verification Strategy
|
||||
|
||||
| Phase | What | How | Status |
|
||||
| :---: | :--- | :-- | :----- |
|
||||
| 1 | GPX parsing accuracy | Unit tests with Gaia/onX/minimal fixtures: source, segments, stats | ✅ automated |
|
||||
| 2 | Map render + offline packs | Visual check + pack download, then disconnect and verify local tiles | manual (Phase 8) |
|
||||
| 3 | LLM extraction | Compare output vs labeled guides, >80% field accuracy | manual, provider-dependent |
|
||||
| 4 | Weather aggregation | Known-answer tier tests with synthetic Open-Meteo payloads | ✅ automated |
|
||||
| 5 | Generator output | Known-answer tests incl. graceful degradation and 409s | ✅ automated |
|
||||
| 6 | GPX round-trip | Export → re-import → segments/waypoints/elevations identical | ✅ automated |
|
||||
| 7 | Sync | Two-store merge tests: newest-wins, idempotent re-merge | ✅ automated |
|
||||
|
||||
Run: `pip install -r requirements.txt pytest && python -m pytest tests/`
|
||||
|
||||
---
|
||||
|
||||
## File Map (as built)
|
||||
|
||||
```
|
||||
app/
|
||||
├── main.py # FastAPI entry, SPA serving, watchdog lifespan
|
||||
├── config.py # env + trekker.yml settings
|
||||
├── deps.py # shared store/service singletons
|
||||
├── models/ # route, trail_guide, trip, assets, map_pack
|
||||
├── ingest/ # gpx_parser, segmenter, watchdog_daemon
|
||||
├── services/ # elevation, offline_maps, weather, llm, scraper
|
||||
├── generators/ # base, trail_card, elevation_map, weather_overlay
|
||||
├── export/ # gpx_writer, kml_writer
|
||||
├── storage/ # database (schema), route_store (all CRUD)
|
||||
├── sync/sync_daemon.py # bundle build/merge + peer sync
|
||||
└── routes/ # upload, trails, weather, export, offline_maps, sync
|
||||
ui/ # index.html, app.js, styles.css (MapLibre)
|
||||
tests/ # 31 tests + GPX fixtures
|
||||
docker-compose.yml, docker/docker-compose.neutron.yml, Dockerfile
|
||||
.env.example, trekker.yml.example
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Revision notes (changes from the original v0 plan)
|
||||
|
||||
- **R1 — Mapbox replaced with MapLibre + open data.** The original plan's own
|
||||
Task 2.5 gate ("confirm the tile provider permits offline storage") fails
|
||||
for Mapbox: its ToS prohibits caching tiles outside the Mapbox SDK offline
|
||||
mechanism, and Terrain-RGB tiles count against a paid token. MapLibre GL JS
|
||||
is API-compatible for our usage and tokenless; **AWS Terrain Tiles**
|
||||
(Terrarium encoding, AWS Open Data) explicitly permit bulk download and use
|
||||
the near-identical decode formula. Net effect: no token to manage, offline
|
||||
packs are licence-clean for terrain.
|
||||
- **R2 — Offline basemap tiles are capped and source-configurable.** OSM's
|
||||
tile usage policy forbids bulk scraping. Packs default to the configured
|
||||
basemap with a hard per-pack tile cap (`tiles.max_pack_tiles`, default
|
||||
4000) and a polite download delay; for big areas point `tiles.base_url` at
|
||||
a self-hosted or bulk-friendly tile source. Terrain layer has no such
|
||||
problem (R1).
|
||||
- **R3 — Sync conflict policy simplified.** "Home wins on structured data,
|
||||
notes merge bidirectionally" was underspecified (no notes entity exists in
|
||||
v0). MVP: per-record newest-`updated_at` wins, both directions, idempotent.
|
||||
Field-level merge can come with the trip-notes feature.
|
||||
- **R4 — shapely/geopandas dropped.** Haversine + index math covers all v0
|
||||
geometry; this keeps the Docker image slim (no GEOS/GDAL native deps).
|
||||
- **R5 — Stale deployment notes removed.** The previous plan referenced a
|
||||
Gitea branch and a live Tailnet deployment that don't correspond to this
|
||||
repository; this repo is now the canonical source.
|
||||
- **R6 — Draft-then-confirm guide flow made explicit in the API.** Scrape/PDF
|
||||
endpoints never write to the DB; only the reviewed `POST /api/trails` does,
|
||||
which is what "manual review before saving" (3b) actually requires.
|
||||
- **R7 — Firecrawl MCP dropped from MVP scrape path.** Plain httpx + bs4 is
|
||||
dependency-free and works offline-ish (LAN sources); Firecrawl can be added
|
||||
later behind the same scraper interface.
|
||||
@@ -0,0 +1,51 @@
|
||||
# Trekker
|
||||
|
||||
Vehicle-aware overland trip intelligence. Ingests GPX (upload + watch
|
||||
folders), trail guides (web scrape / PDF + LLM extraction), Open-Meteo
|
||||
weather, and offline map areas; generates trail cards and map overlays; and
|
||||
exports enriched GPX/KML for onX and Gaia GPS.
|
||||
|
||||
See [PLAN.md](PLAN.md) for architecture and design decisions.
|
||||
|
||||
## Quick start
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
uvicorn app.main:app --reload
|
||||
# open http://localhost:8000/app
|
||||
```
|
||||
|
||||
Optional config: copy `trekker.yml.example` → `trekker.yml` (watch folders,
|
||||
LLM provider, tile sources, sync peer) and `.env.example` → `.env`.
|
||||
|
||||
## Docker
|
||||
|
||||
```bash
|
||||
# Home server
|
||||
docker compose up -d --build
|
||||
|
||||
# Neutron (truck server, with Tailscale sidecar)
|
||||
TAILSCALE_AUTH_KEY=tskey-... \
|
||||
docker compose -f docker-compose.yml -f docker/docker-compose.neutron.yml up -d --build
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
```bash
|
||||
python -m pytest tests/
|
||||
```
|
||||
|
||||
## API surface
|
||||
|
||||
| Endpoint | What |
|
||||
| :--- | :--- |
|
||||
| `POST /api/routes/upload` | Upload a GPX file → segmented `TrekkerRoute` |
|
||||
| `GET /api/routes` / `GET /api/routes/{id}` | List / fetch routes |
|
||||
| `PUT /api/routes/{id}/trail-guide` | Link/unlink a trail guide |
|
||||
| `POST /api/routes/{id}/generate/{name}` | Run `trail_card`, `elevation_map`, or `weather_overlay` |
|
||||
| `POST /api/routes/{id}/refresh-weather` | Manual Open-Meteo refresh (7 or 10 days) |
|
||||
| `GET /api/routes/{id}/export?format=gpx\|kml` | Enriched export download |
|
||||
| `POST /api/trails/scrape` / `POST /api/trails/upload-pdf` | Draft guide extraction (review before saving) |
|
||||
| `POST /api/trails` | Save a reviewed trail guide |
|
||||
| `POST /api/offline-maps` (+ `/estimate`) | Create offline tile packs |
|
||||
| `POST /api/sync` | Manual sync with the configured peer |
|
||||
@@ -0,0 +1,9 @@
|
||||
# Trekker environment configuration (copy to .env)
|
||||
TREKKER_DATA_DIR=data
|
||||
TREKKER_HOST_ROLE=home # home | neutron
|
||||
|
||||
# Optional: path to YAML config with watch/llm/tiles/sync sections
|
||||
# TREKKER_CONFIG=trekker.yml
|
||||
|
||||
# Neutron deployment only
|
||||
# TAILSCALE_AUTH_KEY=tskey-auth-...
|
||||
@@ -0,0 +1,9 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
venv/
|
||||
data/
|
||||
import/
|
||||
.env
|
||||
trekker.yml
|
||||
.pytest_cache/
|
||||
Whitespace-only changes.
@@ -0,0 +1,74 @@
|
||||
"""Trekker configuration: env vars + optional YAML file (TREKKER_CONFIG)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class WatchConfig(BaseModel):
|
||||
internal_path: Optional[str] = None
|
||||
remote_path: Optional[str] = None
|
||||
poll_interval: int = 30 # seconds, polling observer for remote mounts
|
||||
|
||||
|
||||
class LLMConfig(BaseModel):
|
||||
base_url: Optional[str] = None # e.g. http://localhost:11434/v1 for Ollama
|
||||
model: str = "gpt-4o-mini"
|
||||
api_key: Optional[str] = None
|
||||
|
||||
|
||||
class TilesConfig(BaseModel):
|
||||
# Raster basemap URL template. Default OSM is fine for interactive viewing;
|
||||
# bulk offline downloads need a source whose policy permits it (see PLAN.md),
|
||||
# hence the per-pack tile cap below.
|
||||
base_url: str = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
# Terrarium-encoded terrain tiles (AWS Open Data, bulk download permitted).
|
||||
terrain_url: str = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
|
||||
max_pack_tiles: int = 4000
|
||||
user_agent: str = "Trekker/0.1 (+https://github.com/claw-peyo/trekker)"
|
||||
|
||||
|
||||
class SyncConfig(BaseModel):
|
||||
peer_url: Optional[str] = None # e.g. http://neutron.tailnet:8000
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_prefix="TREKKER_", env_file=".env", extra="ignore")
|
||||
|
||||
data_dir: str = "data"
|
||||
host_role: str = "home" # home | neutron
|
||||
watch: WatchConfig = Field(default_factory=WatchConfig)
|
||||
llm: LLMConfig = Field(default_factory=LLMConfig)
|
||||
tiles: TilesConfig = Field(default_factory=TilesConfig)
|
||||
sync: SyncConfig = Field(default_factory=SyncConfig)
|
||||
|
||||
@property
|
||||
def db_path(self) -> str:
|
||||
return str(Path(self.data_dir) / "trekker.db")
|
||||
|
||||
@property
|
||||
def offline_maps_dir(self) -> str:
|
||||
return str(Path(self.data_dir) / "offline_maps")
|
||||
|
||||
@property
|
||||
def pdf_dir(self) -> str:
|
||||
return str(Path(self.data_dir) / "pdfs")
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
"""Env settings, overlaid with YAML from TREKKER_CONFIG if present."""
|
||||
overrides: dict = {}
|
||||
config_path = os.environ.get("TREKKER_CONFIG", "trekker.yml")
|
||||
path = Path(config_path)
|
||||
if path.is_file():
|
||||
overrides = yaml.safe_load(path.read_text()) or {}
|
||||
return Settings(**overrides)
|
||||
|
||||
|
||||
settings = load_settings()
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
"""Shared singletons wired at startup (store, services)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
from app.config import settings
|
||||
from app.services.elevation import ElevationService
|
||||
from app.services.llm import LLMService
|
||||
from app.services.offline_maps import OfflineMapService
|
||||
from app.storage.database import connect
|
||||
from app.storage.route_store import Store
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_store() -> Store:
|
||||
return Store(connect(settings.db_path))
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_llm() -> LLMService:
|
||||
return LLMService(settings.llm)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_elevation() -> ElevationService:
|
||||
return ElevationService()
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_offline_maps() -> OfflineMapService:
|
||||
return OfflineMapService(get_store())
|
||||
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)
|
||||
@@ -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})
|
||||
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)
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
"""Trekker FastAPI entry point."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from app.config import settings
|
||||
from app.deps import get_store
|
||||
from app.ingest.watchdog_daemon import WatchdogDaemon
|
||||
from app.routes import export, offline_maps, sync, trails, upload, weather
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
UI_DIR = Path(__file__).resolve().parent.parent / "ui"
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
daemon = WatchdogDaemon(
|
||||
get_store(),
|
||||
settings.watch.internal_path,
|
||||
settings.watch.remote_path,
|
||||
settings.watch.poll_interval,
|
||||
)
|
||||
daemon.start()
|
||||
yield
|
||||
daemon.stop()
|
||||
|
||||
|
||||
app = FastAPI(title="Trekker", version="0.1.0", lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(upload.router)
|
||||
app.include_router(trails.router)
|
||||
app.include_router(weather.router)
|
||||
app.include_router(export.router)
|
||||
app.include_router(offline_maps.router)
|
||||
app.include_router(sync.router)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
def health():
|
||||
return {"status": "ok", "version": app.version, "host_role": settings.host_role}
|
||||
|
||||
|
||||
@app.get("/api/config")
|
||||
def public_config():
|
||||
"""Non-secret config the UI needs."""
|
||||
return {
|
||||
"host_role": settings.host_role,
|
||||
"tiles_base_url": settings.tiles.base_url,
|
||||
"tiles_terrain_url": settings.tiles.terrain_url,
|
||||
"max_pack_tiles": settings.tiles.max_pack_tiles,
|
||||
"sync_peer_configured": bool(settings.sync.peer_url),
|
||||
"llm_configured": bool(settings.llm.api_key or settings.llm.base_url),
|
||||
"watch": {
|
||||
"internal_path": settings.watch.internal_path,
|
||||
"remote_path": settings.watch.remote_path,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
if UI_DIR.is_dir():
|
||||
app.mount("/app/static", StaticFiles(directory=UI_DIR), name="ui")
|
||||
|
||||
@app.get("/app")
|
||||
@app.get("/app/{path:path}")
|
||||
def spa(path: str = ""):
|
||||
return FileResponse(UI_DIR / "index.html")
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
return FileResponse(UI_DIR / "index.html")
|
||||
Whitespace-only changes.
@@ -0,0 +1,17 @@
|
||||
"""Generated asset model: structured JSON output from generators."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.route import new_id, utcnow_iso
|
||||
|
||||
|
||||
class Asset(BaseModel):
|
||||
id: str = Field(default_factory=new_id)
|
||||
route_id: str
|
||||
generator: str
|
||||
data: dict[str, Any] = Field(default_factory=dict)
|
||||
created_at: str = Field(default_factory=utcnow_iso)
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Offline map pack metadata."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.route import new_id, utcnow_iso
|
||||
|
||||
|
||||
class MapPack(BaseModel):
|
||||
id: str = Field(default_factory=new_id)
|
||||
name: str = "Offline area"
|
||||
bbox: list[float] = Field(default_factory=list) # [min_lon, min_lat, max_lon, max_lat]
|
||||
min_zoom: int = 8
|
||||
max_zoom: int = 14
|
||||
layers: list[str] = Field(default_factory=lambda: ["base"])
|
||||
status: str = "queued" # queued | downloading | ready | failed
|
||||
error: Optional[str] = None
|
||||
path: Optional[str] = None
|
||||
size_bytes: int = 0
|
||||
tile_count: int = 0
|
||||
tiles_done: int = 0
|
||||
created_at: str = Field(default_factory=utcnow_iso)
|
||||
updated_at: str = Field(default_factory=utcnow_iso)
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Canonical route models: TrekkerRoute and its sub-models."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
def utcnow_iso() -> str:
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
def new_id() -> str:
|
||||
return uuid.uuid4().hex
|
||||
|
||||
|
||||
class TrkPt(BaseModel):
|
||||
lat: float
|
||||
lon: float
|
||||
ele: Optional[float] = None
|
||||
time: Optional[str] = None # ISO 8601
|
||||
|
||||
|
||||
class Wpt(BaseModel):
|
||||
lat: float
|
||||
lon: float
|
||||
ele: Optional[float] = None
|
||||
time: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
desc: Optional[str] = None
|
||||
sym: Optional[str] = None
|
||||
type: Optional[str] = None
|
||||
|
||||
|
||||
class Segment(BaseModel):
|
||||
index: int
|
||||
name: str
|
||||
start_wpt: Optional[str] = None
|
||||
end_wpt: Optional[str] = None
|
||||
start_idx: int
|
||||
end_idx: int # inclusive index into route.points
|
||||
distance_m: float = 0.0
|
||||
elevation_gain_m: float = 0.0
|
||||
elevation_loss_m: float = 0.0
|
||||
grade_pct_avg: float = 0.0
|
||||
grade_pct_max: float = 0.0
|
||||
duration_s: Optional[float] = None
|
||||
|
||||
|
||||
class RouteStats(BaseModel):
|
||||
distance_m: float = 0.0
|
||||
elevation_gain_m: float = 0.0
|
||||
elevation_loss_m: float = 0.0
|
||||
elevation_min_m: Optional[float] = None
|
||||
elevation_max_m: Optional[float] = None
|
||||
duration_s: Optional[float] = None
|
||||
|
||||
|
||||
class TrekkerRoute(BaseModel):
|
||||
id: str = Field(default_factory=new_id)
|
||||
name: str = "Unnamed route"
|
||||
source: str = "manual" # gaia | onx | manual
|
||||
trail_guide_id: Optional[str] = None
|
||||
points: list[TrkPt] = Field(default_factory=list)
|
||||
waypoints: list[Wpt] = Field(default_factory=list)
|
||||
segments: list[Segment] = Field(default_factory=list)
|
||||
stats: RouteStats = Field(default_factory=RouteStats)
|
||||
metadata: dict[str, Any] = Field(default_factory=dict)
|
||||
created_at: str = Field(default_factory=utcnow_iso)
|
||||
updated_at: str = Field(default_factory=utcnow_iso)
|
||||
|
||||
def bounds(self) -> Optional[tuple[float, float, float, float]]:
|
||||
"""(min_lon, min_lat, max_lon, max_lat) or None for empty routes."""
|
||||
if not self.points:
|
||||
return None
|
||||
lats = [p.lat for p in self.points]
|
||||
lons = [p.lon for p in self.points]
|
||||
return (min(lons), min(lats), max(lons), max(lats))
|
||||
@@ -0,0 +1,34 @@
|
||||
"""TrailGuide: curated trail knowledge, separate from raw GPS routes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.route import new_id, utcnow_iso
|
||||
|
||||
|
||||
class WaterCrossing(BaseModel):
|
||||
name: Optional[str] = None
|
||||
depth_ft: Optional[float] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
class TrailGuide(BaseModel):
|
||||
id: str = Field(default_factory=new_id)
|
||||
name: str = "Unnamed trail guide"
|
||||
source: str = "manual" # web | pdf | manual
|
||||
source_url: Optional[str] = None
|
||||
pdf_path: Optional[str] = None
|
||||
difficulty: Optional[str] = None
|
||||
obstacle_types: list[str] = Field(default_factory=list)
|
||||
water_crossings: list[WaterCrossing] = Field(default_factory=list)
|
||||
gate_codes: list[str] = Field(default_factory=list)
|
||||
vehicle_recommendations: list[str] = Field(default_factory=list)
|
||||
closures: list[str] = Field(default_factory=list)
|
||||
warnings: list[str] = Field(default_factory=list)
|
||||
notes: Optional[str] = None
|
||||
raw_text: Optional[str] = None # extracted source text kept for re-review
|
||||
created_at: str = Field(default_factory=utcnow_iso)
|
||||
updated_at: str = Field(default_factory=utcnow_iso)
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Trip: a collection of routes with travelers and a date range."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.route import new_id, utcnow_iso
|
||||
|
||||
|
||||
class Trip(BaseModel):
|
||||
id: str = Field(default_factory=new_id)
|
||||
name: str = "Unnamed trip"
|
||||
route_ids: list[str] = Field(default_factory=list)
|
||||
travelers: list[str] = Field(default_factory=list)
|
||||
date_start: Optional[str] = None
|
||||
date_end: Optional[str] = None
|
||||
notes: Optional[str] = None
|
||||
generated_asset_ids: list[str] = Field(default_factory=list)
|
||||
metadata: dict[str, Any] = Field(default_factory=dict)
|
||||
created_at: str = Field(default_factory=utcnow_iso)
|
||||
updated_at: str = Field(default_factory=utcnow_iso)
|
||||
Whitespace-only changes.
@@ -0,0 +1,32 @@
|
||||
"""Export API: enriched GPX / KML downloads."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from fastapi.responses import Response
|
||||
|
||||
from app.deps import get_store
|
||||
from app.export.gpx_writer import write_gpx
|
||||
from app.export.kml_writer import write_kml
|
||||
|
||||
router = APIRouter(prefix="/api/routes", tags=["export"])
|
||||
|
||||
|
||||
@router.get("/{route_id}/export")
|
||||
def export_route(route_id: str, format: str = Query("gpx", pattern="^(gpx|kml)$")):
|
||||
store = get_store()
|
||||
route = store.get_route(route_id)
|
||||
if route is None:
|
||||
raise HTTPException(404, "route not found")
|
||||
guide = store.get_trail_guide(route.trail_guide_id) if route.trail_guide_id else None
|
||||
safe_name = re.sub(r"[^A-Za-z0-9_-]+", "_", route.name).strip("_") or "route"
|
||||
if format == "gpx":
|
||||
body, media = write_gpx(route, guide), "application/gpx+xml"
|
||||
else:
|
||||
body, media = write_kml(route, guide), "application/vnd.google-earth.kml+xml"
|
||||
return Response(
|
||||
content=body, media_type=media,
|
||||
headers={"Content-Disposition": f'attachment; filename="{safe_name}.{format}"'},
|
||||
)
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Offline map pack API: estimate, create, list, status, delete, tile serving."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.deps import get_offline_maps, get_store
|
||||
from app.services.offline_maps import estimate_tile_count
|
||||
|
||||
router = APIRouter(prefix="/api/offline-maps", tags=["offline-maps"])
|
||||
|
||||
AVG_TILE_BYTES = 25_000 # rough raster tile size for the pre-download estimate
|
||||
|
||||
|
||||
class PackBody(BaseModel):
|
||||
name: str = "Offline area"
|
||||
bbox: list[float] = Field(min_length=4, max_length=4) # [min_lon, min_lat, max_lon, max_lat]
|
||||
min_zoom: int = Field(8, ge=0, le=16)
|
||||
max_zoom: int = Field(14, ge=0, le=16)
|
||||
layers: list[str] = Field(default_factory=lambda: ["base", "terrain"])
|
||||
|
||||
|
||||
@router.post("/estimate")
|
||||
def estimate(body: PackBody):
|
||||
if body.max_zoom < body.min_zoom:
|
||||
raise HTTPException(400, "max_zoom must be >= min_zoom")
|
||||
count = estimate_tile_count(body.bbox, body.min_zoom, body.max_zoom) * len(body.layers)
|
||||
return {"tile_count": count, "estimated_bytes": count * AVG_TILE_BYTES}
|
||||
|
||||
|
||||
@router.post("")
|
||||
def create_pack(body: PackBody):
|
||||
if body.max_zoom < body.min_zoom:
|
||||
raise HTTPException(400, "max_zoom must be >= min_zoom")
|
||||
try:
|
||||
return get_offline_maps().create_pack(
|
||||
body.name, body.bbox, body.min_zoom, body.max_zoom, body.layers
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_packs():
|
||||
return get_store().list_map_packs()
|
||||
|
||||
|
||||
@router.get("/{pack_id}")
|
||||
def get_pack(pack_id: str):
|
||||
pack = get_store().get_map_pack(pack_id)
|
||||
if pack is None:
|
||||
raise HTTPException(404, "map pack not found")
|
||||
return pack
|
||||
|
||||
|
||||
@router.delete("/{pack_id}")
|
||||
def delete_pack(pack_id: str):
|
||||
if not get_offline_maps().delete_pack(pack_id):
|
||||
raise HTTPException(404, "map pack not found")
|
||||
return {"deleted": pack_id}
|
||||
|
||||
|
||||
@router.get("/tiles/{layer}/{z}/{x}/{y}.png")
|
||||
def serve_tile(layer: str, z: int, x: int, y: int):
|
||||
"""Serve a tile from any ready pack (the UI's offline tile source)."""
|
||||
path = get_offline_maps().find_tile(layer, z, x, y)
|
||||
if path is None:
|
||||
raise HTTPException(404, "tile not in any offline pack")
|
||||
return FileResponse(path, media_type="image/png")
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Sync API: manual trigger + bundle exchange endpoints (PLAN.md Phase 7)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.config import settings
|
||||
from app.deps import get_store
|
||||
from app.sync.sync_daemon import build_bundle, merge_bundle, sync_with_peer
|
||||
|
||||
router = APIRouter(prefix="/api/sync", tags=["sync"])
|
||||
|
||||
|
||||
@router.get("/bundle")
|
||||
def get_bundle():
|
||||
return build_bundle(get_store())
|
||||
|
||||
|
||||
@router.post("/bundle")
|
||||
def post_bundle(bundle: dict):
|
||||
return {"applied": merge_bundle(get_store(), bundle)}
|
||||
|
||||
|
||||
class SyncBody(BaseModel):
|
||||
peer_url: str | None = None
|
||||
|
||||
|
||||
@router.post("")
|
||||
def trigger_sync(body: SyncBody | None = None):
|
||||
peer = (body.peer_url if body else None) or settings.sync.peer_url
|
||||
if not peer:
|
||||
raise HTTPException(400, "no peer_url given and sync.peer_url not configured")
|
||||
try:
|
||||
return sync_with_peer(get_store(), peer)
|
||||
except Exception as e:
|
||||
raise HTTPException(502, f"sync with {peer} failed: {e}")
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Trail guide API: scrape, PDF extraction (drafts), review/save, CRUD."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException, UploadFile
|
||||
from pydantic import BaseModel
|
||||
from pypdf import PdfReader
|
||||
|
||||
from app.config import settings
|
||||
from app.deps import get_llm, get_store
|
||||
from app.models.trail_guide import TrailGuide
|
||||
from app.services.scraper import guide_from_extraction, scrape_trail_guide
|
||||
|
||||
router = APIRouter(prefix="/api/trails", tags=["trails"])
|
||||
|
||||
|
||||
class ScrapeBody(BaseModel):
|
||||
url: str
|
||||
|
||||
|
||||
@router.post("/scrape")
|
||||
def scrape(body: ScrapeBody) -> TrailGuide:
|
||||
"""Returns a DRAFT guide for review; client saves it via POST /api/trails."""
|
||||
try:
|
||||
return scrape_trail_guide(body.url, get_llm())
|
||||
except Exception as e:
|
||||
raise HTTPException(502, f"scrape failed: {e}")
|
||||
|
||||
|
||||
@router.post("/upload-pdf")
|
||||
async def upload_pdf(file: UploadFile) -> TrailGuide:
|
||||
"""PDF -> pypdf text -> LLM extraction -> DRAFT guide for review."""
|
||||
if not (file.filename or "").lower().endswith(".pdf"):
|
||||
raise HTTPException(400, "expected a .pdf file")
|
||||
raw = await file.read()
|
||||
pdf_dir = Path(settings.pdf_dir)
|
||||
pdf_dir.mkdir(parents=True, exist_ok=True)
|
||||
pdf_path = pdf_dir / Path(file.filename).name
|
||||
pdf_path.write_bytes(raw)
|
||||
try:
|
||||
reader = PdfReader(str(pdf_path))
|
||||
text = "\n".join((page.extract_text() or "") for page in reader.pages)
|
||||
except Exception as e:
|
||||
raise HTTPException(400, f"could not read PDF: {e}")
|
||||
llm = get_llm()
|
||||
extracted: dict = {}
|
||||
if llm.configured and text.strip():
|
||||
try:
|
||||
extracted = llm.extract_trail_data(text)
|
||||
except Exception:
|
||||
pass # user reviews from raw text instead
|
||||
return guide_from_extraction(
|
||||
extracted, source="pdf", pdf_path=str(pdf_path),
|
||||
raw_text=text[:50000], fallback_name=Path(file.filename).stem,
|
||||
)
|
||||
|
||||
|
||||
@router.post("")
|
||||
def save_guide(guide: TrailGuide) -> TrailGuide:
|
||||
"""Save a reviewed/edited guide (the manual-review confirmation step)."""
|
||||
return get_store().save_trail_guide(guide)
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_guides():
|
||||
return get_store().list_trail_guides()
|
||||
|
||||
|
||||
@router.get("/{guide_id}")
|
||||
def get_guide(guide_id: str) -> TrailGuide:
|
||||
guide = get_store().get_trail_guide(guide_id)
|
||||
if guide is None:
|
||||
raise HTTPException(404, "trail guide not found")
|
||||
return guide
|
||||
|
||||
|
||||
@router.delete("/{guide_id}")
|
||||
def delete_guide(guide_id: str):
|
||||
if not get_store().delete_trail_guide(guide_id):
|
||||
raise HTTPException(404, "trail guide not found")
|
||||
return {"deleted": guide_id}
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Route API: upload, list, get, delete, trail-guide linking, generators."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException, UploadFile
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.deps import get_elevation, get_store
|
||||
from app.generators import GENERATORS, GeneratorError
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.ingest.segmenter import build_segments, compute_stats
|
||||
|
||||
router = APIRouter(prefix="/api/routes", tags=["routes"])
|
||||
|
||||
|
||||
@router.post("/upload")
|
||||
async def upload_gpx(file: UploadFile):
|
||||
if not (file.filename or "").lower().endswith(".gpx"):
|
||||
raise HTTPException(400, "expected a .gpx file")
|
||||
raw = await file.read()
|
||||
try:
|
||||
route = parse_gpx(raw, filename=file.filename)
|
||||
except Exception as e:
|
||||
raise HTTPException(400, f"could not parse GPX: {e}")
|
||||
if not route.points:
|
||||
raise HTTPException(400, "GPX contains no track or route points")
|
||||
# Fill elevations from terrain tiles when the exporting app omitted them.
|
||||
if any(p.ele is None for p in route.points):
|
||||
try:
|
||||
if get_elevation().fill_missing_elevations(route.points):
|
||||
route.segments = build_segments(route)
|
||||
route.stats = compute_stats(route)
|
||||
except Exception:
|
||||
pass # offline or tiles unavailable; elevation stays absent
|
||||
return get_store().save_route(route)
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_routes():
|
||||
return [
|
||||
{"id": r.id, "name": r.name, "source": r.source,
|
||||
"trail_guide_id": r.trail_guide_id, "stats": r.stats,
|
||||
"bounds": r.bounds(), "created_at": r.created_at}
|
||||
for r in get_store().list_routes()
|
||||
]
|
||||
|
||||
|
||||
@router.get("/{route_id}")
|
||||
def get_route(route_id: str):
|
||||
route = get_store().get_route(route_id)
|
||||
if route is None:
|
||||
raise HTTPException(404, "route not found")
|
||||
return route
|
||||
|
||||
|
||||
@router.delete("/{route_id}")
|
||||
def delete_route(route_id: str):
|
||||
if not get_store().delete_route(route_id):
|
||||
raise HTTPException(404, "route not found")
|
||||
return {"deleted": route_id}
|
||||
|
||||
|
||||
class LinkBody(BaseModel):
|
||||
trail_guide_id: str | None = None
|
||||
|
||||
|
||||
@router.put("/{route_id}/trail-guide")
|
||||
def link_trail_guide(route_id: str, body: LinkBody):
|
||||
try:
|
||||
route = get_store().link_trail_guide(route_id, body.trail_guide_id)
|
||||
except KeyError as e:
|
||||
raise HTTPException(404, str(e))
|
||||
if route is None:
|
||||
raise HTTPException(404, "route not found")
|
||||
return route
|
||||
|
||||
|
||||
@router.post("/{route_id}/generate/{generator_name}")
|
||||
def generate_asset(route_id: str, generator_name: str):
|
||||
store = get_store()
|
||||
route = store.get_route(route_id)
|
||||
if route is None:
|
||||
raise HTTPException(404, "route not found")
|
||||
gen_cls = GENERATORS.get(generator_name)
|
||||
if gen_cls is None:
|
||||
raise HTTPException(404, f"unknown generator; available: {sorted(GENERATORS)}")
|
||||
guide = store.get_trail_guide(route.trail_guide_id) if route.trail_guide_id else None
|
||||
weather = store.get_weather(route_id)
|
||||
try:
|
||||
asset = gen_cls().generate(route, guide, weather)
|
||||
except GeneratorError as e:
|
||||
raise HTTPException(409, str(e))
|
||||
return store.save_asset(asset)
|
||||
|
||||
|
||||
@router.get("/{route_id}/assets")
|
||||
def list_assets(route_id: str):
|
||||
return get_store().list_assets(route_id)
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Weather API: manual refresh + cached read (PLAN.md 4c: no auto-refetch)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
from app.deps import get_store
|
||||
from app.services.weather import fetch_route_weather
|
||||
|
||||
router = APIRouter(prefix="/api/routes", tags=["weather"])
|
||||
|
||||
|
||||
@router.post("/{route_id}/refresh-weather")
|
||||
def refresh_weather(route_id: str, days: int = Query(7)):
|
||||
store = get_store()
|
||||
route = store.get_route(route_id)
|
||||
if route is None:
|
||||
raise HTTPException(404, "route not found")
|
||||
if days not in (7, 10):
|
||||
raise HTTPException(400, "days must be 7 or 10")
|
||||
try:
|
||||
weather = fetch_route_weather(route, days)
|
||||
except Exception as e:
|
||||
raise HTTPException(502, f"weather fetch failed: {e}")
|
||||
store.save_weather(route_id, weather)
|
||||
return store.get_weather(route_id)
|
||||
|
||||
|
||||
@router.get("/{route_id}/weather")
|
||||
def get_weather(route_id: str):
|
||||
weather = get_store().get_weather(route_id)
|
||||
if weather is None:
|
||||
raise HTTPException(404, "no cached weather; refresh first")
|
||||
return weather
|
||||
Whitespace-only changes.
@@ -0,0 +1,82 @@
|
||||
"""Elevation lookup from Terrarium-encoded terrain tiles (AWS Open Data).
|
||||
|
||||
decode: elevation_m = (R * 256 + G + B / 256) - 32768
|
||||
Tiles are cached on disk under data_dir/terrain so offline packs and the
|
||||
segmenter share the same cache.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import logging
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
from PIL import Image
|
||||
|
||||
from app.config import settings
|
||||
from app.models.route import TrkPt
|
||||
|
||||
log = logging.getLogger("trekker.elevation")
|
||||
|
||||
TILE_SIZE = 256
|
||||
DEFAULT_ZOOM = 12
|
||||
|
||||
|
||||
def lonlat_to_tile(lon: float, lat: float, zoom: int) -> tuple[int, int, float, float]:
|
||||
"""Tile x/y plus fractional pixel offset within the tile."""
|
||||
n = 2**zoom
|
||||
xf = (lon + 180.0) / 360.0 * n
|
||||
lat_r = math.radians(lat)
|
||||
yf = (1.0 - math.asinh(math.tan(lat_r)) / math.pi) / 2.0 * n
|
||||
x, y = int(xf), int(yf)
|
||||
return x, y, (xf - x) * TILE_SIZE, (yf - y) * TILE_SIZE
|
||||
|
||||
|
||||
class ElevationService:
|
||||
def __init__(self, cache_dir: str | None = None, terrain_url: str | None = None):
|
||||
self.terrain_url = terrain_url or settings.tiles.terrain_url
|
||||
self.cache_dir = Path(cache_dir or Path(settings.data_dir) / "terrain")
|
||||
self._mem: dict[tuple[int, int, int], Image.Image] = {}
|
||||
|
||||
def _tile(self, z: int, x: int, y: int) -> Image.Image | None:
|
||||
key = (z, x, y)
|
||||
if key in self._mem:
|
||||
return self._mem[key]
|
||||
path = self.cache_dir / str(z) / str(x) / f"{y}.png"
|
||||
if not path.is_file():
|
||||
url = self.terrain_url.format(z=z, x=x, y=y)
|
||||
try:
|
||||
resp = httpx.get(url, timeout=30,
|
||||
headers={"User-Agent": settings.tiles.user_agent})
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPError as e:
|
||||
log.warning("terrain tile %s/%s/%s fetch failed: %s", z, x, y, e)
|
||||
return None
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(resp.content)
|
||||
img = Image.open(io.BytesIO(path.read_bytes())).convert("RGB")
|
||||
if len(self._mem) > 64:
|
||||
self._mem.clear()
|
||||
self._mem[key] = img
|
||||
return img
|
||||
|
||||
def elevation_at(self, lat: float, lon: float, zoom: int = DEFAULT_ZOOM) -> float | None:
|
||||
x, y, px, py = lonlat_to_tile(lon, lat, zoom)
|
||||
img = self._tile(zoom, x, y)
|
||||
if img is None:
|
||||
return None
|
||||
r, g, b = img.getpixel((min(int(px), TILE_SIZE - 1), min(int(py), TILE_SIZE - 1)))[:3]
|
||||
return (r * 256 + g + b / 256) - 32768
|
||||
|
||||
def fill_missing_elevations(self, points: list[TrkPt], zoom: int = DEFAULT_ZOOM) -> int:
|
||||
"""Populate ele on points lacking it. Returns count filled."""
|
||||
filled = 0
|
||||
for p in points:
|
||||
if p.ele is None:
|
||||
ele = self.elevation_at(p.lat, p.lon, zoom)
|
||||
if ele is not None:
|
||||
p.ele = round(ele, 1)
|
||||
filled += 1
|
||||
return filled
|
||||
@@ -0,0 +1,76 @@
|
||||
"""OpenAI-compatible LLM adapter for trail guide extraction.
|
||||
|
||||
Works with any OpenAI-compatible endpoint (Ollama, Groq, Gemini, OpenAI)
|
||||
via configurable base_url / model / api_key.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
from app.config import LLMConfig
|
||||
|
||||
log = logging.getLogger("trekker.llm")
|
||||
|
||||
EXTRACTION_PROMPT = """You extract structured overland trail data from text.
|
||||
Return ONLY a JSON object with these keys (use null/[] when not mentioned):
|
||||
{
|
||||
"name": string,
|
||||
"difficulty": string|null, // e.g. "easy", "moderate", "difficult", "extreme", or a rating like "7/10"
|
||||
"obstacle_types": [string], // e.g. "rock garden", "ledges", "mud", "off-camber"
|
||||
"water_crossings": [{"name": string|null, "depth_ft": number|null, "notes": string|null}],
|
||||
"gate_codes": [string],
|
||||
"vehicle_recommendations": [string],// e.g. "33-inch tires", "lockers recommended", "winch"
|
||||
"closures": [string], // seasonal or permanent closure notes
|
||||
"warnings": [string],
|
||||
"notes": string|null
|
||||
}"""
|
||||
|
||||
|
||||
class LLMService:
|
||||
def __init__(self, config: LLMConfig):
|
||||
self.config = config
|
||||
|
||||
@property
|
||||
def configured(self) -> bool:
|
||||
return bool(self.config.api_key or self.config.base_url)
|
||||
|
||||
def _client(self) -> OpenAI:
|
||||
return OpenAI(
|
||||
base_url=self.config.base_url or None,
|
||||
api_key=self.config.api_key or "not-needed", # local providers ignore the key
|
||||
)
|
||||
|
||||
def extract_trail_data(self, text: str) -> dict[str, Any]:
|
||||
"""Trail guide text -> structured dict. Raises on provider errors."""
|
||||
client = self._client()
|
||||
# Keep prompts inside small local-model context windows.
|
||||
snippet = text[:24000]
|
||||
kwargs: dict[str, Any] = {
|
||||
"model": self.config.model,
|
||||
"messages": [
|
||||
{"role": "system", "content": EXTRACTION_PROMPT},
|
||||
{"role": "user", "content": snippet},
|
||||
],
|
||||
}
|
||||
try:
|
||||
resp = client.chat.completions.create(
|
||||
**kwargs, response_format={"type": "json_object"}
|
||||
)
|
||||
except Exception:
|
||||
# Some providers reject response_format; retry without it.
|
||||
resp = client.chat.completions.create(**kwargs)
|
||||
content = resp.choices[0].message.content or "{}"
|
||||
# Tolerate code-fenced JSON from smaller models.
|
||||
content = content.strip()
|
||||
if content.startswith("```"):
|
||||
content = content.strip("`")
|
||||
content = content.split("\n", 1)[1] if "\n" in content else content
|
||||
start, end = content.find("{"), content.rfind("}")
|
||||
if start != -1 and end > start:
|
||||
content = content[start : end + 1]
|
||||
return json.loads(content)
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Offline map packs: estimate, download, and serve XYZ tiles locally.
|
||||
|
||||
Tiles are stored under data/offline_maps/{pack_id}/{layer}/{z}/{x}/{y}.png.
|
||||
Downloads run in a background thread and update pack status in SQLite.
|
||||
A per-pack tile cap (settings.tiles.max_pack_tiles) keeps usage within
|
||||
tile-provider policies; see PLAN.md Phase 2 gate notes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
import shutil
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
from app.models.map_pack import MapPack
|
||||
from app.storage.route_store import Store
|
||||
|
||||
log = logging.getLogger("trekker.offline_maps")
|
||||
|
||||
LAYER_URLS = {
|
||||
"base": lambda: settings.tiles.base_url,
|
||||
"terrain": lambda: settings.tiles.terrain_url,
|
||||
}
|
||||
|
||||
|
||||
def _tile_range(bbox: list[float], zoom: int) -> tuple[int, int, int, int]:
|
||||
"""(x_min, x_max, y_min, y_max) tile indices covering bbox at zoom."""
|
||||
min_lon, min_lat, max_lon, max_lat = bbox
|
||||
n = 2**zoom
|
||||
|
||||
def x_of(lon: float) -> int:
|
||||
return min(n - 1, max(0, int((lon + 180.0) / 360.0 * n)))
|
||||
|
||||
def y_of(lat: float) -> int:
|
||||
lat = max(-85.05112878, min(85.05112878, lat))
|
||||
lat_r = math.radians(lat)
|
||||
return min(n - 1, max(0, int((1.0 - math.asinh(math.tan(lat_r)) / math.pi) / 2.0 * n)))
|
||||
|
||||
return x_of(min_lon), x_of(max_lon), y_of(max_lat), y_of(min_lat)
|
||||
|
||||
|
||||
def iter_tiles(bbox: list[float], min_zoom: int, max_zoom: int):
|
||||
for z in range(min_zoom, max_zoom + 1):
|
||||
x0, x1, y0, y1 = _tile_range(bbox, z)
|
||||
for x in range(x0, x1 + 1):
|
||||
for y in range(y0, y1 + 1):
|
||||
yield z, x, y
|
||||
|
||||
|
||||
def estimate_tile_count(bbox: list[float], min_zoom: int, max_zoom: int) -> int:
|
||||
total = 0
|
||||
for z in range(min_zoom, max_zoom + 1):
|
||||
x0, x1, y0, y1 = _tile_range(bbox, z)
|
||||
total += (x1 - x0 + 1) * (y1 - y0 + 1)
|
||||
return total
|
||||
|
||||
|
||||
class OfflineMapService:
|
||||
def __init__(self, store: Store, base_dir: str | None = None):
|
||||
self.store = store
|
||||
self.base_dir = Path(base_dir or settings.offline_maps_dir)
|
||||
|
||||
def pack_dir(self, pack_id: str) -> Path:
|
||||
return self.base_dir / pack_id
|
||||
|
||||
def create_pack(self, name: str, bbox: list[float], min_zoom: int,
|
||||
max_zoom: int, layers: list[str]) -> MapPack:
|
||||
layers = [l for l in layers if l in LAYER_URLS] or ["base"]
|
||||
count = estimate_tile_count(bbox, min_zoom, max_zoom) * len(layers)
|
||||
if count > settings.tiles.max_pack_tiles:
|
||||
raise ValueError(
|
||||
f"pack would need {count} tiles, over the cap of "
|
||||
f"{settings.tiles.max_pack_tiles}; shrink the area or zoom range"
|
||||
)
|
||||
pack = MapPack(name=name, bbox=bbox, min_zoom=min_zoom, max_zoom=max_zoom,
|
||||
layers=layers, status="queued", tile_count=count)
|
||||
pack.path = str(self.pack_dir(pack.id))
|
||||
self.store.save_map_pack(pack)
|
||||
threading.Thread(target=self._download, args=(pack.id,), daemon=True).start()
|
||||
return pack
|
||||
|
||||
def _download(self, pack_id: str) -> None:
|
||||
pack = self.store.get_map_pack(pack_id)
|
||||
if pack is None:
|
||||
return
|
||||
pack.status = "downloading"
|
||||
self.store.save_map_pack(pack)
|
||||
size = 0
|
||||
done = 0
|
||||
try:
|
||||
with httpx.Client(timeout=30, headers={"User-Agent": settings.tiles.user_agent}) as client:
|
||||
for layer in pack.layers:
|
||||
url_tpl = LAYER_URLS[layer]()
|
||||
for z, x, y in iter_tiles(pack.bbox, pack.min_zoom, pack.max_zoom):
|
||||
dest = self.pack_dir(pack.id) / layer / str(z) / str(x) / f"{y}.png"
|
||||
if not dest.is_file():
|
||||
resp = client.get(url_tpl.format(z=z, x=x, y=y))
|
||||
resp.raise_for_status()
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_bytes(resp.content)
|
||||
time.sleep(0.05) # be polite to public tile servers
|
||||
size += dest.stat().st_size
|
||||
done += 1
|
||||
if done % 50 == 0:
|
||||
pack.tiles_done, pack.size_bytes = done, size
|
||||
self.store.save_map_pack(pack)
|
||||
pack.tiles_done, pack.size_bytes, pack.status = done, size, "ready"
|
||||
except Exception as e:
|
||||
log.exception("pack %s download failed", pack_id)
|
||||
pack.status, pack.error = "failed", str(e)
|
||||
self.store.save_map_pack(pack)
|
||||
|
||||
def tile_path(self, pack_id: str, layer: str, z: int, x: int, y: int) -> Path | None:
|
||||
p = self.pack_dir(pack_id) / layer / str(z) / str(x) / f"{y}.png"
|
||||
return p if p.is_file() else None
|
||||
|
||||
def find_tile(self, layer: str, z: int, x: int, y: int) -> Path | None:
|
||||
"""Search all ready packs for a tile (offline fallback serving)."""
|
||||
for pack in self.store.list_map_packs():
|
||||
if pack.status != "ready":
|
||||
continue
|
||||
p = self.tile_path(pack.id, layer, z, x, y)
|
||||
if p:
|
||||
return p
|
||||
return None
|
||||
|
||||
def delete_pack(self, pack_id: str) -> bool:
|
||||
ok = self.store.delete_map_pack(pack_id)
|
||||
shutil.rmtree(self.pack_dir(pack_id), ignore_errors=True)
|
||||
return ok
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Trail guide web scrape: URL -> page text -> draft TrailGuide.
|
||||
|
||||
Uses httpx + BeautifulSoup. If an LLM is configured the text is run through
|
||||
structured extraction; otherwise the user fills the review form from raw text.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from app.models.trail_guide import TrailGuide, WaterCrossing
|
||||
from app.services.llm import LLMService
|
||||
|
||||
log = logging.getLogger("trekker.scraper")
|
||||
|
||||
|
||||
def fetch_page_text(url: str) -> tuple[str, str]:
|
||||
"""Returns (title, readable_text)."""
|
||||
resp = httpx.get(url, timeout=30, follow_redirects=True,
|
||||
headers={"User-Agent": "Trekker/0.1 trail guide fetch"})
|
||||
resp.raise_for_status()
|
||||
soup = BeautifulSoup(resp.text, "html.parser")
|
||||
for tag in soup(["script", "style", "nav", "footer", "header", "noscript"]):
|
||||
tag.decompose()
|
||||
title = soup.title.get_text(strip=True) if soup.title else url
|
||||
text = "\n".join(
|
||||
line for line in (ln.strip() for ln in soup.get_text("\n").splitlines()) if line
|
||||
)
|
||||
return title, text
|
||||
|
||||
|
||||
def guide_from_extraction(extracted: dict, *, source: str, source_url: str | None = None,
|
||||
pdf_path: str | None = None, raw_text: str | None = None,
|
||||
fallback_name: str = "Unnamed trail guide") -> TrailGuide:
|
||||
crossings = [
|
||||
WaterCrossing(**wc) if isinstance(wc, dict) else WaterCrossing(notes=str(wc))
|
||||
for wc in extracted.get("water_crossings") or []
|
||||
]
|
||||
return TrailGuide(
|
||||
name=extracted.get("name") or fallback_name,
|
||||
source=source,
|
||||
source_url=source_url,
|
||||
pdf_path=pdf_path,
|
||||
difficulty=extracted.get("difficulty"),
|
||||
obstacle_types=[str(o) for o in extracted.get("obstacle_types") or []],
|
||||
water_crossings=crossings,
|
||||
gate_codes=[str(g) for g in extracted.get("gate_codes") or []],
|
||||
vehicle_recommendations=[str(v) for v in extracted.get("vehicle_recommendations") or []],
|
||||
closures=[str(c) for c in extracted.get("closures") or []],
|
||||
warnings=[str(w) for w in extracted.get("warnings") or []],
|
||||
notes=extracted.get("notes"),
|
||||
raw_text=raw_text,
|
||||
)
|
||||
|
||||
|
||||
def scrape_trail_guide(url: str, llm: LLMService) -> TrailGuide:
|
||||
title, text = fetch_page_text(url)
|
||||
extracted: dict = {}
|
||||
if llm.configured and text:
|
||||
try:
|
||||
extracted = llm.extract_trail_data(text)
|
||||
except Exception:
|
||||
log.exception("LLM extraction failed for %s; returning raw-text draft", url)
|
||||
return guide_from_extraction(
|
||||
extracted, source="web", source_url=url,
|
||||
raw_text=text[:50000], fallback_name=title,
|
||||
)
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Open-Meteo client and per-segment aggregation into four tiers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from app.models.route import TrekkerRoute
|
||||
|
||||
API_URL = "https://api.open-meteo.com/v1/forecast"
|
||||
HOURLY_VARS = "temperature_2m,precipitation_probability,precipitation,wind_speed_10m,wind_gusts_10m,weather_code"
|
||||
DAILY_VARS = "temperature_2m_max,temperature_2m_min,precipitation_probability_max,precipitation_sum,wind_speed_10m_max,weather_code"
|
||||
|
||||
# Open-Meteo WMO codes indicating thunderstorms (lightning risk)
|
||||
THUNDER_CODES = {95, 96, 99}
|
||||
WIND_ADVISORY_KMH = 40.0
|
||||
|
||||
|
||||
def fetch_forecast(lat: float, lon: float, days: int = 7) -> dict[str, Any]:
|
||||
if days not in (7, 10):
|
||||
raise ValueError("forecast window must be 7 or 10 days")
|
||||
resp = httpx.get(
|
||||
API_URL,
|
||||
params={
|
||||
"latitude": round(lat, 4),
|
||||
"longitude": round(lon, 4),
|
||||
"hourly": HOURLY_VARS,
|
||||
"daily": DAILY_VARS,
|
||||
"forecast_days": days,
|
||||
"timezone": "auto",
|
||||
"wind_speed_unit": "kmh",
|
||||
"temperature_unit": "fahrenheit",
|
||||
"precipitation_unit": "inch",
|
||||
},
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
def _chunk_hours(hourly: dict[str, list], hours_per_bucket: int) -> list[dict[str, Any]]:
|
||||
times = hourly.get("time", [])
|
||||
buckets = []
|
||||
for start in range(0, len(times), hours_per_bucket):
|
||||
end = min(start + hours_per_bucket, len(times))
|
||||
sl = slice(start, end)
|
||||
|
||||
def vals(key: str) -> list[float]:
|
||||
return [v for v in hourly.get(key, [])[sl] if v is not None]
|
||||
|
||||
temps = vals("temperature_2m")
|
||||
winds = vals("wind_speed_10m")
|
||||
codes = vals("weather_code")
|
||||
precip_prob = vals("precipitation_probability")
|
||||
buckets.append({
|
||||
"time_start": times[start],
|
||||
"time_end": times[end - 1],
|
||||
"temp_min_f": min(temps) if temps else None,
|
||||
"temp_max_f": max(temps) if temps else None,
|
||||
"precip_prob_max": max(precip_prob) if precip_prob else None,
|
||||
"precip_sum_in": round(sum(vals("precipitation")), 3),
|
||||
"wind_max_kmh": max(winds) if winds else None,
|
||||
"wind_advisory": bool(winds) and max(winds) >= WIND_ADVISORY_KMH,
|
||||
"lightning_risk": any(int(c) in THUNDER_CODES for c in codes),
|
||||
})
|
||||
return buckets
|
||||
|
||||
|
||||
def build_tiers(forecast: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Hourly / 6h / 12h / daily tiers from a raw Open-Meteo response."""
|
||||
hourly = forecast.get("hourly", {})
|
||||
daily = forecast.get("daily", {})
|
||||
return {
|
||||
"hourly": _chunk_hours(hourly, 1),
|
||||
"6h": _chunk_hours(hourly, 6),
|
||||
"12h": _chunk_hours(hourly, 12),
|
||||
"daily": [
|
||||
{
|
||||
"date": daily["time"][i],
|
||||
"temp_min_f": daily["temperature_2m_min"][i],
|
||||
"temp_max_f": daily["temperature_2m_max"][i],
|
||||
"precip_prob_max": daily["precipitation_probability_max"][i],
|
||||
"precip_sum_in": daily["precipitation_sum"][i],
|
||||
"wind_max_kmh": daily["wind_speed_10m_max"][i],
|
||||
"lightning_risk": int(daily["weather_code"][i]) in THUNDER_CODES,
|
||||
}
|
||||
for i in range(len(daily.get("time", [])))
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def segment_midpoints(route: TrekkerRoute) -> list[tuple[int, float, float]]:
|
||||
out = []
|
||||
for seg in route.segments:
|
||||
mid = route.points[(seg.start_idx + seg.end_idx) // 2]
|
||||
out.append((seg.index, mid.lat, mid.lon))
|
||||
return out
|
||||
|
||||
|
||||
def fetch_route_weather(route: TrekkerRoute, days: int = 7) -> dict[str, Any]:
|
||||
"""Per-segment forecasts. Nearby midpoints share one fetch (0.05° grid)."""
|
||||
cache: dict[tuple[float, float], dict] = {}
|
||||
segments = {}
|
||||
for idx, lat, lon in segment_midpoints(route):
|
||||
key = (round(lat / 0.05) * 0.05, round(lon / 0.05) * 0.05)
|
||||
if key not in cache:
|
||||
cache[key] = build_tiers(fetch_forecast(lat, lon, days))
|
||||
segments[str(idx)] = cache[key]
|
||||
return {"days": days, "segments": segments}
|
||||
Whitespace-only changes.
@@ -0,0 +1,64 @@
|
||||
"""SQLite schema and connection management. JSON-blob storage per PLAN.md 1d."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS routes (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
source TEXT NOT NULL,
|
||||
data JSON NOT NULL,
|
||||
trail_guide_id TEXT REFERENCES trail_guides(id) ON DELETE SET NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS trail_guides (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
source_url TEXT,
|
||||
pdf_path TEXT,
|
||||
data JSON NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS assets (
|
||||
id TEXT PRIMARY KEY,
|
||||
route_id TEXT NOT NULL REFERENCES routes(id) ON DELETE CASCADE,
|
||||
generator TEXT NOT NULL,
|
||||
data JSON NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS weather_cache (
|
||||
route_id TEXT PRIMARY KEY REFERENCES routes(id) ON DELETE CASCADE,
|
||||
data JSON NOT NULL,
|
||||
fetched_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS offline_map_packs (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
bbox JSON NOT NULL,
|
||||
min_zoom INTEGER NOT NULL,
|
||||
max_zoom INTEGER NOT NULL,
|
||||
layers JSON NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
path TEXT,
|
||||
size_bytes INTEGER DEFAULT 0,
|
||||
tile_count INTEGER DEFAULT 0,
|
||||
data JSON NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
def connect(db_path: str) -> sqlite3.Connection:
|
||||
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(db_path, check_same_thread=False)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.executescript(SCHEMA)
|
||||
return conn
|
||||
@@ -0,0 +1,160 @@
|
||||
"""CRUD stores over the SQLite JSON-blob tables."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
import threading
|
||||
from typing import Optional
|
||||
|
||||
from app.models.assets import Asset
|
||||
from app.models.map_pack import MapPack
|
||||
from app.models.route import TrekkerRoute, utcnow_iso
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
class Store:
|
||||
"""All-table store. Serializes writes with a lock (SQLite, multi-thread app)."""
|
||||
|
||||
def __init__(self, conn: sqlite3.Connection):
|
||||
self.conn = conn
|
||||
self.lock = threading.Lock()
|
||||
|
||||
# -- routes ------------------------------------------------------------
|
||||
|
||||
def save_route(self, route: TrekkerRoute) -> TrekkerRoute:
|
||||
route.updated_at = utcnow_iso()
|
||||
with self.lock, self.conn:
|
||||
self.conn.execute(
|
||||
"""INSERT INTO routes (id, name, source, data, trail_guide_id, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name=excluded.name, source=excluded.source, data=excluded.data,
|
||||
trail_guide_id=excluded.trail_guide_id, updated_at=excluded.updated_at""",
|
||||
(route.id, route.name, route.source, route.model_dump_json(),
|
||||
route.trail_guide_id, route.created_at, route.updated_at),
|
||||
)
|
||||
return route
|
||||
|
||||
def get_route(self, route_id: str) -> Optional[TrekkerRoute]:
|
||||
row = self.conn.execute("SELECT data FROM routes WHERE id=?", (route_id,)).fetchone()
|
||||
return TrekkerRoute.model_validate_json(row["data"]) if row else None
|
||||
|
||||
def list_routes(self) -> list[TrekkerRoute]:
|
||||
rows = self.conn.execute("SELECT data FROM routes ORDER BY created_at DESC").fetchall()
|
||||
return [TrekkerRoute.model_validate_json(r["data"]) for r in rows]
|
||||
|
||||
def delete_route(self, route_id: str) -> bool:
|
||||
with self.lock, self.conn:
|
||||
cur = self.conn.execute("DELETE FROM routes WHERE id=?", (route_id,))
|
||||
return cur.rowcount > 0
|
||||
|
||||
def link_trail_guide(self, route_id: str, guide_id: Optional[str]) -> Optional[TrekkerRoute]:
|
||||
route = self.get_route(route_id)
|
||||
if route is None:
|
||||
return None
|
||||
if guide_id is not None and self.get_trail_guide(guide_id) is None:
|
||||
raise KeyError(f"trail guide {guide_id} not found")
|
||||
route.trail_guide_id = guide_id
|
||||
return self.save_route(route)
|
||||
|
||||
# -- trail guides --------------------------------------------------------
|
||||
|
||||
def save_trail_guide(self, guide: TrailGuide) -> TrailGuide:
|
||||
guide.updated_at = utcnow_iso()
|
||||
with self.lock, self.conn:
|
||||
self.conn.execute(
|
||||
"""INSERT INTO trail_guides (id, name, source_url, pdf_path, data, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name=excluded.name, source_url=excluded.source_url, pdf_path=excluded.pdf_path,
|
||||
data=excluded.data, updated_at=excluded.updated_at""",
|
||||
(guide.id, guide.name, guide.source_url, guide.pdf_path,
|
||||
guide.model_dump_json(), guide.created_at, guide.updated_at),
|
||||
)
|
||||
return guide
|
||||
|
||||
def get_trail_guide(self, guide_id: str) -> Optional[TrailGuide]:
|
||||
row = self.conn.execute("SELECT data FROM trail_guides WHERE id=?", (guide_id,)).fetchone()
|
||||
return TrailGuide.model_validate_json(row["data"]) if row else None
|
||||
|
||||
def list_trail_guides(self) -> list[TrailGuide]:
|
||||
rows = self.conn.execute("SELECT data FROM trail_guides ORDER BY created_at DESC").fetchall()
|
||||
return [TrailGuide.model_validate_json(r["data"]) for r in rows]
|
||||
|
||||
def delete_trail_guide(self, guide_id: str) -> bool:
|
||||
with self.lock, self.conn:
|
||||
cur = self.conn.execute("DELETE FROM trail_guides WHERE id=?", (guide_id,))
|
||||
return cur.rowcount > 0
|
||||
|
||||
# -- assets ---------------------------------------------------------------
|
||||
|
||||
def save_asset(self, asset: Asset) -> Asset:
|
||||
with self.lock, self.conn:
|
||||
self.conn.execute(
|
||||
"""INSERT INTO assets (id, route_id, generator, data, created_at)
|
||||
VALUES (?,?,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET data=excluded.data""",
|
||||
(asset.id, asset.route_id, asset.generator,
|
||||
asset.model_dump_json(), asset.created_at),
|
||||
)
|
||||
return asset
|
||||
|
||||
def list_assets(self, route_id: str) -> list[Asset]:
|
||||
rows = self.conn.execute(
|
||||
"SELECT data FROM assets WHERE route_id=? ORDER BY created_at DESC", (route_id,)
|
||||
).fetchall()
|
||||
return [Asset.model_validate_json(r["data"]) for r in rows]
|
||||
|
||||
# -- weather cache ----------------------------------------------------------
|
||||
|
||||
def save_weather(self, route_id: str, data: dict) -> None:
|
||||
with self.lock, self.conn:
|
||||
self.conn.execute(
|
||||
"""INSERT INTO weather_cache (route_id, data, fetched_at) VALUES (?,?,?)
|
||||
ON CONFLICT(route_id) DO UPDATE SET data=excluded.data, fetched_at=excluded.fetched_at""",
|
||||
(route_id, json.dumps(data), utcnow_iso()),
|
||||
)
|
||||
|
||||
def get_weather(self, route_id: str) -> Optional[dict]:
|
||||
row = self.conn.execute(
|
||||
"SELECT data, fetched_at FROM weather_cache WHERE route_id=?", (route_id,)
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
data = json.loads(row["data"])
|
||||
data["fetched_at"] = row["fetched_at"]
|
||||
return data
|
||||
|
||||
# -- offline map packs -----------------------------------------------------
|
||||
|
||||
def save_map_pack(self, pack: MapPack) -> MapPack:
|
||||
pack.updated_at = utcnow_iso()
|
||||
with self.lock, self.conn:
|
||||
self.conn.execute(
|
||||
"""INSERT INTO offline_map_packs
|
||||
(id, name, bbox, min_zoom, max_zoom, layers, status, path,
|
||||
size_bytes, tile_count, data, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name=excluded.name, status=excluded.status, path=excluded.path,
|
||||
size_bytes=excluded.size_bytes, tile_count=excluded.tile_count,
|
||||
data=excluded.data, updated_at=excluded.updated_at""",
|
||||
(pack.id, pack.name, json.dumps(pack.bbox), pack.min_zoom, pack.max_zoom,
|
||||
json.dumps(pack.layers), pack.status, pack.path, pack.size_bytes,
|
||||
pack.tile_count, pack.model_dump_json(), pack.created_at, pack.updated_at),
|
||||
)
|
||||
return pack
|
||||
|
||||
def get_map_pack(self, pack_id: str) -> Optional[MapPack]:
|
||||
row = self.conn.execute("SELECT data FROM offline_map_packs WHERE id=?", (pack_id,)).fetchone()
|
||||
return MapPack.model_validate_json(row["data"]) if row else None
|
||||
|
||||
def list_map_packs(self) -> list[MapPack]:
|
||||
rows = self.conn.execute("SELECT data FROM offline_map_packs ORDER BY created_at DESC").fetchall()
|
||||
return [MapPack.model_validate_json(r["data"]) for r in rows]
|
||||
|
||||
def delete_map_pack(self, pack_id: str) -> bool:
|
||||
with self.lock, self.conn:
|
||||
cur = self.conn.execute("DELETE FROM offline_map_packs WHERE id=?", (pack_id,))
|
||||
return cur.rowcount > 0
|
||||
Whitespace-only changes.
@@ -0,0 +1,56 @@
|
||||
"""Manual bidirectional sync between Home and Neutron Trekker instances.
|
||||
|
||||
MVP conflict policy: newest updated_at wins, per record. Each side exposes
|
||||
GET /api/sync/bundle and POST /api/sync/bundle; "Sync" pulls the peer's
|
||||
bundle, merges it locally, then pushes the local bundle to the peer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from app.models.route import TrekkerRoute
|
||||
from app.models.trail_guide import TrailGuide
|
||||
from app.storage.route_store import Store
|
||||
|
||||
log = logging.getLogger("trekker.sync")
|
||||
|
||||
|
||||
def build_bundle(store: Store) -> dict[str, Any]:
|
||||
return {
|
||||
"routes": [r.model_dump() for r in store.list_routes()],
|
||||
"trail_guides": [g.model_dump() for g in store.list_trail_guides()],
|
||||
}
|
||||
|
||||
|
||||
def merge_bundle(store: Store, bundle: dict[str, Any]) -> dict[str, int]:
|
||||
"""Apply a peer bundle locally; newest updated_at wins. Returns counts."""
|
||||
applied = {"routes": 0, "trail_guides": 0}
|
||||
for raw in bundle.get("trail_guides", []):
|
||||
incoming = TrailGuide.model_validate(raw)
|
||||
local = store.get_trail_guide(incoming.id)
|
||||
if local is None or incoming.updated_at > local.updated_at:
|
||||
store.save_trail_guide(incoming)
|
||||
applied["trail_guides"] += 1
|
||||
for raw in bundle.get("routes", []):
|
||||
incoming = TrekkerRoute.model_validate(raw)
|
||||
local = store.get_route(incoming.id)
|
||||
if local is None or incoming.updated_at > local.updated_at:
|
||||
store.save_route(incoming)
|
||||
applied["routes"] += 1
|
||||
return applied
|
||||
|
||||
|
||||
def sync_with_peer(store: Store, peer_url: str) -> dict[str, Any]:
|
||||
base = peer_url.rstrip("/")
|
||||
with httpx.Client(timeout=60) as client:
|
||||
resp = client.get(f"{base}/api/sync/bundle")
|
||||
resp.raise_for_status()
|
||||
pulled = merge_bundle(store, resp.json())
|
||||
push = client.post(f"{base}/api/sync/bundle", json=build_bundle(store))
|
||||
push.raise_for_status()
|
||||
pushed = push.json()
|
||||
return {"pulled": pulled, "pushed": pushed.get("applied", pushed)}
|
||||
@@ -0,0 +1,12 @@
|
||||
services:
|
||||
trekker:
|
||||
build: .
|
||||
ports:
|
||||
- "8000:8000"
|
||||
volumes:
|
||||
- ./data:/app/data
|
||||
- ./import:/app/import
|
||||
- ./trekker.yml:/app/trekker.yml:ro
|
||||
env_file:
|
||||
- .env
|
||||
restart: unless-stopped
|
||||
@@ -0,0 +1,23 @@
|
||||
# Neutron (truck server) override: adds the Tailscale sidecar.
|
||||
# Usage: docker compose -f docker-compose.yml -f docker/docker-compose.neutron.yml up -d
|
||||
services:
|
||||
trekker:
|
||||
environment:
|
||||
- TREKKER_HOST_ROLE=neutron
|
||||
network_mode: service:tailscale
|
||||
depends_on:
|
||||
- tailscale
|
||||
|
||||
tailscale:
|
||||
image: tailscale/tailscale:latest
|
||||
hostname: neutron-trekker
|
||||
environment:
|
||||
- TS_AUTHKEY=${TAILSCALE_AUTH_KEY}
|
||||
- TS_STATE_DIR=/var/lib/tailscale
|
||||
volumes:
|
||||
- ./data/tailscale:/var/lib/tailscale
|
||||
devices:
|
||||
- /dev/net/tun:/dev/net/tun
|
||||
cap_add:
|
||||
- NET_ADMIN
|
||||
restart: unless-stopped
|
||||
@@ -0,0 +1,13 @@
|
||||
fastapi>=0.115
|
||||
uvicorn[standard]>=0.30
|
||||
gpxpy>=1.6
|
||||
pydantic>=2.7
|
||||
pydantic-settings>=2.3
|
||||
httpx>=0.27
|
||||
watchdog>=4.0
|
||||
pypdf>=4.2
|
||||
beautifulsoup4>=4.12
|
||||
openai>=1.40
|
||||
PyYAML>=6.0
|
||||
Pillow>=10.3
|
||||
python-multipart>=0.0.9
|
||||
Whitespace-only changes.
@@ -0,0 +1,18 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.storage.database import connect
|
||||
from app.storage.route_store import Store
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fixtures() -> Path:
|
||||
return FIXTURES
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path) -> Store:
|
||||
return Store(connect(str(tmp_path / "test.db")))
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx version="1.1" creator="GaiaGPS for Android"
|
||||
xmlns="http://www.topografix.com/GPX/1/1"
|
||||
xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/1">
|
||||
<metadata><name>Windrock Loop</name></metadata>
|
||||
<wpt lat="36.2000" lon="-84.3600"><name>Trailhead</name><sym>Trail Head</sym></wpt>
|
||||
<wpt lat="36.2030" lon="-84.3600"><name>Water Crossing</name><desc>About 1 ft deep</desc></wpt>
|
||||
<wpt lat="36.2060" lon="-84.3600"><name>Camp</name><sym>Campground</sym></wpt>
|
||||
<trk>
|
||||
<name>Windrock Loop</name>
|
||||
<trkseg>
|
||||
<trkpt lat="36.2000" lon="-84.3600"><ele>400.0</ele><time>2026-06-01T10:00:00Z</time></trkpt>
|
||||
<trkpt lat="36.2010" lon="-84.3600"><ele>410.0</ele><time>2026-06-01T10:05:00Z</time></trkpt>
|
||||
<trkpt lat="36.2020" lon="-84.3600"><ele>425.0</ele><time>2026-06-01T10:10:00Z</time></trkpt>
|
||||
<trkpt lat="36.2030" lon="-84.3600"><ele>430.0</ele><time>2026-06-01T10:15:00Z</time></trkpt>
|
||||
<trkpt lat="36.2040" lon="-84.3600"><ele>420.0</ele><time>2026-06-01T10:20:00Z</time></trkpt>
|
||||
<trkpt lat="36.2050" lon="-84.3600"><ele>445.0</ele><time>2026-06-01T10:25:00Z</time></trkpt>
|
||||
<trkpt lat="36.2060" lon="-84.3600"><ele>460.0</ele><time>2026-06-01T10:30:00Z</time></trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx version="1.1" creator="hand" xmlns="http://www.topografix.com/GPX/1/1">
|
||||
<trk>
|
||||
<trkseg>
|
||||
<trkpt lat="36.1000" lon="-84.5000"></trkpt>
|
||||
<trkpt lat="36.1010" lon="-84.5000"></trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx version="1.1" creator="onXmaps offroad android"
|
||||
xmlns="http://www.topografix.com/GPX/1/1"
|
||||
xmlns:onx="http://wwww.onxmaps.com/">
|
||||
<trk>
|
||||
<name>Ridge Run</name>
|
||||
<trkseg>
|
||||
<trkpt lat="36.3000" lon="-84.4000"><ele>500.0</ele></trkpt>
|
||||
<trkpt lat="36.3010" lon="-84.4000"><ele>520.0</ele></trkpt>
|
||||
<trkpt lat="36.3020" lon="-84.4000"><ele>515.0</ele></trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
||||
@@ -0,0 +1,103 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path, monkeypatch, fixtures):
|
||||
from app import config, deps
|
||||
|
||||
monkeypatch.setattr(config.settings, "data_dir", str(tmp_path))
|
||||
deps.get_store.cache_clear()
|
||||
deps.get_llm.cache_clear()
|
||||
deps.get_elevation.cache_clear()
|
||||
deps.get_offline_maps.cache_clear()
|
||||
|
||||
from app.main import app
|
||||
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
deps.get_store.cache_clear()
|
||||
|
||||
|
||||
def test_health(client):
|
||||
resp = client.get("/api/health")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["status"] == "ok"
|
||||
|
||||
|
||||
def test_upload_list_get_export_delete(client, fixtures):
|
||||
resp = client.post(
|
||||
"/api/routes/upload",
|
||||
files={"file": ("gaia.gpx", (fixtures / "gaia.gpx").read_bytes(), "application/gpx+xml")},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
route = resp.json()
|
||||
assert route["source"] == "gaia"
|
||||
rid = route["id"]
|
||||
|
||||
assert len(client.get("/api/routes").json()) == 1
|
||||
assert client.get(f"/api/routes/{rid}").json()["name"] == "Windrock Loop"
|
||||
|
||||
gpx = client.get(f"/api/routes/{rid}/export?format=gpx")
|
||||
assert gpx.status_code == 200
|
||||
assert "grade_pct" in gpx.text
|
||||
kml = client.get(f"/api/routes/{rid}/export?format=kml")
|
||||
assert "LineString" in kml.text
|
||||
|
||||
assert client.delete(f"/api/routes/{rid}").status_code == 200
|
||||
assert client.get(f"/api/routes/{rid}").status_code == 404
|
||||
|
||||
|
||||
def test_upload_rejects_non_gpx(client):
|
||||
resp = client.post("/api/routes/upload", files={"file": ("x.txt", b"hi", "text/plain")})
|
||||
assert resp.status_code == 400
|
||||
|
||||
|
||||
def test_guide_save_and_link(client, fixtures):
|
||||
rid = client.post(
|
||||
"/api/routes/upload",
|
||||
files={"file": ("onx.gpx", (fixtures / "onx.gpx").read_bytes(), "application/gpx+xml")},
|
||||
).json()["id"]
|
||||
|
||||
guide = client.post("/api/trails", json={"name": "Ridge guide", "difficulty": "hard"}).json()
|
||||
linked = client.put(f"/api/routes/{rid}/trail-guide", json={"trail_guide_id": guide["id"]})
|
||||
assert linked.json()["trail_guide_id"] == guide["id"]
|
||||
|
||||
missing = client.put(f"/api/routes/{rid}/trail-guide", json={"trail_guide_id": "nope"})
|
||||
assert missing.status_code == 404
|
||||
|
||||
|
||||
def test_generate_trail_card(client, fixtures):
|
||||
rid = client.post(
|
||||
"/api/routes/upload",
|
||||
files={"file": ("gaia.gpx", (fixtures / "gaia.gpx").read_bytes(), "application/gpx+xml")},
|
||||
).json()["id"]
|
||||
resp = client.post(f"/api/routes/{rid}/generate/trail_card")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["data"]["route_name"] == "Windrock Loop"
|
||||
# weather overlay requires cached weather -> 409
|
||||
resp = client.post(f"/api/routes/{rid}/generate/weather_overlay")
|
||||
assert resp.status_code == 409
|
||||
assert client.post(f"/api/routes/{rid}/generate/bogus").status_code == 404
|
||||
|
||||
|
||||
def test_offline_pack_estimate_and_cap(client):
|
||||
body = {"name": "t", "bbox": [-84.4, 36.1, -84.3, 36.2], "min_zoom": 10, "max_zoom": 12,
|
||||
"layers": ["base"]}
|
||||
est = client.post("/api/offline-maps/estimate", json=body).json()
|
||||
assert est["tile_count"] > 0
|
||||
huge = dict(body, bbox=[-90.0, 30.0, -80.0, 40.0], min_zoom=0, max_zoom=16)
|
||||
resp = client.post("/api/offline-maps", json=huge)
|
||||
assert resp.status_code == 400
|
||||
assert "cap" in resp.json()["detail"]
|
||||
|
||||
|
||||
def test_sync_bundle_endpoints(client, fixtures):
|
||||
client.post(
|
||||
"/api/routes/upload",
|
||||
files={"file": ("onx.gpx", (fixtures / "onx.gpx").read_bytes(), "application/gpx+xml")},
|
||||
)
|
||||
bundle = client.get("/api/sync/bundle").json()
|
||||
assert len(bundle["routes"]) == 1
|
||||
resp = client.post("/api/sync/bundle", json=bundle)
|
||||
assert resp.json()["applied"] == {"routes": 0, "trail_guides": 0}
|
||||
@@ -0,0 +1,38 @@
|
||||
from app.export.gpx_writer import write_gpx
|
||||
from app.export.kml_writer import write_kml
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.models.trail_guide import TrailGuide, WaterCrossing
|
||||
|
||||
|
||||
def test_gpx_round_trip(fixtures):
|
||||
original = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
exported = write_gpx(original)
|
||||
reimported = parse_gpx(exported)
|
||||
assert len(reimported.points) == len(original.points)
|
||||
assert len(reimported.waypoints) == len(original.waypoints)
|
||||
assert [w.name for w in reimported.waypoints] == [w.name for w in original.waypoints]
|
||||
assert [p.ele for p in reimported.points] == [p.ele for p in original.points]
|
||||
# segments regenerate identically from preserved waypoints
|
||||
assert len(reimported.segments) == len(original.segments)
|
||||
assert [s.distance_m for s in reimported.segments] == [s.distance_m for s in original.segments]
|
||||
|
||||
|
||||
def test_gpx_enrichment_fields(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
guide = TrailGuide(
|
||||
name="g", difficulty="moderate",
|
||||
water_crossings=[WaterCrossing(name="Creek", depth_ft=1.0)],
|
||||
)
|
||||
out = write_gpx(route, guide)
|
||||
assert "grade_pct" in out
|
||||
assert 'depth_ft="1.0"' in out
|
||||
assert "moderate" in out
|
||||
|
||||
|
||||
def test_kml_output(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
out = write_kml(route)
|
||||
assert "<" + "?xml" in out or out.startswith("<?xml")
|
||||
assert "LineString" in out
|
||||
assert "Trailhead" in out
|
||||
assert out.count("Placemark") >= 2 # track + waypoints
|
||||
@@ -0,0 +1,67 @@
|
||||
import pytest
|
||||
|
||||
from app.generators import GENERATORS
|
||||
from app.generators.base import GeneratorError
|
||||
from app.generators.elevation_map import grade_color
|
||||
from app.generators.trail_card import TrailCardGenerator
|
||||
from app.generators.weather_overlay import WeatherOverlayGenerator
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
WEATHER = {
|
||||
"days": 7,
|
||||
"segments": {
|
||||
"0": {"daily": [{"date": "2026-06-10", "temp_min_f": 60, "temp_max_f": 85,
|
||||
"precip_prob_max": 70, "precip_sum_in": 0.4,
|
||||
"wind_max_kmh": 45, "lightning_risk": True}]},
|
||||
"1": {"daily": [{"date": "2026-06-10", "temp_min_f": 60, "temp_max_f": 85,
|
||||
"precip_prob_max": 10, "precip_sum_in": 0.0,
|
||||
"wind_max_kmh": 10, "lightning_risk": False}]},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_registry_names():
|
||||
assert set(GENERATORS) == {"trail_card", "elevation_map", "weather_overlay"}
|
||||
|
||||
|
||||
def test_trail_card_degrades_without_guide(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
asset = TrailCardGenerator().generate(route, None, None)
|
||||
assert asset.data["guide"] is None
|
||||
assert len(asset.data["segments"]) == 2
|
||||
assert asset.data["segments"][0]["weather_today"] is None
|
||||
|
||||
|
||||
def test_trail_card_with_guide_and_weather(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
guide = TrailGuide(name="WG", difficulty="difficult", gate_codes=["1234"])
|
||||
asset = TrailCardGenerator().generate(route, guide, WEATHER)
|
||||
assert asset.data["guide"]["gate_codes"] == ["1234"]
|
||||
assert asset.data["segments"][0]["difficulty"] == "difficult"
|
||||
assert asset.data["segments"][0]["weather_today"]["precip_prob_max"] == 70
|
||||
|
||||
|
||||
def test_weather_overlay_requires_weather(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
with pytest.raises(GeneratorError):
|
||||
WeatherOverlayGenerator().generate(route, None, None)
|
||||
asset = WeatherOverlayGenerator().generate(route, None, WEATHER)
|
||||
segs = asset.data["segments"]
|
||||
assert segs[0]["wind_advisory"] is True
|
||||
assert segs[0]["lightning_risk"] is True
|
||||
assert segs[1]["wind_advisory"] is False
|
||||
|
||||
|
||||
def test_elevation_map(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
asset = GENERATORS["elevation_map"]().generate(route, None, None)
|
||||
assert len(asset.data["segments"]) == 2
|
||||
assert {c["type"] for c in asset.data["callouts"]} == {"summit", "low_point"}
|
||||
summit = next(c for c in asset.data["callouts"] if c["type"] == "summit")
|
||||
assert summit["ele_m"] == 460.0
|
||||
|
||||
|
||||
def test_grade_colors():
|
||||
assert grade_color(2) == "#2ecc71"
|
||||
assert grade_color(30) == "#e74c3c"
|
||||
@@ -0,0 +1,31 @@
|
||||
from app.ingest.gpx_parser import detect_source, parse_gpx
|
||||
|
||||
|
||||
def test_gaia_parse(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes(), filename="gaia.gpx")
|
||||
assert route.source == "gaia"
|
||||
assert route.name == "Windrock Loop"
|
||||
assert len(route.points) == 7
|
||||
assert len(route.waypoints) == 3
|
||||
assert route.points[0].ele == 400.0
|
||||
assert route.points[0].time.startswith("2026-06-01T10:00:00")
|
||||
|
||||
|
||||
def test_onx_parse(fixtures):
|
||||
route = parse_gpx((fixtures / "onx.gpx").read_bytes())
|
||||
assert route.source == "onx"
|
||||
assert route.name == "Ridge Run"
|
||||
assert len(route.points) == 3
|
||||
assert route.waypoints == []
|
||||
|
||||
|
||||
def test_minimal_parse(fixtures):
|
||||
route = parse_gpx((fixtures / "minimal.gpx").read_bytes(), filename="minimal.gpx")
|
||||
assert route.source == "manual"
|
||||
assert route.name == "minimal"
|
||||
assert all(p.ele is None for p in route.points)
|
||||
|
||||
|
||||
def test_detect_source_namespace_fallback():
|
||||
assert detect_source(None, '<gpx xmlns:onx="http://wwww.onxmaps.com/">') == "onx"
|
||||
assert detect_source("Some App", "<gpx>") == "manual"
|
||||
@@ -0,0 +1,42 @@
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.ingest.segmenter import haversine_m
|
||||
|
||||
|
||||
def test_haversine_known_distance():
|
||||
# ~111.2 km per degree of latitude
|
||||
d = haversine_m(36.0, -84.0, 37.0, -84.0)
|
||||
assert 110_000 < d < 112_500
|
||||
|
||||
|
||||
def test_waypoint_driven_segments(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
# Trailhead (idx 0), Water Crossing (idx 3), Camp (idx 6) -> two segments
|
||||
assert len(route.segments) == 2
|
||||
s1, s2 = route.segments
|
||||
assert s1.start_wpt == "Trailhead" and s1.end_wpt == "Water Crossing"
|
||||
assert s2.start_wpt == "Water Crossing" and s2.end_wpt == "Camp"
|
||||
assert s1.start_idx == 0 and s1.end_idx == 3
|
||||
assert s2.start_idx == 3 and s2.end_idx == 6
|
||||
# each leg covers 0.003 deg lat ~ 334m
|
||||
assert 300 < s1.distance_m < 370
|
||||
assert s1.elevation_gain_m == 30.0
|
||||
assert s1.duration_s == 900.0
|
||||
assert s2.elevation_gain_m == 40.0 # 430->420 (-10), 420->445 (+25), 445->460 (+15)
|
||||
assert s2.elevation_loss_m == 10.0
|
||||
|
||||
|
||||
def test_no_waypoints_single_segment(fixtures):
|
||||
route = parse_gpx((fixtures / "onx.gpx").read_bytes())
|
||||
assert len(route.segments) == 1
|
||||
seg = route.segments[0]
|
||||
assert seg.start_idx == 0 and seg.end_idx == 2
|
||||
assert seg.elevation_gain_m == 20.0
|
||||
assert seg.elevation_loss_m == 5.0
|
||||
|
||||
|
||||
def test_route_stats(fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
assert route.stats.elevation_min_m == 400.0
|
||||
assert route.stats.elevation_max_m == 460.0
|
||||
assert route.stats.elevation_gain_m == 70.0
|
||||
assert route.stats.duration_s == 1800.0
|
||||
@@ -0,0 +1,34 @@
|
||||
import pytest
|
||||
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.models.trail_guide import TrailGuide
|
||||
|
||||
|
||||
def test_route_crud(store, fixtures):
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
store.save_route(route)
|
||||
loaded = store.get_route(route.id)
|
||||
assert loaded == route
|
||||
assert [r.id for r in store.list_routes()] == [route.id]
|
||||
assert store.delete_route(route.id)
|
||||
assert store.get_route(route.id) is None
|
||||
|
||||
|
||||
def test_trail_guide_link(store, fixtures):
|
||||
route = store.save_route(parse_gpx((fixtures / "onx.gpx").read_bytes()))
|
||||
guide = store.save_trail_guide(TrailGuide(name="Ridge guide", difficulty="moderate"))
|
||||
linked = store.link_trail_guide(route.id, guide.id)
|
||||
assert linked.trail_guide_id == guide.id
|
||||
unlinked = store.link_trail_guide(route.id, None)
|
||||
assert unlinked.trail_guide_id is None
|
||||
with pytest.raises(KeyError):
|
||||
store.link_trail_guide(route.id, "nonexistent")
|
||||
|
||||
|
||||
def test_weather_cache(store, fixtures):
|
||||
route = store.save_route(parse_gpx((fixtures / "onx.gpx").read_bytes()))
|
||||
assert store.get_weather(route.id) is None
|
||||
store.save_weather(route.id, {"days": 7, "segments": {}})
|
||||
cached = store.get_weather(route.id)
|
||||
assert cached["days"] == 7
|
||||
assert "fetched_at" in cached
|
||||
@@ -0,0 +1,30 @@
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.models.trail_guide import TrailGuide
|
||||
from app.storage.database import connect
|
||||
from app.storage.route_store import Store
|
||||
from app.sync.sync_daemon import build_bundle, merge_bundle
|
||||
|
||||
|
||||
def test_bundle_merge_newest_wins(tmp_path, fixtures):
|
||||
home = Store(connect(str(tmp_path / "home.db")))
|
||||
neutron = Store(connect(str(tmp_path / "neutron.db")))
|
||||
|
||||
route = parse_gpx((fixtures / "gaia.gpx").read_bytes())
|
||||
home.save_route(route)
|
||||
home.save_trail_guide(TrailGuide(name="Guide A"))
|
||||
|
||||
applied = merge_bundle(neutron, build_bundle(home))
|
||||
assert applied == {"routes": 1, "trail_guides": 1}
|
||||
assert neutron.get_route(route.id).name == route.name
|
||||
|
||||
# neutron edits the route -> newer updated_at -> wins on merge back
|
||||
edited = neutron.get_route(route.id)
|
||||
edited.name = "Renamed on Neutron"
|
||||
neutron.save_route(edited)
|
||||
applied = merge_bundle(home, build_bundle(neutron))
|
||||
assert applied["routes"] == 1
|
||||
assert home.get_route(route.id).name == "Renamed on Neutron"
|
||||
|
||||
# re-merging identical data is a no-op
|
||||
applied = merge_bundle(home, build_bundle(neutron))
|
||||
assert applied == {"routes": 0, "trail_guides": 0}
|
||||
@@ -0,0 +1,45 @@
|
||||
from app.services.weather import THUNDER_CODES, build_tiers
|
||||
|
||||
RAW = {
|
||||
"hourly": {
|
||||
"time": [f"2026-06-10T{h:02d}:00" for h in range(24)],
|
||||
"temperature_2m": [60 + h for h in range(24)],
|
||||
"precipitation_probability": [10] * 12 + [80] * 12,
|
||||
"precipitation": [0.0] * 12 + [0.1] * 12,
|
||||
"wind_speed_10m": [10] * 23 + [50],
|
||||
"wind_gusts_10m": [15] * 24,
|
||||
"weather_code": [1] * 23 + [95],
|
||||
},
|
||||
"daily": {
|
||||
"time": ["2026-06-10"],
|
||||
"temperature_2m_max": [83],
|
||||
"temperature_2m_min": [60],
|
||||
"precipitation_probability_max": [80],
|
||||
"precipitation_sum": [1.2],
|
||||
"wind_speed_10m_max": [50],
|
||||
"weather_code": [95],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_build_tiers_buckets():
|
||||
tiers = build_tiers(RAW)
|
||||
assert len(tiers["hourly"]) == 24
|
||||
assert len(tiers["6h"]) == 4
|
||||
assert len(tiers["12h"]) == 2
|
||||
assert len(tiers["daily"]) == 1
|
||||
|
||||
|
||||
def test_tier_aggregation():
|
||||
tiers = build_tiers(RAW)
|
||||
first12, last12 = tiers["12h"]
|
||||
assert first12["precip_prob_max"] == 10
|
||||
assert last12["precip_prob_max"] == 80
|
||||
assert last12["wind_advisory"] is True
|
||||
assert last12["lightning_risk"] is True
|
||||
assert first12["lightning_risk"] is False
|
||||
assert tiers["daily"][0]["lightning_risk"] is True
|
||||
|
||||
|
||||
def test_thunder_codes():
|
||||
assert 95 in THUNDER_CODES and 99 in THUNDER_CODES
|
||||
@@ -0,0 +1,18 @@
|
||||
# Trekker YAML config (copy to trekker.yml; overrides env defaults)
|
||||
watch:
|
||||
internal_path: "import/gpx"
|
||||
remote_path: null # e.g. /mnt/remote/trekker-import (SMB/Nextcloud/USB)
|
||||
poll_interval: 30
|
||||
|
||||
llm:
|
||||
base_url: null # e.g. http://localhost:11434/v1 for Ollama
|
||||
model: "gpt-4o-mini"
|
||||
api_key: null
|
||||
|
||||
tiles:
|
||||
base_url: "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
terrain_url: "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
|
||||
max_pack_tiles: 4000
|
||||
|
||||
sync:
|
||||
peer_url: null # e.g. http://neutron-trekker:8000
|
||||
@@ -0,0 +1,442 @@
|
||||
/* Trekker UI: full-screen MapLibre map; sidebar, slide-out panel, modals. */
|
||||
"use strict";
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const api = async (path, opts = {}) => {
|
||||
const resp = await fetch(path, opts);
|
||||
if (!resp.ok) {
|
||||
let detail = resp.statusText;
|
||||
try { detail = (await resp.json()).detail || detail; } catch {}
|
||||
throw new Error(detail);
|
||||
}
|
||||
return resp.json();
|
||||
};
|
||||
|
||||
const state = {
|
||||
config: null,
|
||||
routes: [],
|
||||
guides: [],
|
||||
current: null, // full TrekkerRoute of selected route
|
||||
weather: null, // cached weather for selected route
|
||||
online: navigator.onLine,
|
||||
};
|
||||
|
||||
/* ---------- map ---------- */
|
||||
let map;
|
||||
function initMap(cfg) {
|
||||
// Base tiles via the backend-configured source; offline packs are served
|
||||
// from /api/offline-maps/tiles and tried first when offline.
|
||||
const sources = {
|
||||
base: {
|
||||
type: "raster",
|
||||
tiles: state.online
|
||||
? [cfg.tiles_base_url]
|
||||
: ["/api/offline-maps/tiles/base/{z}/{x}/{y}.png"],
|
||||
tileSize: 256,
|
||||
attribution: "© OpenStreetMap contributors",
|
||||
},
|
||||
};
|
||||
map = new maplibregl.Map({
|
||||
container: "map",
|
||||
style: { version: 8, sources, layers: [{ id: "base", type: "raster", source: "base" }] },
|
||||
center: [-84.36, 36.2], // Windrock-ish default
|
||||
zoom: 11,
|
||||
});
|
||||
map.addControl(new maplibregl.NavigationControl());
|
||||
map.on("load", () => {
|
||||
map.addSource("route", { type: "geojson", data: emptyFC() });
|
||||
map.addLayer({
|
||||
id: "route-line", type: "line", source: "route",
|
||||
paint: { "line-color": ["coalesce", ["get", "color"], "#2d6cdf"], "line-width": 4 },
|
||||
});
|
||||
map.addSource("waypoints", { type: "geojson", data: emptyFC() });
|
||||
map.addLayer({
|
||||
id: "wpt-circles", type: "circle", source: "waypoints",
|
||||
paint: { "circle-radius": 6, "circle-color": "#e67e22", "circle-stroke-color": "#fff", "circle-stroke-width": 1.5 },
|
||||
});
|
||||
map.addLayer({
|
||||
id: "wpt-labels", type: "symbol", source: "waypoints",
|
||||
layout: { "text-field": ["get", "name"], "text-size": 11, "text-offset": [0, 1.2] },
|
||||
paint: { "text-color": "#fff", "text-halo-color": "#000", "text-halo-width": 1 },
|
||||
});
|
||||
map.on("click", "wpt-circles", (e) => {
|
||||
const f = e.features[0];
|
||||
new maplibregl.Popup().setLngLat(f.geometry.coordinates)
|
||||
.setHTML(`<b>${f.properties.name || "Waypoint"}</b><br>${f.properties.desc || ""}`)
|
||||
.addTo(map);
|
||||
});
|
||||
});
|
||||
}
|
||||
const emptyFC = () => ({ type: "FeatureCollection", features: [] });
|
||||
|
||||
function gradeColor(g) {
|
||||
const a = Math.abs(g);
|
||||
return a < 5 ? "#2ecc71" : a < 10 ? "#f1c40f" : a < 18 ? "#e67e22" : "#e74c3c";
|
||||
}
|
||||
function precipColor(p) {
|
||||
p = p || 0;
|
||||
return p < 20 ? "#d6eaf8" : p < 50 ? "#5dade2" : p < 80 ? "#2e86c1" : "#1b4f72";
|
||||
}
|
||||
|
||||
function renderRouteOnMap() {
|
||||
const r = state.current;
|
||||
if (!r || !map.getSource("route")) return;
|
||||
const useGrade = $("layer-grade").checked;
|
||||
const useWeather = $("layer-weather").checked && state.weather;
|
||||
const tier = $("weather-tier").value;
|
||||
const features = r.segments.map((seg) => {
|
||||
let color = "#2d6cdf";
|
||||
if (useWeather) {
|
||||
const tiers = (state.weather.segments || {})[String(seg.index)] || {};
|
||||
const buckets = tiers[tier] || tiers.daily || [];
|
||||
const now = buckets[0] || {};
|
||||
color = precipColor(now.precip_prob_max);
|
||||
} else if (useGrade) {
|
||||
color = gradeColor(seg.grade_pct_max);
|
||||
}
|
||||
return {
|
||||
type: "Feature",
|
||||
properties: { color, name: seg.name },
|
||||
geometry: {
|
||||
type: "LineString",
|
||||
coordinates: r.points.slice(seg.start_idx, seg.end_idx + 1).map((p) => [p.lon, p.lat]),
|
||||
},
|
||||
};
|
||||
});
|
||||
if (!features.length && r.points.length > 1) {
|
||||
features.push({
|
||||
type: "Feature", properties: { color: "#2d6cdf" },
|
||||
geometry: { type: "LineString", coordinates: r.points.map((p) => [p.lon, p.lat]) },
|
||||
});
|
||||
}
|
||||
map.getSource("route").setData({ type: "FeatureCollection", features });
|
||||
map.getSource("waypoints").setData({
|
||||
type: "FeatureCollection",
|
||||
features: r.waypoints.map((w) => ({
|
||||
type: "Feature",
|
||||
properties: { name: w.name, desc: w.desc },
|
||||
geometry: { type: "Point", coordinates: [w.lon, w.lat] },
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- sidebar: routes ---------- */
|
||||
async function loadRoutes() {
|
||||
state.routes = await api("/api/routes");
|
||||
const filter = $("route-search").value.toLowerCase();
|
||||
const ul = $("route-list");
|
||||
ul.innerHTML = "";
|
||||
for (const r of state.routes) {
|
||||
if (filter && !r.name.toLowerCase().includes(filter)) continue;
|
||||
const li = document.createElement("li");
|
||||
if (state.current && state.current.id === r.id) li.classList.add("active");
|
||||
const mi = (r.stats.distance_m / 1609.344).toFixed(1);
|
||||
li.innerHTML = `<b>${r.name}</b><span class="route-sub">${r.source} · ${mi} mi · ${r.created_at.slice(0, 10)}</span>`;
|
||||
li.onclick = () => selectRoute(r.id);
|
||||
ul.appendChild(li);
|
||||
}
|
||||
}
|
||||
|
||||
async function selectRoute(id) {
|
||||
state.current = await api(`/api/routes/${id}`);
|
||||
try { state.weather = await api(`/api/routes/${id}/weather`); }
|
||||
catch { state.weather = null; }
|
||||
const b = boundsOf(state.current);
|
||||
if (b) map.fitBounds(b, { padding: 60 });
|
||||
renderRouteOnMap();
|
||||
openRoutePanel();
|
||||
loadRoutes();
|
||||
}
|
||||
function boundsOf(r) {
|
||||
if (!r.points.length) return null;
|
||||
const lons = r.points.map((p) => p.lon), lats = r.points.map((p) => p.lat);
|
||||
return [[Math.min(...lons), Math.min(...lats)], [Math.max(...lons), Math.max(...lats)]];
|
||||
}
|
||||
|
||||
/* ---------- route panel ---------- */
|
||||
function openRoutePanel() {
|
||||
const r = state.current;
|
||||
$("route-panel").hidden = false;
|
||||
$("rp-name").textContent = r.name;
|
||||
const mi = (r.stats.distance_m / 1609.344).toFixed(1);
|
||||
const gain = Math.round(r.stats.elevation_gain_m * 3.28084);
|
||||
const loss = Math.round(r.stats.elevation_loss_m * 3.28084);
|
||||
$("rp-meta").textContent = `${r.source} · ${mi} mi · +${gain} ft / −${loss} ft`;
|
||||
$("rp-export-gpx").href = `/api/routes/${r.id}/export?format=gpx`;
|
||||
$("rp-export-kml").href = `/api/routes/${r.id}/export?format=kml`;
|
||||
const ul = $("rp-segments");
|
||||
ul.innerHTML = "";
|
||||
for (const s of r.segments) {
|
||||
const li = document.createElement("li");
|
||||
li.style.borderLeftColor = gradeColor(s.grade_pct_max);
|
||||
li.innerHTML = `<b>${s.name}</b><br>${(s.distance_m / 1609.344).toFixed(2)} mi · max grade ${s.grade_pct_max}%`;
|
||||
ul.appendChild(li);
|
||||
}
|
||||
const sel = $("rp-guide");
|
||||
sel.innerHTML = `<option value="">— no trail guide —</option>` +
|
||||
state.guides.map((g) => `<option value="${g.id}" ${g.id === r.trail_guide_id ? "selected" : ""}>${g.name}</option>`).join("");
|
||||
$("rp-card-output").innerHTML = "";
|
||||
renderWeatherBadge();
|
||||
}
|
||||
|
||||
function renderWeatherBadge() {
|
||||
const el = $("rp-weather");
|
||||
if (!state.weather) { el.hidden = true; return; }
|
||||
const seg0 = (state.weather.segments || {})["0"] || {};
|
||||
const today = (seg0.daily || [])[0];
|
||||
if (!today) { el.hidden = true; return; }
|
||||
el.hidden = false;
|
||||
el.innerHTML = `🌡 ${Math.round(today.temp_min_f)}–${Math.round(today.temp_max_f)}°F · ` +
|
||||
`🌧 ${today.precip_prob_max ?? "?"}% · 💨 ${Math.round(today.wind_max_kmh || 0)} km/h` +
|
||||
(today.lightning_risk ? " · ⚡" : "");
|
||||
}
|
||||
|
||||
/* ---------- modals ---------- */
|
||||
function openModal(id) {
|
||||
$("modal-backdrop").hidden = false;
|
||||
for (const m of document.querySelectorAll(".modal")) m.hidden = m.id !== id;
|
||||
}
|
||||
function closeModal() { $("modal-backdrop").hidden = true; }
|
||||
|
||||
/* ---------- trail guide review form ---------- */
|
||||
let guideDraft = null;
|
||||
function showGuideReview(draft) {
|
||||
guideDraft = draft;
|
||||
const f = $("guide-review");
|
||||
f.hidden = false;
|
||||
f.name.value = draft.name || "";
|
||||
f.difficulty.value = draft.difficulty || "";
|
||||
f.obstacle_types.value = (draft.obstacle_types || []).join(", ");
|
||||
f.water_crossings.value = (draft.water_crossings || [])
|
||||
.map((w) => [w.name || "", w.depth_ft ?? "", w.notes || ""].join(" | ")).join("\n");
|
||||
f.gate_codes.value = (draft.gate_codes || []).join(", ");
|
||||
f.vehicle_recommendations.value = (draft.vehicle_recommendations || []).join(", ");
|
||||
f.warnings.value = (draft.warnings || []).join("\n");
|
||||
f.notes.value = draft.notes || "";
|
||||
}
|
||||
|
||||
const csv = (v) => v.split(",").map((s) => s.trim()).filter(Boolean);
|
||||
const lines = (v) => v.split("\n").map((s) => s.trim()).filter(Boolean);
|
||||
|
||||
/* ---------- offline packs ---------- */
|
||||
async function loadPacks() {
|
||||
const packs = await api("/api/offline-maps");
|
||||
const ul = $("pack-list");
|
||||
ul.innerHTML = "";
|
||||
let anyActive = false;
|
||||
for (const p of packs) {
|
||||
const li = document.createElement("li");
|
||||
const pct = p.tile_count ? Math.round((p.tiles_done / p.tile_count) * 100) : 0;
|
||||
const status = p.status === "downloading" ? `downloading ${pct}%` :
|
||||
p.status === "ready" ? `ready · ${(p.size_bytes / 1e6).toFixed(1)} MB` : p.status;
|
||||
li.innerHTML = `<b>${p.name}</b><span class="pack-sub">z${p.min_zoom}–${p.max_zoom} · ${status}</span>`;
|
||||
li.title = "Click to delete";
|
||||
li.onclick = async () => {
|
||||
if (confirm(`Delete offline pack "${p.name}"?`)) {
|
||||
await api(`/api/offline-maps/${p.id}`, { method: "DELETE" });
|
||||
loadPacks();
|
||||
}
|
||||
};
|
||||
ul.appendChild(li);
|
||||
if (p.status === "downloading" || p.status === "queued") anyActive = true;
|
||||
}
|
||||
if (anyActive) setTimeout(loadPacks, 2000);
|
||||
}
|
||||
|
||||
/* ---------- online/offline state ---------- */
|
||||
function updateNetState() {
|
||||
state.online = navigator.onLine;
|
||||
const badge = $("net-badge");
|
||||
badge.textContent = state.online ? "online" : "offline";
|
||||
badge.className = `badge ${state.online ? "online" : "offline"}`;
|
||||
$("btn-sync").hidden = !(state.online && state.config && state.config.sync_peer_configured);
|
||||
$("rp-weather-refresh").disabled = !state.online;
|
||||
}
|
||||
|
||||
/* ---------- wire-up ---------- */
|
||||
async function main() {
|
||||
try { state.config = await api("/api/config"); } catch { state.config = {}; }
|
||||
initMap(state.config);
|
||||
updateNetState();
|
||||
window.addEventListener("online", updateNetState);
|
||||
window.addEventListener("offline", updateNetState);
|
||||
|
||||
$("sidebar-toggle").onclick = () => $("sidebar").classList.toggle("collapsed");
|
||||
$("route-search").oninput = loadRoutes;
|
||||
$("layer-grade").onchange = renderRouteOnMap;
|
||||
$("layer-weather").onchange = renderRouteOnMap;
|
||||
$("weather-tier").onchange = renderRouteOnMap;
|
||||
|
||||
document.body.addEventListener("click", (e) => {
|
||||
const t = e.target.closest("[data-close]");
|
||||
if (!t) return;
|
||||
if (t.dataset.close === "modal") closeModal();
|
||||
else $(t.dataset.close).hidden = true;
|
||||
});
|
||||
|
||||
$("btn-upload").onclick = () => { $("upload-status").textContent = ""; openModal("modal-upload"); };
|
||||
$("gpx-submit").onclick = async () => {
|
||||
const file = $("gpx-file").files[0];
|
||||
if (!file) return;
|
||||
const fd = new FormData();
|
||||
fd.append("file", file);
|
||||
$("upload-status").textContent = "Uploading…";
|
||||
try {
|
||||
const route = await api("/api/routes/upload", { method: "POST", body: fd });
|
||||
closeModal();
|
||||
await loadRoutes();
|
||||
selectRoute(route.id);
|
||||
} catch (err) { $("upload-status").textContent = err.message; }
|
||||
};
|
||||
|
||||
$("btn-scrape").onclick = () => {
|
||||
$("guide-status").textContent = "";
|
||||
$("guide-review").hidden = true;
|
||||
openModal("modal-guide");
|
||||
};
|
||||
$("guide-scrape").onclick = async () => {
|
||||
const url = $("guide-url").value.trim();
|
||||
if (!url) return;
|
||||
$("guide-status").textContent = "Scraping + extracting…";
|
||||
try {
|
||||
showGuideReview(await api("/api/trails/scrape", {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ url }),
|
||||
}));
|
||||
$("guide-status").textContent = "Review the extraction, then save.";
|
||||
} catch (err) { $("guide-status").textContent = err.message; }
|
||||
};
|
||||
$("guide-pdf-submit").onclick = async () => {
|
||||
const file = $("guide-pdf").files[0];
|
||||
if (!file) return;
|
||||
const fd = new FormData();
|
||||
fd.append("file", file);
|
||||
$("guide-status").textContent = "Extracting from PDF…";
|
||||
try {
|
||||
showGuideReview(await api("/api/trails/upload-pdf", { method: "POST", body: fd }));
|
||||
$("guide-status").textContent = "Review the extraction, then save.";
|
||||
} catch (err) { $("guide-status").textContent = err.message; }
|
||||
};
|
||||
$("guide-review").onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const guide = Object.assign({}, guideDraft, {
|
||||
name: f.name.value,
|
||||
difficulty: f.difficulty.value || null,
|
||||
obstacle_types: csv(f.obstacle_types.value),
|
||||
water_crossings: lines(f.water_crossings.value).map((ln) => {
|
||||
const [name, depth, notes] = ln.split("|").map((s) => s.trim());
|
||||
return { name: name || null, depth_ft: depth ? parseFloat(depth) : null, notes: notes || null };
|
||||
}),
|
||||
gate_codes: csv(f.gate_codes.value),
|
||||
vehicle_recommendations: csv(f.vehicle_recommendations.value),
|
||||
warnings: lines(f.warnings.value),
|
||||
notes: f.notes.value || null,
|
||||
});
|
||||
await api("/api/trails", {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(guide),
|
||||
});
|
||||
state.guides = await api("/api/trails");
|
||||
closeModal();
|
||||
if (state.current) openRoutePanel();
|
||||
};
|
||||
|
||||
$("rp-guide").onchange = async (e) => {
|
||||
const r = state.current;
|
||||
state.current = await api(`/api/routes/${r.id}/trail-guide`, {
|
||||
method: "PUT", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ trail_guide_id: e.target.value || null }),
|
||||
});
|
||||
};
|
||||
$("rp-delete").onclick = async () => {
|
||||
if (!confirm(`Delete route "${state.current.name}"?`)) return;
|
||||
await api(`/api/routes/${state.current.id}`, { method: "DELETE" });
|
||||
state.current = null;
|
||||
$("route-panel").hidden = true;
|
||||
map.getSource("route").setData(emptyFC());
|
||||
map.getSource("waypoints").setData(emptyFC());
|
||||
loadRoutes();
|
||||
};
|
||||
$("rp-weather-refresh").onclick = async () => {
|
||||
const btn = $("rp-weather-refresh");
|
||||
btn.disabled = true;
|
||||
try {
|
||||
state.weather = await api(`/api/routes/${state.current.id}/refresh-weather`, { method: "POST" });
|
||||
renderWeatherBadge();
|
||||
renderRouteOnMap();
|
||||
} catch (err) { alert(`Weather refresh failed: ${err.message}`); }
|
||||
btn.disabled = false;
|
||||
};
|
||||
$("rp-card").onclick = async () => {
|
||||
try {
|
||||
const asset = await api(`/api/routes/${state.current.id}/generate/trail_card`, { method: "POST" });
|
||||
renderTrailCard(asset.data);
|
||||
} catch (err) { alert(err.message); }
|
||||
};
|
||||
|
||||
$("btn-offline").onclick = () => { $("pack-estimate").textContent = ""; openModal("modal-offline"); };
|
||||
const packBody = () => {
|
||||
const b = map.getBounds();
|
||||
return {
|
||||
name: $("pack-name").value,
|
||||
bbox: [b.getWest(), b.getSouth(), b.getEast(), b.getNorth()],
|
||||
min_zoom: parseInt($("pack-minz").value, 10),
|
||||
max_zoom: parseInt($("pack-maxz").value, 10),
|
||||
layers: $("pack-terrain").checked ? ["base", "terrain"] : ["base"],
|
||||
};
|
||||
};
|
||||
$("pack-estimate-btn").onclick = async () => {
|
||||
try {
|
||||
const est = await api("/api/offline-maps/estimate", {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(packBody()),
|
||||
});
|
||||
$("pack-estimate").textContent =
|
||||
`${est.tile_count} tiles ≈ ${(est.estimated_bytes / 1e6).toFixed(1)} MB`;
|
||||
} catch (err) { $("pack-estimate").textContent = err.message; }
|
||||
};
|
||||
$("pack-create").onclick = async () => {
|
||||
try {
|
||||
await api("/api/offline-maps", {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(packBody()),
|
||||
});
|
||||
closeModal();
|
||||
loadPacks();
|
||||
} catch (err) { $("pack-estimate").textContent = err.message; }
|
||||
};
|
||||
|
||||
$("btn-sync").onclick = async () => {
|
||||
try {
|
||||
const res = await api("/api/sync", {
|
||||
method: "POST", headers: { "Content-Type": "application/json" }, body: "{}",
|
||||
});
|
||||
alert(`Synced. Pulled ${res.pulled.routes} routes, ${res.pulled.trail_guides} guides.`);
|
||||
loadRoutes();
|
||||
} catch (err) { alert(err.message); }
|
||||
};
|
||||
|
||||
state.guides = await api("/api/trails").catch(() => []);
|
||||
await loadRoutes();
|
||||
loadPacks();
|
||||
}
|
||||
|
||||
function renderTrailCard(card) {
|
||||
const out = $("rp-card-output");
|
||||
const guide = card.guide;
|
||||
out.innerHTML = `<div class="card"><h4>Trail card — ${card.route_name}</h4>
|
||||
<div>${card.total_distance_mi} mi · +${card.total_gain_ft} ft</div>
|
||||
${guide ? `<div>Difficulty: ${guide.difficulty || "n/a"}</div>
|
||||
${guide.gate_codes.length ? `<div>Gate codes: ${guide.gate_codes.join(", ")}</div>` : ""}
|
||||
${guide.warnings.map((w) => `<div>⚠ ${w}</div>`).join("")}` : "<div><i>No trail guide linked.</i></div>"}
|
||||
${card.segments.map((s) => `<div class="card">
|
||||
<h4>${s.name}</h4>
|
||||
<div>${s.distance_mi} mi · max grade ${s.grade_pct_max}% · ${s.difficulty}</div>
|
||||
${s.obstacles.length ? `<div>Obstacles: ${s.obstacles.join(", ")}</div>` : ""}
|
||||
${s.weather_today ? `<div>Today: ${Math.round(s.weather_today.temp_min_f)}–${Math.round(s.weather_today.temp_max_f)}°F, rain ${s.weather_today.precip_prob_max ?? "?"}%</div>` : ""}
|
||||
</div>`).join("")}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
main();
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Trekker</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.css" />
|
||||
<link rel="stylesheet" href="/app/static/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div>
|
||||
|
||||
<!-- collapsible left sidebar: the map is the app -->
|
||||
<aside id="sidebar">
|
||||
<header>
|
||||
<h1>Trekker</h1>
|
||||
<span id="net-badge" class="badge online">online</span>
|
||||
</header>
|
||||
<div class="sidebar-actions">
|
||||
<button id="btn-upload">Upload GPX</button>
|
||||
<button id="btn-scrape">Trail guide</button>
|
||||
<button id="btn-offline">Save area</button>
|
||||
<button id="btn-sync" hidden>Sync</button>
|
||||
</div>
|
||||
<input id="route-search" type="search" placeholder="Filter routes…" />
|
||||
<section>
|
||||
<h2>Routes</h2>
|
||||
<ul id="route-list"></ul>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Layers</h2>
|
||||
<label><input type="checkbox" id="layer-grade" /> Grade coloring</label>
|
||||
<label><input type="checkbox" id="layer-weather" /> Weather overlay</label>
|
||||
<label>Weather tier
|
||||
<select id="weather-tier">
|
||||
<option value="daily">Daily</option>
|
||||
<option value="12h">12-hour</option>
|
||||
<option value="6h">6-hour</option>
|
||||
<option value="hourly">Hourly</option>
|
||||
</select>
|
||||
</label>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Offline packs</h2>
|
||||
<ul id="pack-list"></ul>
|
||||
</section>
|
||||
</aside>
|
||||
<button id="sidebar-toggle" title="Toggle sidebar">☰</button>
|
||||
|
||||
<!-- slide-out route panel -->
|
||||
<aside id="route-panel" hidden>
|
||||
<button class="close" data-close="route-panel">×</button>
|
||||
<h2 id="rp-name"></h2>
|
||||
<p id="rp-meta"></p>
|
||||
<div id="rp-weather" class="weather-badge" hidden></div>
|
||||
<div class="rp-actions">
|
||||
<button id="rp-card">Trail card</button>
|
||||
<button id="rp-weather-refresh">Weather ⟳</button>
|
||||
<a id="rp-export-gpx" class="button">GPX ↓</a>
|
||||
<a id="rp-export-kml" class="button">KML ↓</a>
|
||||
<button id="rp-delete" class="danger">Delete</button>
|
||||
</div>
|
||||
<label>Trail guide
|
||||
<select id="rp-guide"></select>
|
||||
</label>
|
||||
<h3>Segments</h3>
|
||||
<ul id="rp-segments"></ul>
|
||||
<div id="rp-card-output"></div>
|
||||
</aside>
|
||||
|
||||
<!-- modals -->
|
||||
<div id="modal-backdrop" hidden>
|
||||
<div id="modal-upload" class="modal" hidden>
|
||||
<button class="close" data-close="modal">×</button>
|
||||
<h2>Upload GPX</h2>
|
||||
<input type="file" id="gpx-file" accept=".gpx" />
|
||||
<button id="gpx-submit">Upload</button>
|
||||
<p class="status" id="upload-status"></p>
|
||||
</div>
|
||||
|
||||
<div id="modal-guide" class="modal" hidden>
|
||||
<button class="close" data-close="modal">×</button>
|
||||
<h2>Trail guide</h2>
|
||||
<div id="guide-source">
|
||||
<label>Trail URL <input type="url" id="guide-url" placeholder="https://…" /></label>
|
||||
<button id="guide-scrape">Scrape</button>
|
||||
<p>— or —</p>
|
||||
<input type="file" id="guide-pdf" accept=".pdf" />
|
||||
<button id="guide-pdf-submit">Extract from PDF</button>
|
||||
<p class="status" id="guide-status"></p>
|
||||
</div>
|
||||
<form id="guide-review" hidden>
|
||||
<h3>Review before saving</h3>
|
||||
<label>Name <input name="name" required /></label>
|
||||
<label>Difficulty <input name="difficulty" /></label>
|
||||
<label>Obstacles (comma-sep) <input name="obstacle_types" /></label>
|
||||
<label>Water crossings (one/line: name | depth_ft | notes)
|
||||
<textarea name="water_crossings" rows="3"></textarea></label>
|
||||
<label>Gate codes (comma-sep) <input name="gate_codes" /></label>
|
||||
<label>Vehicle recs (comma-sep) <input name="vehicle_recommendations" /></label>
|
||||
<label>Warnings (one/line) <textarea name="warnings" rows="2"></textarea></label>
|
||||
<label>Notes <textarea name="notes" rows="3"></textarea></label>
|
||||
<button type="submit">Save guide</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div id="modal-offline" class="modal" hidden>
|
||||
<button class="close" data-close="modal">×</button>
|
||||
<h2>Save area for offline</h2>
|
||||
<p>Saves the current viewport as an offline map pack.</p>
|
||||
<label>Name <input id="pack-name" value="Offline area" /></label>
|
||||
<label>Min zoom <input id="pack-minz" type="number" value="10" min="0" max="16" /></label>
|
||||
<label>Max zoom <input id="pack-maxz" type="number" value="14" min="0" max="16" /></label>
|
||||
<label><input type="checkbox" id="pack-terrain" checked /> Include terrain tiles</label>
|
||||
<p class="status" id="pack-estimate"></p>
|
||||
<button id="pack-estimate-btn">Estimate</button>
|
||||
<button id="pack-create">Download pack</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.js"></script>
|
||||
<script src="/app/static/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,84 @@
|
||||
* { box-sizing: border-box; }
|
||||
html, body { margin: 0; height: 100%; font-family: system-ui, sans-serif; }
|
||||
#map { position: absolute; inset: 0; }
|
||||
|
||||
#sidebar {
|
||||
position: absolute; top: 0; left: 0; bottom: 0; width: 300px;
|
||||
background: rgba(20, 24, 28, 0.94); color: #eee; z-index: 10;
|
||||
padding: 12px; overflow-y: auto; transition: transform 0.2s;
|
||||
}
|
||||
#sidebar.collapsed { transform: translateX(-100%); }
|
||||
#sidebar header { display: flex; align-items: center; justify-content: space-between; }
|
||||
#sidebar h1 { font-size: 20px; margin: 4px 0; }
|
||||
#sidebar h2 { font-size: 13px; text-transform: uppercase; color: #9ab; margin: 14px 0 6px; }
|
||||
#sidebar section label { display: block; margin: 4px 0; font-size: 14px; }
|
||||
#sidebar-toggle {
|
||||
position: absolute; top: 10px; left: 310px; z-index: 11;
|
||||
transition: left 0.2s; background: #14181c; color: #eee;
|
||||
border: none; border-radius: 4px; padding: 6px 10px; cursor: pointer;
|
||||
}
|
||||
#sidebar.collapsed ~ #sidebar-toggle { left: 10px; }
|
||||
|
||||
.sidebar-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
|
||||
button, .button {
|
||||
background: #2d6cdf; color: #fff; border: none; border-radius: 4px;
|
||||
padding: 6px 10px; font-size: 13px; cursor: pointer; text-decoration: none;
|
||||
display: inline-block;
|
||||
}
|
||||
button.danger { background: #c0392b; }
|
||||
button.close {
|
||||
position: absolute; top: 6px; right: 6px; background: transparent;
|
||||
font-size: 20px; color: inherit;
|
||||
}
|
||||
#route-search { width: 100%; padding: 6px; border-radius: 4px; border: none; margin: 4px 0; }
|
||||
|
||||
#route-list, #pack-list { list-style: none; margin: 0; padding: 0; }
|
||||
#route-list li, #pack-list li {
|
||||
padding: 8px; border-radius: 4px; cursor: pointer; font-size: 14px;
|
||||
}
|
||||
#route-list li:hover { background: rgba(255,255,255,0.08); }
|
||||
#route-list li.active { background: rgba(45,108,223,0.35); }
|
||||
.route-sub, .pack-sub { font-size: 12px; color: #9ab; display: block; }
|
||||
|
||||
.badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; }
|
||||
.badge.online { background: #27ae60; }
|
||||
.badge.offline { background: #c0392b; }
|
||||
|
||||
#route-panel {
|
||||
position: absolute; top: 0; right: 0; bottom: 0; width: 360px;
|
||||
background: rgba(20, 24, 28, 0.96); color: #eee; z-index: 10;
|
||||
padding: 16px; overflow-y: auto;
|
||||
}
|
||||
#route-panel h2 { margin: 4px 0; }
|
||||
#route-panel h3 { font-size: 13px; text-transform: uppercase; color: #9ab; }
|
||||
#route-panel label { display: block; margin: 10px 0; font-size: 14px; }
|
||||
#route-panel select { width: 100%; padding: 4px; }
|
||||
.rp-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
|
||||
#rp-segments { list-style: none; padding: 0; font-size: 13px; }
|
||||
#rp-segments li { padding: 6px; border-left: 3px solid #2d6cdf; margin: 4px 0; background: rgba(255,255,255,0.05); }
|
||||
|
||||
.weather-badge {
|
||||
display: inline-block; background: rgba(45,108,223,0.25);
|
||||
border-radius: 6px; padding: 6px 10px; font-size: 13px; margin: 6px 0;
|
||||
}
|
||||
|
||||
#rp-card-output .card {
|
||||
background: rgba(255,255,255,0.06); border-radius: 6px; padding: 10px;
|
||||
margin: 10px 0; font-size: 13px;
|
||||
}
|
||||
#rp-card-output .card h4 { margin: 4px 0; }
|
||||
|
||||
#modal-backdrop {
|
||||
position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 20;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
}
|
||||
.modal {
|
||||
position: relative; background: #1c2228; color: #eee; border-radius: 8px;
|
||||
padding: 20px; width: min(440px, 92vw); max-height: 86vh; overflow-y: auto;
|
||||
}
|
||||
.modal label { display: block; margin: 8px 0; font-size: 14px; }
|
||||
.modal input:not([type="checkbox"]), .modal textarea, .modal select {
|
||||
width: 100%; padding: 6px; border-radius: 4px; border: 1px solid #445;
|
||||
background: #11161b; color: #eee;
|
||||
}
|
||||
.status { font-size: 13px; color: #f1c40f; min-height: 1em; }
|
||||
Reference in new issue
Block a user