Schema and rule for person-defined routes

Person-defined routes are now listed at the same 50-mile floor as
designated trails, as records that say where the route is got from its
creator. A line is included only where the creator's terms allow it or
where OpenStreetMap carries the route; nothing is included for a sold,
withheld or unsettled route.

Adds kind, [route] and [[guide]] to the schema, the checks in
tools/validate.py, kind and line_policy in trails.csv and
index.geojson, and a second generated README table for routes.
This commit is contained in:
swaits committed 2026-11-10 14:23:21 -07:00
1 parent aacb43d38e
commit 4ac5815af9
10 files changed
+340 -128

No files matched your search

+89 -13
View File
@@ -19,19 +19,29 @@ except ModuleNotFoundError: # Python < 3.11: minimal fallback for this repo's T
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BEGIN = "<!-- BEGIN TRAIL TABLE -->"
END = "<!-- END TRAIL TABLE -->"
COUNT_BEGIN = "<!-- BEGIN COUNTS -->"
COUNT_END = "<!-- END COUNTS -->"
ROUTE_BEGIN = "<!-- BEGIN ROUTE TABLE -->"
ROUTE_END = "<!-- END ROUTE TABLE -->"
DESIGNATED = "designated"
def load_toml(path):
if tomllib is not None:
with open(path, "rb") as f:
return tomllib.load(f)
rec, section = {}, None
rec, table = {}, None
for raw in open(path, encoding="utf-8"):
line = raw.strip()
if not line or line.startswith("#"):
continue
if line.startswith("[[") and line.endswith("]]"):
# array of tables, for example [[guide]]
table = {}
rec.setdefault(line[2:-2].strip(), []).append(table)
continue
if line.startswith("[") and line.endswith("]"):
section = rec.setdefault(line[1:-1], {})
table = rec.setdefault(line[1:-1].strip(), {})
continue
key, _, val = line.partition("=")
val = val.strip()
@@ -46,7 +56,7 @@ def load_toml(path):
out = int(val)
except ValueError:
out = float(val)
(section if section is not None else rec)[key.strip()] = out
(table if table is not None else rec)[key.strip()] = out
return rec
@@ -64,17 +74,25 @@ def load_trails(root=ROOT):
return trails
def trail_kind(t):
return t.get("kind") or DESIGNATED
def line_policy(t):
return t.get("route", {}).get("line_policy", "") if trail_kind(t) == "route" else ""
def csv_text(trails):
buf = io.StringIO()
w = csv.writer(buf, lineterminator="\n")
w.writerow(["id", "name", "country", "regions", "length_km", "status",
"line_source", "licence"])
"line_source", "licence", "kind", "line_policy"])
for t in trails:
src = t.get("source", {})
status = "included" if src.get("line_file") else "record-only"
w.writerow([t["id"], t["name"], t["country"], ";".join(t.get("regions", [])),
t["length_km"], status, src.get("line_source", ""),
src.get("licence", "")])
src.get("licence", ""), trail_kind(t), line_policy(t)])
return buf.getvalue()
@@ -146,39 +164,97 @@ def index_geojson_text(trails, root=ROOT):
new_geom = {"type": "MultiLineString", "coordinates": parts}
else:
continue
props = {"id": t["id"], "name": t["name"], "kind": trail_kind(t)}
if trail_kind(t) == "route":
props["line_policy"] = line_policy(t)
features.append({
"type": "Feature",
"properties": {"id": t["id"], "name": t["name"]},
"properties": props,
"geometry": new_geom,
})
fc = {"type": "FeatureCollection", "features": features}
return json.dumps(fc, ensure_ascii=False, separators=(",", ":")) + "\n"
def _cell(value):
return str(value).replace("|", "\\|")
def readme_table(trails):
lines = [
"| ID | Trail | Country | States / provinces | Length (km) | Line | Licence |",
"| --- | --- | --- | --- | ---: | --- | --- |",
]
for t in trails:
if trail_kind(t) != DESIGNATED:
continue
src = t.get("source", {})
line = "yes" if src.get("line_file") else "no (record only)"
regions = ", ".join(t.get("regions", []))
name = str(t["name"]).replace("|", "\\|")
lines.append(
f"| {t['id']} | {name} | {t['country']} | {regions} | {t['length_km']} | {line} | {src.get('licence','')} |"
f"| {t['id']} | {_cell(t['name'])} | {t['country']} | {regions} | "
f"{t['length_km']} | {line} | {src.get('licence','')} |"
)
return "\n".join(lines)
def first_guide(t):
guides = t.get("guide") or []
if not guides:
return ""
g = guides[0]
title = _cell(g.get("title", ""))
url = g.get("url", "")
access = g.get("access", "")
return f"[{title}]({url}) ({access})" if url else f"{title} ({access})"
def readme_route_table(trails):
lines = [
"| ID | Route | Country | States / provinces | Length (km) | Creator | Line policy | Line | First guide |",
"| --- | --- | --- | --- | ---: | --- | --- | --- | --- |",
]
for t in trails:
if trail_kind(t) != "route":
continue
src = t.get("source", {})
line = "yes" if src.get("line_file") else "no"
regions = ", ".join(t.get("regions", []))
lines.append(
f"| {t['id']} | {_cell(t['name'])} | {t['country']} | {regions} | "
f"{t['length_km']} | {_cell(t.get('route', {}).get('creator', ''))} | "
f"{line_policy(t)} | {line} | {first_guide(t)} |"
)
return "\n".join(lines)
def counts_line(trails):
total = len(trails)
with_line = sum(1 for t in trails if t.get("source", {}).get("line_file"))
routes = [t for t in trails if trail_kind(t) == "route"]
routes_with_line = sum(1 for t in routes if t.get("source", {}).get("line_file"))
return (
f"{total} records, {with_line} with a line, {total - with_line} records only. "
f"Of these, {len(routes)} are person-defined routes: {routes_with_line} with a line "
f"and {len(routes) - routes_with_line} records only."
)
def replace_block(text, begin, end, body, path):
if begin not in text or end not in text:
raise SystemExit(f"README markers {begin!r} and {end!r} not found in {path}")
before, rest = text.split(begin, 1)
_, after = rest.split(end, 1)
return before + begin + "\n\n" + body + "\n\n" + end + after
def render_readme(readme_path, trails):
with open(readme_path, encoding="utf-8") as f:
text = f.read()
if BEGIN not in text or END not in text:
raise SystemExit(f"README trail-table markers not found in {readme_path}")
before, rest = text.split(BEGIN, 1)
_, after = rest.split(END, 1)
return before + BEGIN + "\n\n" + readme_table(trails) + "\n\n" + END + after
text = replace_block(text, COUNT_BEGIN, COUNT_END, counts_line(trails), readme_path)
text = replace_block(text, BEGIN, END, readme_table(trails), readme_path)
text = replace_block(text, ROUTE_BEGIN, ROUTE_END, readme_route_table(trails), readme_path)
return text
def main():
+62
View File
@@ -22,6 +22,8 @@ ALLOWED_LICENCES = {
"CC-BY-NC-ND-4.0", "none-stated", "restricted", "unknown",
}
DATA_LICENCES = {"us-gov-public-domain", "CC-BY-4.0", "CC-BY", "ODbL-1.0"}
LINE_POLICIES = {"open", "sold", "withheld", "unclear"}
NO_LINE_POLICIES = {"sold", "withheld", "unclear"}
DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
spec = importlib.util.spec_from_file_location(
@@ -196,6 +198,66 @@ def main():
if not rec.get("verification", {}).get("verified") in (True, False):
errors.append(f"{tid}: missing required field verification.verified")
kind = rec.get("kind")
route = rec.get("route")
guides = rec.get("guide")
if kind is not None and kind != "route":
errors.append(f"{tid}: kind must be \"route\" when present, not {kind!r}")
if kind == "route":
if not isinstance(route, dict):
errors.append(f"{tid}: kind is \"route\" but there is no [route] table")
route = {}
if not str(route.get("creator", "")).strip():
errors.append(f"{tid}: route.creator is missing")
policy = route.get("line_policy")
if policy not in LINE_POLICIES:
errors.append(
f"{tid}: route.line_policy must be one of "
f"{', '.join(sorted(LINE_POLICIES))}, not {policy!r}")
if (str(route.get("creator_words", "")).strip()
and not str(route.get("creator_words_url", "")).strip()):
errors.append(
f"{tid}: route.creator_words is present but "
"route.creator_words_url is missing")
if not isinstance(guides, list) or not guides:
errors.append(f"{tid}: kind is \"route\" but there is no [[guide]] table")
else:
for i, g in enumerate(guides, 1):
for field in ("title", "author", "access", "url"):
if not str(g.get(field, "")).strip():
errors.append(f"{tid}: guide {i} is missing {field}")
if g.get("access") not in ("free", "purchase"):
errors.append(
f"{tid}: guide {i} access must be \"free\" or \"purchase\", "
f"not {g.get('access')!r}")
if not str(g.get("url", "")).startswith(("https://", "http://")):
errors.append(f"{tid}: guide {i} url must be an http(s) URL")
if (str(g.get("price", "")).strip()
and not DATE_RE.match(str(g.get("price_checked", "")))):
errors.append(
f"{tid}: guide {i} has a price but no price_checked date "
"(YYYY-MM-DD)")
if policy in NO_LINE_POLICIES:
if src.get("line_file"):
errors.append(
f"{tid}: line_policy is {policy}, so source.line_file must be empty")
for name in ("line.geojson", "alternates.geojson"):
if os.path.isfile(os.path.join(tdir, name)):
errors.append(
f"{tid}: line_policy is {policy}, so {name} must not be present")
if osm.get("relation"):
errors.append(
f"{tid}: line_policy is {policy}, so osm.relation must be 0")
if not str(src.get("line_absent_reason", "")).strip():
errors.append(
f"{tid}: line_policy is {policy}, so source.line_absent_reason "
"must say why no line is included")
else:
if route is not None:
errors.append(f"{tid}: [route] table present but kind is not \"route\"")
if guides:
errors.append(f"{tid}: [[guide]] present but kind is not \"route\"")
for dirpath, dirnames, filenames in os.walk(ROOT):
if ".git" in dirpath:
dirnames[:] = []