From e59c152e7c6c9d912701055338b4e38b849ca433 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 10:39:51 -0700 Subject: [PATCH] List a park and not its same-trail wilderness twin When a wilderness area and a national park hold exactly the same trails and one name's words contain the other's, only the park is kept, so Sequoia and Kings Canyon and Yosemite do not get two pages. The rule runs on the tracked sets in propose and on every record's tracked and stated places in --check. Co-Authored-By: Claude Sonnet 5.5 --- tools/place_trails.py | 44 ++++++++++++++++++++++++++++++++ tools/tests/test_place_trails.py | 34 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/tools/place_trails.py b/tools/place_trails.py index 93558a0..c5825c2 100644 --- a/tools/place_trails.py +++ b/tools/place_trails.py @@ -639,6 +639,15 @@ def propose(records, outlines): hit.trails.sort(key=lambda t: (-t[1], t[0])) hit.page = len(hit.trails) >= 3 hit.where = where_of(hit.outline) + sets = {k: {t for t, _km in h.trails} for k, h in p.per_place.items()} + names = {k: h.outline.name for k, h in p.per_place.items()} + gone = doubled_places(sets, names) + for k in gone: + del p.per_place[k] + if gone: + for tid in p.per_trail: + p.per_trail[tid] = [(k, km) for k, km in p.per_trail[tid] if k not in gone] + p.near = [n for n in p.near if n.key not in gone] # Parents are never stored on a record (the build closes membership over within): # a range drops out of a trail's list when the trail is also in one of its # descendants, by the GMBA Path. Place counts stay as measured. @@ -658,6 +667,32 @@ def propose(records, outlines): return p +_NAME_FILLER = {"national", "park", "wilderness", "area", "preserve", "and", "the", "of"} + + +def _core_name(name): + return {w for w in re.split(r"[^a-z0-9]+", name.lower()) if w and w not in _NAME_FILLER} + + +def doubled_places(sets, names): + """Wilderness areas that would be a second page for a park: same trails exactly, and + one name's words contain the other's (Sequoia-Kings Canyon Wilderness beside Sequoia + and Kings Canyon). The park is kept; the wilderness is returned. sets is + {(kind, slug): set of trail ids}, tracked and stated together; names {(kind, slug): name}.""" + drop = set() + for wk, wset in sets.items(): + if wk[0] != "wilderness" or not wset: + continue + wc = _core_name(names.get(wk, wk[1])) + for pk, pset in sets.items(): + if pk[0] != "park" or pset != wset: + continue + pc = _core_name(names.get(pk, pk[1])) + if wc and pc and (wc <= pc or pc <= wc): + drop.add(wk) + return drop + + def range_candidates(records, all_ranges, allowlisted=()): """GMBA ranges that 3 or more tracks cross (1 mile or more), minus the allow-list.""" outlines = [Outline("range", "gmba-v2", g["gmba_id"], g["name"], [], g["geometry"], @@ -1022,6 +1057,15 @@ def check(root, cache, outlines=None, versions=None, records=None): if was != now: diffs.append(f"{k[0]}/{k[1]}: crossed the 3-trail line " f"({'up to' if now else 'down to'} {new_counts.get(k, 0)} trails)") + final, shown = {}, {} + for e in registry_entries(root): + shown[(e.get("kind"), e.get("slug"))] = e.get("name") or e.get("slug") + for r in records: + for pl in (r.get("rec") or {}).get("place") or []: + final.setdefault((pl.get("kind"), pl.get("slug")), set()).add(r["id"]) + for k in sorted(doubled_places(final, shown)): + diffs.append(f"{k[0]}/{k[1]}: holds exactly the trails of a park of the same name; " + f"list the park only") for e in registry_entries(root): v = versions.get(e.get("source")) if v and e.get("source_version") and e["source_version"] != v: diff --git a/tools/tests/test_place_trails.py b/tools/tests/test_place_trails.py index dae63a5..9ad8c3b 100644 --- a/tools/tests/test_place_trails.py +++ b/tools/tests/test_place_trails.py @@ -197,6 +197,40 @@ class ProposalTest(unittest.TestCase): touching = outline("wilderness", "3", "Bear Wilderness", self.range.geometry, hint=["WY"]) self.assertEqual(len(P.merge_same_name([a, touching])), 1) + def test_a_wilderness_with_a_parks_exact_trails_and_a_matching_name_is_not_listed(self): + wild = outline("wilderness", "W1", "Alpha Wilderness", self.park.geometry) + wild.source = "wilderness-connect" + records = [rec("a", ["WY"], self.park_only), rec("b", ["WY"], self.park_only), + rec("c", ["WY"], self.park_only)] + p = P.propose(records, [self.park, wild]) + self.assertIn(("park", "alpha-park"), p.per_place) + self.assertNotIn(("wilderness", "alpha-wilderness"), p.per_place) + self.assertTrue(all(k[0] != "wilderness" for k, _ in p.per_trail["a"])) + self.assertEqual([n for n in p.near if n.key[0] == "wilderness"], []) + + def test_the_joined_park_and_its_hyphenated_wilderness_are_one_page(self): + sets = {("park", "sequoia-and-kings-canyon"): {"a", "b", "c"}, + ("wilderness", "sequoia-kings-canyon-wilderness"): {"a", "b", "c"}, + ("wilderness", "john-muir-wilderness"): {"a", "b", "c"}} + names = {("park", "sequoia-and-kings-canyon"): "Sequoia and Kings Canyon", + ("wilderness", "sequoia-kings-canyon-wilderness"): "Sequoia-Kings Canyon Wilderness", + ("wilderness", "john-muir-wilderness"): "John Muir Wilderness"} + self.assertEqual(P.doubled_places(sets, names), {("wilderness", "sequoia-kings-canyon-wilderness")}) + + def test_a_wilderness_with_more_or_fewer_trails_than_the_park_is_kept(self): + names = {("park", "alpha"): "Alpha National Park", ("wilderness", "alpha-wilderness"): "Alpha Wilderness"} + sets = {("park", "alpha"): {"a", "b"}, ("wilderness", "alpha-wilderness"): {"a"}} + self.assertEqual(P.doubled_places(sets, names), set()) + sets[("wilderness", "alpha-wilderness")] = {"a", "b", "c"} + self.assertEqual(P.doubled_places(sets, names), set()) + + def test_a_check_counts_stated_trails_too_when_it_finds_a_doubled_page(self): + names = {("park", "alpha"): "Alpha National Park", ("wilderness", "alpha-wilderness"): "Alpha Wilderness"} + tracked = {("park", "alpha"): {"a", "b"}, ("wilderness", "alpha-wilderness"): {"a", "b"}} + self.assertEqual(len(P.doubled_places(tracked, names)), 1) + stated = {("park", "alpha"): {"a", "b", "z"}, ("wilderness", "alpha-wilderness"): {"a", "b"}} + self.assertEqual(P.doubled_places(stated, names), set()) + def test_a_slug_collision_inside_a_kind_gets_the_state_code(self): a = outline("park", "P1", "Glacier", self.park.geometry, slug=None, hint=["MT"]) b = outline("park", "P2", "Glacier", self.range.geometry, slug=None, hint=["BC"])