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}
|
||||
|
||||
@@ -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]
|
||||
@@ -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'<figure class="mapfig[^>]*>', str(html)).group(0)
|
||||
|
||||
|
||||
def rounded(box):
|
||||
return [round(v, 4) for v in box]
|
||||
|
||||
|
||||
class PlaceViewTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# The tracks' extents are worked out by the build; here each trail is given one: a small box
|
||||
# at the middle of its first place, and the American Discovery Trail's, from coast to coast.
|
||||
cls.records = copy.deepcopy(records.load_records(ROOT))
|
||||
for r in cls.records:
|
||||
key = facets.place_keys(r)[0] if facets.place_keys(r) else None
|
||||
box = bounds.place_box(key) if key else None
|
||||
cx, cy = ((box[0] + box[2]) / 2, (box[1] + box[3]) / 2) if box else (-100.0, 40.0)
|
||||
r["derived"]["bbox"] = [-123.0, 33.0, -75.0, 40.0] if r["id"] == "american-discovery-trail" \
|
||||
else [cx - 0.3, cy - 0.2, cx + 0.3, cy + 0.2]
|
||||
r["derived"]["files"] = dict(r["derived"].get("files", {}), line="x.gpx")
|
||||
cls.site = site.load()
|
||||
cls.by_slug, _ = regions.collect_regions(cls.records)
|
||||
|
||||
def region_figure(self, slug):
|
||||
return figure(regions.region_page(slug, self.records, self.site)[2])
|
||||
|
||||
def test_every_place_the_site_has_a_page_for_has_a_box(self):
|
||||
for _, _, keys in facets.PLACE_GROUPS:
|
||||
for key in keys:
|
||||
box = bounds.place_box(key)
|
||||
self.assertIsNotNone(box, key)
|
||||
w, s, e, n = box
|
||||
self.assertTrue(-180 <= w < e <= 180 and -90 <= s < n <= 90, key) # none wraps the antimeridian
|
||||
|
||||
def test_utah_opens_on_utah_and_not_on_its_trails(self):
|
||||
fig = self.region_figure("us-ut")
|
||||
self.assertEqual(attr(fig, "bbox"), [-114.04, 37.0, -109.05, 42.0])
|
||||
self.assertEqual(attr(fig, "frame"), [float(f"{v:g}") for v in sketch.frame(bounds.place_box("US-UT"))])
|
||||
trails = home.overview_box(self.by_slug["us-ut"]["records"])
|
||||
self.assertLess(trails[0], -114.04 - 5) # the American Discovery Trail runs far past the state
|
||||
self.assertIn("american-discovery-trail", re.search(r'data-ids="([^"]*)"', fig).group(1))
|
||||
|
||||
def test_a_group_opens_on_the_union_of_its_members_with_trails(self):
|
||||
g = "mountain-west"
|
||||
html = regions.group_page(g, self.records, self.site)[2]
|
||||
keys = next(k for x, _, k in facets.PLACE_GROUPS if x == g)
|
||||
boxes = [bounds.place_box(k) for k in keys if k.lower() in self.by_slug]
|
||||
want = [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)]
|
||||
self.assertEqual(attr(figure(html), "bbox"), rounded(want))
|
||||
|
||||
def test_a_member_with_no_trails_is_left_out_of_the_union(self):
|
||||
by_slug = {"us-id": {}, "us-mt": {}}
|
||||
self.assertEqual(regions.group_box("mountain-west", by_slug), [-117.22, 42.0, -104.04, 48.99])
|
||||
|
||||
def test_a_place_with_no_bounds_keeps_the_extent_of_its_trails(self):
|
||||
with mock.patch.object(bounds, "_boxes", {}):
|
||||
fig = self.region_figure("us-ut")
|
||||
self.assertIsNone(bounds.union(["US-UT", "US-CO"]))
|
||||
want = home.overview_box(self.by_slug["us-ut"]["records"])
|
||||
self.assertEqual(attr(fig, "bbox"), rounded(want))
|
||||
|
||||
def test_one_missing_member_box_leaves_the_group_on_its_trails(self):
|
||||
boxes = {k: v for k, v in bounds.boxes().items() if k != "US-WY"}
|
||||
with mock.patch.object(bounds, "_boxes", boxes):
|
||||
html = regions.group_page("mountain-west", self.records, self.site)[2]
|
||||
recs = regions.group_records(self.records, "mountain-west")
|
||||
self.assertEqual(attr(figure(html), "bbox"), rounded(home.overview_box(recs)))
|
||||
|
||||
def test_a_place_and_kind_page_opens_on_its_place(self):
|
||||
pages = [p for p in placefacets.plan(self.records) if p["slug"] == "us-ut"]
|
||||
if not pages:
|
||||
self.skipTest("no Utah page of one kind of trail")
|
||||
fig = figure(placefacets.page(pages[0], self.records, self.site, pages)[2])
|
||||
self.assertEqual(attr(fig, "bbox"), [-114.04, 37.0, -109.05, 42.0])
|
||||
|
||||
def test_the_home_and_list_maps_open_on_their_trails(self):
|
||||
sample = self.records[:12]
|
||||
box = home.overview_box(sample)
|
||||
fig = figure(home.overview_map(sample, "Map"))
|
||||
self.assertEqual(attr(fig, "bbox"), rounded(box))
|
||||
self.assertEqual(attr(fig, "frame"), [float(f"{v:g}") for v in sketch.frame(box)])
|
||||
whole = figure(home.overview_map(self.records, "Map", scope="all"))
|
||||
self.assertEqual(attr(whole, "bbox"), rounded(home.overview_box(self.records)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user