Hiking trails: the main checkbox loads the countries in view from zoom 10 (and those panned into), not all at once — together ~1 GB of GeoJSON the browser never finished; /api/hiking-trails also sends each country's bbox

This commit is contained in:
randy committed 2026-11-27 14:29:29 +01:00
1 parent 477f68f5b3
commit f0ff5f390f
5 files changed
+52 -20

No files matched your search

+3 -2
View File
@@ -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
+8
View File
@@ -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)
+38 -15
View File
@@ -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) {
+2 -2
View File
@@ -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) {
+1 -1
View File
@@ -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