diff --git a/app/package-lock.json b/app/package-lock.json index 59dff7fa..43e819bd 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -14,6 +14,7 @@ "@mantine/core": "^8.3.18", "@mantine/hooks": "^8.3.18", "@mantine/notifications": "^8.3.18", + "@maplibre/maplibre-gl-style-spec": "^24.8.5", "@tabler/icons-react": "^3.50.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^14.6.8", diff --git a/app/package.json b/app/package.json index 22790335..a22618b8 100644 --- a/app/package.json +++ b/app/package.json @@ -9,6 +9,7 @@ "@mantine/core": "^8.3.18", "@mantine/hooks": "^8.3.18", "@mantine/notifications": "^8.3.18", + "@maplibre/maplibre-gl-style-spec": "^24.8.5", "@tabler/icons-react": "^3.50.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^14.6.8", diff --git a/app/src/components/Map.js b/app/src/components/Map.js index 6a730d5f..5556973b 100644 --- a/app/src/components/Map.js +++ b/app/src/components/Map.js @@ -372,7 +372,11 @@ function ManageMap() { {file.title && file.title !== file.name.replace(/\.pmtiles$/, '') &&
{file.title}
} - {MAP_FILE_KINDS[file.kind] || ''} + + {MAP_FILE_KINDS[file.kind] || ''} + {/* A style beside the file says how it is drawn. */} + {file.kind === 'overlay' && file.style_path ? ' (styled)' : ''} + {humanFileSize(file.size)} {/* A search index is built from a basemap's places; other files have none. */} diff --git a/app/src/components/MapViewer.js b/app/src/components/MapViewer.js index 2e04fe58..35f6c998 100644 --- a/app/src/components/MapViewer.js +++ b/app/src/components/MapViewer.js @@ -18,12 +18,13 @@ import { isOverlayEnabled, isOverlayLayerEnabled, isOverlayLayerId, - isRasterOverlay, + isStyledOverlay, loadOverlayState, mediaUrl, overlayColor, overlayLabel, overlayPopupHtml, + overlayToggleLayers, planMapFiles, pmtilesUrl, saveOverlayState, @@ -31,6 +32,7 @@ import { setOverlayEnabled, setOverlayLayerEnabled, } from "./mapOverlays"; +import {loadOverlayStyles, overlaySpriteId, overlaySpriteUrl} from "./mapOverlayStyle"; // Layer groups matching the standalone viewer. const LAYER_GROUPS = { @@ -209,10 +211,16 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " addOverlaysToStyle(styleSources, allLayers, overlays, overlayState); + // An overlay drawn with its own style may bring its own icons: a second sprite beside the + // basemap's, whose images are named `:`. + const basemapSprite = `${window.location.origin}/map-assets/sprites/${sprite}`; + const overlaySprites = overlays.filter(f => isStyledOverlay(f) && f.has_sprite) + .map(f => ({id: overlaySpriteId(f), url: overlaySpriteUrl(f)})); + return { version: 8, glyphs: "/map-assets/fonts/{fontstack}/{range}.pbf", - sprite: `${window.location.origin}/map-assets/sprites/${sprite}`, + sprite: overlaySprites.length ? [{id: "default", url: basemapSprite}, ...overlaySprites] : basemapSprite, sources: styleSources, layers: allLayers, }; @@ -259,7 +267,8 @@ function applyGroupVisibility(map, visibility) { function OverlayControl({file, label, state, onToggleFile, onToggleLayer, onToggleAllLayers}) { const [expanded, setExpanded] = useState(false); const enabled = isOverlayEnabled(state, file); - const vectorLayers = isRasterOverlay(file) ? [] : (file.vector_layers || []); + const vectorLayers = overlayToggleLayers(file); + const styled = isStyledOverlay(file); const zooms = Number.isFinite(file.min_zoom) && Number.isFinite(file.max_zoom) ? `z${file.min_zoom}–${file.max_zoom}` : null; const title = [file.description, file.path].filter(Boolean).join("\n"); @@ -288,15 +297,19 @@ function OverlayControl({file, label, state, onToggleFile, onToggleLayer, onTogg onToggleAllLayers(file, true)}>All onToggleAllLayers(file, false)}>None + {file.styleSkipped > 0 &&
+ {file.styleSkipped} style {file.styleSkipped === 1 ? "layer" : "layers"} skipped +
} {vectorLayers.map(layer =>
- + }}/>} {layer.id} } checked={isOverlayLayerEnabled(state, file, layer.id)} @@ -678,7 +691,8 @@ export default function MapViewer() { const data = await getMapFiles(); if (data?.files) { const {planet, regions, terrain, overlays: foundOverlays} = planMapFiles(data.files); - overlayFiles = foundOverlays; + // An overlay which came with a style is drawn with it. + overlayFiles = await loadOverlayStyles(foundOverlays); if (terrain) { hasTerrain = true; terrainPath = terrain.path; diff --git a/app/src/components/MapViewerOverlays.test.js b/app/src/components/MapViewerOverlays.test.js index e522818b..07691f84 100644 --- a/app/src/components/MapViewerOverlays.test.js +++ b/app/src/components/MapViewerOverlays.test.js @@ -277,3 +277,89 @@ describe('the basemap layer groups', () => { expect(map.visibility(WATER)).toBe('none'); }); }); + +describe('an overlay with a style beside it', () => { + const STYLED = {...FEATURES, style_path: 'map/extras/style.json', has_sprite: true}; + const STYLE = { + version: 8, + sources: {features: {type: 'vector', url: 'https://example.com/features.json'}}, + layers: [ + {id: 'background', type: 'background'}, + {id: 'lines', type: 'line', source: 'features', 'source-layer': 'power_lines'}, + { + id: 'towers', type: 'symbol', source: 'features', 'source-layer': 'towers', + layout: {'icon-image': 'tower', 'text-field': ['get', 'name'], 'text-font': ['Roboto Bold']}, + }, + ], + }; + const STYLED_LINES = 'overlay:extras/features.pmtiles:power_lines:lines'; + const STYLED_TOWERS = 'overlay:extras/features.pmtiles:towers:towers'; + + // The style is fetched from the media directory; the overview blob is asked for with HEAD. + const serveStyle = (style) => { + global.fetch = jest.fn(async (url) => { + if (String(url).endsWith('style.json')) { + if (!style) return {ok: false}; + return {ok: true, json: async () => style}; + } + return {ok: true}; + }); + }; + + test('is drawn with that style and its own sprite', async () => { + serveStyle(STYLE); + const map = await renderViewer([PLANET, STYLED]); + expect(global.fetch).toHaveBeenCalledWith('/media/map/extras/style.json'); + + const overlayLayers = map.style.layers.filter(l => l.source === 'overlay:extras/features.pmtiles'); + expect(overlayLayers.map(l => l.id)).toEqual([STYLED_LINES, STYLED_TOWERS]); + expect(map.getLayer(STYLED_TOWERS).layout['icon-image']).toBe('overlay:extras/features.pmtiles:tower'); + expect(map.getLayer(STYLED_TOWERS).layout['text-font']).toEqual(['Noto Sans Medium']); + // The basemap keeps its sprite; the overlay's is beside it. + expect(map.style.sprite).toEqual([ + {id: 'default', url: `${window.location.origin}/map-assets/sprites/light`}, + { + id: 'overlay:extras/features.pmtiles', + url: `${window.location.origin}/api/map/files/sprite/extras/features.pmtiles`, + }, + ]); + // Nothing of the style's own sources reaches the map. + expect(JSON.stringify(map.style)).not.toContain('example.com'); + }); + + test('starts hidden, and offers only the layers its style draws', async () => { + serveStyle(STYLE); + const map = await renderViewer([PLANET, STYLED]); + expect([STYLED_LINES, STYLED_TOWERS].map(id => map.visibility(id))).toEqual(['none', 'none']); + + await click(screen.getByLabelText('Topographic Features')); + expect([STYLED_LINES, STYLED_TOWERS].map(id => map.visibility(id))).toEqual(['visible', 'visible']); + + await click(screen.getByRole('button', {name: 'Show layers of Topographic Features'})); + // The style has no layer for the file's `water`. + expect(screen.queryByLabelText('water')).not.toBeInTheDocument(); + await click(screen.getByLabelText('towers')); + expect([STYLED_LINES, STYLED_TOWERS].map(id => map.visibility(id))).toEqual(['visible', 'none']); + }); + + test('says how many layers of the style could not be used', async () => { + serveStyle({ + ...STYLE, + layers: [...STYLE.layers, {id: 'bad', type: 'line', source: 'features', 'source-layer': 'water', paint: {'line-width': 'wide'}}], + }); + const map = await renderViewer([PLANET, STYLED]); + await click(screen.getByRole('button', {name: 'Show layers of Topographic Features'})); + expect(screen.getByText('1 style layer skipped')).toBeInTheDocument(); + expect(map.style.layers.filter(l => l.source === 'overlay:extras/features.pmtiles').map(l => l.id)) + .toEqual([STYLED_LINES, STYLED_TOWERS]); + }); + + test('is drawn generically when the style cannot be fetched', async () => { + serveStyle(null); + const map = await renderViewer([PLANET, STYLED]); + expect(map.style.layers.filter(l => l.source === 'overlay:extras/features.pmtiles').map(l => l.id)) + .toEqual([WATER, POWER_LINES, TOWERS]); + // No overlay sprite is asked for. + expect(map.style.sprite).toBe(`${window.location.origin}/map-assets/sprites/light`); + }); +}); diff --git a/app/src/components/mapOverlayStyle.js b/app/src/components/mapOverlayStyle.js new file mode 100644 index 00000000..a4f728a4 --- /dev/null +++ b/app/src/components/mapOverlayStyle.js @@ -0,0 +1,250 @@ +// A style a person put beside a PMTiles file says how that overlay is drawn, in place of the generic +// colors. It is an ordinary MapLibre style document written for some other map, so only what is +// safe and meaningful here is taken from it: the layers which draw this file. Its sources, sprite +// and glyph URLs are never used, so a style cannot make the map ask another host for anything. + +import {validateStyleMin} from "@maplibre/maplibre-gl-style-spec"; +import {mediaUrl, overlaySourceId} from "./mapOverlays"; + +// The fonts the map has glyphs for (modules/map/static/fonts). The map has one glyph URL, so a +// style's own fonts cannot be loaded; each is replaced by the nearest of these. +const FONT_REGULAR = "Noto Sans Regular"; +const FONT_MEDIUM = "Noto Sans Medium"; +const FONT_ITALIC = "Noto Sans Italic"; + +// Properties whose value names an image of the sprite. +const IMAGE_PROPERTIES = { + layout: ["icon-image"], + paint: ["fill-pattern", "line-pattern", "fill-extrusion-pattern"], +}; + +// Expression operators a `text-font` value can start with; anything else is a plain list of fonts. +const FONT_EXPRESSIONS = new Set(["literal", "match", "case", "step", "coalesce", "let", "var", "get"]); + +export function bundledFont(name) { + if (/italic|oblique/i.test(name)) return FONT_ITALIC; + if (/bold|medium|black|heavy/i.test(name)) return FONT_MEDIUM; + return FONT_REGULAR; +} + +const fontList = fonts => [...new Set(fonts.map(f => bundledFont(String(f))))]; + +// Inside an expression only a `literal` list is a list of fonts; other lists are not (the labels +// of a `match` are a list of strings too). +function adaptFontExpression(value) { + if (!Array.isArray(value)) return value; + if (value[0] === "literal" && Array.isArray(value[1])) return ["literal", fontList(value[1])]; + return value.map(adaptFontExpression); +} + +// Replace every font a `text-font` value names, as a plain list or as the literal lists of an +// expression. +function adaptFonts(value) { + if (!Array.isArray(value)) return value; + return FONT_EXPRESSIONS.has(value[0]) ? adaptFontExpression(value) : fontList(value); +} + +// A `text-field` can set the font of a part of the text (the options of a `format` section); those +// fonts are replaced too. +function adaptTextFieldFonts(value) { + if (Array.isArray(value)) return value.map(adaptTextFieldFonts); + if (value && typeof value === "object") { + const options = {}; + for (const [key, option] of Object.entries(value)) { + options[key] = key === "text-font" ? adaptFonts(option) : adaptTextFieldFonts(option); + } + return options; + } + return value; +} + +// The id of an overlay's sprite; MapLibre knows its images as `:`. +export function overlaySpriteId(file) { + return overlaySourceId(file); +} + +// The sprite sheet of an overlay, as MapLibre asks for it (it appends `@2x` and the extension). +export function overlaySpriteUrl(file) { + const path = String(file.map_path).split("/").map(encodeURIComponent).join("/"); + return `${window.location.origin}/api/map/files/sprite/${path}`; +} + +const isZoom = value => Array.isArray(value) && value.length === 1 && value[0] === "zoom"; + +// An image may be named by a property function (the older form of an expression: an object with +// `stops`, or `type: "identity"`). It is rewritten as the expression which gives the same name, +// so that the name can be pointed at the overlay's sprite. Returns undefined for a function +// this does not know how to rewrite. +function imageFunctionExpression(fn) { + const fallback = typeof fn.default === "string" ? fn.default : ""; + if (fn.type === "identity" && typeof fn.property === "string") { + return ["coalesce", ["get", fn.property], fallback]; + } + + const stops = fn.stops; + if (!Array.isArray(stops) || !stops.length || !stops.every(i => Array.isArray(i) && i.length === 2)) { + return undefined; + } + const inputs = stops.map(i => i[0]); + const outputs = stops.map(i => i[1]); + if (!outputs.every(i => typeof i === "string")) return undefined; + + if (fn.type === "categorical") { + // One name per value of the property. + const labels = inputs.every(i => typeof i === "string") || inputs.every(i => typeof i === "number"); + if (typeof fn.property !== "string" || !labels || new Set(inputs).size !== inputs.length) return undefined; + return ["match", ["get", fn.property], ...stops.flat(), fallback]; + } + if (fn.type !== undefined && fn.type !== "interval") return undefined; + if (!inputs.every(i => typeof i === "number") || inputs.some((v, i) => i > 0 && v <= inputs[i - 1])) { + return undefined; + } + // A single stop is one name, whatever the input. + if (stops.length === 1) return outputs[0]; + // One name per range of the zoom, or of a numeric property; below the first stop is the first. + const input = fn.property === undefined ? ["zoom"] + : typeof fn.property === "string" ? ["to-number", ["get", fn.property], inputs[0]] : undefined; + if (!input) return undefined; + return ["step", input, outputs[0], ...stops.slice(1).flat()]; +} + +// Point an image name at the overlay's sprite. The value is wrapped, not rewritten: the strings of +// an expression are not all image names (the labels of a `match` are not), but whatever it +// evaluates to is. An empty name stays empty: no image. Returns undefined when the value is one +// this cannot point at the sprite. +function adaptImage(value, spriteId) { + if (typeof value === "string") { + return value ? `${spriteId}:${value}` : value; + } + if (value && typeof value === "object" && !Array.isArray(value)) { + const expression = imageFunctionExpression(value); + return expression === undefined ? undefined : adaptImage(expression, spriteId); + } + if (!Array.isArray(value)) return undefined; + if (value[0] === "step" && isZoom(value[1])) { + // The zoom may only be the input of the outermost expression, so each name it chooses + // between is wrapped, not the whole. + const adapted = value.map((part, index) => + (index >= 2 && index % 2 === 0) ? adaptImage(part, spriteId) : part); + return adapted.includes(undefined) ? undefined : adapted; + } + return ["image", ["let", "wrolpi_image", ["to-string", value], + ["case", ["==", ["var", "wrolpi_image"], ""], "", + ["concat", `${spriteId}:`, ["var", "wrolpi_image"]]]]]; +} + +// The source of the style which is this file: the one the style names for us, else the one most of +// the layers which draw this file's layers use. +function chooseSource(style, fileLayerIds) { + const named = style.metadata?.["wrolpi:source"]; + if (named) return named; + const counts = {}; + for (const layer of style.layers) { + if (layer && layer.source && fileLayerIds.has(layer["source-layer"])) { + counts[layer.source] = (counts[layer.source] || 0) + 1; + } + } + return Object.keys(counts).sort((a, b) => counts[b] - counts[a])[0]; +} + +function validationErrors(layer, source, spriteId) { + return validateStyleMin({ + version: 8, + sources: {[source]: {type: "vector", url: "pmtiles://overlay"}}, + sprite: [{id: "default", url: "https://localhost/default"}, {id: spriteId, url: "https://localhost/overlay"}], + glyphs: "https://localhost/{fontstack}/{range}.pbf", + layers: [layer], + }); +} + +// Turn a style written for another map into the layers which draw `file` on this one. +// Returns {layers, skipped}: `skipped` counts the layers of this file which were not usable. +export function adaptOverlayStyle(style, file) { + if (!style || !Array.isArray(style.layers)) return {layers: [], skipped: 0}; + + const fileLayerIds = new Set((file.vector_layers || []).map(l => l.id)); + const styleSource = chooseSource(style, fileLayerIds); + const source = overlaySourceId(file); + const spriteId = overlaySpriteId(file); + // A style drawn for one scale (a printed map, say) is unreadable zoomed out; it can say the zoom + // it is meant to be seen from. + const minZoom = Number(style.metadata?.["wrolpi:minzoom"]); + const layers = []; + const ids = new Set(); + let skipped = 0; + + for (const original of style.layers) { + // Only layers which draw this file: not the background, nor another source (hillshade, contours). + if (!original || original.source !== styleSource || !fileLayerIds.has(original["source-layer"])) continue; + + const layer = JSON.parse(JSON.stringify(original)); + layer.id = `${source}:${layer["source-layer"]}:${original.id}`; + layer.source = source; + if (Number.isFinite(minZoom) && minZoom > (layer.minzoom || 0)) { + layer.minzoom = minZoom; + } + + if (layer.layout?.["text-font"] !== undefined) { + layer.layout["text-font"] = adaptFonts(layer.layout["text-font"]); + } + if (layer.layout?.["text-field"] !== undefined) { + layer.layout["text-field"] = adaptTextFieldFonts(layer.layout["text-field"]); + // MapLibre's own default font is not one the map has. + if (layer.layout["text-font"] === undefined) { + layer.layout["text-font"] = [FONT_REGULAR]; + } + } + for (const [group, properties] of Object.entries(IMAGE_PROPERTIES)) { + for (const property of properties) { + if (layer[group]?.[property] === undefined) continue; + // Without a sprite sheet there are no images to draw. An image this cannot point at + // the overlay's sprite would be looked for in the basemap's; the layer is drawn + // without it (its text still is). + const image = file.has_sprite ? adaptImage(layer[group][property], spriteId) : undefined; + if (image === undefined) { + delete layer[group][property]; + } else { + layer[group][property] = image; + } + } + } + + // One bad layer must not stop the map: MapLibre refuses a whole style over it. + let errors; + try { + errors = validationErrors(layer, source, spriteId); + } catch (e) { + errors = [e]; + } + if (errors.length || ids.has(layer.id)) { + skipped += 1; + continue; + } + ids.add(layer.id); + layers.push(layer); + } + return {layers, skipped}; +} + +// Fetch and adapt the style beside an overlay file. Returns null when the file has none, or it +// cannot be used; the overlay is then drawn generically. +export async function loadOverlayStyle(file) { + if (!file.style_path) return null; + try { + const response = await fetch(mediaUrl(file.style_path)); + if (!response.ok) return null; + const adapted = adaptOverlayStyle(await response.json(), file); + return adapted.layers.length ? adapted : null; + } catch (e) { + console.error(`Could not use the style of ${file.map_path}:`, e); + return null; + } +} + +// Attach its style to each overlay file which has one (`styleLayers`, `styleSkipped`). +export async function loadOverlayStyles(overlays) { + return Promise.all((overlays || []).map(async file => { + const adapted = await loadOverlayStyle(file); + return adapted ? {...file, styleLayers: adapted.layers, styleSkipped: adapted.skipped} : file; + })); +} diff --git a/app/src/components/mapOverlayStyle.test.js b/app/src/components/mapOverlayStyle.test.js new file mode 100644 index 00000000..bd3af2c3 --- /dev/null +++ b/app/src/components/mapOverlayStyle.test.js @@ -0,0 +1,421 @@ +import { + adaptOverlayStyle, + bundledFont, + loadOverlayStyle, + loadOverlayStyles, + overlaySpriteUrl, +} from './mapOverlayStyle'; +import {buildOverlayLayers, overlayToggleLayers, setOverlayEnabled, setOverlayLayerEnabled} from './mapOverlays'; + +// A style written for another map is adapted to draw one overlay file on this map. + +const layer = (id) => ({id, geometry: null, minzoom: 0, maxzoom: 13, description: null}); +const FILE = { + name: 'features.pmtiles', map_path: 'extras/features.pmtiles', path: 'map/extras/features.pmtiles', + kind: 'overlay', tile_type: 'mvt', visible: true, has_sprite: true, + style_path: 'map/extras/style.json', + vector_layers: [layer('repeaters'), layer('roads'), layer('parks')], +}; +const SOURCE = 'overlay:extras/features.pmtiles'; + +// As a style arrives: its own source names and URLs, and layers of sources which are not this file. +const STYLE = { + version: 8, + sprite: 'https://example.com/sprites/topo', + glyphs: 'https://example.com/glyphs/{fontstack}/{range}.pbf', + sources: { + features: {type: 'vector', url: 'https://example.com/features.json'}, + relief: {type: 'raster', tiles: ['https://example.com/relief/{z}/{x}/{y}.png']}, + contours: {type: 'vector', url: 'https://example.com/contours.json'}, + }, + layers: [ + {id: 'background', type: 'background', paint: {'background-color': '#fff'}}, + {id: 'relief', type: 'raster', source: 'relief'}, + {id: 'parks', type: 'fill', source: 'features', 'source-layer': 'parks', paint: {'fill-color': '#9c9'}}, + {id: 'contours', type: 'line', source: 'contours', 'source-layer': 'contours'}, + {id: 'roads', type: 'line', source: 'features', 'source-layer': 'roads', paint: {'line-color': '#c60'}}, + // A layer of the file's source which this file does not have. + {id: 'buildings', type: 'fill', source: 'features', 'source-layer': 'buildings'}, + { + id: 'repeaters', type: 'symbol', source: 'features', 'source-layer': 'repeaters', minzoom: 10, + layout: { + 'icon-image': 'ham_radio', + 'text-field': ['get', 'callsign'], + 'text-font': ['Roboto Condensed Bold'], + }, + }, + ], +}; + +const adapted = (style = STYLE, file = FILE) => adaptOverlayStyle(style, file); +const byId = (result, id) => result.layers.find(l => l.id.endsWith(`:${id}`)); + +describe('adaptOverlayStyle', () => { + test("keeps the layers which draw this file, in the style's order", () => { + const result = adapted(); + expect(result.layers.map(l => l.id)).toEqual([ + `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + ]); + expect(result.skipped).toBe(0); + // What the style said of a layer is kept. + expect(byId(result, 'roads').paint).toEqual({'line-color': '#c60'}); + expect(byId(result, 'repeaters').minzoom).toBe(10); + }); + + test('draws from the overlay file, and names nothing the style would have fetched', () => { + const result = adapted(); + expect(new Set(result.layers.map(l => l.source))).toEqual(new Set([SOURCE])); + expect(JSON.stringify(result)).not.toContain('example.com'); + // The style given is not changed. + expect(STYLE.layers[2].source).toBe('features'); + }); + + test('the source which is this file is the one most of its layers use', () => { + const style = { + ...STYLE, + layers: [ + // Another source has a layer of the same name. + {id: 'other-roads', type: 'line', source: 'contours', 'source-layer': 'roads'}, + ...STYLE.layers, + ], + }; + expect(adapted(style).layers.map(l => l.id)).toEqual([ + `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + ]); + }); + + test('the style can name the source which is this file', () => { + const style = {...STYLE, metadata: {'wrolpi:source': 'contours'}}; + const file = {...FILE, vector_layers: [layer('contours')]}; + expect(adapted(style, file).layers.map(l => l.id)).toEqual([`${SOURCE}:contours:contours`]); + }); + + test('the style can say the zoom it is meant to be seen from', () => { + const result = adapted({...STYLE, metadata: {'wrolpi:minzoom': 9}}); + // A layer which already starts later keeps its own zoom. + expect(result.layers.map(l => l.minzoom)).toEqual([9, 9, 10]); + // Without it, layers are as the style has them. + expect(adapted().layers.map(l => l.minzoom)).toEqual([undefined, undefined, 10]); + expect(adapted({...STYLE, metadata: {'wrolpi:minzoom': 'soon'}}).layers.map(l => l.minzoom)) + .toEqual([undefined, undefined, 10]); + }); + + test('a style of other files draws nothing', () => { + expect(adapted(STYLE, {...FILE, vector_layers: [layer('towers')]})).toEqual({layers: [], skipped: 0}); + expect(adapted({version: 8}, FILE)).toEqual({layers: [], skipped: 0}); + expect(adapted(null, FILE)).toEqual({layers: [], skipped: 0}); + }); + + test('a layer the map could not draw is skipped, and the rest are kept', () => { + const style = { + ...STYLE, + layers: [ + {id: 'bad-paint', type: 'line', source: 'features', 'source-layer': 'roads', paint: {'line-width': 'wide'}}, + {id: 'bad-type', type: 'sparkles', source: 'features', 'source-layer': 'roads'}, + ...STYLE.layers, + ], + }; + const result = adapted(style); + expect(result.skipped).toBe(2); + expect(result.layers.map(l => l.id)).toEqual([ + `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + ]); + }); + + test('a second layer of the same id is skipped', () => { + const style = {...STYLE, layers: [...STYLE.layers, STYLE.layers[4]]}; + const result = adapted(style); + expect(result.skipped).toBe(1); + expect(result.layers).toHaveLength(3); + }); +}); + +describe('fonts', () => { + test('a font the map does not have is replaced by the nearest it has', () => { + expect(bundledFont('Roboto Condensed Bold')).toBe('Noto Sans Medium'); + expect(bundledFont('Roboto Slab Semibold')).toBe('Noto Sans Medium'); + expect(bundledFont('Roboto Condensed Italic')).toBe('Noto Sans Italic'); + expect(bundledFont('Roboto Condensed Bold Italic')).toBe('Noto Sans Italic'); + expect(bundledFont('Open Sans Regular')).toBe('Noto Sans Regular'); + expect(bundledFont('Noto Sans Regular')).toBe('Noto Sans Regular'); + }); + + test('a list of fonts', () => { + expect(byId(adapted(), 'repeaters').layout['text-font']).toEqual(['Noto Sans Medium']); + const style = { + ...STYLE, + layers: [{ + ...STYLE.layers[6], + layout: {'text-field': 'x', 'text-font': ['Roboto Bold', 'Arial Unicode MS Bold', 'Roboto Regular']}, + }], + }; + // Two fonts which become the same one are listed once. + expect(byId(adapted(style), 'repeaters').layout['text-font']).toEqual(['Noto Sans Medium', 'Noto Sans Regular']); + }); + + test('fonts chosen by an expression', () => { + const style = { + ...STYLE, + layers: [{ + ...STYLE.layers[6], + layout: { + 'text-field': 'x', + 'text-font': ['match', ['get', 'class'], + ['city', 'town'], ['literal', ['Roboto Condensed Bold']], + ['locality'], ['literal', ['Roboto Condensed Italic']], + ['literal', ['Roboto Condensed Regular']]], + }, + }], + }; + const result = adapted(style); + expect(result.skipped).toBe(0); + expect(byId(result, 'repeaters').layout['text-font']).toEqual(['match', ['get', 'class'], + ['city', 'town'], ['literal', ['Noto Sans Medium']], + ['locality'], ['literal', ['Noto Sans Italic']], + ['literal', ['Noto Sans Regular']]]); + }); + + test('the font of a part of the text', () => { + const style = { + ...STYLE, + layers: [{ + ...STYLE.layers[6], + layout: { + 'text-font': ['Roboto Condensed Regular'], + 'text-field': ['format', + ['get', 'name'], {'text-font': ['literal', ['Roboto Condensed Italic']]}, + '\n', {}, + ['get', 'ele'], {'text-font': ['literal', ['Atkinson Hyperlegible Mono Bold']], 'font-scale': 0.8}], + }, + }], + }; + const result = adapted(style); + expect(result.skipped).toBe(0); + expect(byId(result, 'repeaters').layout['text-field']).toEqual(['format', + ['get', 'name'], {'text-font': ['literal', ['Noto Sans Italic']]}, + '\n', {}, + ['get', 'ele'], {'text-font': ['literal', ['Noto Sans Medium']], 'font-scale': 0.8}]); + }); + + test('text without a font is given one the map has', () => { + const style = {...STYLE, layers: [{...STYLE.layers[6], layout: {'text-field': ['get', 'callsign']}}]}; + expect(byId(adapted(style), 'repeaters').layout['text-font']).toEqual(['Noto Sans Regular']); + // A layer without text is not given a font. + expect(byId(adapted(), 'roads').layout).toBeUndefined(); + }); +}); + +describe('icons', () => { + const withIcon = (icon, file = FILE) => { + const style = {...STYLE, layers: [{...STYLE.layers[6], layout: {'icon-image': icon}}]}; + return adapted(style, file); + }; + + test("an image is one of the overlay's own sprite", () => { + expect(byId(adapted(), 'repeaters').layout['icon-image']).toBe(`${SOURCE}:ham_radio`); + expect(byId(withIcon('{class}_11'), 'repeaters').layout['icon-image']).toBe(`${SOURCE}:{class}_11`); + }); + + test('an image chosen by an expression: what it evaluates to is renamed, not its strings', () => { + const icon = ['match', ['get', 'class'], 'hospital', 'hospital', 'pin']; + const result = withIcon(icon); + expect(result.skipped).toBe(0); + const value = byId(result, 'repeaters').layout['icon-image']; + // The expression is intact inside the wrapping: the label "hospital" still matches the class. + expect(JSON.stringify(value)).toContain(JSON.stringify(icon)); + expect(JSON.stringify(value)).toContain(`"${SOURCE}:"`); + }); + + describe('an image named by a property function', () => { + // The wrapping every image gets; `expression` is what gives the name. + const wrapped = expression => ['image', ['let', 'wrolpi_image', ['to-string', expression], + ['case', ['==', ['var', 'wrolpi_image'], ''], '', + ['concat', `${SOURCE}:`, ['var', 'wrolpi_image']]]]]; + const iconOf = (fn) => { + const result = withIcon(fn); + // The layer is kept: its text is drawn whatever becomes of its image. + expect(result.skipped).toBe(0); + expect(result.layers).toHaveLength(1); + return byId(result, 'repeaters').layout['icon-image']; + }; + + test('the value of a property', () => { + expect(iconOf({type: 'identity', property: 'icon'})) + .toEqual(wrapped(['coalesce', ['get', 'icon'], ''])); + expect(iconOf({type: 'identity', property: 'icon', default: 'pin'})) + .toEqual(wrapped(['coalesce', ['get', 'icon'], 'pin'])); + }); + + test('a name for each value of a property', () => { + expect(iconOf({property: 'class', type: 'categorical', stops: [['hospital', 'cross'], ['fuel', 'pump']]})) + .toEqual(wrapped(['match', ['get', 'class'], 'hospital', 'cross', 'fuel', 'pump', ''])); + expect(iconOf({property: 'rank', type: 'categorical', stops: [[1, 'big'], [2, 'small']], default: 'pin'})) + .toEqual(wrapped(['match', ['get', 'rank'], 1, 'big', 2, 'small', 'pin'])); + }); + + test('a name for each range of a numeric property', () => { + expect(iconOf({property: 'height', stops: [[0, 'low'], [100, 'tall']]})) + .toEqual(wrapped(['step', ['to-number', ['get', 'height'], 0], 'low', 100, 'tall'])); + // A single stop is one name. + expect(iconOf({property: 'height', type: 'interval', stops: [[10, 'only']]})).toBe(`${SOURCE}:only`); + expect(iconOf({stops: [[10, 'only']]})).toBe(`${SOURCE}:only`); + }); + + test('a name for each range of the zoom: each name is renamed, the zoom stays outermost', () => { + expect(iconOf({stops: [[0, 'dot'], [12, 'tower'], [15, '{class}_big']]})) + .toEqual(['step', ['zoom'], `${SOURCE}:dot`, 12, `${SOURCE}:tower`, 15, `${SOURCE}:{class}_big`]); + }); + + test('a function which cannot be rewritten: the layer is drawn without the image', () => { + for (const fn of [ + // By zoom and property at once. + {property: 'rank', stops: [[{zoom: 0, value: 1}, 'a'], [{zoom: 10, value: 1}, 'b']]}, + {property: 'on', type: 'categorical', stops: [[true, 'a'], [false, 'b']]}, + {property: 'height', type: 'exponential', stops: [[0, 'a'], [1, 'b']]}, + {property: 'height', stops: [[5, 'a'], [1, 'b']]}, + {stops: []}, + {}, + ]) { + const style = { + ...STYLE, + layers: [{...STYLE.layers[6], layout: {'icon-image': fn, 'text-field': ['get', 'callsign']}}], + }; + const result = adapted(style); + expect(result.layers).toHaveLength(1); + expect(byId(result, 'repeaters').layout['icon-image']).toBeUndefined(); + expect(byId(result, 'repeaters').layout['text-field']).toEqual(['get', 'callsign']); + } + }); + + test('a pattern named by a function', () => { + const style = { + ...STYLE, + layers: [{ + id: 'parks', type: 'fill', source: 'features', 'source-layer': 'parks', + paint: {'fill-pattern': {type: 'identity', property: 'pattern'}}, + }], + }; + const result = adapted(style); + expect(result.skipped).toBe(0); + expect(byId(result, 'parks').paint['fill-pattern']).toEqual(wrapped(['coalesce', ['get', 'pattern'], ''])); + }); + }); + + test('an image chosen by the zoom in an expression: the zoom stays outermost', () => { + const result = withIcon(['step', ['zoom'], 'dot', 12, ['match', ['get', 'class'], 'tower', 'tower', 'pin']]); + expect(result.skipped).toBe(0); + const value = byId(result, 'repeaters').layout['icon-image']; + expect(value.slice(0, 4)).toEqual(['step', ['zoom'], `${SOURCE}:dot`, 12]); + expect(JSON.stringify(value[4])).toContain(JSON.stringify(['match', ['get', 'class'], 'tower', 'tower', 'pin'])); + }); + + test('a pattern is an image too', () => { + const style = { + ...STYLE, + layers: [{ + id: 'parks', type: 'fill', source: 'features', 'source-layer': 'parks', + paint: {'fill-pattern': 'forest', 'fill-opacity': 0.5}, + }], + }; + expect(byId(adapted(style), 'parks').paint).toEqual({'fill-pattern': `${SOURCE}:forest`, 'fill-opacity': 0.5}); + }); + + test('without a sprite sheet there are no images, and the text is still drawn', () => { + const result = adapted(STYLE, {...FILE, has_sprite: false}); + expect(byId(result, 'repeaters').layout).toEqual({ + 'text-field': ['get', 'callsign'], 'text-font': ['Noto Sans Medium'], + }); + expect(result.skipped).toBe(0); + }); + + test("the sprite is asked of this WROLPi, by the file's path", () => { + expect(overlaySpriteUrl({map_path: 'extras/my features.pmtiles'})) + .toBe(`${window.location.origin}/api/map/files/sprite/extras/my%20features.pmtiles`); + }); +}); + +describe('loadOverlayStyle', () => { + const respond = (body, ok = true) => { + global.fetch = jest.fn().mockResolvedValue({ok, json: async () => body}); + }; + + test('fetches the style beside the file', async () => { + respond(STYLE); + const result = await loadOverlayStyle(FILE); + expect(global.fetch).toHaveBeenCalledWith('/media/map/extras/style.json'); + expect(result.layers).toHaveLength(3); + }); + + test('a file without a style is not asked for one', async () => { + respond(STYLE); + expect(await loadOverlayStyle({...FILE, style_path: null})).toBeNull(); + expect(global.fetch).not.toHaveBeenCalled(); + }); + + test('a style which cannot be used is no style', async () => { + jest.spyOn(console, 'error').mockImplementation(() => { + }); + respond(STYLE, false); + expect(await loadOverlayStyle(FILE)).toBeNull(); + // Draws none of this file's layers. + respond({version: 8, layers: [{id: 'background', type: 'background'}]}); + expect(await loadOverlayStyle(FILE)).toBeNull(); + // Not JSON. + global.fetch = jest.fn().mockResolvedValue({ + ok: true, json: async () => { + throw new SyntaxError('Unexpected token'); + }, + }); + expect(await loadOverlayStyle(FILE)).toBeNull(); + global.fetch = jest.fn().mockRejectedValue(new Error('offline')); + expect(await loadOverlayStyle(FILE)).toBeNull(); + }); + + test('each overlay gets its own style, or stays as it was', async () => { + respond(STYLE); + const plain = {...FILE, map_path: 'plain.pmtiles', style_path: null}; + const [styled, same] = await loadOverlayStyles([FILE, plain]); + expect(styled.styleLayers).toHaveLength(3); + expect(styled.styleSkipped).toBe(0); + expect(same).toBe(plain); + }); +}); + +describe('a styled overlay on the map', () => { + const styled = {...FILE, styleLayers: adapted().layers}; + const visibility = state => buildOverlayLayers(styled, state).map(l => l.layout.visibility); + + test('is drawn with its style, not the generic colors', () => { + const layers = buildOverlayLayers(styled, setOverlayEnabled({}, styled, true)); + expect(layers.map(l => [l.id, l.type])).toEqual([ + [`${SOURCE}:parks:parks`, 'fill'], [`${SOURCE}:roads:roads`, 'line'], + [`${SOURCE}:repeaters:repeaters`, 'symbol'], + ]); + expect(layers[2].layout['icon-image']).toBe(`${SOURCE}:ham_radio`); + }); + + test('starts hidden, and its layers are shown and hidden by the layer of the file they draw', () => { + expect(visibility({})).toEqual(['none', 'none', 'none']); + let state = setOverlayEnabled({}, styled, true); + expect(visibility(state)).toEqual(['visible', 'visible', 'visible']); + state = setOverlayLayerEnabled(state, styled, 'roads', false); + expect(visibility(state)).toEqual(['visible', 'none', 'visible']); + }); + + test('a layer the style hides stays hidden', () => { + const hidden = { + ...styled, + styleLayers: styled.styleLayers.map(l => l.type === 'fill' ? {...l, layout: {visibility: 'none'}} : l), + }; + const layers = buildOverlayLayers(hidden, setOverlayEnabled({}, hidden, true)); + expect(layers.map(l => l.layout.visibility)).toEqual(['none', 'visible', 'visible']); + }); + + test('only the layers its style draws can be chosen', () => { + const file = {...styled, vector_layers: [...FILE.vector_layers, layer('buildings')]}; + expect(overlayToggleLayers(file).map(l => l.id)).toEqual(['repeaters', 'roads', 'parks']); + // Without a style, every layer of the file is drawn. + expect(overlayToggleLayers({...file, styleLayers: undefined}).map(l => l.id)) + .toEqual(['repeaters', 'roads', 'parks', 'buildings']); + }); +}); diff --git a/app/src/components/mapOverlays.js b/app/src/components/mapOverlays.js index ad40385b..135efa0f 100644 --- a/app/src/components/mapOverlays.js +++ b/app/src/components/mapOverlays.js @@ -60,6 +60,21 @@ export function isRasterOverlay(file) { return file.tile_type !== "mvt"; } +// Whether the overlay is drawn with a style which came beside the file (see mapOverlayStyle.js). +export function isStyledOverlay(file) { + return !isRasterOverlay(file) && Array.isArray(file.styleLayers) && file.styleLayers.length > 0; +} + +// The layers of a vector overlay the user can show and hide: all of them, or with its own style +// only those the style draws. +export function overlayToggleLayers(file) { + if (isRasterOverlay(file)) return []; + const layers = file.vector_layers || []; + if (!isStyledOverlay(file)) return layers; + const drawn = new Set(file.styleLayers.map(l => l["source-layer"])); + return layers.filter(l => drawn.has(l.id)); +} + function fileStem(name) { return String(name).replace(/\.pmtiles$/, ""); } @@ -115,7 +130,7 @@ export function setOverlayLayerEnabled(state, file, layerId, enabled) { export function setAllOverlayLayersEnabled(state, file, enabled) { const layers = {}; - for (const l of file.vector_layers || []) layers[l.id] = enabled; + for (const l of overlayToggleLayers(file)) layers[l.id] = enabled; return {...state, [file.map_path]: {...state?.[file.map_path], layers}}; } @@ -173,6 +188,19 @@ export function buildOverlayLayers(file, state) { }]; } + if (isStyledOverlay(file)) { + // The file came with its own style: its layers, each shown when the file and its source + // layer are on (and the style does not itself hide it). + return file.styleLayers.map(layer => ({ + ...layer, + layout: { + ...layer.layout, + visibility: visibility(enabled && layer.layout?.visibility !== "none" + && isOverlayLayerEnabled(state, file, layer["source-layer"])), + }, + })); + } + const styleLayers = []; for (const layer of file.vector_layers || []) { const color = overlayColor(layer.id); @@ -194,21 +222,28 @@ export function buildOverlayLayers(file, state) { } // Add the overlays to a style's sources and layers (the basemap's are already there). A raster -// overlay goes under the basemap's labels so the map stays readable; vector overlays go on top, -// fills under lines under points. +// overlay goes under the basemap's labels so the map stays readable; vector overlays go on top: +// an overlay with its own style in that style's order, then the generic ones, fills under lines +// under points. export function addOverlaysToStyle(styleSources, allLayers, overlays, state) { const rasters = []; + const styled = []; const vectors = {fill: [], line: [], circle: []}; for (const file of overlays || []) { styleSources[overlaySourceId(file)] = buildOverlaySource(file); - for (const layer of buildOverlayLayers(file, state)) { + const layers = buildOverlayLayers(file, state); + if (isStyledOverlay(file)) { + styled.push(...layers); + continue; + } + for (const layer of layers) { if (layer.type === "raster") rasters.push(layer); else vectors[layer.type].push(layer); } } const firstLabel = allLayers.findIndex(l => l.type === "symbol"); allLayers.splice(firstLabel === -1 ? allLayers.length : firstLabel, 0, ...rasters); - allLayers.push(...vectors.fill, ...vectors.line, ...vectors.circle); + allLayers.push(...styled, ...vectors.fill, ...vectors.line, ...vectors.circle); } // --- Popup --- diff --git a/modules/map/api.py b/modules/map/api.py index 44a549ca..b935c471 100644 --- a/modules/map/api.py +++ b/modules/map/api.py @@ -49,6 +49,19 @@ async def post_file_visible(_: Request, body: schema.MapFileVisibleRequest): return response.json({'error': 'File not found'}, HTTPStatus.NOT_FOUND) +@map_bp.get('/files/sprite/') +@openapi.description("Get the sprite sheet beside a PMTiles map file: `[@2x].json` or `.png`") +async def get_file_sprite(_: Request, name: str): + try: + sprite = lib.get_sidecar_sprite_file(name) + except ValueError as e: + return response.json({'error': str(e)}, HTTPStatus.BAD_REQUEST) + + if sprite: + return await response.file(sprite) + return response.json({'error': 'Sprite not found'}, HTTPStatus.NOT_FOUND) + + @map_bp.get('/subscribe') @openapi.description('Get map catalog and current subscriptions') def get_subscriptions(request: Request): diff --git a/modules/map/lib.py b/modules/map/lib.py index 27dc9f3e..844b992e 100644 --- a/modules/map/lib.py +++ b/modules/map/lib.py @@ -1,4 +1,5 @@ import json +import re import tempfile from pathlib import Path from typing import List, Dict @@ -41,6 +42,48 @@ def get_map_path(path: Path) -> str: return path.relative_to(get_map_directory()).as_posix() +def find_sidecar_style(path: Path) -> Path | None: + """The MapLibre style a person put beside a PMTiles file to say how it is drawn: `.style.json`, else the + `style.json` shared by the files of its directory.""" + for candidate in (path.with_suffix('.style.json'), path.parent / 'style.json'): + if candidate.is_file(): + return candidate + return None + + +def find_sidecar_sprite(path: Path, extension: str = 'json', high_resolution: bool = False) -> Path | None: + """The sprite sheet (the icons of a sidecar style) beside a PMTiles file: `.sprite.json` and + `.sprite.png`, else the `sprite.json` and `sprite.png` shared by the files of its directory. + + A sheet may also come at double resolution (`sprite@2x.json`, `sprite@2x.png`). The sheet of the other + resolution is returned when the one asked for does not exist; a sprite file says its own pixel ratio.""" + for base in (path.with_suffix('.sprite'), path.parent / 'sprite'): + resolutions = [i for i in ('', '@2x') + if base.with_name(f'{base.name}{i}.json').is_file() + and base.with_name(f'{base.name}{i}.png').is_file()] + if resolutions: + wanted = '@2x' if high_resolution else '' + resolution = wanted if wanted in resolutions else resolutions[0] + return base.with_name(f'{base.name}{resolution}.{extension}') + return None + + +SPRITE_NAME = re.compile(r'^(?P.+\.pmtiles)(?P@2x)?\.(?Pjson|png)$') + + +def get_sidecar_sprite_file(name: str) -> Path | None: + """The sprite file MapLibre asks for as `[@2x].(json|png)`, if that map file has a sprite. + + Raises ValueError if the name is not such a name, or leads outside of the map directory.""" + match = SPRITE_NAME.match(name) + if not match: + raise ValueError(f'Invalid sprite name: {name}') + path = _resolve_pmtiles_file(match.group('filename')) + if not path: + return None + return find_sidecar_sprite(path, match.group('extension'), bool(match.group('high_resolution'))) + + def get_pmtiles_files() -> List[dict]: """Find all .pmtiles files in the map directory and return their metadata.""" maps_config = get_maps_config() @@ -49,6 +92,7 @@ def get_pmtiles_files() -> List[dict]: for path in iter_map_files('.pmtiles'): stat = path.stat() map_path = get_map_path(path) + style = find_sidecar_style(path) files.append(dict( name=path.name, path=str(path.relative_to(get_media_directory())), @@ -57,6 +101,8 @@ def get_pmtiles_files() -> List[dict]: mtime=stat.st_mtime, has_search_index=path.with_suffix('.search.db').is_file(), visible=not maps_config.is_ignored(map_path), + style_path=str(style.relative_to(get_media_directory())) if style else None, + has_sprite=find_sidecar_sprite(path) is not None, **pmtiles.describe(path), )) diff --git a/modules/map/test/test_api.py b/modules/map/test/test_api.py index b4b2e442..c1106b3f 100644 --- a/modules/map/test/test_api.py +++ b/modules/map/test/test_api.py @@ -287,3 +287,56 @@ def test_maps_config_is_shared(async_client): worker and overwritten by another), and is among the configs WROLPi manages.""" assert get_maps_config()._config is api_app.shared_ctx.maps_config assert get_all_configs()['maps.yaml'] is get_maps_config() + + +@pytest.mark.asyncio +async def test_get_file_sprite(async_client, test_directory, make_pmtiles): + """MapLibre asks for the sprite of a map file as `.json` and `.png`, and as `@2x` on a dense screen; + it gets the single resolution sheet when that is all there is.""" + make_pmtiles('map/extras/features.pmtiles', vector_metadata('towers')) + extras = test_directory / 'map/extras' + (extras / 'sprite.json').write_text('{"tower": {"x": 0, "y": 0, "width": 8, "height": 8, "pixelRatio": 1}}') + (extras / 'sprite.png').write_bytes(b'single') + + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.json') + assert response.status_code == HTTPStatus.OK + assert response.json['tower']['pixelRatio'] == 1 + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.png') + assert response.status_code == HTTPStatus.OK + assert response.body == b'single' + # There is no double resolution sheet. + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.png') + assert response.status_code == HTTPStatus.OK + assert response.body == b'single' + + (extras / 'sprite@2x.json').write_text('{"tower": {"x": 0, "y": 0, "width": 16, "height": 16, "pixelRatio": 2}}') + (extras / 'sprite@2x.png').write_bytes(b'double') + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.png') + assert response.body == b'double' + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles@2x.json') + assert response.json['tower']['pixelRatio'] == 2 + request, response = await async_client.get('/api/map/files/sprite/extras/features.pmtiles.png') + assert response.body == b'single' + + +@pytest.mark.asyncio +async def test_get_file_sprite_missing(async_client, test_directory, make_pmtiles): + """Only the sprite of a map file is served.""" + make_pmtiles('map/features.pmtiles', vector_metadata('towers')) + (test_directory / 'secret.json').write_text('{"secret": true}') + (test_directory / 'map/notes.json').write_text('{"notes": true}') + + # The file has no sprite. + request, response = await async_client.get('/api/map/files/sprite/features.pmtiles.json') + assert response.status_code == HTTPStatus.NOT_FOUND + # There is no such map file. + request, response = await async_client.get('/api/map/files/sprite/missing.pmtiles.json') + assert response.status_code == HTTPStatus.NOT_FOUND + # Not the name of a sprite. + for name in ('notes.json', 'features.pmtiles', 'features.pmtiles.txt', 'features.pmtiles@3x.png'): + request, response = await async_client.get(f'/api/map/files/sprite/{name}') + assert response.status_code == HTTPStatus.BAD_REQUEST, name + # Outside of the map directory. + request, response = await async_client.get('/api/map/files/sprite/..%2Fsecret.pmtiles.json') + assert response.status_code in (HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND) + assert b'secret' not in response.body or response.status_code == HTTPStatus.BAD_REQUEST diff --git a/modules/map/test/test_lib.py b/modules/map/test/test_lib.py index 9f7491cb..ed64cd7e 100644 --- a/modules/map/test/test_lib.py +++ b/modules/map/test/test_lib.py @@ -261,3 +261,70 @@ def test_delete_pmtiles_file_forgets_ignored(async_client, test_directory, make_ make_pmtiles('map/ignored-then-deleted.pmtiles') file, = lib.get_pmtiles_files() assert file['visible'] is True + + +def _make_sprite(base, resolutions=('',)): + """Create sprite sheets (`.json`, `.png`); each says which it is.""" + for resolution in resolutions: + base.with_name(f'{base.name}{resolution}.json').write_text(f'{{"sheet": "{base.name}{resolution}"}}') + base.with_name(f'{base.name}{resolution}.png').write_bytes(f'{base.name}{resolution}'.encode()) + + +def test_sidecar_style(test_directory, make_pmtiles): + """A style beside a map file says how it is drawn: its own, else the one its directory shares.""" + own = make_pmtiles('map/extras/own.pmtiles', vector_metadata('towers')) + shared = make_pmtiles('map/extras/shared.pmtiles', vector_metadata('towers')) + elsewhere = make_pmtiles('map/elsewhere.pmtiles', vector_metadata('towers')) + assert lib.find_sidecar_style(own) is None + + (test_directory / 'map/extras/style.json').write_text('{}') + (test_directory / 'map/extras/own.style.json').write_text('{}') + assert lib.find_sidecar_style(own) == test_directory / 'map/extras/own.style.json' + assert lib.find_sidecar_style(shared) == test_directory / 'map/extras/style.json' + # A directory's style is not the style of the files in other directories. + assert lib.find_sidecar_style(elsewhere) is None + + files = {i['map_path']: i for i in lib.get_pmtiles_files()} + assert files['extras/own.pmtiles']['style_path'] == 'map/extras/own.style.json' + assert files['extras/shared.pmtiles']['style_path'] == 'map/extras/style.json' + assert files['elsewhere.pmtiles']['style_path'] is None + assert {i['has_sprite'] for i in files.values()} == {False} + + +def test_sidecar_sprite(test_directory, make_pmtiles): + """A sprite sheet is a .json and a .png; a file's own is used before the one its directory shares.""" + own = make_pmtiles('map/extras/own.pmtiles', vector_metadata('towers')) + shared = make_pmtiles('map/extras/shared.pmtiles', vector_metadata('towers')) + extras = test_directory / 'map/extras' + + # Half of a sheet is not a sheet. + (extras / 'sprite.json').write_text('{}') + assert lib.find_sidecar_sprite(shared) is None + + _make_sprite(extras / 'sprite') + _make_sprite(extras / 'own.sprite') + assert lib.find_sidecar_sprite(shared, 'json') == extras / 'sprite.json' + assert lib.find_sidecar_sprite(shared, 'png') == extras / 'sprite.png' + assert lib.find_sidecar_sprite(own, 'png') == extras / 'own.sprite.png' + + files = {i['map_path']: i['has_sprite'] for i in lib.get_pmtiles_files()} + assert files == {'extras/own.pmtiles': True, 'extras/shared.pmtiles': True} + + +def test_sidecar_sprite_resolution(test_directory, make_pmtiles): + """The sheet of the other resolution is used when the one asked for does not exist.""" + path = make_pmtiles('map/extras/features.pmtiles', vector_metadata('towers')) + extras = test_directory / 'map/extras' + + _make_sprite(extras / 'sprite') + assert lib.find_sidecar_sprite(path, 'png', high_resolution=True) == extras / 'sprite.png' + + _make_sprite(extras / 'sprite', ('@2x',)) + assert lib.find_sidecar_sprite(path, 'png', high_resolution=True) == extras / 'sprite@2x.png' + assert lib.find_sidecar_sprite(path, 'json', high_resolution=True) == extras / 'sprite@2x.json' + assert lib.find_sidecar_sprite(path, 'png') == extras / 'sprite.png' + + # Only the double resolution sheet exists. + (extras / 'sprite.json').unlink() + assert lib.find_sidecar_sprite(path, 'png') == extras / 'sprite@2x.png' + assert lib.find_sidecar_sprite(path, 'json') == extras / 'sprite@2x.json'