Repository navigation
Expand file tree
/
Copy pathplanner_runtime.py
More file actions
88 lines (72 loc) · 4.29 KB
/
Copy pathplanner_runtime.py
File metadata and controls
88 lines (72 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Planner release identity and file verification."""
import hashlib
import json
from pathlib import Path, PurePosixPath
import re
from urllib.request import Request, urlopen
# Optional data layers. Each is one archive `maps/NAME.pmtiles`, baked when the region recipe has the
# field NAME. The value is a metadata key that every complete archive has. The catalogue entry lists
# each layer in `layers`; the web planner shows snow and climate through its module in
# builder/web/src/lib/planner/layers/registry.ts, and TrailView reads `snow` and the `trailview-*` archives of
# specs/trailview-features.md and specs/trailview-tiles.md.
TRAILVIEW = ("ground", "colour-q1", "colour-q2", "colour-q3", "colour-q4")
DATA_LAYERS = {
"snow": "snow_format", "snow-levels": "snow_format", "snow-seasons": "snow_format", "climate": "years",
"trailview-features": "features_format", "trailview-stamps": "stamp_version", "trailview-canopy": "canopy_format",
**{f"trailview-{name}": "trailview_format" for name in TRAILVIEW},
}
# The format of the offline catalog, of its cell files and of `search/<region>.grid.json`: the format of
# `planner/index`, the layer that writes them.
INDEX_FORMAT = 4
# The format of `public/grid.json`, `GRID` of host/obc-data/src/cli/deploy_cli.rs: format 4 keeps the
# pointers of all packs of a base square of a map layer in the block index of the square two zooms
# above it, which holds 4 × 4 base squares (specs/planner-release.md).
GRID_FORMAT = 4
# The base levels of the map packs: a tile zoom belongs to the deepest level not deeper than it.
LEVELS = (0, 7, 9)
# A route catalog file has its pointer in the block index 2^BLOCK_ZOOMS × 2^BLOCK_ZOOMS cells wide.
BLOCK_ZOOMS = 4
# The most bytes of a block index: the limit of `readJSON` of planner/tiles/src/grid.mjs.
INDEX_BYTES = 64 * 1024
CELL = re.compile(r"((?:maps|routes)/tiles(?:/.+)?)/([0-9]+)-([0-9]+)-([0-9]+)\.[a-z]+")
def encoded(value):
return (json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False) + "\n").encode()
def digest(path):
with Path(path).open("rb") as stream:
return hashlib.file_digest(stream, "sha256").hexdigest()
def open_url(url, timeout=60):
request = Request(url) if isinstance(url, str) else url
request.add_header("User-Agent", "OpenBikeComputer/1.0")
return urlopen(request, timeout=timeout)
def read_url(url):
with open_url(url) as response:
return json.load(response)
def relative_path(name):
"""A release file name or bucket key: a normalized relative POSIX path on one line."""
path = PurePosixPath(name)
if not path.parts or path.is_absolute() or ".." in path.parts or path.as_posix() != name or any(c in name for c in "\\\r\n"):
raise ValueError(f"Invalid path {name!r}")
return path
def base_level(zoom):
return max(level for level in LEVELS if level <= zoom)
def public_metadata(files):
"""Small immutable pointers let tile requests avoid a world-sized catalogue. The packs of a grid cell
share a block index, so a release does not hold a key for each cell of each layer."""
result = {"public/grid.json": encoded({"format": GRID_FORMAT})}
blocks = {}
for name, item in files.items():
pointer = {**item["transport"], "decoded_bytes": item["bytes"]}
cell = CELL.fullmatch(name)
if cell:
folder, z, x, y = cell[1], *map(int, cell.group(2, 3, 4))
zoom = max(base_level(z) - 2, 0) if folder.startswith("maps/") else max(z - BLOCK_ZOOMS, 0)
block = f"public/{folder}/{zoom}-{x >> (z - zoom)}-{y >> (z - zoom)}.json"
blocks.setdefault(block, {})[name[len(folder) + 1:]] = pointer
elif name.startswith(("maps/tiles/", "maps/assets/", "routes/tiles/")) or name in {"maps/basemap.json", "maps/places.json", "maps/overlays.json", "maps/terrain.json", "device/catalog.json", "trailview/rules.json",
*(f"maps/{layer}.json" for layer in DATA_LAYERS)}:
result["public/" + name + ".json"] = encoded(pointer)
for block, pointers in blocks.items():
result[block] = encoded(pointers)
if len(result[block]) > INDEX_BYTES:
raise ValueError(f"{block} exceeds the {INDEX_BYTES} bytes that the tiles Worker reads")
return result