NPS park maps overlay: park points and map footprints, a park search, a park popup listing its Harpers Ferry Center maps with local PDF/GeoTIFF links, and the 289 georeferenced ones laid over the base map with an opacity slider; data from data/nps served by /api/nps and /nps/{overlay,geotiff,pdf}
This commit is contained in:
1 parent
ff5924faf1
commit
3fc8c75c60
8 files changed
+421
-6
No files matched your search
@@ -2885,3 +2885,111 @@ body.side-pinned:has(.basemap-panel:not([hidden]), .gpx-panel:not([hidden])) .pr
|
||||
font-weight: normal;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
/* NPS park maps overlay (nps.js): park search, the shown map's opacity, and
|
||||
the park popup listing its maps. */
|
||||
.nps-panel {
|
||||
margin: 6px 0 0 26px;
|
||||
}
|
||||
|
||||
.nps-panel__search {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 5px 8px;
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
.nps-panel__results {
|
||||
list-style: none;
|
||||
margin: 2px 0 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.nps-panel__results button {
|
||||
width: 100%;
|
||||
padding: 5px 6px;
|
||||
border: none;
|
||||
background: none;
|
||||
font: inherit;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.nps-panel__results button:hover {
|
||||
background: var(--accent-bg);
|
||||
}
|
||||
|
||||
.nps-panel__results small,
|
||||
.nps-panel__none {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.nps-panel__shown {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 4px 8px;
|
||||
margin-top: 6px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.nps-panel__shown[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.nps-panel__shown-title {
|
||||
flex-basis: 100%;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.nps-panel__hide {
|
||||
border: none;
|
||||
background: none;
|
||||
color: #a33;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.nps-popup ul {
|
||||
list-style: none;
|
||||
margin: 6px 0 0;
|
||||
padding: 0;
|
||||
max-height: 260px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.nps-popup li {
|
||||
padding: 4px 0;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
|
||||
.nps-popup__title {
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.nps-popup__links {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 4px 10px;
|
||||
margin-top: 2px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.nps-popup__links a {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.nps-popup__links button {
|
||||
padding: 2px 8px;
|
||||
border: 1px solid var(--accent);
|
||||
border-radius: 4px;
|
||||
background: #fff;
|
||||
color: var(--accent);
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.nps-popup__links button.active {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { ShadowControl } from "/js/shadow.js";
|
||||
import { ContourControl } from "/js/contour.js";
|
||||
import { HikingTrailsControl } from "/js/hiking_trails.js";
|
||||
import { LiveControl } from "/js/live.js";
|
||||
import { NpsControl } from "/js/nps.js";
|
||||
|
||||
const ICON_IMAGE_SIZE = 48; // rasterized once at this size, rendered much smaller by icon-size below
|
||||
const ICON_SIZE = 0.3; // 48 * 0.3 = ~14px, sized to sit inside the marker badge below
|
||||
@@ -188,6 +189,7 @@ export class OverlaysControl {
|
||||
this._contour = new ContourControl(map);
|
||||
this._hikingTrails = new HikingTrailsControl(map);
|
||||
this._live = new LiveControl(map);
|
||||
this._nps = new NpsControl(map);
|
||||
this._wikiEnabled = false; // the checkbox's own state
|
||||
this._wikiMoveHandler = null; // the moveend listener driving its live refetch, while enabled
|
||||
this._wikiRequestSeq = 0; // ignores an in-flight refetch a newer one has already superseded
|
||||
@@ -223,8 +225,9 @@ export class OverlaysControl {
|
||||
<li class="overlays-panel__item"></li>
|
||||
<li class="overlays-panel__item overlays-panel__item--expandable"></li>
|
||||
<li class="overlays-panel__item"></li>
|
||||
<li class="overlays-panel__item"></li>
|
||||
`;
|
||||
const [hillshadeItem, wikiItem, radarItem, shadowItem, contourItem, hikingTrailsItem, liveItem] = list.children;
|
||||
const [hillshadeItem, wikiItem, radarItem, shadowItem, contourItem, hikingTrailsItem, liveItem, npsItem] = list.children;
|
||||
this._hillshade.mount(hillshadeItem);
|
||||
this._mountWiki(wikiItem);
|
||||
this._radar.mount(radarItem);
|
||||
@@ -232,6 +235,7 @@ export class OverlaysControl {
|
||||
this._contour.mount(contourItem);
|
||||
this._hikingTrails.mount(hikingTrailsItem);
|
||||
this._live.mount(liveItem);
|
||||
this._nps.mount(npsItem);
|
||||
|
||||
onlyOneSidePanelOpen(this._panel, () => this._icon.classList.remove("active"));
|
||||
|
||||
|
||||
Reference in new issue
Block a user