Sorting. Every list of trails has a "Sort by" menu: name, length, time or climb, each in both directions. Where the list is a table, the Trail, Length, Time and Climb heads sort on click and reverse on a second click. Lists open by name, A to Z, except the four that are defined by an order (the longest, under 100 km, a week or less, one to two weeks). The chosen order is kept in the page address. The pages are written in their default order, so they are right without JavaScript. Maps. A trail with no track but a known start and end is drawn as a dotted straight line between the two with a ring at each; one with a single known point is drawn as a ring. They follow the filters, the row highlight and the popup like any track, and that trail's own page shows a map in place of the empty frame. The legend, the caption and the popup say the dotted line is not the trail's path. It gives no download, no GPX status and no elevation. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
165 lines
5.3 KiB
Python
165 lines
5.3 KiB
Python
"""The hiker facts of a record: its optional [hike] table (docs/schema.md).
|
|
|
|
Every field is optional, and most records have no [hike] table at all, so
|
|
every function here returns "" (or an empty list) when its field is missing.
|
|
"""
|
|
import math
|
|
import re
|
|
|
|
from . import words
|
|
|
|
# Words after which a full stop does not end a sentence ("Mt. Rainier", "U.S. Forest Service").
|
|
_ABBREVIATIONS = {"mt", "st", "ft", "dr", "mr", "mrs", "ms", "jr", "sr", "no", "vs", "ca", "approx",
|
|
"u.s", "e.g", "i.e", "etc", "hwy", "rd", "mi", "km"}
|
|
_SENTENCE_END = re.compile(r"[.!?](?=\s+[A-Z0-9“\"(])")
|
|
|
|
|
|
def table(rec):
|
|
value = rec.get("hike")
|
|
return value if isinstance(value, dict) else {}
|
|
|
|
|
|
def has(rec):
|
|
return bool(table(rec))
|
|
|
|
|
|
def summary(rec):
|
|
return (table(rec).get("summary") or "").strip()
|
|
|
|
|
|
def first_sentence(text):
|
|
"""The first sentence of a text: "A. B." -> "A.". Initials and common
|
|
abbreviations ("Mt.", "U.S.") do not end a sentence."""
|
|
text = (text or "").strip()
|
|
for m in _SENTENCE_END.finditer(text):
|
|
word = re.search(r"([\w.]+)$", text[:m.start()])
|
|
w = word.group(1).lower() if word else ""
|
|
if w in _ABBREVIATIONS or (len(w) == 1 and w.isalpha()):
|
|
continue
|
|
return text[:m.end()]
|
|
return text
|
|
|
|
|
|
def one_line(rec):
|
|
"""The one-line description: the first sentence of the summary, or ""."""
|
|
return first_sentence(summary(rec))
|
|
|
|
|
|
def days(rec):
|
|
return words.fmt_days(table(rec).get("days"))
|
|
|
|
|
|
def days_max(rec):
|
|
"""The most days it usually takes, or None."""
|
|
d = table(rec).get("days")
|
|
if isinstance(d, (int, float)):
|
|
d = [d]
|
|
d = [x for x in (d or []) if isinstance(x, (int, float)) and x > 0]
|
|
return max(d) if d else None
|
|
|
|
|
|
def shape(rec):
|
|
return table(rec).get("shape", "")
|
|
|
|
|
|
def terrain(rec):
|
|
return [t for t in table(rec).get("terrain", []) if t in words.TERRAIN]
|
|
|
|
|
|
def official_url(rec):
|
|
url = table(rec).get("official_url", "")
|
|
return url if isinstance(url, str) and url.startswith(("http://", "https://")) else ""
|
|
|
|
|
|
def ends(rec):
|
|
"""(label, text) for where it starts and ends, or None.
|
|
|
|
point-to-point (or no shape): "Start and end", "A to B"; loop: "Loop", "Starts and
|
|
ends at A"; network: "Network of trails", "Most walks start at A".
|
|
"""
|
|
h = table(rec)
|
|
start, end, sh = h.get("start", ""), h.get("end", ""), h.get("shape", "")
|
|
if sh == "loop":
|
|
return ("Loop", f"Starts and ends at {start}" if start else "Starts and ends at the same place")
|
|
if sh == "network":
|
|
return ("Network of trails", f"Usual start: {start}" if start else "Several ways to start and end")
|
|
if start and end:
|
|
return ("Start and end", (start, end))
|
|
if start:
|
|
return ("Start", start)
|
|
if end:
|
|
return ("End", end)
|
|
return None
|
|
|
|
|
|
def sources(rec):
|
|
return [s for s in table(rec).get("source", []) if isinstance(s, dict)]
|
|
|
|
|
|
def _lonlat(value):
|
|
"""A [lon, lat] pair as floats, or None when it is missing or not a place on Earth."""
|
|
if not isinstance(value, (list, tuple)) or len(value) < 2:
|
|
return None
|
|
lon, lat = value[0], value[1]
|
|
if isinstance(lon, bool) or isinstance(lat, bool):
|
|
return None
|
|
if not isinstance(lon, (int, float)) or not isinstance(lat, (int, float)):
|
|
return None
|
|
lon, lat = float(lon), float(lat)
|
|
if not (-180.0 <= lon <= 180.0 and -90.0 <= lat <= 90.0):
|
|
return None
|
|
return [lon, lat]
|
|
|
|
|
|
def start_point(rec):
|
|
"""[lon, lat] of the place named in [hike].start, or None."""
|
|
return _lonlat(table(rec).get("start_lonlat"))
|
|
|
|
|
|
def end_point(rec):
|
|
"""[lon, lat] of the place named in [hike].end, or None. A loop has none."""
|
|
return _lonlat(table(rec).get("end_lonlat"))
|
|
|
|
|
|
def sketch_points(rec):
|
|
"""The points a map may sketch for a trail with no track: [start, end]
|
|
when both are on file, or the one that is. Empty when the trail has a
|
|
track or neither point (see sketch_end for which end a lone point is).
|
|
|
|
A sketch is only ever these points. It is never a track: nothing here
|
|
feeds a download, a GPX status or an elevation figure.
|
|
"""
|
|
derived = rec.get("derived", {})
|
|
if derived.get("has_line") or "line" in derived.get("files", {}):
|
|
return []
|
|
points = [p for p in (start_point(rec), end_point(rec)) if p]
|
|
return points[:1] if len(points) == 2 and points[0] == points[1] else points
|
|
|
|
|
|
def sketch_end(rec):
|
|
""""start" or "end": which end a one-point sketch marks. "" otherwise."""
|
|
if len(sketch_points(rec)) != 1:
|
|
return ""
|
|
return "start" if start_point(rec) else "end"
|
|
|
|
|
|
def sketch_bbox(points, *, pad=False):
|
|
"""[min lon, min lat, max lon, max lat] of sketch points, or None.
|
|
|
|
pad: widen for framing a map. One point gets about a county of ground;
|
|
two points get a margin around the pair.
|
|
"""
|
|
if not points:
|
|
return None
|
|
box = [min(p[0] for p in points), min(p[1] for p in points),
|
|
max(p[0] for p in points), max(p[1] for p in points)]
|
|
if not pad:
|
|
return box
|
|
if len(points) == 1:
|
|
lat_pad = 0.35
|
|
lon_pad = 0.35 / max(math.cos(math.radians(points[0][1])), 0.25)
|
|
else:
|
|
lat_pad = max((box[3] - box[1]) * 0.12, 0.05)
|
|
lon_pad = max((box[2] - box[0]) * 0.12, 0.05)
|
|
return [box[0] - lon_pad, box[1] - lat_pad, box[2] + lon_pad, box[3] + lat_pad]
|