The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
/**
* P2A-03: which leg of a tour a spot on its drawn route belongs to. Every
* waypoint is placed along the line (searching on from where the one before
* lay, so a loop or a route that passes a point twice keeps its order), then
* the spot; the leg is the one whose two points enclose it.
*/
/** Where a spot falls along a line, as the distance from its start in the line's own degrees (flat earth is plenty for ordering). */
function alongLine(
line: readonly [number, number][],
lat: number,
lng: number,
fromIndex: number
): { at: number; index: number; vertex: number } {
const cos = Math.cos((lat * Math.PI) / 180);
let travelled = 0;
let best = { at: 0, index: fromIndex, vertex: fromIndex, distance: Infinity };
for (let i = 0; i < line.length - 1; i += 1) {
const [aLat, aLng] = line[i];
const [bLat, bLng] = line[i + 1];
const dx = (bLng - aLng) * cos;
const dy = bLat - aLat;
const length = Math.hypot(dx, dy);
if (i >= fromIndex) {
const t =
length === 0 ? 0 : Math.max(0, Math.min(1, ((lng - aLng) * cos * dx + (lat - aLat) * dy) / (length * length)));
const distance = Math.hypot(aLng * cos + dx * t - lng * cos, aLat + dy * t - lat);
if (distance < best.distance)
best = { at: travelled + length * t, index: i, vertex: t < 0.5 ? i : i + 1, distance };
}
travelled += length;
}
return { at: best.at, index: best.index, vertex: best.vertex };
}
/** Every waypoint placed along the line, each searched for from where the one before it lay. */
function placedAlong(points: ReadonlyArray<{ lat: number; lng: number }>, line: readonly [number, number][]) {
let from = 0;
return points.map((point) => {
const placed = alongLine(line, point.lat, point.lng, from);
from = placed.index;
return placed;
});
}
/** The id of the waypoint that starts the leg holding the spot, or null when the route has no such leg. */
export function legStartAt(
points: ReadonlyArray<{ id: string; lat: number; lng: number }>,
line: readonly [number, number][] | null,
lat: number,
lng: number
): string | null {
if (!line || line.length < 2 || points.length < 2) return null;
const positions = placedAlong(points, line).map((placed) => placed.at);
const spot = alongLine(line, lat, lng, 0).at;
for (let i = 0; i < points.length - 1; i += 1) {
if (spot >= positions[i] && spot <= positions[i + 1]) return points[i].id;
}
return null;
}
/**
* RTE-02: the stretch of the drawn route that runs from waypoint `index` to the
* next one, as the vertices it spans (`to` included), or null when the route
* has no such leg. A route and its heights share their vertices, so the span
* cuts both alike.
*/
export function legSpanOf(
points: ReadonlyArray<{ lat: number; lng: number }>,
line: readonly [number, number][] | null,
index: number
): { from: number; to: number } | null {
if (!line || line.length < 2 || index < 0 || index >= points.length - 1) return null;
const placed = placedAlong(points, line);
const from = placed[index].vertex;
const to = placed[index + 1].vertex;
return to > from ? { from, to } : null;
}