feat(tours): show the known surfaces and way types along a hike (MAP-04)

The hike's "SAC grades" card becomes "Trail details": one switch, then a
segmented choice between SAC, Surface and Way type, three things kept
apart and drawn along the route on the planner map one at a time. Every
facet reads from the same answer: the ways under the line are matched on
foot once, their OpenStreetMap values fetched once through the
trail-grades read MAP-03 already uses, and each stretch is placed against
the tour's own line once. Switching facets asks nothing new.

The server now hands on a way's surface and highway tags as OpenStreetMap
wrote them, only trimmed and bounded. The planner groups them (asphalt or
concrete, paving stones, compacted, gravel, natural ground, grass, rock,
wood or metal, unpaved without a kind, other; path, footway, steps, farm
or forest track, cycle path, minor road, road, via ferrata, other), and
each row's tooltip names the values recorded, such as surface=gravel,
surface=pebblestone. The router's own surface reading is never used: it
is normalised and a default where nothing is recorded.

What is not known stays unknown and says why, in every facet: no value
recorded, a value no group takes or two at once (unclear, never the
nearest group), no way clearly under the tour's line, or a way that could
not be asked about. Imported tracks and hand-drawn stretches keep their
geometry; a matched way only counts where it keeps within 25 m of it.
A summary badge gives the known share and the most common class; the SAC
facet keeps its highest step and routing limit badges. Nothing about the
tour, its places or its days changes, and no routing preference is set.
This commit is contained in:
Maurice
2026-10-20 12:49:53 +02:00
parent ea9e997338
commit 3572f4b8a3
57 changed files with 2749 additions and 1048 deletions
@@ -10,9 +10,11 @@ import { Body, Controller, HttpCode, Post, UseGuards } from '@nestjs/common';
import { tourTrailGradeResponseSchema } from '@trek/shared';
/**
* BIK-05 and MAP-03 POST /api/trips/:tripId/tours/trail-grades: the trail
* grades of the ways a tour runs on, mountain bike and SAC alike. A read, so anyone who sees the trip may
* ask; the ways come in the body because the draft may not be saved yet.
* BIK-05, MAP-03 and MAP-04 POST /api/trips/:tripId/tours/trail-grades: what
* OpenStreetMap records for the ways a tour runs on, the mountain bike and SAC
* grades, the surface and the kind of way alike. A read, so anyone who sees
* the trip may ask; the ways come in the body because the draft may not be
* saved yet.
*/
@Controller('api/trips/:tripId/tours')
@UseGuards(AddonGuard, JwtAuthGuard, TripAccessGuard)
@@ -1,7 +1,7 @@
import { OverpassElementsClient } from '../overpass/overpass-elements.service';
import { Injectable } from '@nestjs/common';
import {
SAC_SCALE_UNCLEAR_MAX,
OSM_TAG_VALUE_MAX,
sacScaleSchema,
type TourTrailGrade,
type TourTrailGradeRequest,
@@ -30,6 +30,15 @@ function step(value: string | undefined, top: number, signed: boolean): string |
return signed ? `${match[1]}${match[2] ?? ''}` : match[1];
}
/**
* MAP-04: a tag's value as OpenStreetMap wrote it, only trimmed and bounded,
* never normalised: the planner groups it and says what was written.
*/
function written(value: string | undefined): string | null {
const trimmed = value?.trim() ?? '';
return trimmed ? trimmed.slice(0, OSM_TAG_VALUE_MAX) : null;
}
/**
* MAP-03: a way's `sac_scale` as one of the scale's six values, read as
* OpenStreetMap wrote it. A value that names no single step (`T3`,
@@ -37,31 +46,33 @@ function step(value: string | undefined, top: number, signed: boolean): string |
* a step, so the planner says unclear rather than picking one.
*/
function sacOf(value: string | undefined): Pick<TourTrailGrade, 'sac_scale' | 'sac_scale_unclear'> {
const written = value?.trim() ?? '';
if (!written) return { sac_scale: null, sac_scale_unclear: null };
const known = sacScaleSchema.safeParse(written.toLowerCase());
const text = written(value);
if (!text) return { sac_scale: null, sac_scale_unclear: null };
const known = sacScaleSchema.safeParse(text.toLowerCase());
if (known.success) return { sac_scale: known.data, sac_scale_unclear: null };
return { sac_scale: null, sac_scale_unclear: written.slice(0, SAC_SCALE_UNCLEAR_MAX) };
return { sac_scale: null, sac_scale_unclear: text };
}
function gradeOf(id: number, tags: Record<string, string> = {}): TourTrailGrade | null {
const grade = {
const grade: TourTrailGrade = {
id,
mtb_scale: step(tags['mtb:scale'], 6, true),
mtb_scale_uphill: step(tags['mtb:scale:uphill'], 5, true),
mtb_scale_imba: step(tags['mtb:scale:imba'], 4, false),
...sacOf(tags['sac_scale']),
surface: written(tags['surface']),
highway: written(tags['highway']),
};
const graded = grade.mtb_scale || grade.mtb_scale_uphill || grade.mtb_scale_imba;
return graded || grade.sac_scale || grade.sac_scale_unclear ? grade : null;
// A way none of whose values is recorded is left out: the planner reads its absence as unknown.
return Object.entries(grade).some(([key, value]) => key !== 'id' && value !== null) ? grade : null;
}
/**
* BIK-05 and MAP-03: the trail grades of the ways a tour runs on (the mountain
* bike scales, and the SAC hiking scale of a hike), read from OpenStreetMap
* through the operator's Overpass mirrors. What was read is kept
* a day, so the same tour asked about again costs the mirrors nothing; ways a
* mirror could not answer are said, never guessed.
* BIK-05, MAP-03 and MAP-04: what OpenStreetMap records for the ways a tour
* runs on (the mountain bike scales; the SAC hiking scale, the surface and the
* kind of way of a hike), read through the operator's Overpass mirrors. What
* was read is kept a day, so the same tour asked about again costs the mirrors
* nothing; ways a mirror could not answer are said, never guessed.
*/
@Injectable()
export class TourTrailGradeService {