Bicycle and foot route profiles (Valhalla bicycle/pedestrian costing); hiking trail colours in the elevation profile; power, splits and best efforts in the profile; TCX import; FIT course export
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-16 12:18:06 +01:00
1 parent 4c704045c5
commit f8755ea65a
21 files changed
+634 -40

No files matched your search

+62
View File
@@ -83,6 +83,67 @@ function normalizedTrailColor(raw) {
return /^#[0-9a-fA-F]{3,8}$/.test(trimmed) ? trimmed : DEFAULT_TRAIL_COLOR;
}
// The offline trails currently drawn (every ticked country), for
// trailColorsAlong.
let shownTrails = [];
const TRAIL_MATCH_M = 20; // GPS/OSM offset a point may be off its trail
const GRID_DEG = 0.001; // ~100 m cells
// For each {lon, lat} point, the colors of the drawn trails within
// TRAIL_MATCH_M of it ([] off-trail), for the elevation profile's trail
// band; null when no point is on a drawn trail. Only what the hiking
// trails overlay shows, so ticking a country there is what turns it on.
export function trailColorsAlong(points) {
if (!shownTrails.length || !points.length) return null;
const lons = points.map((p) => p.lon);
const lats = points.map((p) => p.lat);
const [w, e] = [Math.min(...lons) - GRID_DEG, Math.max(...lons) + GRID_DEG];
const [s, n] = [Math.min(...lats) - GRID_DEG, Math.max(...lats) + GRID_DEG];
// Only segments near the points, bucketed by cell.
const grid = new Map();
for (const { geometry, properties } of shownTrails) {
const lines = geometry.type === "LineString" ? [geometry.coordinates] : geometry.coordinates;
for (const line of lines) {
for (let i = 1; i < line.length; i++) {
const [a, b] = [line[i - 1], line[i]];
if (Math.max(a[0], b[0]) < w || Math.min(a[0], b[0]) > e || Math.max(a[1], b[1]) < s || Math.min(a[1], b[1]) > n) continue;
for (let x = Math.floor(Math.min(a[0], b[0]) / GRID_DEG); x <= Math.floor(Math.max(a[0], b[0]) / GRID_DEG); x++) {
for (let y = Math.floor(Math.min(a[1], b[1]) / GRID_DEG); y <= Math.floor(Math.max(a[1], b[1]) / GRID_DEG); y++) {
const key = `${x},${y}`;
if (!grid.has(key)) grid.set(key, []);
grid.get(key).push({ a, b, color: properties.color });
}
}
}
}
}
let any = false;
const colors = points.map(({ lon, lat }) => {
const found = new Set();
const [cx, cy] = [Math.floor(lon / GRID_DEG), Math.floor(lat / GRID_DEG)];
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
for (const { a, b, color } of grid.get(`${cx + dx},${cy + dy}`) || []) {
if (!found.has(color) && segmentDistanceM([lon, lat], a, b) <= TRAIL_MATCH_M) found.add(color);
}
}
}
if (found.size) any = true;
return [...found];
});
return any ? colors : null;
}
// Point to segment, on a local flat projection (fine at these distances).
function segmentDistanceM([lon, lat], a, b) {
const kx = 111320 * Math.cos((lat * Math.PI) / 180);
const ky = 110540;
const [ax, ay, bx, by] = [(a[0] - lon) * kx, (a[1] - lat) * ky, (b[0] - lon) * kx, (b[1] - lat) * ky];
const [dx, dy] = [bx - ax, by - ay];
const t = Math.max(0, Math.min(1, -(ax * dx + ay * dy) / (dx * dx + dy * dy || 1)));
return Math.hypot(ax + t * dx, ay + t * dy);
}
// Online mode's highlight outline for whichever route(s) the last click
// matched — drawn as its own separate source/layer (real geometry, from the
// Overpass response), since the raster tiles have no paintable features to
@@ -227,6 +288,7 @@ export class HikingTrailsControl {
async _rebuildLanes() {
const build = (this._lanesBuild = {});
const features = [...this._selectedCountries].flatMap((code) => this._trailData.get(code)?.features || []);
shownTrails = features;
let graph = null;
if (features.length > 0) {
graph = buildLineGraph(features.flatMap(splitOneway), { routeProperty: "id", onewayProperty: "oneway" });