Files
TREK/server/src/nest/ski-routing/ski-graph.contract.ts
T
Maurice c8eb510bd4 feat(ski-routing): plan ski tours on the piste graphs of TREK Places
A new Nest domain plans the legs of a ski and winter tour on the runs,
lifts, cross-country trails, ski touring and winter paths the Places host
builds from OpenStreetMap, one SGR1 graph per ski area. TREK fetches the
index once, downloads an area by its id when a planner first plans in it,
checks it against the index's SHA-256 and keeps it in data/ski; no
clicked point ever leaves the server. A leg whose area is slower than two
seconds answers loading, and an older copy stands in while the host is
down.

The reader decodes the writer's golden file byte for byte, including the
two edge flags it added (area_has_run, type_unknown). The search is A* in
a worker thread over a profile per discipline (alpine, snowboard,
cross-country by technique, touring, snowshoe, winter hike) with the
times and penalties of the cost model, up to five snap candidates per
point, lift stations as entry points, legs that go on from each other,
pieces of a split network stitched by their boundary nodes, other ways by
the penalty method, and a failed leg explained by relaxing one rule at a
time (difficulty, lift class, technique, riding down, uphill) without ever
handing out the relaxed line.

POST /api/trips/:tripId/ski/route, GET .../ski/area-near and
GET .../ski/network/:areaId sit behind Tours and the trip, with
plan_ski_route as their MCP twin. A daily job looks for a new build and
sweeps areas unused for 90 days. The admin switch ski_routing_enabled is
on wherever TREK Places is.
2026-10-20 21:13:48 +02:00

196 lines
4.4 KiB
TypeScript

