Files
TREK/client/src/components/Tours/planner/legVariantRouting.ts
T
Maurice 64445a5204 feat(tours): compare other ways for one section of a hike (RTE-02)
A routed section of a hike between two neighbouring points can now be
compared with the other ways the router knows between the same two points,
under the same limit and preferences. "Compare ways for the next leg" in a
waypoint's menu opens a card over the planner map: the way taken now and up
to three others, each drawn on the map in its own colour, with length,
climb, descent and walking time by the signposts' rule. Figures without
heights stay unknown, and the differences to the current way are shown
only where both are known. When the router knows no other way, or does not
answer, the card says so.

Showing, hovering and choosing change nothing. Only "Take this way" does:
the router is first asked whether a few shaping points hold the chosen way
(the check the road trip's picker already uses), and only then are they set
into that one leg as a single step of the history, with a "Way taken" note
and its undo at the foot of the map. Other legs, fixed lines, days and
points stay as they were, the totals follow from the replanned route, and
the shaping points can be dragged like any other. A section that changed
while the router was asked, or a way it does not keep to, is not written,
and the card says why. The ways are drawn in 3D too.
2026-10-20 15:18:38 +02:00

59 lines
2.5 KiB
TypeScript

import { valhallaAlternates, valhallaRun } from '../../Map/valhallaRoute';
import type { RailLegRoute } from '../../Roadtrip/useRoadtripRoutes';
import { OTHER_WAY_KEYS, offeredVariants, withVariantHeights, type ComparedLeg, type LegVariant } from './legVariants';
import { enrichTourElevations } from './tourRouting';
/** What a comparison has found so far: the router's answer and the ways it offered, their heights as they come in. */
export interface OtherWaysAnswer {
/** `failed`: the router did not answer; `none`: it knows no other way; `choose`: there are ways to choose from. */
phase: 'failed' | 'none' | 'choose';
variants: LegVariant[];
}
/**
* RTE-02: the other ways between the two ends of a hike's section, asked
* under the hike's own rules, then the heights of each, one after the other
* (the router allows a question a second). `report` hears every step, so the
* figures fill in as they arrive; nothing is reported once `signal` aborts.
*/
export async function askOtherWays(
leg: ComparedLeg,
current: LegVariant,
signal: AbortSignal,
report: (answer: OtherWaysAnswer) => void
): Promise<void> {
const offers = await valhallaAlternates(leg.from, leg.to, 'walking', [], OTHER_WAY_KEYS.length, signal, leg.costing);
if (signal.aborted) return;
if (!offers) {
report({ phase: 'failed', variants: [current] });
return;
}
let variants = offeredVariants(current, offers);
const phase = variants.length > 1 ? 'choose' : 'none';
report({ phase, variants });
for (const variant of variants) {
if (variant.heights !== 'pending') continue;
const heights = await enrichTourElevations(variant.coordinates, signal);
if (signal.aborted) return;
variants = variants.map((known) => (known.key === variant.key ? withVariantHeights(known, heights) : known));
report({ phase, variants });
}
}
/**
* The section routed again through `pins` under its own rules, the way the
* planner will route it once the pins are its shaping points. It is what a
* choice is checked against before anything is written: a way the router
* does not keep to with a few pins is no way the tour can hold.
*/
export async function routeSection(
leg: ComparedLeg,
pins: ReadonlyArray<{ lat: number; lng: number }>,
signal: AbortSignal
): Promise<RailLegRoute> {
const run = await valhallaRun([leg.from, ...pins, leg.to], 'walking', [], signal, leg.costing);
if (!run) throw new Error('No route found');
const { coordinates, distance, duration, hasFerry } = run.total;
return { coordinates, distance, duration, hasFerry };
}