From 9b8d3624df96be6004fb89233fd5bd45ee0ca32d Mon Sep 17 00:00:00 2001 From: Lethonium Dev Server <38733902+RalfLeonFasler@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:33:31 +0000 Subject: [PATCH] Add a GPX track for the velodrome Apps that only ride GPS routes, such as Incyclist, can import routes/velodrome-250.gpx. tools/velodrome_gpx.py writes it: OpenRadlr's 250 m oval at a fixed anchor, scaled to 250 m by the haversine formula, closed by repeating the first point. The validator checks that each GPX track matches its route's length within 1 cm and closes on a loop. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + README.md | 6 ++- routes/velodrome-250.gpx | 78 ++++++++++++++++++++++++++++++++ tools/validate.py | 43 ++++++++++++++++++ tools/velodrome_gpx.py | 97 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 routes/velodrome-250.gpx create mode 100644 tools/velodrome_gpx.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/README.md b/README.md index 0af12e3..65d159b 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,14 @@ for the overall goal. - [`spec/v0.md`](spec/v0.md): the specification (messages, transport, versioning). - [`routes/`](routes/): shared route files with stable IDs, so that a position on a route means - the same spot in every app. + the same spot in every app. A route may also come as a GPX track (`routes/.gpx`) for apps + that only ride GPS routes; the JSON file stays the authority. `tools/velodrome_gpx.py` writes + the velodrome's track. - [`schemas/`](schemas/): JSON Schemas for routes and messages. - [`examples/`](examples/): example messages, used as shared test data. - [`apps.md`](apps.md): optional list of apps that speak the protocol. -Run `python3 tools/validate.py` to check routes, schemas and examples. +Run `python3 tools/validate.py` to check routes, GPX tracks, schemas and examples. ## Servers diff --git a/routes/velodrome-250.gpx b/routes/velodrome-250.gpx new file mode 100644 index 0000000..184c88a --- /dev/null +++ b/routes/velodrome-250.gpx @@ -0,0 +1,78 @@ + + + + Velodrome 250 m + OpenBikeNet shared route velodrome-250: a flat 250 m loop. + + velodrome-250 + + + + Velodrome 250 m + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + diff --git a/tools/validate.py b/tools/validate.py index c54b4c4..51dd4b8 100644 --- a/tools/validate.py +++ b/tools/validate.py @@ -5,8 +5,10 @@ Paths are relative to the repository root. """ import json +import math import re import sys +import xml.etree.ElementTree as ET from functools import lru_cache from pathlib import Path @@ -20,6 +22,9 @@ SCHEMAS = ROOT / "schemas" EXAMPLES = ROOT / "examples" SPEC = ROOT / "spec" / "v0.md" +# GPX tracks next to a route are measured on this sphere (metres), the radius Incyclist uses. +EARTH_RADIUS = 6378100.0 +GPX_TOLERANCE = 0.01 MESSAGE_SCHEMAS = ("app-message", "server-message", "stats") # The 14 error codes of spec 7.6. ERROR_CODES = { @@ -105,6 +110,39 @@ def check_route(path, route): return problems +def haversine(p, q): + lat1, lng1, lat2, lng2 = map(math.radians, (*p, *q)) + a = (math.sin((lat2 - lat1) / 2) ** 2 + + math.cos(lat1) * math.cos(lat2) * math.sin((lng2 - lng1) / 2) ** 2) + return 2 * EARTH_RADIUS * math.asin(math.sqrt(a)) + + +def check_gpx(path, routes): + """A GPX track is only a convenience copy of a route: it must match the route's length and loop.""" + name = rel(path) + route = routes.get(path.stem) + if route is None: + return [f"{name}: no route {path.stem!r} for this track"] + try: + root = ET.parse(path).getroot() + except ET.ParseError as error: + return [f"{name}: not valid XML: {error}"] + track = [ + (float(point.get("lat")), float(point.get("lon"))) + for point in root.iter("{http://www.topografix.com/GPX/1/1}trkpt") + ] + if len(track) < 2: + return [f"{name}: track has fewer than 2 points"] + + problems = [] + length = sum(haversine(p, q) for p, q in zip(track, track[1:])) + if is_number(route.get("length")) and abs(length - route["length"]) > GPX_TOLERANCE: + problems.append(f"{name}: track is {length:.3f} m long, expected {route['length']}") + if route.get("loop") is True and track[0] != track[-1]: + problems.append(f"{name}: loop route, but the track does not end at its first point") + return problems + + def spec_objects(): """Yield (block number, line number, object) for every JSON object in the spec's json blocks.""" text = SPEC.read_text(encoding="utf-8") @@ -179,6 +217,7 @@ def main(): return report(problems) ids = {} + routes = {} for path in sorted(ROUTES.glob("*.json")): route, error = load_json(path) if error: @@ -193,6 +232,10 @@ def main(): ) else: ids[route_id] = path + routes[route_id] = route + + for path in sorted(ROUTES.glob("*.gpx")): + problems.extend(check_gpx(path, routes)) for folder in (INVALID_ROUTES, MISNAMED_ROUTES): for path in sorted(folder.glob("*.json")): diff --git a/tools/velodrome_gpx.py b/tools/velodrome_gpx.py new file mode 100644 index 0000000..492dbf7 --- /dev/null +++ b/tools/velodrome_gpx.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +"""Write routes/velodrome-250.gpx, a GPS track for apps that only ride GPS routes: + + python3 tools/velodrome_gpx.py > routes/velodrome-250.gpx + +The oval is the one OpenRadlr draws: two 50 m straights and two semicircles, a point every +~2.5 m on the bends, starting at the start/finish line and riding anticlockwise. It is placed +at ANCHOR and scaled so the closed track, measured with the haversine formula on a sphere of +radius 6378.1 km (the radius Incyclist uses), is 250 m long to within a millimetre. The last +point is the first point again, so the track ends where it starts. + +routes/velodrome-250.json stays the authority: the GPX is only a convenience. The output is +deterministic, so running this again gives the same file byte for byte. +""" +import math +import sys + +LENGTH = 250.0 +STRAIGHT = 50.0 +BEND_STEP = 2.5 +EARTH_RADIUS = 6378100.0 +# The start/finish line. Round numbers on purpose: this is not a real velodrome. +ANCHOR = (47.0, 8.0) +DIGITS = 8 + + +def oval(): + """The unscaled oval as (x, y) metres, x to the east and y to the north.""" + radius = (LENGTH - 2 * STRAIGHT) / (2 * math.pi) + steps = round(math.pi * radius / BEND_STEP) + points = [(0.0, 0.0), (STRAIGHT, 0.0)] + for i in range(1, steps): + a = -math.pi / 2 + math.pi * i / steps + points.append((STRAIGHT + radius * math.cos(a), radius + radius * math.sin(a))) + points += [(STRAIGHT, 2 * radius), (0.0, 2 * radius)] + for i in range(1, steps): + a = math.pi / 2 + math.pi * i / steps + points.append((radius * math.cos(a), radius + radius * math.sin(a))) + return points + + +def to_lat_lng(points, scale): + lat0, lng0 = ANCHOR + metres_per_degree = EARTH_RADIUS * math.pi / 180 + lng_factor = metres_per_degree * math.cos(math.radians(lat0)) + return [ + (round(lat0 + y * scale / metres_per_degree, DIGITS), + round(lng0 + x * scale / lng_factor, DIGITS)) + for x, y in points + ] + + +def haversine(p, q): + lat1, lng1, lat2, lng2 = map(math.radians, (*p, *q)) + a = (math.sin((lat2 - lat1) / 2) ** 2 + + math.cos(lat1) * math.cos(lat2) * math.sin((lng2 - lng1) / 2) ** 2) + return 2 * EARTH_RADIUS * math.asin(math.sqrt(a)) + + +def track_length(track): + return sum(haversine(p, q) for p, q in zip(track, track[1:])) + + +def velodrome_track(): + """The closed track as (lat, lng) points, the first point repeated at the end.""" + points = oval() + points.append(points[0]) + scale = 1.0 + for _ in range(3): + scale *= LENGTH / track_length(to_lat_lng(points, scale)) + return to_lat_lng(points, scale) + + +def gpx(track): + lines = [ + '', + '', + ' ', + ' Velodrome 250 m', + ' OpenBikeNet shared route velodrome-250: a flat 250 m loop.', + ' ', + ' velodrome-250', + ' ', + ' ', + ' ', + ' Velodrome 250 m', + ' ', + ] + lines += [f' 0' + for lat, lng in track] + lines += [' ', ' ', ''] + return '\n'.join(lines) + '\n' + + +if __name__ == '__main__': + sys.stdout.write(gpx(velodrome_track()))