From adb0adbd94f85abd54e241a15e029edc8738f5c5 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:11:01 -0700 Subject: [PATCH] The map draws any PMTiles file as an overlay, in any sub-directory (#962) Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically. Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config. PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths. --- app/src/api.js | 22 +- app/src/components/Map.js | 71 +++-- app/src/components/MapPreview.test.js | 37 ++- app/src/components/MapTables.test.js | 78 +++++ app/src/components/MapViewer.js | 271 +++++++++++++----- app/src/components/MapViewerOverlays.test.js | 279 ++++++++++++++++++ app/src/components/mapOverlays.js | 247 ++++++++++++++++ app/src/components/mapOverlays.test.js | 285 +++++++++++++++++++ app/src/components/mapPoiPopup.js | 2 +- conftest.py | 1 + modules/map/api.py | 19 +- modules/map/config.py | 60 ++++ modules/map/conftest.py | 51 ++++ modules/map/lib.py | 102 +++++-- modules/map/pmtiles.py | 195 +++++++++++++ modules/map/schema.py | 6 + modules/map/search.py | 37 ++- modules/map/static/embed.html | 16 +- modules/map/static/index.html | 29 +- modules/map/test/test_api.py | 107 +++++++ modules/map/test/test_lib.py | 91 ++++++ modules/map/test/test_pmtiles.py | 137 +++++++++ modules/map/test/test_search.py | 97 ++++++- wrolpi/common.py | 15 + wrolpi/contexts.py | 7 + wrolpi/scripts/build_map_search.py | 22 +- wrolpi/sync/lib.py | 1 + 27 files changed, 2115 insertions(+), 170 deletions(-) create mode 100644 app/src/components/MapViewerOverlays.test.js create mode 100644 app/src/components/mapOverlays.js create mode 100644 app/src/components/mapOverlays.test.js create mode 100644 modules/map/config.py create mode 100644 modules/map/conftest.py create mode 100644 modules/map/pmtiles.py create mode 100644 modules/map/test/test_pmtiles.py diff --git a/app/src/api.js b/app/src/api.js index f6e69e75..6130bfbb 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1679,14 +1679,28 @@ export async function getMapFiles() { } } -export async function deleteMapFile(filename) { - const response = await apiDelete(`${API_URI}/map/files/${encodeURIComponent(filename)}`); +// A map file is named by its path within the map directory; it may be in a sub-directory. +function encodeMapPath(mapPath) { + return String(mapPath).split('/').map(encodeURIComponent).join('/'); +} + +export async function deleteMapFile(mapPath) { + const response = await apiDelete(`${API_URI}/map/files/${encodeMapPath(mapPath)}`); if (!response.ok) { const message = await getErrorMessage(response, 'Could not delete map file'); toast({type: 'error', title: 'Map Error', description: message, time: 5000}); } } +// Show a map file on the map, or ignore it. +export async function setMapFileVisible(mapPath, visible) { + const response = await apiPost(`${API_URI}/map/files/visible`, {map_path: mapPath, visible}); + if (!response.ok) { + const message = await getErrorMessage(response, 'Could not change the map file'); + toast({type: 'error', title: 'Map Error', description: message, time: 5000}); + } +} + export async function fetchMapSubscriptions() { const response = await apiGet(`${API_URI}/map/subscribe`); if (response.ok) { @@ -1793,8 +1807,8 @@ export async function searchEstimateMap(search_str) { return {mapPlaces: data.map_places || 0}; } -export async function rebuildMapSearchIndex(filename) { - const response = await apiPost(`${API_URI}/map/search/rebuild/${encodeURIComponent(filename)}`); +export async function rebuildMapSearchIndex(mapPath) { + const response = await apiPost(`${API_URI}/map/search/rebuild/${encodeMapPath(mapPath)}`); if (!response.ok) { const message = await getErrorMessage(response, 'Could not rebuild search index'); toast({type: 'error', title: 'Map Error', description: message, time: 5000}); diff --git a/app/src/components/Map.js b/app/src/components/Map.js index 46b2554d..6a730d5f 100644 --- a/app/src/components/Map.js +++ b/app/src/components/Map.js @@ -12,9 +12,9 @@ import { import {Route, Routes, useNavigate} from "react-router"; import { deleteMapFile, deleteMapPin, fetchMapSubscriptions, getMapFiles, getMapPins, - mapSubscribe, mapUnsubscribe, rebuildMapSearchIndex, updateMapPin + mapSubscribe, mapUnsubscribe, rebuildMapSearchIndex, setMapFileVisible, updateMapPin } from "../api"; -import {Button, Divider, Group, Header, Icon, Modal, Table, TextInput} from "./ui"; +import {Button, Divider, Group, Header, Icon, Modal, Table, TextInput, Toggle} from "./ui"; import {SortableTable} from "./SortableTable"; import {Media, StatusContext} from "../contexts/contexts"; import {MAP_VIEWER_URI} from "./Vars"; @@ -24,29 +24,29 @@ import {solarCalculatorPath} from "./calculators/solar"; import maplibregl from "maplibre-gl"; import {Protocol} from "pmtiles"; import layers from "protomaps-themes-base"; +import {planMapFiles, pmtilesUrl} from "./mapOverlays"; // Ensure PMTiles protocol is registered (may already be registered by MapViewer). try { maplibregl.addProtocol("pmtiles", new Protocol().tile); } catch (e) { /* already registered */ } const MAP_OVERVIEW_URL = "/blobs/map-overview.pmtiles"; -// Detect full planet files: "planet-20260329.pmtiles" or bare date like "20260329.pmtiles". -function isPlanetFile(name) { - if (name.startsWith("planet-")) return true; - return /^\d{8}\.pmtiles$/.test(name); -} +// How the map draws a file (the API's `kind`). +const MAP_FILE_KINDS = { + basemap: 'Basemap', + overlay: 'Overlay', + terrain: 'Terrain', + unknown: 'Unreadable', +}; // Resolve the best available base map source for preview maps. // Checks for a user's planet file first, falls back to the overview blob. async function getPreviewMapSource() { try { const data = await getMapFiles(); - if (data?.files) { - for (const f of data.files) { - if (isPlanetFile(f.name)) { - return {type: 'vector', url: `pmtiles:///media/map/${f.name}`}; - } - } + const {planet} = planMapFiles(data?.files); + if (planet) { + return {type: 'vector', url: pmtilesUrl(planet.path)}; } } catch (e) { /* ignore */ } return {type: 'vector', url: `pmtiles://${MAP_OVERVIEW_URL}`}; @@ -343,30 +343,47 @@ function ManageMap() { // --- Files table --- const fileHeaders = [ - {key: 'name', text: 'File', sortBy: i => i.name.toLowerCase()}, + {key: 'name', text: 'File', sortBy: i => i.map_path.toLowerCase()}, + {key: 'kind', text: 'Type', sortBy: i => i.kind || ''}, {key: 'size', text: 'Size', sortBy: i => i.size || 0}, {key: 'search', text: 'Search', sortBy: null}, {key: 'actions', text: '', sortBy: null}, + {key: 'visible', text: 'Visible', sortBy: i => (i.visible === false ? 0 : 1)}, ]; - const handleBuildSearch = async (filename) => { - await rebuildMapSearchIndex(filename); + const handleBuildSearch = async (mapPath) => { + await rebuildMapSearchIndex(mapPath); await fetchFiles(); }; + const handleVisible = async (file, visible) => { + try { + await setMapFileVisible(file.map_path, visible); + } finally { + await fetchFiles(); + } + }; + const fileRowFunc = (file) => { - return