Hiking trails: lanes built only from the route pieces around the view (half a view each side), rebuilt when the view leaves it, drawn from zoom 11 — a whole country's graph took seconds per rebuild on the main thread and froze the browser
This commit is contained in:
1 parent
cf43c3e926
commit
4bcd85f269
1 file changed
+49
-8
@@ -64,7 +64,7 @@ const OPACITY = 0.85;
|
|||||||
const LANES_LAYER_ID = "hiking-trails-lanes";
|
const LANES_LAYER_ID = "hiking-trails-lanes";
|
||||||
// Zoomed out further, a whole country's lanes took seconds per frame/zoom
|
// 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.
|
// 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 });
|
const LANE_SIZES = () => ({ spacing: 3.5, width: 3, casingWidth: 0.5 });
|
||||||
function offlineUrl(code) {
|
function offlineUrl(code) {
|
||||||
return `/tiles/hiking_trails/${code}.geojson`;
|
return `/tiles/hiking_trails/${code}.geojson`;
|
||||||
@@ -175,7 +175,10 @@ export class HikingTrailsControl {
|
|||||||
|
|
||||||
// Each country entered while the main checkbox is on joins in.
|
// Each country entered while the main checkbox is on joins in.
|
||||||
// ponytail: a country left behind stays loaded until unticked.
|
// 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));
|
map.on("click", (event) => this._handleClick(event));
|
||||||
|
|
||||||
@@ -333,14 +336,26 @@ export class HikingTrailsControl {
|
|||||||
this._rebuildLanes();
|
this._rebuildLanes();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rebuilt from scratch on every country toggle and style swap (setStyle
|
// Rebuilt from scratch on every country toggle, style swap (setStyle
|
||||||
// drops custom layers too) — lane order depends on every route drawn, so
|
// drops custom layers too) and when the view leaves the area built for —
|
||||||
// there's no cheaper incremental path. Ordering runs in a worker; a newer
|
// lane order depends on every route drawn, so there's no cheaper
|
||||||
// rebuild started meanwhile wins, same token guard as _handleOnlineClick.
|
// 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() {
|
async _rebuildLanes() {
|
||||||
const build = (this._lanesBuild = {});
|
const build = (this._lanesBuild = {});
|
||||||
const features = [...this._selectedCountries].flatMap((code) => this._trailData.get(code)?.features || []);
|
const all = [...this._selectedCountries].flatMap((code) => this._trailData.get(code)?.features || []);
|
||||||
shownTrails = 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;
|
let graph = null;
|
||||||
if (features.length > 0) {
|
if (features.length > 0) {
|
||||||
graph = buildLineGraph(features.flatMap(splitOneway), { routeProperty: "id", onewayProperty: "oneway" });
|
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);
|
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) {
|
_handleClick(event) {
|
||||||
if (this._selectedCountries.size > 0) this._handleOfflineClick(event);
|
if (this._selectedCountries.size > 0) this._handleOfflineClick(event);
|
||||||
else if (this._active) this._handleOnlineClick(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
|
// 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
|
// 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.
|
// 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) {
|
function splitOneway(feature) {
|
||||||
const oneway = new Set(feature.properties.oneway_lines);
|
const oneway = new Set(feature.properties.oneway_lines);
|
||||||
if (oneway.size === 0) return [feature];
|
if (oneway.size === 0) return [feature];
|
||||||
|
|||||||
Reference in new issue
Block a user