feat(tours): plan rides on horseback from signposted horse routes

A tour can now be a ride (HOR-01). The planner offers "Horse ride" as a
tour type with its own horseshoe, and a Horse routes card beside the
waypoints looks, when asked, for the route=horse relations OpenStreetMap
knows in the map view. A route opens in a dialog that shows it on a map in
the sections it is made of, split at forks, gaps and crossings, so nothing
is ever joined across a gap. The rider picks a section and its direction,
sees what the horse=* tags along it say (open, limited, closed, or unknown
where untagged) and whether it is signposted the other way, and takes it as
one undoable step. A section that does not start where the ride ends is
only joined by a straight manual stretch once the rider switched that on.

Nothing of a ride is routed: every leg follows a section or is drawn by
hand, and the waypoint list and the map say which (a section as a solid
line with its route, a manual stretch dashed and marked unchecked). A point
at a section stays where the route has it, a ride works out no riding time,
and distance, heights, day assignment, GPX import and export, the 3D view
and the reversed ride keep working with each leg's origin saved and opened
again. A failed lookup leaves the ride and the routes already found as they
were.

The server reads the routes through the shared Overpass client
(GET /api/trips/:tripId/tours/horse-routes, Tours addon, trip access),
keeps an answer twelve hours per grid-aligned view, and the tour contract
refuses a routed leg, a via ferrata or a riding time on a ride and a horse
route section on any other type. A migration enables the horse tour type.
This commit is contained in:
Maurice
2026-10-20 12:48:41 +02:00
parent dfe11e9c4c
commit 0331d9950f
103 changed files with 5470 additions and 82 deletions
@@ -0,0 +1,88 @@
import {
HORSE_ACCESS_VALUES,
HORSE_ROUTES_ATTRIBUTION,
horseAccessTone,
horseRoutesQuerySchema,
horseRoutesResponseSchema,
turnedSignposted,
} from './horse-route.schema';
import { describe, expect, it } from 'vitest';
const section = {
number: 1,
geometry: [
[47.1, 11.2],
[47.11, 11.21],
],
length_m: 1350,
access: [
{ value: 'designated', length_m: 900 },
{ value: 'unknown', length_m: 450 },
],
signposted: null,
};
const route = {
id: 'relation/42',
name: 'Reitweg Isartal',
ref: 'R1',
osmUrl: 'https://www.openstreetmap.org/relation/42',
continues: false,
sections: [section],
};
const answer = { routes: [route], truncated: false, source: 'openstreetmap', attribution: HORSE_ROUTES_ATTRIBUTION };
describe('horse routes contract (HOR-01)', () => {
it('HORSE-SCHEMA-001: reads a map view box as the via ferrata layer does', () => {
expect(horseRoutesQuerySchema.parse({ bbox: '47.1,11.2,47.4,11.6' }).bbox).toEqual({
south: 47.1,
west: 11.2,
north: 47.4,
east: 11.6,
});
expect(horseRoutesQuerySchema.safeParse({ bbox: '46,11,47,11.4' }).success).toBe(false);
});
it('HORSE-SCHEMA-002: takes a route with its sections, their access and signposting', () => {
expect(horseRoutesResponseSchema.parse(answer)).toEqual(answer);
const oneWay = { ...section, signposted: 'with' };
expect(horseRoutesResponseSchema.safeParse({ ...answer, routes: [{ ...route, sections: [oneWay] }] }).success).toBe(
true,
);
});
it('HORSE-SCHEMA-003: refuses a route of another element, without sections, or a section of one point', () => {
const refused = [
{ ...route, id: 'way/42' },
{ ...route, sections: [] },
{ ...route, sections: [{ ...section, geometry: [[47.1, 11.2]] }] },
{ ...route, sections: [{ ...section, number: 0 }] },
{ ...route, sections: [{ ...section, access: [{ value: 'maybe', length_m: 1 }] }] },
{ ...route, sections: [{ ...section, signposted: 'forward' }] },
];
for (const item of refused) {
expect(horseRoutesResponseSchema.safeParse({ ...answer, routes: [item] }).success).toBe(false);
}
});
it('HORSE-SCHEMA-004: sorts every horse=* value into one of four steps, the untagged as unknown', () => {
expect(Object.fromEntries(HORSE_ACCESS_VALUES.map((value) => [value, horseAccessTone(value)]))).toEqual({
designated: 'open',
yes: 'open',
permissive: 'open',
destination: 'limited',
discouraged: 'limited',
other: 'limited',
private: 'closed',
no: 'closed',
unknown: 'unknown',
});
});
it('HORSE-SCHEMA-005: turns the signposted direction round, and leaves the unsaid unsaid', () => {
expect(turnedSignposted('with')).toBe('against');
expect(turnedSignposted('against')).toBe('with');
expect(turnedSignposted(null)).toBeNull();
expect(turnedSignposted(undefined)).toBeNull();
});
});
+141
View File
@@ -0,0 +1,141 @@
import { FERRATAS_ATTRIBUTION, ferratasQuerySchema } from '../ferratas/ferratas.schema';
import { z } from 'zod';
/**
* HOR-01: the signposted horse routes around a trip, read from OpenStreetMap
* (relations tagged `route=horse`) through Overpass
* (GET /api/trips/:tripId/tours/horse-routes?bbox=south,west,north,east), and
* what a horse tour keeps of a stretch it takes from one.
*
* A route comes in sections: the stretches of its ways that run on without a
* fork, a gap or a crossing of the route with itself. A section is taken whole
* and in the direction the rider picks; nothing here joins two sections or
* fills a gap between them. Being part of a route says nothing about whether a
* way may be ridden: the `horse=*` tags of its ways are given apart, and a way
* without one stays unknown.
*/
/** The same box the via ferrata layer asks with: one map view, at most half a degree a side. */
export const horseRoutesQuerySchema = ferratasQuerySchema;
export type HorseRoutesQuery = z.output<typeof horseRoutesQuerySchema>;
/** The most routes one answer carries; past it the answer says `truncated`. */
export const HORSE_ROUTES_RESULT_CAP = 60;
/** The most sections one route carries; a route cut there says `truncated` too. */
export const HORSE_ROUTE_SECTIONS_CAP = 200;
/**
* The most points a section carries: the most a fixed leg of a tour takes, so
* a section always goes onto the tour as it came. A longer stretch is cut
* into sections of this length that meet end to end.
*/
export const HORSE_ROUTE_SECTION_MAX_POINTS = 5000;
/** ODbL asks for this next to anything drawn from the data. */
export const HORSE_ROUTES_ATTRIBUTION = FERRATAS_ATTRIBUTION;
/** The `code` beside `error` when no Overpass mirror answered, so the client can say "try again later". */
export const HORSE_ROUTES_UNAVAILABLE_CODE = 'horse_routes_unavailable';
/**
* `horse=*` as OpenStreetMap tags a way. The common values are kept as they
* are; any other tagged value is `other`, and a way without the tag `unknown`.
*/
export const HORSE_ACCESS_VALUES = [
'designated',
'yes',
'permissive',
'destination',
'discouraged',
'private',
'no',
'other',
'unknown',
] as const;
export const horseAccessSchema = z.enum(HORSE_ACCESS_VALUES);
export type HorseAccess = z.infer<typeof horseAccessSchema>;
/**
* What a value says for a rider, in four steps: open (designated, yes,
* permissive), limited (destination, discouraged, any other value), closed
* (private, no) and unknown (not tagged). A step sorts what is tagged; it is
* no permission, and the ground may say otherwise.
*/
export type HorseAccessTone = 'open' | 'limited' | 'closed' | 'unknown';
const ACCESS_TONES: Readonly<Record<HorseAccess, HorseAccessTone>> = {
designated: 'open',
yes: 'open',
permissive: 'open',
destination: 'limited',
discouraged: 'limited',
other: 'limited',
private: 'closed',
no: 'closed',
unknown: 'unknown',
};
export function horseAccessTone(value: HorseAccess): HorseAccessTone {
return ACCESS_TONES[value];
}
/** How much of a line carries one `horse=*` value, in metres. */
export const horseAccessShareSchema = z.object({
value: horseAccessSchema,
length_m: z.number().finite().nonnegative(),
});
export type HorseAccessShare = z.infer<typeof horseAccessShareSchema>;
/** The values along a line, each once, with the metres it covers. */
export const horseAccessSharesSchema = z.array(horseAccessShareSchema).max(HORSE_ACCESS_VALUES.length);
/**
* Whether a line runs the way its route is signposted (`with`) or the other
* way (`against`), where the route is signposted one way only along all of
* it. Null where it is signposted both ways, or the data does not say.
*/
export const horseSignpostedSchema = z.enum(['with', 'against']);
export type HorseSignposted = z.infer<typeof horseSignpostedSchema>;
/** The other direction: a section or a leg turned round. */
export function turnedSignposted(value: HorseSignposted | null | undefined): HorseSignposted | null {
if (value == null) return null;
return value === 'with' ? 'against' : 'with';
}
const latitude = z.number().min(-90).max(90);
const longitude = z.number().min(-180).max(180);
export const horseRouteSectionSchema = z.object({
/** Its place among the route's sections, from 1, in the order the route lists its ways. */
number: z.number().int().positive(),
/** The line as [lat, lng] pairs, in the order the route's ways run. */
geometry: z
.array(z.tuple([latitude, longitude]))
.min(2)
.max(HORSE_ROUTE_SECTION_MAX_POINTS),
length_m: z.number().finite().nonnegative(),
access: horseAccessSharesSchema,
/** Along `geometry`, as the route's member roles say. */
signposted: horseSignpostedSchema.nullable(),
});
export type HorseRouteSection = z.infer<typeof horseRouteSectionSchema>;
export const horseRouteSchema = z.object({
/** The OSM relation, as `relation/<id>`. */
id: z.string().regex(/^relation\/\d+$/),
name: z.string().max(255).nullable(),
ref: z.string().max(64).nullable(),
osmUrl: z.string().url(),
/** The route has ways beyond the box asked for: it goes on past what the sections show. */
continues: z.boolean(),
sections: z.array(horseRouteSectionSchema).min(1).max(HORSE_ROUTE_SECTIONS_CAP),
});
export type HorseRoute = z.infer<typeof horseRouteSchema>;
export const horseRoutesResponseSchema = z.object({
routes: z.array(horseRouteSchema).max(HORSE_ROUTES_RESULT_CAP),
/** True when the box held more than the caps allow and the rest was left out. */
truncated: z.boolean(),
source: z.literal('openstreetmap'),
attribution: z.literal(HORSE_ROUTES_ATTRIBUTION),
});
export type HorseRoutesResponse = z.infer<typeof horseRoutesResponseSchema>;
@@ -27,6 +27,8 @@ export const TOUR_POI_CATEGORIES_BY_TYPE: Record<PlannableTourType, readonly Tou
kayak: ['slipway', 'boat_rental', 'drinking_water', 'food', 'toilets', 'picnic', 'campsite', 'shelter'],
walk: ['sights', 'museum', 'park', 'viewpoint', 'cafe', 'restaurant', 'market', 'toilets', 'drinking_water'],
run: ['drinking_water', 'toilets', 'park', 'viewpoint', 'cafe', 'food', 'shelter', 'sights'],
// HOR-01: water for horse and rider first; a ride stops where a walk would.
horse: ['drinking_water', 'shelter', 'viewpoint', 'food', 'picnic', 'toilets', 'campsite', 'sights'],
};
/**
+61
View File
@@ -683,3 +683,64 @@ describe('runs (RUN-01)', () => {
expect(tourListItemSchema.parse(listed).run_profile).toBeUndefined();
});
});
describe('horse tours (HOR-01)', () => {
const line = [
[48, 11],
[48.005, 11.01],
[48.01, 11.02],
];
const section = {
kind: 'horse_route' as const,
ref: 'relation/42',
name: 'Reitweg Isartal',
geometry: line,
signposted: 'with' as const,
access: [{ value: 'designated' as const, length_m: 1200 }],
};
const drawn = { kind: 'free' as const, geometry: [line[0], line[2]] };
const ride = (leg: object | null, extra: object = {}) => ({
...baseRequest,
tour_type: 'horse' as const,
waypoints: [{ ...baseRequest.waypoints[0], ...(leg ? { fixed_leg: leg } : {}) }, baseRequest.waypoints[1]],
...extra,
});
it('saves a ride of horse route sections and stretches drawn by hand, with no hiking limit', () => {
expect(tourCreateRequestSchema.parse(ride(section))).toMatchObject({
tour_type: 'horse',
max_hiking_difficulty: null,
waypoints: [{ fixed_leg: section }, {}],
});
expect(tourCreateRequestSchema.safeParse(ride(drawn)).success).toBe(true);
expect(tourCreateRequestSchema.safeParse(ride({ ...section, signposted: null, access: undefined })).success).toBe(
true,
);
});
it('refuses a routed leg on a ride, a via ferrata, a hiking limit or a riding time worked out', () => {
expect(() => tourCreateRequestSchema.parse(ride(null))).toThrow(/follows a horse route or is drawn by hand/);
const ferrata = { kind: 'ferrata', ref: 'way/1', name: null, geometry: [line[0], line[2]] };
expect(() => tourCreateRequestSchema.parse(ride(ferrata))).toThrow(/horse tour has no via ferrata/);
expect(() => tourCreateRequestSchema.parse(ride(section, { max_hiking_difficulty: 2 }))).toThrow(
/hiking difficulty/,
);
expect(() => tourCreateRequestSchema.parse(ride(section, { duration_seconds: 3600 }))).toThrow(/riding time/);
expect(tourCreateRequestSchema.safeParse(ride(section, { planned_duration_minutes: 240 })).success).toBe(true);
});
it('keeps a horse route section to a horse tour, and to a relation of OpenStreetMap', () => {
const hike = {
...baseRequest,
waypoints: [{ ...baseRequest.waypoints[0], fixed_leg: section }, baseRequest.waypoints[1]],
};
expect(() => tourCreateRequestSchema.parse(hike)).toThrow(/belongs to a horse tour/);
expect(tourCreateRequestSchema.safeParse(ride({ ...section, ref: 'way/42' })).success).toBe(false);
expect(tourCreateRequestSchema.safeParse(ride({ ...section, signposted: 'forward' })).success).toBe(false);
});
it('imports a GPX file as a ride, with nothing of another type', () => {
expect(tourGpxImportFieldsSchema.parse({ tour_type: 'horse' })).toEqual({ tour_type: 'horse' });
expect(tourGpxImportFieldsSchema.safeParse({ tour_type: 'horse', bike_profile: 'road' }).success).toBe(false);
});
});
+48 -9
View File
@@ -1,3 +1,4 @@
import { horseAccessSharesSchema, horseSignpostedSchema } from './horse-route.schema';
import { paddleCheckSchema, tourObstacleCheckSchema } from './paddle-check.schema';
import {
paddleLockSchema,
@@ -24,7 +25,7 @@ import { z } from 'zod';
* single-day, Road-Trip = motorized + multi-day.
*/
export const tourTypeKeySchema = z.enum(['hike', 'bike', 'run', 'ski', 'kayak', 'walk', 'food']);
export const tourTypeKeySchema = z.enum(['hike', 'bike', 'run', 'ski', 'kayak', 'walk', 'food', 'horse']);
export type TourTypeKey = z.infer<typeof tourTypeKeySchema>;
/**
@@ -33,9 +34,11 @@ export type TourTypeKey = z.infer<typeof tourTypeKeySchema>;
* the start; the app calls it a paddle tour, and the boat is a field of its
* own. A bike tour (BIK-01) names its profile the same way. `walk` is the city
* walk (CW-01): stations joined on foot, without a hiking limit. `run` is a
* run (RUN-01), planned on foot with the runner's own pace.
* run (RUN-01), planned on foot with the runner's own pace. `horse` is a
* ride (HOR-01): sections of signposted horse routes and stretches the rider
* draws by hand, never routed.
*/
export const plannableTourTypeSchema = z.enum(['hike', 'walk', 'run', 'bike', 'kayak']);
export const plannableTourTypeSchema = z.enum(['hike', 'walk', 'run', 'bike', 'kayak', 'horse']);
export type PlannableTourType = z.infer<typeof plannableTourTypeSchema>;
/**
@@ -43,7 +46,7 @@ export type PlannableTourType = z.infer<typeof plannableTourTypeSchema>;
* and ski (SKI-02), which TREK does not route. A ski track comes in as it was
* recorded and stays so; the planner never opens it as a draft.
*/
export const importableTourTypeSchema = z.enum(['hike', 'walk', 'run', 'bike', 'kayak', 'ski']);
export const importableTourTypeSchema = z.enum(['hike', 'walk', 'run', 'bike', 'kayak', 'horse', 'ski']);
export type ImportableTourType = z.infer<typeof importableTourTypeSchema>;
/**
@@ -319,7 +322,10 @@ const fixedLegGeometrySchema = z
* the next: off the path network, across a lake, wherever no router goes.
* Nothing about a free leg was checked by a router. A portage (PAD-03) is a
* free leg on land: the boat is carried along it, so it counts as land and
* not as water, with the time getting out and back in takes.
* not as water, with the time getting out and back in takes. A horse route
* section (HOR-01) is a stretch of a signposted horse route from
* OpenStreetMap, taken in the direction the rider chose, with the `horse=*`
* tags its ways carried then; being on the route is no permission to ride it.
*/
export const tourFixedLegSchema = z.discriminatedUnion('kind', [
z.object({
@@ -339,6 +345,20 @@ export const tourFixedLegSchema = z.discriminatedUnion('kind', [
/** Getting out, unloading and back in, on the first leg of a portage; null takes the starting value. */
extra_minutes: paddleStopMinutesSchema.nullable().optional(),
}),
z.object({
kind: z.literal('horse_route'),
/** The OSM relation the section came from, `relation/<id>` style. */
ref: z
.string()
.max(64)
.regex(/^relation\/\d+$/),
name: z.string().trim().max(255).nullable(),
geometry: fixedLegGeometrySchema,
/** Whether the leg runs the way the route is signposted; null both ways or not said. */
signposted: horseSignpostedSchema.nullable().optional(),
/** The `horse=*` tags along the section when it was taken, in metres per value. */
access: horseAccessSharesSchema.optional(),
}),
]);
export type TourFixedLeg = z.infer<typeof tourFixedLegSchema>;
export type TourFixedLegKind = TourFixedLeg['kind'];
@@ -530,6 +550,7 @@ const TYPE_NAMED: Record<PlannableTourType, string> = {
walk: 'a city walk',
run: 'a run',
hike: 'a hike',
horse: 'a horse tour',
};
const PADDLE_FIELDS = [
@@ -544,10 +565,12 @@ const PADDLE_FIELDS = [
* What belongs to which type: a hike carries a routing limit and no boat; a
* bike tour and a run exactly one profile each and nothing of a hike's or a
* paddler's, a run also its pace; a paddle tour exactly one boat and its
* water, and no hiking limit.
* water, and no hiking limit; a horse tour (HOR-01) only legs taken from a
* horse route or drawn by hand, and no riding time worked out for it.
*/
function tourTypeIssues(value: {
tour_type: PlannableTourType;
duration_seconds?: number | null;
max_hiking_difficulty?: number | null;
hiking_preferences?: HikingPreferences | null;
bike_profile?: BikeProfile | null;
@@ -595,14 +618,30 @@ function tourTypeIssues(value: {
if (type !== 'bike' && value.bike_profile != null)
issues.push({ path: ['bike_profile'], message: 'bike_profile belongs to a bike tour' });
issues.push(...runIssues(value));
// No router plans a ride, so no time is worked out for it; a planned total the rider enters is theirs.
if (type === 'horse' && value.duration_seconds != null)
issues.push({ path: ['duration_seconds'], message: 'a horse tour has no riding time worked out' });
const points = value.waypoints ?? [];
// A portage and a lock are a paddler's; a via ferrata is climbed, never ridden.
(value.waypoints ?? []).forEach((point, index) => {
if (type !== 'kayak' && point.fixed_leg?.kind === 'portage')
points.forEach((point, index) => {
const kind = point.fixed_leg?.kind;
if (type !== 'kayak' && kind === 'portage')
issues.push({ path: ['waypoints', index, 'fixed_leg'], message: 'a portage belongs to a paddle tour' });
if (type !== 'kayak' && point.lock != null)
issues.push({ path: ['waypoints', index, 'lock'], message: 'a lock belongs to a paddle tour' });
if ((type === 'bike' || type === 'walk' || type === 'run') && point.fixed_leg?.kind === 'ferrata')
if ((type === 'bike' || type === 'walk' || type === 'run' || type === 'horse') && kind === 'ferrata')
issues.push({ path: ['waypoints', index, 'fixed_leg'], message: `${named} has no via ferrata` });
if (type !== 'horse' && kind === 'horse_route')
issues.push({
path: ['waypoints', index, 'fixed_leg'],
message: 'a horse route section belongs to a horse tour',
});
// HOR-01: nothing of a ride is routed, so no leg may pass for one that was.
if (type === 'horse' && index < points.length - 1 && kind !== 'horse_route' && kind !== 'free')
issues.push({
path: ['waypoints', index, 'fixed_leg'],
message: 'each leg of a horse tour follows a horse route or is drawn by hand',
});
// A stay, an importance and a protection are a city walk's stations'.
if (type !== 'walk' && (point.stay_minutes != null || point.importance != null || point.protected === true))
issues.push({ path: ['waypoints', index], message: 'stations belong to a city walk' });