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:
1 parent
477f68f5b3
commit
f0ff5f390f
5 files changed
+51
-19
No files matched your search
@@ -600,9 +600,10 @@ module Maptool
|
|||||||
# the country they actually pick in the Hiking trails overlay's own
|
# the country they actually pick in the Hiking trails overlay's own
|
||||||
# checklist (public/js/hiking_trails.js), not every selected country's
|
# checklist (public/js/hiking_trails.js), not every selected country's
|
||||||
# trails at once — `countries` lists which ones exist to build that
|
# 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
|
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
|
end
|
||||||
|
|
||||||
# Fetched client-side only for a country the viewer actually ticks in
|
# Fetched client-side only for a country the viewer actually ticks in
|
||||||
|
|||||||
@@ -186,6 +186,14 @@ module Maptool
|
|||||||
end
|
end
|
||||||
private_class_method :region_bbox
|
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)
|
def self.ring_coordinates(geometry)
|
||||||
case geometry["type"]
|
case geometry["type"]
|
||||||
when "Polygon" then geometry["coordinates"].flatten(1)
|
when "Polygon" then geometry["coordinates"].flatten(1)
|
||||||
|
|||||||
+37
-14
@@ -170,6 +170,12 @@ export class HikingTrailsControl {
|
|||||||
this._trailData = new Map(); // code -> fetched GeoJSON, kept across toggles/style swaps
|
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._routeInfo = new Map(); // String(relation id) -> feature properties, for the panel
|
||||||
this._lanes = null; // the current LaneLayer, null when no country is shown
|
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));
|
map.on("click", (event) => this._handleClick(event));
|
||||||
|
|
||||||
@@ -206,7 +212,7 @@ export class HikingTrailsControl {
|
|||||||
this._countryList = li.querySelector(".hiking-trail-countries");
|
this._countryList = li.querySelector(".hiking-trail-countries");
|
||||||
|
|
||||||
this._checkbox.addEventListener("change", (event) => {
|
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);
|
else this._toggleOnline(event.target.checked);
|
||||||
});
|
});
|
||||||
this._nameButton.addEventListener("click", () => {
|
this._nameButton.addEventListener("click", () => {
|
||||||
@@ -223,15 +229,17 @@ export class HikingTrailsControl {
|
|||||||
// both offered at once, since once a real local extract exists there's
|
// 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
|
// no reason to still fall back to a third-party tile server for any of
|
||||||
// it.
|
// it.
|
||||||
setOfflineCountries(countries) {
|
setOfflineCountries(countries, bboxes = {}) {
|
||||||
this._offlineCountries = countries;
|
this._offlineCountries = countries;
|
||||||
|
this._bboxes = bboxes;
|
||||||
if (countries.length === 0) return;
|
if (countries.length === 0) return;
|
||||||
|
|
||||||
if (this._active) this._toggleOnline(false);
|
if (this._active) this._toggleOnline(false);
|
||||||
// The main checkbox stays: offline it switches every country at once
|
// The main checkbox stays: offline it shows the countries in view,
|
||||||
// (indeterminate while only some are on); the name opens the list.
|
// 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.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
|
this._countryList.innerHTML = countries
|
||||||
.map(
|
.map(
|
||||||
@@ -262,20 +270,35 @@ export class HikingTrailsControl {
|
|||||||
this._syncMainCheckbox();
|
this._syncMainCheckbox();
|
||||||
}
|
}
|
||||||
|
|
||||||
_toggleAllCountries(active) {
|
_toggleInView(active) {
|
||||||
this._countryList.querySelectorAll(".hiking-trail-countries__toggle").forEach((input) => {
|
this._inView = active;
|
||||||
if (input.checked === active) return;
|
if (active) {
|
||||||
input.checked = active;
|
this._addCountriesInView();
|
||||||
this._toggleCountry(input.value, active);
|
} else {
|
||||||
|
this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").forEach((input) => {
|
||||||
|
input.checked = false;
|
||||||
|
this._toggleCountry(input.value, false);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
this._syncMainCheckbox();
|
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() {
|
_syncMainCheckbox() {
|
||||||
const inputs = [...this._countryList.querySelectorAll(".hiking-trail-countries__toggle")];
|
const on = this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").length;
|
||||||
const on = inputs.filter((input) => input.checked).length;
|
this._checkbox.checked = this._inView;
|
||||||
this._checkbox.checked = on > 0 && on === inputs.length;
|
this._checkbox.indeterminate = !this._inView && on > 0;
|
||||||
this._checkbox.indeterminate = on > 0 && on < inputs.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_toggleOnline(active) {
|
_toggleOnline(active) {
|
||||||
|
|||||||
@@ -255,8 +255,8 @@ export class OverlaysControl {
|
|||||||
this._hillshade.setOfflineAvailable(available);
|
this._hillshade.setOfflineAvailable(available);
|
||||||
}
|
}
|
||||||
|
|
||||||
setHikingTrailsOfflineCountries(countries) {
|
setHikingTrailsOfflineCountries(countries, bboxes) {
|
||||||
this._hikingTrails.setOfflineCountries(countries);
|
this._hikingTrails.setOfflineCountries(countries, bboxes);
|
||||||
}
|
}
|
||||||
|
|
||||||
showHikingTrailsCountry(code) {
|
showHikingTrailsCountry(code) {
|
||||||
|
|||||||
+1
-1
@@ -75,7 +75,7 @@ class AppTest < Minitest::Test
|
|||||||
|
|
||||||
def test_hiking_trails_reports_no_countries_when_none_extracted_yet
|
def test_hiking_trails_reports_no_countries_when_none_extracted_yet
|
||||||
response = request.get("/api/hiking-trails")
|
response = request.get("/api/hiking-trails")
|
||||||
assert_equal({ "countries" => [] }, json_body(response))
|
assert_equal({ "countries" => [], "bboxes" => {} }, json_body(response))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hiking_trails_file_404s_for_a_country_with_no_extract
|
def test_hiking_trails_file_404s_for_a_country_with_no_extract
|
||||||
|
|||||||
Reference in new issue
Block a user