Files
longhikes/web/olt/records.py
T
swaitsandClaude Fable 5.2 b817d2097b
Mirror to forge / mirror (push) Canceled after 0s
Validate / validate (push) Canceled after 0s
Validate / site (push) Canceled after 0s
Validate / deploy (push) Canceled after 0s
Rebuild the site for people browsing trails
The site read like a database. It is now a place to find and read about
long trails, and everything on it is still derived from the records.

- One kind of thing: a trail. A route is a kind of trail, explained
  where the word first appears. The navigation is Trails, Places, About.
- The home page is the catalogue, with filters for where, length, time,
  season, terrain, kind, walking, shape and GPX. A filter is offered
  only when at least a third of the trails have a value for it.
- Lists (for example National Scenic Trails and Routes) and a Places
  page that groups states and provinces into regions.
- A trail page leads with what a hiker needs: summary, at a glance,
  map, downloads, elevation profile and where to go next. The dataset's
  own fields sit in a collapsed "About this data" section.
- The trail list is a compact card on phones and beside the filters,
  and a column table where it has the full page width.
- web/serve.py takes --bind, for viewing from another device.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 01:32:22 -07:00

68 lines
2.1 KiB
Python

"""Load the trail records (one trail.toml per directory) with derived fields."""
import os
import tomllib
from . import elevation
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SHIELD_NAMES = ("shield.svg", "shield.png")
def trail_kind(rec):
return rec.get("kind") or "designated"
def trail_status(rec):
"""Same rule as the status column of trails.csv (tools/build_index.py)."""
return "included" if rec.get("source", {}).get("line_file") else "record-only"
def find_files(root, tid, rec):
"""Return the data files that exist for a trail, as paths relative to root."""
d = os.path.join(root, tid)
files = {}
def rel(name):
return f"{tid}/{name}"
line_file = rec.get("source", {}).get("line_file") or ""
if line_file and os.path.isfile(os.path.join(d, line_file)):
files["line"] = rel(line_file)
if os.path.isfile(os.path.join(d, "alternates.geojson")):
files["alternates"] = rel("alternates.geojson")
extracts = {}
for name in sorted(os.listdir(d)):
if name.startswith("line-") and name.endswith(".geojson"):
extracts[name[len("line-"):-len(".geojson")]] = rel(name)
if extracts:
files["extracts"] = extracts
for name in SHIELD_NAMES:
if os.path.isfile(os.path.join(d, name)):
files["shield"] = rel(name)
break
return files
def load_record(root, tid):
with open(os.path.join(root, tid, "trail.toml"), "rb") as f:
rec = tomllib.load(f)
length_km = rec.get("length_km", 0)
rec["derived"] = {
"kind": trail_kind(rec),
"has_line": bool(rec.get("source", {}).get("line_file")),
"length_mi": int(round(length_km / 1.609344)),
"files": find_files(root, tid, rec),
"elevation": elevation.load_file(root, tid),
"status": trail_status(rec),
}
return rec
def load_records(root=ROOT):
"""Every */trail.toml under root, sorted by id."""
ids = sorted(
d for d in os.listdir(root)
if os.path.isfile(os.path.join(root, d, "trail.toml"))
)
return [load_record(root, tid) for tid in ids]