Treat relation members with the role route as main in the track tool
OpenStreetMap's wiki gives the role route as the old name of the empty role, along with the cardinal-direction roles. The tool treated every role outside its two lists as a side trail, which dropped 36 of the 49 ways of the Teton Crest Trail relation. Roles the wiki does not define now stop the run, unless every member has the same one, so no ways are dropped without a message. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
59cc1962d4
commit
5dc9d3e06c
2 files changed
+106
-17
No files matched your search
+45
-8
@@ -34,10 +34,17 @@ record's [verification] and [osm] sections need: ways in main and in
|
||||
alternates, measured length of the main track, and the number of parts left
|
||||
after joining way ends within 100 m (the rule tools/elevation.py uses).
|
||||
|
||||
Roles. A member way or sub-relation with an empty or main-type role (see
|
||||
MAIN_ROLES) is main. Anything with a side role (SIDE_ROLES) is alternate, and
|
||||
so is every way inside a side sub-relation. An unknown role is treated as
|
||||
side, with a warning. The count per role is printed. A way listed more than
|
||||
Roles, following the OpenStreetMap wiki (Relation:route, "Members", and
|
||||
Roles for recreational route relations). A member way or sub-relation with an
|
||||
empty role, main, route (the old name for the empty role), forward, backward,
|
||||
or a cardinal direction (north, south, east, west: the route's own directions
|
||||
in a single relation) is main. alternative, excursion, approach and connection
|
||||
are side, as are the informal alternate, link, side_trail, spur, detour and
|
||||
variant; every way inside a side sub-relation is alternate. A role the wiki
|
||||
does not define is reported loudly. If every way and sub-relation member of the
|
||||
relation tree carries that one role it is treated as main; otherwise the tool
|
||||
stops with an error naming the roles and counts, and nothing is dropped
|
||||
silently. The count per role is printed. A way listed more than
|
||||
once is kept once, at its first place; a way that is main in one place and
|
||||
side in another (a side relation sharing ways with the main route) is main.
|
||||
|
||||
@@ -68,7 +75,8 @@ RETRY_WAITS_S = (10, 30, 60) # between rounds of all endpoints
|
||||
JOIN_GAP_M = 100.0 # same distance as tools/elevation.py
|
||||
EARTH_R = 6371008.8 # metres; same as tools/elevation.py
|
||||
|
||||
MAIN_ROLES = {"", "main", "forward", "backward"}
|
||||
MAIN_ROLES = {"", "main", "route", "forward", "backward",
|
||||
"north", "south", "east", "west"}
|
||||
SIDE_ROLES = {"alternate", "alternative", "excursion", "approach", "connection",
|
||||
"link", "side_trail", "spur", "detour", "variant"}
|
||||
|
||||
@@ -228,11 +236,40 @@ def parse(doc, relation_id, warn=None, exclude_names=()):
|
||||
key = f"{kind}:{role or '(empty)'}"
|
||||
roles[key] = roles.get(key, 0) + 1
|
||||
|
||||
# Roles the wiki does not define: find them all first, so that the tool
|
||||
# can stop before writing anything rather than drop ways quietly.
|
||||
members_seen, unknown = [], {}
|
||||
stack0, done = [int(relation_id)], set()
|
||||
while stack0:
|
||||
rid = stack0.pop()
|
||||
if rid in done or rid not in rels:
|
||||
continue
|
||||
done.add(rid)
|
||||
for m in rels[rid].get("members", []):
|
||||
if m.get("type") in ("way", "relation"):
|
||||
r = m.get("role", "")
|
||||
members_seen.append(r)
|
||||
if r not in MAIN_ROLES and r not in SIDE_ROLES:
|
||||
unknown[r] = unknown.get(r, 0) + 1
|
||||
if m["type"] == "relation":
|
||||
stack0.append(m["ref"])
|
||||
unknown_as_main = set()
|
||||
if unknown:
|
||||
listing = ", ".join(f"{r!r} x{n}" for r, n in sorted(unknown.items()))
|
||||
if len(unknown) == 1 and all(r in unknown for r in members_seen):
|
||||
unknown_as_main = set(unknown)
|
||||
warn(f"UNKNOWN ROLE {listing}: not defined by OpenStreetMap, but every "
|
||||
"way and sub-relation member has it, so all are treated as main")
|
||||
else:
|
||||
raise TrackError(
|
||||
f"relation {relation_id}: member roles the OpenStreetMap wiki does not "
|
||||
f"define: {listing} (of {len(members_seen)} way and sub-relation "
|
||||
"members). They are neither main nor side by any rule, so nothing was "
|
||||
"written; decide what they mean and extend MAIN_ROLES or SIDE_ROLES")
|
||||
|
||||
def classify(role, inherited_side, what):
|
||||
if role in MAIN_ROLES:
|
||||
if role in MAIN_ROLES or role in unknown_as_main:
|
||||
return inherited_side
|
||||
if role not in SIDE_ROLES:
|
||||
warn(f"unknown role {role!r} on {what}; treated as side")
|
||||
return True
|
||||
|
||||
def walk(rel, is_side, stack):
|
||||
|
||||
@@ -64,7 +64,7 @@ def response():
|
||||
{"type": "way", "ref": 13, "role": "backward"},
|
||||
{"type": "relation", "ref": 2, "role": ""},
|
||||
{"type": "relation", "ref": 3, "role": "connection"},
|
||||
{"type": "way", "ref": 11, "role": "oddity"}, # already seen
|
||||
{"type": "way", "ref": 11, "role": "route"}, # already seen
|
||||
]})
|
||||
return {"elements": els}
|
||||
|
||||
@@ -97,7 +97,7 @@ class ParseTests(unittest.TestCase):
|
||||
self.assertEqual(r["way:(empty)"], 3)
|
||||
self.assertEqual(r["way:alternative"], 1)
|
||||
self.assertEqual(r["relation:connection"], 1)
|
||||
self.assertEqual(r["way:oddity"], 1)
|
||||
self.assertEqual(r["way:route"], 1)
|
||||
|
||||
def test_version_and_properties(self):
|
||||
self.assertEqual((self.track["version"], self.track["timestamp"]),
|
||||
@@ -106,20 +106,72 @@ class ParseTests(unittest.TestCase):
|
||||
self.assertEqual((w["name"], w["highway"]), ("Main", "path"))
|
||||
self.assertNotIn("name", self.track["main"][1])
|
||||
|
||||
def test_unknown_role_is_side_with_warning(self):
|
||||
def test_wiki_main_roles_are_main(self):
|
||||
doc = response()
|
||||
for i, role in enumerate(["route", "north", "south", "east", "west"]):
|
||||
wid = 100 + i
|
||||
doc["elements"].append({"type": "way", "id": wid, "nodes": [1, 2]})
|
||||
for el in doc["elements"]:
|
||||
if el["type"] == "relation" and el["id"] == 1:
|
||||
el["members"].append({"type": "way", "ref": wid, "role": role})
|
||||
track = T.parse(doc, 1, warn=lambda m: None)
|
||||
ids = [w["osm_id"] for w in track["main"]]
|
||||
for i in range(5):
|
||||
self.assertIn(str(100 + i), ids)
|
||||
|
||||
def test_wiki_side_roles_are_side(self):
|
||||
doc = response()
|
||||
for i, role in enumerate(["alternative", "excursion", "approach", "connection"]):
|
||||
wid = 200 + i
|
||||
doc["elements"].append({"type": "way", "id": wid, "nodes": [1, 2]})
|
||||
for el in doc["elements"]:
|
||||
if el["type"] == "relation" and el["id"] == 1:
|
||||
el["members"].append({"type": "way", "ref": wid, "role": role})
|
||||
track = T.parse(doc, 1, warn=lambda m: None)
|
||||
for i in range(4):
|
||||
self.assertIn(str(200 + i), [w["osm_id"] for w in track["side"]])
|
||||
self.assertNotIn(str(200 + i), [w["osm_id"] for w in track["main"]])
|
||||
|
||||
def test_unknown_role_among_other_roles_stops_the_tool(self):
|
||||
doc = response()
|
||||
for el in doc["elements"]:
|
||||
if el["id"] == 1 and el["type"] == "relation":
|
||||
el["members"].append({"type": "way", "ref": 40, "role": "odd"})
|
||||
# way 40 was already seen through the sub-relation, so use a new way
|
||||
doc["elements"].append({"type": "way", "id": 50, "nodes": [1, 2]})
|
||||
for el in doc["elements"]:
|
||||
if el["id"] == 1 and el["type"] == "relation":
|
||||
el["members"].append({"type": "way", "ref": 50, "role": "odd"})
|
||||
with self.assertRaisesRegex(T.TrackError, "odd.*do not define|do(es)? not define.*odd"):
|
||||
T.parse(doc, 1, warn=lambda m: None)
|
||||
|
||||
def test_unknown_role_on_every_member_is_main_with_loud_warning(self):
|
||||
doc = {"elements": [
|
||||
{"type": "node", "id": 1, "lon": 0.0, "lat": 0.0},
|
||||
{"type": "node", "id": 2, "lon": 0.0, "lat": 0.01},
|
||||
{"type": "way", "id": 5, "nodes": [1, 2]},
|
||||
{"type": "relation", "id": 1, "tags": {"type": "route"},
|
||||
"members": [{"type": "way", "ref": 5, "role": "odd"}]}]}
|
||||
msgs, warn = quiet()
|
||||
track = T.parse(doc, 1, warn=warn)
|
||||
self.assertIn("50", [w["osm_id"] for w in track["side"]])
|
||||
self.assertTrue(any("odd" in m for m in msgs))
|
||||
self.assertEqual([w["osm_id"] for w in track["main"]], ["5"])
|
||||
self.assertTrue(any("UNKNOWN ROLE" in m and "odd" in m for m in msgs))
|
||||
|
||||
def test_teton_shaped_relation_keeps_route_role_ways(self):
|
||||
# Relation 3895101 (Teton Crest Trail): 36 of 49 ways carry the role
|
||||
# "route", the other 13 the empty role; all are the main line, 57.5 km.
|
||||
step = 0.0146 # degrees of longitude, about 1.17 km at 43.7 N
|
||||
els, members = [], []
|
||||
for i in range(50):
|
||||
els.append({"type": "node", "id": 1000 + i, "lon": -110.9 + step * i, "lat": 43.7})
|
||||
for i in range(49):
|
||||
els.append({"type": "way", "id": 2000 + i, "nodes": [1000 + i, 1001 + i]})
|
||||
members.append({"type": "way", "ref": 2000 + i,
|
||||
"role": "" if i % 4 == 0 and i < 49 and sum(
|
||||
1 for j in range(i) if j % 4 == 0) < 13 else "route"})
|
||||
roles = [m["role"] for m in members]
|
||||
self.assertEqual((roles.count("route"), roles.count("")), (36, 13))
|
||||
els.append({"type": "relation", "id": 3895101, "version": 1, "tags": {"type": "route"},
|
||||
"members": members})
|
||||
track = T.parse({"elements": els}, 3895101, warn=lambda m: None)
|
||||
self.assertEqual((len(track["main"]), len(track["side"])), (49, 0))
|
||||
self.assertAlmostEqual(T.figures(track)["measured_km"], 57.5, delta=0.3)
|
||||
|
||||
def test_way_main_in_one_place_and_side_in_another_is_main(self):
|
||||
doc = response()
|
||||
|
||||
Reference in new issue
Block a user