Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd8444ce2a | ||
|
|
f0ff5f390f |
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
@@ -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) {
|
||||
|
||||
+16
-4
@@ -23,7 +23,7 @@ import { getPosition } from "/vendor/suncalc/suncalc.js";
|
||||
import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js";
|
||||
import { isLoggedIn } from "/js/auth.js";
|
||||
import { DebugControl } from "/js/debug.js";
|
||||
import { patchOnlineStyle, patternImage, NightMap } from "/js/map_themes.js";
|
||||
import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap } from "/js/map_themes.js";
|
||||
import { offerSharedFiles } from "/js/share_import.js";
|
||||
import HomeButton from "/vendor/npmap-home-button/home-button.esm.js";
|
||||
import OverviewMap from "/vendor/npmap-overview/overview.esm.js";
|
||||
@@ -473,8 +473,20 @@ setInterval(updateSun, 60000);
|
||||
const NIGHT_MODES = { auto: ["◐", "Night mode: automatic (after sunset)"], on: ["☾", "Night mode: on"], off: ["☀", "Night mode: off"] };
|
||||
const nightButton = document.querySelector(".topbar__night");
|
||||
const nightMap = new NightMap(map);
|
||||
// The base style's texture patterns (forest, grass, military hatch) are
|
||||
// drawn on demand rather than shipped in a sprite sheet.
|
||||
// The base style's texture patterns (grass, scrub, military hatch) and the
|
||||
// peak triangle are drawn at runtime rather than shipped in a sprite
|
||||
// sheet, added with every style before its tiles ask for them: since
|
||||
// MapLibre 6 an image added on styleimagemissing (or from the missing-image
|
||||
// resolver) misses the tiles that asked for it.
|
||||
const addPatterns = () => {
|
||||
for (const id of PATTERN_IDS) {
|
||||
const pattern = !map.hasImage(id) && patternImage(id);
|
||||
if (pattern) map.addImage(id, pattern.image, pattern.options);
|
||||
}
|
||||
};
|
||||
map.on("style.load", addPatterns);
|
||||
if (map.isStyleLoaded()) addPatterns();
|
||||
// Still the fallback for one asked for before that.
|
||||
map.on("styleimagemissing", ({ id }) => {
|
||||
const pattern = !map.hasImage(id) && patternImage(id);
|
||||
if (pattern) map.addImage(id, pattern.image, pattern.options);
|
||||
@@ -592,7 +604,7 @@ fetch("/api/terrain-tiles")
|
||||
|
||||
fetch("/api/hiking-trails")
|
||||
.then((response) => response.json())
|
||||
.then(({ countries }) => overlaysControl.setHikingTrailsOfflineCountries(countries))
|
||||
.then(({ countries, bboxes }) => overlaysControl.setHikingTrailsOfflineCountries(countries, bboxes))
|
||||
.catch(() => {});
|
||||
|
||||
fetch("/api/kiwix")
|
||||
|
||||
@@ -103,7 +103,7 @@ const NATIONAL_PARK = ["any", ...["nation", "národn", "narodn", "nemzeti", "naz
|
||||
// texture from zoom 13, scrub / dwarf pine its own bushes from 11 (no forest texture: OSM rarely says what kind, and
|
||||
// trees everywhere were too busy).
|
||||
// The pattern images are drawn at runtime (patternImage below, on
|
||||
// MapLibre's styleimagemissing), so there's no sprite sheet to extend and
|
||||
// map.js adds them with every style), so there's no sprite sheet to extend and
|
||||
// they work offline alike.
|
||||
// ponytail: same substring trick as NATIONAL_PARK, a word per language.
|
||||
const STRICT_RESERVE = ["any", ...["rezerv", "reserv", "wilderness", "wildnis", "pamiatk", "památk", "monument", "denkmal"].map((word) => ["in", word, ["coalesce", ["get", "class"], ""]])];
|
||||
@@ -230,6 +230,8 @@ const PATTERNS = {
|
||||
military: { day: "rgba(192, 57, 43, 0.4)", night: "rgba(192, 57, 43, 0.35)" },
|
||||
};
|
||||
|
||||
export const PATTERN_IDS = Object.keys(PATTERNS).flatMap((name) => [`maptool-pattern-${name}`, `maptool-pattern-${name}-night`]);
|
||||
|
||||
// "maptool-pattern-<name>[-night]" → { width, height, data } for
|
||||
// map.addImage, drawn on a canvas at 2× (pixelRatio 2); null for other ids.
|
||||
export function patternImage(id) {
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user