From 4bcd85f2691abd9012e29f5f24e6c37ca8808d68 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Fri, 27 Nov 2026 14:34:24 +0100 Subject: [PATCH] =?UTF-8?q?Hiking=20trails:=20lanes=20built=20only=20from?= =?UTF-8?q?=20the=20route=20pieces=20around=20the=20view=20(half=20a=20vie?= =?UTF-8?q?w=20each=20side),=20rebuilt=20when=20the=20view=20leaves=20it,?= =?UTF-8?q?=20drawn=20from=20zoom=2011=20=E2=80=94=20a=20whole=20country's?= =?UTF-8?q?=20graph=20took=20seconds=20per=20rebuild=20on=20the=20main=20t?= =?UTF-8?q?hread=20and=20froze=20the=20browser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/hiking_trails.js | 57 ++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/public/js/hiking_trails.js b/public/js/hiking_trails.js index 02a7447..a87a9d9 100644 --- a/public/js/hiking_trails.js +++ b/public/js/hiking_trails.js @@ -64,7 +64,7 @@ const OPACITY = 0.85; const LANES_LAYER_ID = "hiking-trails-lanes"; // Zoomed out further, a whole country's lanes took seconds per frame/zoom // build and froze the map — and a trail line at that scale tells nothing. -const LANES_MIN_ZOOM = 10; +const LANES_MIN_ZOOM = 11; const LANE_SIZES = () => ({ spacing: 3.5, width: 3, casingWidth: 0.5 }); function offlineUrl(code) { return `/tiles/hiking_trails/${code}.geojson`; @@ -175,7 +175,10 @@ export class HikingTrailsControl { // Each country entered while the main checkbox is on joins in. // ponytail: a country left behind stays loaded until unticked. - map.on("moveend", () => this._inView && this._addCountriesInView()); + map.on("moveend", () => { + if (this._inView) this._addCountriesInView(); + if (this._selectedCountries.size > 0 && this._needsRebuild()) this._rebuildLanes(); + }); map.on("click", (event) => this._handleClick(event)); @@ -333,14 +336,26 @@ export class HikingTrailsControl { this._rebuildLanes(); } - // Rebuilt from scratch on every country toggle and style swap (setStyle - // drops custom layers too) — lane order depends on every route drawn, so - // there's no cheaper incremental path. Ordering runs in a worker; a newer - // rebuild started meanwhile wins, same token guard as _handleOnlineClick. + // Rebuilt from scratch on every country toggle, style swap (setStyle + // drops custom layers too) and when the view leaves the area built for — + // lane order depends on every route drawn, so there's no cheaper + // incremental path. Only the route pieces around the view (half a view + // around it): a whole country's graph took seconds on the main thread + // per rebuild and froze the browser; at zoom 11 this is ~0.5 s. Ordering runs in a worker; a newer rebuild started + // meanwhile wins, same token guard as _handleOnlineClick. async _rebuildLanes() { const build = (this._lanesBuild = {}); - const features = [...this._selectedCountries].flatMap((code) => this._trailData.get(code)?.features || []); - shownTrails = features; + const all = [...this._selectedCountries].flatMap((code) => this._trailData.get(code)?.features || []); + shownTrails = all; + let features = []; + this._builtArea = null; + if (all.length > 0 && this._map.getZoom() >= LANES_MIN_ZOOM) { + const view = this._map.getBounds(); + const [dx, dy] = [view.getEast() - view.getWest(), view.getNorth() - view.getSouth()]; + const area = [view.getWest() - dx / 2, view.getSouth() - dy / 2, view.getEast() + dx / 2, view.getNorth() + dy / 2]; + this._builtArea = area; + features = all.flatMap((feature) => clipToArea(feature, area)); + } let graph = null; if (features.length > 0) { graph = buildLineGraph(features.flatMap(splitOneway), { routeProperty: "id", onewayProperty: "oneway" }); @@ -361,6 +376,15 @@ export class HikingTrailsControl { this._map.setLayerZoomRange(LANES_LAYER_ID, LANES_MIN_ZOOM, 24); } + // Zoomed in from below LANES_MIN_ZOOM, or out of the built area. + _needsRebuild() { + if (this._map.getZoom() < LANES_MIN_ZOOM) return false; + if (!this._builtArea) return true; + const view = this._map.getBounds(); + const [w, s, e, n] = this._builtArea; + return view.getWest() < w || view.getSouth() < s || view.getEast() > e || view.getNorth() > n; + } + _handleClick(event) { if (this._selectedCountries.size > 0) this._handleOfflineClick(event); else if (this._active) this._handleOnlineClick(event); @@ -567,6 +591,23 @@ function routeHtml(info) { // A route's one-way lines (oneway_lines, already pointing the way they're // walked: lib/maptool/hiking_trails.rb) as a feature of their own marked // oneway, so maplibre-gl-lanes keeps the route on them in that direction. +// The route's lines (its member ways) with a vertex in `area` [w, s, e, +// n], [] when none; oneway_lines renumbered to match. +function clipToArea(feature, area) { + const [w, s, e, n] = area; + const lines = feature.geometry.type === "LineString" ? [feature.geometry.coordinates] : feature.geometry.coordinates; + const oneway = new Set(feature.properties.oneway_lines); + const kept = []; + const keptOneway = []; + lines.forEach((line, i) => { + if (!line.some(([x, y]) => x >= w && x <= e && y >= s && y <= n)) return; + if (oneway.has(i)) keptOneway.push(kept.length); + kept.push(line); + }); + if (kept.length === 0) return []; + return [{ ...feature, properties: { ...feature.properties, oneway_lines: keptOneway }, geometry: { type: "MultiLineString", coordinates: kept } }]; +} + function splitOneway(feature) { const oneway = new Set(feature.properties.oneway_lines); if (oneway.size === 0) return [feature];