Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<id>.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

Expand Down
78 changes: 78 additions & 0 deletions routes/velodrome-250.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="OpenBikeNet tools/velodrome_gpx.py" xmlns="http://www.topografix.com/GPX/1/1">
<metadata>
<name>Velodrome 250 m</name>
<desc>OpenBikeNet shared route velodrome-250: a flat 250 m loop.</desc>
<link href="https://github.com/OpenBikeNet/protocol/blob/main/routes/velodrome-250.json">
<text>velodrome-250</text>
</link>
</metadata>
<trk>
<name>Velodrome 250 m</name>
<trkseg>
<trkpt lat="47.00000000" lon="8.00000000"><ele>0</ele></trkpt>
<trkpt lat="47.00000000" lon="8.00065878"><ele>0</ele></trkpt>
<trkpt lat="47.00000118" lon="8.00069166"><ele>0</ele></trkpt>
<trkpt lat="47.00000469" lon="8.00072417"><ele>0</ele></trkpt>
<trkpt lat="47.00001050" lon="8.00075598"><ele>0</ele></trkpt>
<trkpt lat="47.00001855" lon="8.00078671"><ele>0</ele></trkpt>
<trkpt lat="47.00002874" lon="8.00081605"><ele>0</ele></trkpt>
<trkpt lat="47.00004097" lon="8.00084366"><ele>0</ele></trkpt>
<trkpt lat="47.00005510" lon="8.00086925"><ele>0</ele></trkpt>
<trkpt lat="47.00007098" lon="8.00089253"><ele>0</ele></trkpt>
<trkpt lat="47.00008843" lon="8.00091325"><ele>0</ele></trkpt>
<trkpt lat="47.00010726" lon="8.00093118"><ele>0</ele></trkpt>
<trkpt lat="47.00012727" lon="8.00094613"><ele>0</ele></trkpt>
<trkpt lat="47.00014823" lon="8.00095793"><ele>0</ele></trkpt>
<trkpt lat="47.00016992" lon="8.00096645"><ele>0</ele></trkpt>
<trkpt lat="47.00019209" lon="8.00097160"><ele>0</ele></trkpt>
<trkpt lat="47.00021452" lon="8.00097332"><ele>0</ele></trkpt>
<trkpt lat="47.00023694" lon="8.00097160"><ele>0</ele></trkpt>
<trkpt lat="47.00025912" lon="8.00096645"><ele>0</ele></trkpt>
<trkpt lat="47.00028081" lon="8.00095793"><ele>0</ele></trkpt>
<trkpt lat="47.00030177" lon="8.00094613"><ele>0</ele></trkpt>
<trkpt lat="47.00032178" lon="8.00093118"><ele>0</ele></trkpt>
<trkpt lat="47.00034061" lon="8.00091325"><ele>0</ele></trkpt>
<trkpt lat="47.00035806" lon="8.00089253"><ele>0</ele></trkpt>
<trkpt lat="47.00037394" lon="8.00086925"><ele>0</ele></trkpt>
<trkpt lat="47.00038807" lon="8.00084366"><ele>0</ele></trkpt>
<trkpt lat="47.00040030" lon="8.00081605"><ele>0</ele></trkpt>
<trkpt lat="47.00041049" lon="8.00078671"><ele>0</ele></trkpt>
<trkpt lat="47.00041854" lon="8.00075598"><ele>0</ele></trkpt>
<trkpt lat="47.00042435" lon="8.00072417"><ele>0</ele></trkpt>
<trkpt lat="47.00042786" lon="8.00069166"><ele>0</ele></trkpt>
<trkpt lat="47.00042904" lon="8.00065878"><ele>0</ele></trkpt>
<trkpt lat="47.00042904" lon="8.00000000"><ele>0</ele></trkpt>
<trkpt lat="47.00042786" lon="7.99996712"><ele>0</ele></trkpt>
<trkpt lat="47.00042435" lon="7.99993460"><ele>0</ele></trkpt>
<trkpt lat="47.00041854" lon="7.99990280"><ele>0</ele></trkpt>
<trkpt lat="47.00041049" lon="7.99987206"><ele>0</ele></trkpt>
<trkpt lat="47.00040030" lon="7.99984273"><ele>0</ele></trkpt>
<trkpt lat="47.00038807" lon="7.99981512"><ele>0</ele></trkpt>
<trkpt lat="47.00037394" lon="7.99978953"><ele>0</ele></trkpt>
<trkpt lat="47.00035806" lon="7.99976625"><ele>0</ele></trkpt>
<trkpt lat="47.00034061" lon="7.99974553"><ele>0</ele></trkpt>
<trkpt lat="47.00032178" lon="7.99972760"><ele>0</ele></trkpt>
<trkpt lat="47.00030177" lon="7.99971265"><ele>0</ele></trkpt>
<trkpt lat="47.00028081" lon="7.99970085"><ele>0</ele></trkpt>
<trkpt lat="47.00025912" lon="7.99969233"><ele>0</ele></trkpt>
<trkpt lat="47.00023694" lon="7.99968718"><ele>0</ele></trkpt>
<trkpt lat="47.00021452" lon="7.99968546"><ele>0</ele></trkpt>
<trkpt lat="47.00019209" lon="7.99968718"><ele>0</ele></trkpt>
<trkpt lat="47.00016992" lon="7.99969233"><ele>0</ele></trkpt>
<trkpt lat="47.00014823" lon="7.99970085"><ele>0</ele></trkpt>
<trkpt lat="47.00012727" lon="7.99971265"><ele>0</ele></trkpt>
<trkpt lat="47.00010726" lon="7.99972760"><ele>0</ele></trkpt>
<trkpt lat="47.00008843" lon="7.99974553"><ele>0</ele></trkpt>
<trkpt lat="47.00007098" lon="7.99976625"><ele>0</ele></trkpt>
<trkpt lat="47.00005510" lon="7.99978953"><ele>0</ele></trkpt>
<trkpt lat="47.00004097" lon="7.99981512"><ele>0</ele></trkpt>
<trkpt lat="47.00002874" lon="7.99984273"><ele>0</ele></trkpt>
<trkpt lat="47.00001855" lon="7.99987206"><ele>0</ele></trkpt>
<trkpt lat="47.00001050" lon="7.99990280"><ele>0</ele></trkpt>
<trkpt lat="47.00000469" lon="7.99993460"><ele>0</ele></trkpt>
<trkpt lat="47.00000118" lon="7.99996712"><ele>0</ele></trkpt>
<trkpt lat="47.00000000" lon="8.00000000"><ele>0</ele></trkpt>
</trkseg>
</trk>
</gpx>
43 changes: 43 additions & 0 deletions tools/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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")):
Expand Down
97 changes: 97 additions & 0 deletions tools/velodrome_gpx.py
Original file line number Diff line number Diff line change
@@ -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 = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<gpx version="1.1" creator="OpenBikeNet tools/velodrome_gpx.py"'
' xmlns="http://www.topografix.com/GPX/1/1">',
' <metadata>',
' <name>Velodrome 250 m</name>',
' <desc>OpenBikeNet shared route velodrome-250: a flat 250 m loop.</desc>',
' <link href="https://github.com/OpenBikeNet/protocol/blob/main/routes/velodrome-250.json">',
' <text>velodrome-250</text>',
' </link>',
' </metadata>',
' <trk>',
' <name>Velodrome 250 m</name>',
' <trkseg>',
]
lines += [f' <trkpt lat="{lat:.{DIGITS}f}" lon="{lng:.{DIGITS}f}"><ele>0</ele></trkpt>'
for lat, lng in track]
lines += [' </trkseg>', ' </trk>', '</gpx>']
return '\n'.join(lines) + '\n'


if __name__ == '__main__':
sys.stdout.write(gpx(velodrome_track()))
Loading