From cfa80263007d0693fc087da4ad29f4ceab230f22 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 18 Nov 2026 17:00:42 -0700 Subject: [PATCH] Open a place page's map on the place, not on the extent of its trails Utah's map opened on the whole country because the American Discovery Trail crosses it. Every state, province and group page now opens on its own box (web/place-bounds.toml, Natural Earth, public domain); tracks that run past it are still drawn in full and leave the opening view. Co-Authored-By: Claude Fable 5.2 --- web/olt/bounds.py | 41 +++++++++++++ web/olt/home.py | 17 ++++-- web/olt/placefacets.py | 4 +- web/olt/regions.py | 13 ++++- web/place-bounds.toml | 80 ++++++++++++++++++++++++++ web/tests/test_place_view.py | 108 +++++++++++++++++++++++++++++++++++ 6 files changed, 252 insertions(+), 11 deletions(-) create mode 100644 web/olt/bounds.py create mode 100644 web/place-bounds.toml create mode 100644 web/tests/test_place_view.py diff --git a/web/olt/bounds.py b/web/olt/bounds.py new file mode 100644 index 0000000..3a8a159 --- /dev/null +++ b/web/olt/bounds.py @@ -0,0 +1,41 @@ +"""Where a place is, for the map its page opens on (web/place-bounds.toml). + +A place page opens its map on the place, not on the extent of its trails: a trail that crosses the +country on its way elsewhere would otherwise make the map open on all of it. The tracks of such a +trail are still drawn in full; they run off the edge of the opening view, and zooming out shows them. +""" +import os +import tomllib + +PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "place-bounds.toml") +_boxes = None + + +def boxes(): + """key ("US-UT") -> [west, south, east, north], read once.""" + global _boxes + if _boxes is None: + try: + with open(PATH, "rb") as f: + _boxes = {k: [float(v) for v in box] for k, box in (tomllib.load(f).get("box") or {}).items()} + except (OSError, ValueError): + _boxes = {} + return _boxes + + +def place_box(key): + """The box of a state or province (key as in olt/facets.py), or None when none is held.""" + return boxes().get(key) + + +def union(keys): + """The box that holds the places in keys, or None when there are none or any has no box: the + page then keeps the extent of its trails rather than show part of its place. + + A group page passes the keys of its members that have trails. A country with far-flung parts + (the United States with Alaska and Hawaii) would be handled the same way, so that it opens on the + parts the site has trails in and not on the whole width of the map.""" + found = [place_box(k) for k in keys] + if not found or any(b is None for b in found): + return None + return [min(b[0] for b in found), min(b[1] for b in found), max(b[2] for b in found), max(b[3] for b in found)] diff --git a/web/olt/home.py b/web/olt/home.py index ad1dd96..2c05441 100644 --- a/web/olt/home.py +++ b/web/olt/home.py @@ -80,9 +80,10 @@ def overview_box(records): return [min(b[0] for b in boxes), min(b[1] for b in boxes), max(b[2] for b in boxes), max(b[3] for b in boxes)] -def overview_bbox(records): - """data-bbox attribute: the extent of every track and sketch, so the map opens on it before the data loads.""" - box = overview_box(records) +def overview_bbox(records, box=None): + """data-bbox attribute: the extent of every track and sketch, so the map opens on it before the data loads. + box: the ground to open on instead (a place page's, olt/bounds.py).""" + box = box or overview_box(records) return f' data-bbox="{",".join(f"{v:.4f}" for v in box)}"' if box else "" @@ -627,7 +628,7 @@ def drawable_ids(recs): return [r["id"] for r in recs if r["id"] in tracks or hike.sketch_points(r)] -def overview_map(recs, label, *, wide=False, scope="", saved=False): +def overview_map(recs, label, *, wide=False, scope="", saved=False, place=None): """The map of the given trails (the overview geometry, limited to their ids unless every trail is shown). A note instead when too little can be drawn. @@ -635,6 +636,10 @@ def overview_map(recs, label, *, wide=False, scope="", saved=False): app.js limits it to the trails shown; the figure lists the ids it can draw (data-drawable), so app.js leaves the map out when it would be empty, and its caption counts nothing, since the list is the visitor's. + + place: [west, south, east, north] of the place a place page is about. The map opens on it, and + returns to it when the filters are cleared; the tracks are drawn in full, and those that run + beyond it leave the opening view. Without it the map opens on the extent of the tracks. """ ids = drawable_ids(recs) if not ids: @@ -681,7 +686,7 @@ def overview_map(recs, label, *, wide=False, scope="", saved=False): # The tracks are drawn into the page, on the ground the live map opens on (olt/sketch.py). The # Saved page has none: which trails it draws is the visitor's own, so app.js draws them. frame_attr, drawing = "", "" - box = overview_box(recs) + box = place or overview_box(recs) if box and not saved: fr = sketch.frame(box) frame_attr = f' data-frame="{",".join(f"{v:g}" for v in fr)}"' @@ -689,7 +694,7 @@ def overview_map(recs, label, *, wide=False, scope="", saved=False): # The frame takes the shape of the ground it shows, within calm bounds, so the tracks fill it # (style.css .browse__map); the drawing and the live map fit the same ground either way. frame_attr += f' style="--frame-n: {frame_ratio(fr):.3f}"' - return Markup(f"""
+ return Markup(f"""
diff --git a/web/olt/placefacets.py b/web/olt/placefacets.py index 51828cb..2ad1c66 100644 --- a/web/olt/placefacets.py +++ b/web/olt/placefacets.py @@ -5,7 +5,7 @@ least MIN_SIZE trails in the list, no more than MAX_SHARE of the place's trails would repeat the place page), and its trails are not nearly the same set as another such page of the same place. A new trail or a corrected record moves pages in and out with no change here. """ -from . import browse, common, facets, home, regions, summary +from . import bounds, browse, common, facets, home, regions, summary from .markup import Markup, esc SAME_SET = 0.8 # two pages of one place whose trails overlap by this share (of the smaller union) are one page @@ -105,7 +105,7 @@ def page(p, records, site, all_pages):
- {home.browse_view(home.listing(recs, default_sort=lst.sort, top=2, where=""), home.overview_map(recs, f"Map of the trails in {name}: {lst.title}"), + {home.browse_view(home.listing(recs, default_sort=lst.sort, top=2, where=""), home.overview_map(recs, f"Map of the trails in {name}: {lst.title}", place=bounds.place_box(info["key"])), about=summary.facts(recs, records), about_title=f"About these trails in {name}")} {nav} diff --git a/web/olt/regions.py b/web/olt/regions.py index 6b8c43e..74a93b0 100644 --- a/web/olt/regions.py +++ b/web/olt/regions.py @@ -6,7 +6,7 @@ The section is called Places; its pages keep their /regions/ URLs. """ import collections -from . import browse, common, facets, home, summary, words +from . import bounds, browse, common, facets, home, summary, words from .markup import Markup, esc @@ -33,6 +33,13 @@ def collect_regions(records): return by_slug, across +def group_box(g, by_slug): + """The ground a group page opens on: the union of the boxes of its members that have trails, + or None when one of them has no box (the map then opens on its trails).""" + keys = next(k for x, _, k in facets.PLACE_GROUPS if x == g) + return bounds.union([k for k in keys if k.lower() in by_slug]) + + def group_records(records, group): return [r for r in records if group in facets.place_groups(r)] @@ -153,7 +160,7 @@ def group_page(g, records, site):
- {home.browse_view(home.listing(recs, top=2, where="g:" + g), home.overview_map(recs, f"Map of the trails in {name}"), + {home.browse_view(home.listing(recs, top=2, where="g:" + g), home.overview_map(recs, f"Map of the trails in {name}", place=group_box(g, by_slug)), about=f"{summary.facts(recs, records)}{summary.details(recs)}", about_title=f"About the trails in {name}")} {nav}
""" @@ -206,7 +213,7 @@ def region_page(slug, records, site, facet_pages=()):
- {home.browse_view(home.listing(recs, top=2, where=""), home.overview_map(recs, f"Map of the trails in {info['name']}"), + {home.browse_view(home.listing(recs, top=2, where=""), home.overview_map(recs, f"Map of the trails in {info['name']}", place=bounds.place_box(key)), about=f"{summary.facts(recs, records)}{summary.details(recs)}", about_title=f"About the trails in {info['name']}")} {nav} diff --git a/web/place-bounds.toml b/web/place-bounds.toml new file mode 100644 index 0000000..747f5a2 --- /dev/null +++ b/web/place-bounds.toml @@ -0,0 +1,80 @@ +# Where each state, province and territory is: the ground a place page's map opens on. +# +# One box per place the site has a page for (every key of olt/facets.py PLACE_GROUPS), as +# [west, south, east, north] in degrees (WGS 84), rounded to two decimals. olt/bounds.py reads this +# file; a group page takes the union of its members' boxes, and a place with no entry here keeps the +# map fitted to its trails. Nothing is fetched at build time or in the browser. +# +# Source: Natural Earth 1:10m Admin 1 - States, Provinces (ne_10m_admin_1_states_provinces, +# 2022 release), https://www.naturalearthdata.com/. Licence: public domain (Natural Earth's terms of +# use put no restriction on use, with or without credit). Each box is the extent of the place's +# polygons, leaving out small outlying islands (polygons under 1% of the place's largest) so that, +# for example, Hawaii's box is the main islands. Alaska's box is the mainland and the Alaska +# Peninsula only: the Aleutian Islands cross the antimeridian (longitude 180) and would make a box +# that wraps the world, and no trail is there. No box crosses the antimeridian. + +[box] +"CA-AB" = [-120.00, 48.99, -110.00, 60.00] +"CA-BC" = [-139.06, 48.31, -114.04, 60.00] +"CA-MB" = [-102.00, 48.99, -88.95, 60.00] +"CA-NB" = [-69.06, 45.06, -63.77, 48.08] +"CA-NL" = [-67.81, 46.62, -52.62, 60.30] +"CA-NS" = [-66.21, 43.47, -59.80, 47.03] +"CA-NT" = [-136.45, 60.00, -101.99, 77.56] +"CA-NU" = [-120.68, 60.00, -61.09, 83.12] +"CA-ON" = [-95.16, 41.67, -74.35, 56.86] +"CA-PE" = [-64.42, 45.95, -61.98, 47.07] +"CA-QC" = [-79.77, 45.00, -57.10, 62.59] +"CA-SK" = [-110.00, 48.99, -101.36, 60.00] +"CA-YT" = [-141.01, 60.00, -123.82, 69.65] +"US-AK" = [-168.14, 54.76, -129.99, 71.41] +"US-AL" = [-88.49, 30.22, -84.90, 35.01] +"US-AR" = [-94.62, 33.01, -89.67, 36.50] +"US-AZ" = [-114.82, 31.33, -109.05, 37.00] +"US-CA" = [-124.41, 32.53, -114.12, 42.00] +"US-CO" = [-109.05, 37.00, -102.04, 41.00] +"US-CT" = [-73.72, 41.00, -71.80, 42.05] +"US-DC" = [-77.12, 38.80, -76.91, 39.00] +"US-DE" = [-75.79, 38.45, -75.04, 39.84] +"US-FL" = [-87.63, 25.12, -80.03, 31.00] +"US-GA" = [-85.61, 30.36, -80.84, 35.00] +"US-HI" = [-160.25, 18.91, -154.81, 22.24] +"US-IA" = [-96.64, 40.38, -90.14, 43.50] +"US-ID" = [-117.22, 42.00, -111.05, 48.99] +"US-IL" = [-91.52, 36.98, -87.02, 42.51] +"US-IN" = [-88.11, 37.78, -84.78, 41.76] +"US-KS" = [-102.05, 37.00, -94.61, 40.00] +"US-KY" = [-89.42, 36.50, -81.97, 39.15] +"US-LA" = [-94.04, 28.93, -89.01, 33.01] +"US-MA" = [-73.51, 41.30, -69.92, 42.89] +"US-MD" = [-79.49, 37.93, -75.08, 39.72] +"US-ME" = [-71.08, 43.08, -66.98, 47.46] +"US-MI" = [-90.41, 41.70, -82.15, 48.31] +"US-MN" = [-97.23, 43.50, -89.50, 49.37] +"US-MO" = [-95.77, 35.99, -89.12, 40.62] +"US-MS" = [-91.66, 30.18, -88.09, 35.00] +"US-MT" = [-116.05, 44.38, -104.04, 48.99] +"US-NC" = [-84.32, 33.87, -75.72, 36.61] +"US-ND" = [-104.05, 45.94, -96.56, 48.99] +"US-NE" = [-104.05, 40.00, -95.32, 43.00] +"US-NH" = [-72.56, 42.70, -70.70, 45.30] +"US-NJ" = [-75.56, 38.92, -73.91, 41.36] +"US-NM" = [-109.05, 31.33, -103.00, 37.00] +"US-NV" = [-120.00, 35.00, -114.04, 42.00] +"US-NY" = [-79.76, 40.54, -71.86, 45.01] +"US-OH" = [-84.82, 38.41, -80.52, 42.32] +"US-OK" = [-103.00, 33.65, -94.44, 37.00] +"US-OR" = [-124.57, 42.00, -116.45, 46.24] +"US-PA" = [-80.52, 39.72, -74.70, 42.54] +"US-RI" = [-71.88, 41.15, -71.12, 42.02] +"US-SC" = [-83.36, 32.03, -78.57, 35.22] +"US-SD" = [-104.06, 42.52, -96.45, 45.94] +"US-TN" = [-90.31, 34.98, -81.66, 36.69] +"US-TX" = [-106.66, 25.84, -93.51, 36.50] +"US-UT" = [-114.04, 37.00, -109.05, 42.00] +"US-VA" = [-83.67, 36.54, -75.38, 39.45] +"US-VT" = [-73.44, 42.73, -71.51, 45.01] +"US-WA" = [-124.73, 45.57, -116.88, 48.99] +"US-WI" = [-92.90, 42.49, -86.25, 47.31] +"US-WV" = [-82.64, 37.20, -77.73, 40.65] +"US-WY" = [-111.05, 41.00, -104.05, 45.00] diff --git a/web/tests/test_place_view.py b/web/tests/test_place_view.py new file mode 100644 index 0000000..0cb5c92 --- /dev/null +++ b/web/tests/test_place_view.py @@ -0,0 +1,108 @@ +"""A place page's map opens on the place, not on the extent of its trails (olt/bounds.py).""" +import copy +import os +import re +import sys +import unittest +from unittest import mock + +WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +ROOT = os.path.dirname(WEB) +sys.path.insert(0, WEB) + +from olt import bounds, facets, home, placefacets, records, regions, site, sketch # noqa: E402 + + +def attr(html, name): + m = re.search(rf'\bdata-{name}="([^"]*)"', html) + return [float(v) for v in m.group(1).split(",")] if m else None + + +def figure(html): + return re.search(r'