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 <noreply@anthropic.com>
This commit is contained in:
1 parent
9703848d95
commit
cfa8026300
6 files changed
+252
-11
No files matched your search
@@ -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)]
|
||||
+11
-6
@@ -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"""<figure class="mapfig{" mapfig--wide" if wide else ""}" data-map="overview" data-src="/data/overview.geojson"{ids_attr}{overview_bbox(recs)}{frame_attr}>
|
||||
return Markup(f"""<figure class="mapfig{" mapfig--wide" if wide else ""}" data-map="overview" data-src="/data/overview.geojson"{ids_attr}{overview_bbox(recs, place)}{frame_attr}>
|
||||
<div class="mapfig__frame">
|
||||
<span class="mapfig__corner mapfig__corner--nw" aria-hidden="true"></span>
|
||||
<div class="map" role="region" aria-label="{esc(label)}">
|
||||
|
||||
@@ -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):
|
||||
</div></header>
|
||||
</div></div>
|
||||
<div class="container section place">
|
||||
{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 class="region-nav" aria-label="Lists"><a href="{esc(browse.path(lst))}">{esc(lst.title)} everywhere</a> · <a href="/lists/">All lists</a></nav>
|
||||
{nav}
|
||||
|
||||
+10
-3
@@ -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):
|
||||
</div></header>
|
||||
</div></div>
|
||||
<div class="container section place">
|
||||
{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}
|
||||
</div>"""
|
||||
@@ -206,7 +213,7 @@ def region_page(slug, records, site, facet_pages=()):
|
||||
</div></header>
|
||||
</div></div>
|
||||
<div class="container section place">
|
||||
{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}
|
||||
|
||||
Reference in new issue
Block a user