249 lines
10 KiB
JavaScript
249 lines
10 KiB
JavaScript
import { Popup } from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
|
import { NPS_SYMBOLS } from "/js/icons.js";
|
|
import { escapeHtml } from "/js/gpx.js";
|
|
|
|
// NPS park maps: the National Park Service's own (Harpers Ferry Center)
|
|
// brochure maps, public domain. An Overlays item. app.rb serves the index
|
|
// (/api/nps: every park with maps, every map with its download links) and
|
|
// the files under /nps/. The 289 georeferenced maps can be laid over the
|
|
// base map: each is an image source with four corners (nps.json's
|
|
// `corners`, computed from the map's geoTransform when it was built).
|
|
|
|
const SOURCE_PARKS = "nps-parks";
|
|
const SOURCE_FOOTPRINTS = "nps-footprints";
|
|
const SOURCE_OVERLAY = "nps-overlay";
|
|
const PARK_COLOR = "#546619";
|
|
const FOOTPRINT_COLOR = "#a65523";
|
|
const MAX_RESULTS = 8;
|
|
|
|
export class NpsControl {
|
|
constructor(map) {
|
|
this._map = map;
|
|
this._enabled = false;
|
|
this._data = null;
|
|
this._overlay = null; // the map shown over the base map, if any
|
|
map.on("style.load", () => {
|
|
if (!this._enabled) return;
|
|
this._addLayers();
|
|
if (this._overlay) this._showOverlay(this._overlay, { fit: false });
|
|
});
|
|
}
|
|
|
|
mount(li) {
|
|
li.innerHTML = `
|
|
<label>
|
|
<input type="checkbox" class="overlays-panel__toggle">
|
|
<span class="overlays-panel__icon">${NPS_SYMBOLS.maps}</span>
|
|
<span>NPS park maps</span>
|
|
</label>
|
|
<div class="nps-panel" hidden>
|
|
<input type="search" class="nps-panel__search" placeholder="Find a park">
|
|
<ul class="nps-panel__results"></ul>
|
|
<div class="nps-panel__shown" hidden>
|
|
<span class="nps-panel__shown-title"></span>
|
|
<label>Opacity <input type="range" class="nps-panel__opacity" min="0" max="1" step="0.05" value="0.9"></label>
|
|
<button type="button" class="nps-panel__hide" title="Remove this map">✕</button>
|
|
</div>
|
|
</div>
|
|
`;
|
|
this._li = li;
|
|
this._toggle = li.querySelector(".overlays-panel__toggle");
|
|
this._panel = li.querySelector(".nps-panel");
|
|
this._search = li.querySelector(".nps-panel__search");
|
|
this._results = li.querySelector(".nps-panel__results");
|
|
this._shown = li.querySelector(".nps-panel__shown");
|
|
this._opacity = li.querySelector(".nps-panel__opacity");
|
|
this._toggle.addEventListener("change", () => (this._toggle.checked ? this._enable() : this._disable()));
|
|
this._search.addEventListener("input", () => this._renderResults());
|
|
this._results.addEventListener("click", (event) => {
|
|
const code = event.target.closest("[data-park]")?.dataset.park;
|
|
if (code) this._goToPark(code);
|
|
});
|
|
this._opacity.addEventListener("input", () => {
|
|
if (this._map.getLayer(SOURCE_OVERLAY)) this._map.setPaintProperty(SOURCE_OVERLAY, "raster-opacity", +this._opacity.value);
|
|
});
|
|
li.querySelector(".nps-panel__hide").addEventListener("click", () => this._showOverlay(null));
|
|
}
|
|
|
|
async _enable() {
|
|
this._enabled = true;
|
|
this._panel.hidden = false;
|
|
if (!this._data) {
|
|
try {
|
|
const response = await fetch("/api/nps");
|
|
if (!response.ok) throw new Error(response.status);
|
|
this._index(await response.json());
|
|
} catch (err) {
|
|
this._toggle.checked = false;
|
|
this._toggle.disabled = true;
|
|
this._panel.hidden = true;
|
|
this._li.title = "No NPS map data on this server";
|
|
this._enabled = false;
|
|
return;
|
|
}
|
|
}
|
|
this._addLayers();
|
|
this._renderResults();
|
|
}
|
|
|
|
_disable() {
|
|
this._enabled = false;
|
|
this._panel.hidden = true;
|
|
this._showOverlay(null);
|
|
for (const id of ["nps-footprints", "nps-park-labels", "nps-parks"]) if (this._map.getLayer(id)) this._map.removeLayer(id);
|
|
for (const id of [SOURCE_PARKS, SOURCE_FOOTPRINTS]) if (this._map.getSource(id)) this._map.removeSource(id);
|
|
}
|
|
|
|
// Parks that have maps, each with its maps (georeferenced ones first).
|
|
_index(data) {
|
|
const byPark = new Map();
|
|
for (const m of data.maps) for (const code of m.parks) byPark.set(code, [...(byPark.get(code) || []), m]);
|
|
this._parks = [...byPark.keys()]
|
|
.filter((code) => data.parks[code])
|
|
.map((code) => ({
|
|
code,
|
|
...data.parks[code],
|
|
maps: byPark.get(code).sort((a, b) => !!b.corners - !!a.corners || a.title.localeCompare(b.title)),
|
|
}))
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
this._parkByCode = new Map(this._parks.map((p) => [p.code, p]));
|
|
this._data = data;
|
|
}
|
|
|
|
_addLayers() {
|
|
if (this._map.getSource(SOURCE_PARKS)) return;
|
|
this._map.addSource(SOURCE_PARKS, {
|
|
type: "geojson",
|
|
data: {
|
|
type: "FeatureCollection",
|
|
features: this._parks.map((p) => {
|
|
const [w, s, e, n] = p.bbox;
|
|
return {
|
|
type: "Feature",
|
|
properties: { code: p.code, name: p.name },
|
|
geometry: { type: "Point", coordinates: [(w + e) / 2, (s + n) / 2] },
|
|
};
|
|
}),
|
|
},
|
|
});
|
|
this._map.addSource(SOURCE_FOOTPRINTS, {
|
|
type: "geojson",
|
|
data: {
|
|
type: "FeatureCollection",
|
|
features: this._data.maps
|
|
.filter((m) => m.corners)
|
|
.map((m) => ({
|
|
type: "Feature",
|
|
properties: { park: m.parks[0] },
|
|
geometry: { type: "Polygon", coordinates: [[...m.corners, m.corners[0]]] },
|
|
})),
|
|
},
|
|
});
|
|
this._map.addLayer({
|
|
id: "nps-footprints",
|
|
type: "line",
|
|
source: SOURCE_FOOTPRINTS,
|
|
minzoom: 5,
|
|
paint: { "line-color": FOOTPRINT_COLOR, "line-width": 1.2, "line-dasharray": [3, 2] },
|
|
});
|
|
this._map.addLayer({
|
|
id: "nps-parks",
|
|
type: "circle",
|
|
source: SOURCE_PARKS,
|
|
paint: {
|
|
"circle-radius": ["interpolate", ["linear"], ["zoom"], 3, 4, 8, 7],
|
|
"circle-color": PARK_COLOR,
|
|
"circle-stroke-color": "#fff",
|
|
"circle-stroke-width": 1.5,
|
|
},
|
|
});
|
|
this._map.addLayer({
|
|
id: "nps-park-labels",
|
|
type: "symbol",
|
|
source: SOURCE_PARKS,
|
|
minzoom: 5,
|
|
layout: { "text-field": ["get", "name"], "text-font": ["Noto Sans Regular"], "text-size": 12, "text-offset": [0, 1.1], "text-anchor": "top" },
|
|
paint: { "text-color": PARK_COLOR, "text-halo-color": "#fff", "text-halo-width": 1.5 },
|
|
});
|
|
if (!this._interactive) {
|
|
this._interactive = true;
|
|
const open = (event) => this._openPark(event.features[0].properties.code || event.features[0].properties.park, event.lngLat);
|
|
for (const id of ["nps-parks", "nps-footprints"]) {
|
|
this._map.on("click", id, open);
|
|
this._map.on("mouseenter", id, () => (this._map.getCanvas().style.cursor = "pointer"));
|
|
this._map.on("mouseleave", id, () => (this._map.getCanvas().style.cursor = ""));
|
|
}
|
|
}
|
|
}
|
|
|
|
_renderResults() {
|
|
const query = this._search.value.trim().toLowerCase();
|
|
if (!query) {
|
|
this._results.innerHTML = "";
|
|
return;
|
|
}
|
|
const hits = this._parks.filter((p) => p.name.toLowerCase().includes(query) || p.code === query).slice(0, MAX_RESULTS);
|
|
this._results.innerHTML = hits.length
|
|
? hits.map((p) => `<li><button type="button" data-park="${p.code}">${escapeHtml(p.name)} <small>${p.maps.length}</small></button></li>`).join("")
|
|
: `<li class="nps-panel__none">No park found</li>`;
|
|
}
|
|
|
|
_goToPark(code) {
|
|
const park = this._parkByCode.get(code);
|
|
const [w, s, e, n] = park.bbox;
|
|
// Listening first: without animation (reduced motion) fitBounds fires
|
|
// moveend before it returns.
|
|
this._map.once("moveend", () => this._openPark(code, [(w + e) / 2, (s + n) / 2]));
|
|
this._map.fitBounds([[w, s], [e, n]], { padding: 40, maxZoom: 12 });
|
|
}
|
|
|
|
// A popup listing the park's maps: lay a georeferenced one over the map,
|
|
// or open/download it.
|
|
_openPark(code, lngLat) {
|
|
const park = this._parkByCode.get(code);
|
|
if (!park) return;
|
|
const item = (m) => {
|
|
const links = [
|
|
m.pdf ? `<a href="/nps/${m.pdf}" target="_blank">PDF</a>` : m.dl.pdf && `<a href="${m.dl.pdf}" target="_blank" rel="noopener">PDF ↗</a>`,
|
|
m.tif && `<a href="/nps/${m.tif}" download>GeoTIFF</a>`,
|
|
m.dl.jpg && `<a href="${m.dl.jpg}" target="_blank" rel="noopener">JPG ↗</a>`,
|
|
].filter(Boolean);
|
|
return `<li>
|
|
<div class="nps-popup__title">${escapeHtml(m.title)}</div>
|
|
<div class="nps-popup__links">${m.corners ? `<button type="button" data-map="${m.id}" class="${this._overlay?.id === m.id ? "active" : ""}">On map</button>` : ""}${links.join("")}</div>
|
|
</li>`;
|
|
};
|
|
const element = document.createElement("div");
|
|
element.className = "nps-popup";
|
|
element.innerHTML = `<strong>${escapeHtml(park.name)}</strong><ul>${park.maps.map(item).join("")}</ul>`;
|
|
element.addEventListener("click", (event) => {
|
|
const id = event.target.closest("button[data-map]")?.dataset.map;
|
|
if (!id) return;
|
|
const m = park.maps.find((x) => x.id === id);
|
|
this._showOverlay(this._overlay?.id === id ? null : m);
|
|
element.querySelectorAll("button[data-map]").forEach((b) => b.classList.toggle("active", b.dataset.map === this._overlay?.id));
|
|
});
|
|
new Popup({ maxWidth: "300px" }).setLngLat(lngLat).setDOMContent(element).addTo(this._map);
|
|
}
|
|
|
|
// One brochure map at a time over the base map (null = none).
|
|
_showOverlay(m, { fit = true } = {}) {
|
|
if (this._map.getLayer(SOURCE_OVERLAY)) this._map.removeLayer(SOURCE_OVERLAY);
|
|
if (this._map.getSource(SOURCE_OVERLAY)) this._map.removeSource(SOURCE_OVERLAY);
|
|
this._overlay = m;
|
|
this._shown.hidden = !m;
|
|
if (!m) return;
|
|
this._shown.querySelector(".nps-panel__shown-title").textContent = m.title;
|
|
this._map.addSource(SOURCE_OVERLAY, { type: "image", url: `/nps/overlay/${m.id}.jpg`, coordinates: m.corners });
|
|
this._map.addLayer(
|
|
{ id: SOURCE_OVERLAY, type: "raster", source: SOURCE_OVERLAY, paint: { "raster-opacity": +this._opacity.value } },
|
|
this._map.getLayer("nps-footprints") ? "nps-footprints" : undefined,
|
|
);
|
|
if (fit) {
|
|
const xs = m.corners.map((c) => c[0]);
|
|
const ys = m.corners.map((c) => c[1]);
|
|
this._map.fitBounds([[Math.min(...xs), Math.min(...ys)], [Math.max(...xs), Math.max(...ys)]], { padding: 20 });
|
|
}
|
|
}
|
|
}
|