/**
* SGR1, the graph of one ski area as the Places host writes it
* (trek-places-api `winter/sgr.py`, codes in `winter/model.py`). Every number
* here is part of that contract: a change to an existing value is a new
* format, never a fix. The golden file `tests/fixtures/ski/golden-v1.sgr` is
* the same bytes the writer's own test pins.
*/
export const SGR_MAGIC = 'SGR1';
export const SGR_FORMAT = 1;
/** What an edge may be used for, per direction (`edgeFwdUses` a to b, `edgeBackUses` b to a). */
export const Use = {
DOWNHILL: 1 << 0,
LIFT_UP: 1 << 1,
LIFT_DOWN: 1 << 2,
NORDIC_CLASSIC: 1 << 3,
NORDIC_SKATING: 1 << 4,
SKITOUR_UP: 1 << 5,
SKITOUR_DOWN: 1 << 6,
SNOWSHOE: 1 << 7,
WINTER_HIKE: 1 << 8,
WALK: 1 << 9,
} as const;
export const Kind = {
RUN: 0,
LIFT: 1,
RAIL: 2,
BOARD: 3,
ALIGHT: 4,
NORDIC: 5,
SKITOUR: 6,
HIKE: 7,
SLED_WALKUP: 8,
CONNECTION: 9,
AREA: 10,
LINK: 11,
CROSSING: 12,
WALK: 13,
} as const;
/** `edgeDiff`: novice to extreme, 255 where the data gives none. */
export const DIFFICULTY_NAMES = [
'novice',
'easy',
'intermediate',
'advanced',
'expert',
'freeride',
'extreme',
] as const;
export const DIFF_UNKNOWN = 255;
export const DIFF_EXPERT = 4;
/** `edgeGroom` bits. */
export const Groom = {
CLASSIC: 1 << 0,
SKATING: 1 << 1,
SCOOTER: 1 << 2,
BACKCOUNTRY: 1 << 3,
MOGUL: 1 << 4,
ASSUMED: 1 << 7,
} as const;
/** `edgeLift`, as OpenStreetMap names each type. */
export const LIFT_NAMES = [
'none',
'cable_car',
'gondola',
'mixed_lift',
'chair_detachable',
'chair_fixed',
'drag_lift',
't-bar',
'j-bar',
'platter',
'rope_tow',
'magic_carpet',
'funicular',
'rack_railway',
] as const;
export const Lift = {
NONE: 0,
CABLE_CAR: 1,
GONDOLA: 2,
MIXED: 3,
CHAIR_DETACHABLE: 4,
CHAIR_FIXED: 5,
DRAG: 6,
TBAR: 7,
JBAR: 8,
PLATTER: 9,
ROPE_TOW: 10,
MAGIC_CARPET: 11,
FUNICULAR: 12,
RACK: 13,
} as const;
/** `nodeFlags` bits. */
export const NodeFlag = {
STATION: 1 << 0,
PORTAL: 1 << 1,
DISMOUNT_YES: 1 << 2,
DISMOUNT_SOMETIMES: 1 << 3,
LIFT_INTERNAL: 1 << 4,
BOUNDARY: 1 << 5,
ELE_MISSING: 1 << 6,
AREA_INTERIOR: 1 << 7,
} as const;
/**
* `edgeFlags` bits. Bits 17 and 18 are the writer's beyond the first draft of
* the format: an area edge whose polygon holds a mapped run (cost x1.3), and a
* way tagged `piste:type=yes`, usable as a connection only (cost x1.5).
*/
export const EdgeFlag = {
DIR_FROM_DEM: 1 << 0,
DIR_CONFLICT: 1 << 1,
RELATION_INHERITED: 1 << 2,
TECHNIQUE_ASSUMED: 1 << 3,
INFERRED_LINK: 1 << 4,
INFERRED_CROSSING: 1 << 5,
AREA_DERIVED: 1 << 6,
TUNNEL: 1 << 7,
LIT: 1 << 8,
SKITOUR_UPHILL_OK: 1 << 9,
HIGHWAY_SHARED: 1 << 10,
FLAT_FOR_BOARD: 1 << 11,
RIDE_DOWN: 1 << 12,
HEATED: 1 << 13,
BUBBLE: 1 << 14,
NORDIC_SOFT_ONEWAY: 1 << 15,
HALO: 1 << 16,
AREA_HAS_RUN: 1 << 17,
TYPE_UNKNOWN: 1 << 18,
} as const;
/** A height the terrain data does not have. */
export const ELE_MISSING = -32768;
/** A string offset that names no string. */
export const NO_STRING = 0xffffffff;
/** The head of an SGR1 file, as JSON. */
export interface SkiGraphHeader {
format: number;
build: string | null;
areaId: string | null;
name: string | null;
kind: string | null;
country: string | null;
colourScheme: string | null;
/** [minLng, minLat, maxLng, maxLat] */
bbox: [number, number, number, number] | null;
counts: Record<string, number> | null;
nodeCount: number;
edgeCount: number;
geomCount: number;
stringBytes: number;
crc32: number;
}
/** One area's graph as decoded: the file's arrays, as typed arrays, and its string pool. */
export interface SkiGraphData {
header: SkiGraphHeader;
nodeLatE7: Int32Array;
nodeLngE7: Int32Array;
nodeEle: Int16Array;
nodeFlags: Uint16Array;
nodeOsm: Float64Array;
edgeA: Uint32Array;
edgeB: Uint32Array;
edgeLen: Float32Array;
edgeUp: Uint16Array;
edgeDown: Uint16Array;
edgeKind: Uint8Array;
edgeFwdUses: Uint16Array;
edgeBackUses: Uint16Array;
edgeDiff: Uint8Array;
edgeGroom: Uint8Array;
edgeLift: Uint8Array;
edgeLiftSec: Uint16Array;
edgeLiftCap: Uint16Array;
edgeFlags: Uint32Array;
edgeName: Uint32Array;
edgeRef: Uint32Array;
edgeWay: Float64Array;
edgeGeomOff: Uint32Array;
geomLatE7: Int32Array;
geomLngE7: Int32Array;
geomEle: Int16Array;
/** The string pool: UTF-8, each string ended by a NUL, addressed by its byte offset. */
strings: Uint8Array;
}