From 3fc8c75c6007b54326efaff30df9b06073db59d9 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Thu, 19 Nov 2026 10:52:17 +0100 Subject: [PATCH] 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} --- .gitignore | 4 + TODO.adoc | 11 +- app.rb | 24 ++++ public/css/style.css | 108 ++++++++++++++++++ public/js/nps.js | 248 ++++++++++++++++++++++++++++++++++++++++++ public/js/overlays.js | 6 +- test/test_app.rb | 25 +++++ views/importmap.erb | 1 + 8 files changed, 421 insertions(+), 6 deletions(-) create mode 100644 public/js/nps.js diff --git a/.gitignore b/.gitignore index f24026a..49162b7 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,10 @@ # Offline routing: Valhalla tiles and its status files (lib/maptool/routing.rb) /data/routing/ +# NPS park maps (index, overlays, GeoTIFFs, PDFs) for the NPS overlay, +# built outside this repo (~/Projects/NPS_Maps) +/data/nps + # Garmin LiveTrack session IDs received via webhook /data/livetrack.json diff --git a/TODO.adoc b/TODO.adoc index e623cb8..7654e80 100644 --- a/TODO.adoc +++ b/TODO.adoc @@ -98,11 +98,12 @@ Prototype outside this repo in `~/Projects/NPS_Maps/` (`nps-maps.py` + a standal finder `index.html`); background in `docs/nps-resources.adoc` ("The NPS mobile app's offline downloads"). Data and features move here once settled. -* [ ] NPS brochure maps as an overlay layer — 289 georeferenced Harpers Ferry Center park maps - (`brochure-maps.json`: pixel size + geoTransform + proj4) rendered from the NPGallery PDFs; - in MapLibre each is just an `image` source with four corners, plus an opacity slider -* [ ] Park finder panel — every park with its maps from the full NPGallery catalog (963 maps, - PDF/JPG/Illustrator ZIP each), jump to park, toggle its brochure map +* [x] NPS brochure maps as an overlay layer — "NPS park maps" in Overlays (`public/js/nps.js`): + park points and map footprints, a park search, a park's popup lists its maps; a georeferenced + one (289 of them) goes over the base map as an `image` source with four corners, with an + opacity slider. Data in `data/nps/` (`nps.json` index from `nps-maps.py web`, `overlay/` + JPEGs, `geotiff/`, `pdf/` — all 947 PDFs), served by `/api/nps` and `/nps/…` +* [x] Park finder — the overlay's park search + the popup's links (local PDF/GeoTIFF, NPS JPG) * [ ] NPS campgrounds (and alerts) as points with our NPS icons — via the home NPS MCP server, no API key of our own (its API key only lives in home-gitops' Gitea secrets) * [ ] Export brochure maps as MBTiles (`gdal_translate -of MBTILES` + `gdaladdo`) for Locus / diff --git a/app.rb b/app.rb index a989e40..29c21c5 100644 --- a/app.rb +++ b/app.rb @@ -1011,6 +1011,30 @@ module Maptool json(type: "FeatureCollection", features: features) end + # NPS park maps (the "NPS park maps" overlay, public/js/nps.js): Harpers + # Ferry Center brochure maps, public domain. data/nps/ holds nps.json (the + # index: parks with bboxes, maps with download links and, for the 289 + # georeferenced ones, overlay corners) plus overlay/ (JPEGs), geotiff/ and + # pdf/ — built outside this app by ~/Projects/NPS_Maps/nps-maps.py. Absent + # = the overlay stays disabled. + NPS_DIR = File.join(DATA_DIR, "nps") + + get "/api/nps" do + path = File.join(NPS_DIR, "nps.json") + halt 404, json(error: "no NPS data") unless File.exist?(path) + + cache_control :public, max_age: 3600 + send_file path, type: "application/json" + end + + get %r{/nps/(overlay|geotiff|pdf)/([\w.@-]+)} do |kind, name| + path = File.join(NPS_DIR, kind, name) + halt 404 unless File.file?(path) + + cache_control :public, max_age: 86_400 + send_file path, disposition: kind == "geotiff" ? "attachment" : "inline" + end + get "/api/live" do require_login! now = Time.now.to_f diff --git a/public/css/style.css b/public/css/style.css index 3140177..ecdedb6 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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; +} diff --git a/public/js/nps.js b/public/js/nps.js new file mode 100644 index 0000000..d5a0820 --- /dev/null +++ b/public/js/nps.js @@ -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 = ` + + + `; + 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) => `
  • `).join("") + : `
  • No park found
  • `; + } + + _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 ? `PDF` : m.dl.pdf && `PDF ↗`, + m.tif && `GeoTIFF`, + m.dl.jpg && `JPG ↗`, + ].filter(Boolean); + return `
  • +
    ${escapeHtml(m.title)}
    + +
  • `; + }; + const element = document.createElement("div"); + element.className = "nps-popup"; + element.innerHTML = `${escapeHtml(park.name)}`; + 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 }); + } + } +} diff --git a/public/js/overlays.js b/public/js/overlays.js index 8130a1c..fa52968 100644 --- a/public/js/overlays.js +++ b/public/js/overlays.js @@ -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 {
  • +
  • `; - 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")); diff --git a/test/test_app.rb b/test/test_app.rb index ecbe7a8..8ac2e4e 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -108,6 +108,31 @@ class AppTest < Minitest::Test assert_equal 401, request.get("/api/gnss/position").status end + def test_nps_index_404s_without_data + assert_equal 404, request.get("/api/nps").status + end + + # The NPS overlay's index and files, public; only overlay/, geotiff/ and + # pdf/ under data/nps/ are reachable. + def test_nps_serves_the_index_and_its_files_only + dir = Maptool::App::NPS_DIR + FileUtils.mkdir_p(File.join(dir, "pdf")) + File.write(File.join(dir, "nps.json"), '{"parks":{},"maps":[]}') + File.write(File.join(dir, "pdf", "zion_abc.pdf"), "%PDF") + File.write(File.join(dir, "secret.txt"), "no") + + response = request.get("/api/nps") + assert_equal 200, response.status + assert_equal({ "parks" => {}, "maps" => [] }, json_body(response)) + assert_equal "%PDF", request.get("/nps/pdf/zion_abc.pdf").body + assert_equal 404, request.get("/nps/pdf/missing.pdf").status + assert_equal 404, request.get("/nps/secret.txt").status + assert_equal 404, request.get("/nps/pdf/..%2Fsecret.txt").status + assert_equal 404, request.get("/nps/other/zion_abc.pdf").status + ensure + FileUtils.rm_rf(Maptool::App::NPS_DIR) + end + def test_live_requires_login assert_equal 401, request.get("/api/live").status end diff --git a/views/importmap.erb b/views/importmap.erb index 74ab246..95b1a75 100644 --- a/views/importmap.erb +++ b/views/importmap.erb @@ -24,6 +24,7 @@ "/js/map_themes.js": "/js/map_themes.js?v=<%= ASSET_VERSION %>", "/js/measure.js": "/js/measure.js?v=<%= ASSET_VERSION %>", "/js/notes.js": "/js/notes.js?v=<%= ASSET_VERSION %>", + "/js/nps.js": "/js/nps.js?v=<%= ASSET_VERSION %>", "/js/debug.js": "/js/debug.js?v=<%= ASSET_VERSION %>", "/js/offline_data.js": "/js/offline_data.js?v=<%= ASSET_VERSION %>", "/js/overlays.js": "/js/overlays.js?v=<%= ASSET_VERSION %>",