diff --git a/tools/place_trails.py b/tools/place_trails.py index ab0dbe4..93558a0 100644 --- a/tools/place_trails.py +++ b/tools/place_trails.py @@ -466,6 +466,40 @@ def assign_slugs(outlines): return outlines +def merge_same_name(outlines): + """One name within a kind and source is one place: the outlines are unioned and the + place carries every id. The source lists some ranges twice under different ids.""" + merged, order = {}, [] + for o in outlines: + key = (o.kind, o.source, " ".join(o.name.lower().split())) + # Two wilderness areas or parks of one name in different places are two places + # (assign_slugs tells them apart by state); a range is one wherever its pieces lie. + if key in merged and o.kind != "range" and not any( + o.geometry.intersects(m.geometry) for m in merged[key]): + key = key + (str(o.source_id),) + if key not in merged: + merged[key] = [o] + order.append(key) + else: + merged[key].append(o) + out = [] + for key in order: + group = merged[key] + if len(group) == 1: + out.append(group[0]) + continue + first = group[0] + ids = sorted({str(i) for o in group + for i in (o.source_id if isinstance(o.source_id, list) else [o.source_id])}) + first.source_id = ids + first.geometry = _union_geoms(o.geometry for o in group) + first.within = tuple(sorted({w for o in group for w in o.within})) + first.state_codes_hint = sorted({h for o in group for h in o.state_codes_hint}) + first.where_hand = first.where_hand or next((o.where_hand for o in group if o.where_hand), ()) + out.append(first) + return out + + def catalogue(cache, allowlist=None): """The outlines to measure: parks whole, wilderness whole, ranges allow-listed only.""" allowlist = allowlist if allowlist is not None else load_allowlist(default_allowlist_path()) @@ -507,7 +541,7 @@ def catalogue(cache, allowlist=None): slug=spec.get("slug") or "", within=tuple(spec.get("within") or ()), path=g["path"], where_hand=tuple(spec.get("where") or ()))) - return assign_slugs(out) + return assign_slugs(merge_same_name(out)) _GMBA_CACHE = {} diff --git a/tools/tests/test_place_trails.py b/tools/tests/test_place_trails.py index 86c17c3..dae63a5 100644 --- a/tools/tests/test_place_trails.py +++ b/tools/tests/test_place_trails.py @@ -170,6 +170,33 @@ class ProposalTest(unittest.TestCase): o = outline("park", "YOSE", "Yosemite National Park", yose, hint=["CA"]) self.assertEqual(P.where_of(o), ["CA"]) + def test_one_name_in_a_kind_and_source_is_one_place_with_both_ids(self): + a = outline("range", "17222", "Palisades", self.park.geometry) + b = outline("range", "19588", "Palisades", self.range.geometry) + c = outline("range", "5", "Other Range", self.range.geometry) + out = P.merge_same_name([a, b, c]) + self.assertEqual([o.name for o in out], ["Palisades", "Other Range"]) + self.assertEqual(out[0].source_id, ["17222", "19588"]) + self.assertAlmostEqual(out[0].geometry.area, + self.park.geometry.union(self.range.geometry).area) + out = P.assign_slugs(out) + self.assertEqual(out[0].slug, "palisades") # no numeric suffix, no state code + + def test_the_same_name_from_two_sources_or_kinds_is_not_merged(self): + a = outline("park", "1", "Glacier", self.park.geometry, hint=["MT"]) + b = outline("park", "2", "Glacier", self.range.geometry, hint=["BC"]) + b.source = "cpcad" + w = outline("wilderness", "3", "Glacier", self.range.geometry) + self.assertEqual(len(P.merge_same_name([a, b, w])), 3) + + def test_two_far_apart_wilderness_areas_of_one_name_stay_two_places(self): + far = Polygon([(-80, 35), (-79, 35), (-79, 36), (-80, 36)]) + a = outline("wilderness", "1", "Bear Wilderness", self.park.geometry, hint=["WY"]) + b = outline("wilderness", "2", "Bear Wilderness", far, hint=["NC"]) + self.assertEqual(len(P.merge_same_name([a, b])), 2) + touching = outline("wilderness", "3", "Bear Wilderness", self.range.geometry, hint=["WY"]) + self.assertEqual(len(P.merge_same_name([a, touching])), 1) + 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"])