From f0ff5f390fc47904ae9145b9ba3a81a243b05c66 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Fri, 27 Nov 2026 14:29:29 +0100 Subject: [PATCH] =?UTF-8?q?Hiking=20trails:=20the=20main=20checkbox=20load?= =?UTF-8?q?s=20the=20countries=20in=20view=20from=20zoom=2010=20(and=20tho?= =?UTF-8?q?se=20panned=20into),=20not=20all=20at=20once=20=E2=80=94=20toge?= =?UTF-8?q?ther=20~1=20GB=20of=20GeoJSON=20the=20browser=20never=20finishe?= =?UTF-8?q?d;=20/api/hiking-trails=20also=20sends=20each=20country's=20bbo?= =?UTF-8?q?x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.rb | 5 ++-- lib/maptool/pmtiles_builder.rb | 8 +++++ public/js/hiking_trails.js | 53 ++++++++++++++++++++++++---------- public/js/overlays.js | 4 +-- test/test_app.rb | 2 +- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/app.rb b/app.rb index f56ce21..2513559 100644 --- a/app.rb +++ b/app.rb @@ -600,9 +600,10 @@ module Maptool # the country they actually pick in the Hiking trails overlay's own # checklist (public/js/hiking_trails.js), not every selected country's # trails at once — `countries` lists which ones exist to build that - # checklist from. + # checklist from, `bboxes` ([w, s, e, n] each) which are in view. get "/api/hiking-trails" do - json countries: PmtilesBuilder.hiking_trails_countries(DATA_DIR) + countries = PmtilesBuilder.hiking_trails_countries(DATA_DIR) + json countries: countries, bboxes: countries.to_h { |code| [code, PmtilesBuilder.country_bbox(code)] }.compact end # Fetched client-side only for a country the viewer actually ticks in diff --git a/lib/maptool/pmtiles_builder.rb b/lib/maptool/pmtiles_builder.rb index 3db2082..f632ec3 100644 --- a/lib/maptool/pmtiles_builder.rb +++ b/lib/maptool/pmtiles_builder.rb @@ -186,6 +186,14 @@ module Maptool end private_class_method :region_bbox + # [west, south, east, north] of one region, nil for an unknown code. + def self.country_bbox(code) + south, west, north, east = region_bbox([code]) + [west, south, east, north] + rescue BuildError + nil + end + def self.ring_coordinates(geometry) case geometry["type"] when "Polygon" then geometry["coordinates"].flatten(1) diff --git a/public/js/hiking_trails.js b/public/js/hiking_trails.js index ff142cc..02a7447 100644 --- a/public/js/hiking_trails.js +++ b/public/js/hiking_trails.js @@ -170,6 +170,12 @@ export class HikingTrailsControl { this._trailData = new Map(); // code -> fetched GeoJSON, kept across toggles/style swaps this._routeInfo = new Map(); // String(relation id) -> feature properties, for the panel this._lanes = null; // the current LaneLayer, null when no country is shown + this._bboxes = {}; // code -> [w, s, e, n] + this._inView = false; // main checkbox, offline: the countries in view + + // 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("click", (event) => this._handleClick(event)); @@ -206,7 +212,7 @@ export class HikingTrailsControl { this._countryList = li.querySelector(".hiking-trail-countries"); this._checkbox.addEventListener("change", (event) => { - if (this._offlineCountries.length) this._toggleAllCountries(event.target.checked); + if (this._offlineCountries.length) this._toggleInView(event.target.checked); else this._toggleOnline(event.target.checked); }); this._nameButton.addEventListener("click", () => { @@ -223,15 +229,17 @@ export class HikingTrailsControl { // both offered at once, since once a real local extract exists there's // no reason to still fall back to a third-party tile server for any of // it. - setOfflineCountries(countries) { + setOfflineCountries(countries, bboxes = {}) { this._offlineCountries = countries; + this._bboxes = bboxes; if (countries.length === 0) return; if (this._active) this._toggleOnline(false); - // The main checkbox stays: offline it switches every country at once - // (indeterminate while only some are on); the name opens the list. + // The main checkbox stays: offline it shows the countries in view, + // never all at once (together they're ~1 GB of GeoJSON, more than a + // browser parses); the name opens the list to pick some by hand. this._checkbox.checked = false; - this._checkbox.title = "All countries — click the name to pick some"; + this._checkbox.title = `Countries in view, from zoom ${LANES_MIN_ZOOM} — click the name to pick some`; this._countryList.innerHTML = countries .map( @@ -262,20 +270,35 @@ export class HikingTrailsControl { this._syncMainCheckbox(); } - _toggleAllCountries(active) { - this._countryList.querySelectorAll(".hiking-trail-countries__toggle").forEach((input) => { - if (input.checked === active) return; - input.checked = active; - this._toggleCountry(input.value, active); - }); + _toggleInView(active) { + this._inView = active; + if (active) { + this._addCountriesInView(); + } else { + this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").forEach((input) => { + input.checked = false; + this._toggleCountry(input.value, false); + }); + } this._syncMainCheckbox(); } + // Trails are drawn from LANES_MIN_ZOOM only, so nothing loads before. + _addCountriesInView() { + if (this._map.getZoom() < LANES_MIN_ZOOM) return; + const view = this._map.getBounds(); + this._countryList.querySelectorAll(".hiking-trail-countries__toggle:not(:checked)").forEach((input) => { + const [w, s, e, n] = this._bboxes[input.value] || []; + if (n === undefined || w > view.getEast() || e < view.getWest() || s > view.getNorth() || n < view.getSouth()) return; + input.checked = true; + this._toggleCountry(input.value, true); + }); + } + _syncMainCheckbox() { - const inputs = [...this._countryList.querySelectorAll(".hiking-trail-countries__toggle")]; - const on = inputs.filter((input) => input.checked).length; - this._checkbox.checked = on > 0 && on === inputs.length; - this._checkbox.indeterminate = on > 0 && on < inputs.length; + const on = this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").length; + this._checkbox.checked = this._inView; + this._checkbox.indeterminate = !this._inView && on > 0; } _toggleOnline(active) { diff --git a/public/js/overlays.js b/public/js/overlays.js index 4bb6412..830c2ee 100644 --- a/public/js/overlays.js +++ b/public/js/overlays.js @@ -255,8 +255,8 @@ export class OverlaysControl { this._hillshade.setOfflineAvailable(available); } - setHikingTrailsOfflineCountries(countries) { - this._hikingTrails.setOfflineCountries(countries); + setHikingTrailsOfflineCountries(countries, bboxes) { + this._hikingTrails.setOfflineCountries(countries, bboxes); } showHikingTrailsCountry(code) { diff --git a/test/test_app.rb b/test/test_app.rb index 91c88f8..9646f1e 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -75,7 +75,7 @@ class AppTest < Minitest::Test def test_hiking_trails_reports_no_countries_when_none_extracted_yet response = request.get("/api/hiking-trails") - assert_equal({ "countries" => [] }, json_body(response)) + assert_equal({ "countries" => [], "bboxes" => {} }, json_body(response)) end def test_hiking_trails_file_404s_for_a_country_with_no_extract