diff --git a/app/package-lock.json b/app/package-lock.json index 06f1831b..62f30a43 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", + "@mapbox/vector-tile": "^2.0.4", "@maplibre/maplibre-gl-style-spec": "^24.8.5", "@tabler/icons-react": "^3.50.0", "@testing-library/react": "^13.4.0", @@ -25,6 +26,7 @@ "maplibre-gl": "^5.24.0", "markdown-it": "^15.0.3", "mathjs": "^15.2.0", + "pbf": "^4.0.1", "pmtiles": "^4.5.0", "protomaps-themes-base": "^4.5.0", "query-string": "^10.0.0", diff --git a/app/package.json b/app/package.json index b8505ce0..6ccdda79 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", + "@mapbox/vector-tile": "^2.0.4", "@maplibre/maplibre-gl-style-spec": "^24.8.5", "@tabler/icons-react": "^3.50.0", "@testing-library/react": "^13.4.0", @@ -20,6 +21,7 @@ "maplibre-gl": "^5.24.0", "markdown-it": "^15.0.3", "mathjs": "^15.2.0", + "pbf": "^4.0.1", "pmtiles": "^4.5.0", "protomaps-themes-base": "^4.5.0", "query-string": "^10.0.0", @@ -40,7 +42,7 @@ "test": "TZ=America/New_York react-scripts test", "eject": "react-scripts eject", "cy:run": "cypress run", - "sync:map-shared": "mkdir -p ../modules/map/static/js && cp src/map-shared/overlays.js src/map-shared/overlayStyle.js ../modules/map/static/js/", + "sync:map-shared": "mkdir -p ../modules/map/static/js && cp src/map-shared/overlays.js src/map-shared/overlayStyle.js src/map-shared/terrain.js ../modules/map/static/js/", "cy:open": "cypress open" }, "eslintConfig": { @@ -75,7 +77,7 @@ "^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "/jest/babelTransform.js" }, "transformIgnorePatterns": [ - "node_modules/(?!(react-hotkeys-hook|three)/)" + "node_modules/(?!(react-hotkeys-hook|three|@mapbox/vector-tile|@mapbox/point-geometry|pbf)/)" ] }, "overrides": { diff --git a/app/src/api.exportMapPdf.test.js b/app/src/api.exportMapPdf.test.js new file mode 100644 index 00000000..77360f60 --- /dev/null +++ b/app/src/api.exportMapPdf.test.js @@ -0,0 +1,262 @@ +import {ExportCancelled, exportMapPdf} from './api'; + +const MAP = { + width: 60, height: 90, dpi: 300, title: 'A quadrangle', bounds: [-150, 61.125, -149.875, 61.25], + sheet: {name: 'A quadrangle', credits: [], date: '2026-11-15'}, + layers: [{name: 'Background', blob: new Blob(['background'])}, {name: 'Water', blob: new Blob(['water'])}], +}; + +const reply = (status, body) => ({ + ok: status >= 200 && status < 300, + status, + json: () => Promise.resolve(body), + blob: () => Promise.resolve(body), + clone() { + return this; + }, +}); + +// A server whose Job says each of `jobs` in turn, and then the last of them. +function fakeServer(jobs, {pdf = new Blob(['%PDF-']), replies = {}} = {}) { + const calls = []; + let asked = 0; + global.fetch = jest.fn(async (url, init = {}) => { + const path = String(url).replace(/^.*\/api/, ''); + const method = init.method || 'GET'; + calls.push(`${method} ${path}`); + if (replies[`${method} ${path}`]) return replies[`${method} ${path}`](); + if (path === '/map/export') return reply(201, {export_id: 'abc'}); + if (path.startsWith('/map/export/abc/layer/')) return reply(204, null); + if (path === '/map/export/abc/start') return reply(202, {job_id: 'job-1'}); + if (path === '/map/export/abc' && method === 'DELETE') return reply(204, null); + if (path === '/jobs/job-1') return reply(200, {job: jobs[Math.min(asked++, jobs.length - 1)]}); + if (path === '/map/export/abc/pdf') return reply(200, pdf); + return reply(404, {error: 'Not found'}); + }); + return calls; +} + +describe('exportMapPdf', () => { + afterEach(() => { + delete global.fetch; + }); + + test('describes the map, sends each layer, watches the Job, and fetches the PDF', async () => { + const pdf = new Blob(['%PDF-']); + const calls = fakeServer([ + {status: 'pending'}, {status: 'running', progress: 12.4}, {status: 'running', progress: 75}, + {status: 'complete'}, + ], {pdf}); + const statuses = []; + + const result = await exportMapPdf(MAP, {onStatus: text => statuses.push(text), pollMs: 0}); + + expect(result).toBe(pdf); + expect(calls).toEqual([ + 'POST /map/export', + 'POST /map/export/abc/layer/0', + 'POST /map/export/abc/layer/1', + 'POST /map/export/abc/start', + 'GET /jobs/job-1', 'GET /jobs/job-1', 'GET /jobs/job-1', 'GET /jobs/job-1', + 'GET /map/export/abc/pdf', + // With the PDF in hand, the server need keep nothing of the export. + 'DELETE /map/export/abc', + ]); + expect(statuses).toEqual([ + 'Sending the map…', + 'Sending Background (1 of 2)…', + 'Sending Water (2 of 2)…', + 'Starting the PDF…', + 'Making the PDF (12%)…', + 'Making the PDF (75%)…', + 'Fetching the PDF…', + ]); + // The map is described without its images, and each image is sent as it is. + const [, described] = global.fetch.mock.calls[0]; + expect(JSON.parse(described.body)).toEqual({ + width: 60, height: 90, dpi: 300, title: 'A quadrangle', bounds: MAP.bounds, sheet: MAP.sheet, + layers: ['Background', 'Water'], + }); + expect(global.fetch.mock.calls[2][1].body).toBe(MAP.layers[1].blob); + }); + + test('says it is waiting when another job is in the way', async () => { + fakeServer([...Array(5).fill({status: 'pending'}), {status: 'complete'}]); + const statuses = []; + await exportMapPdf(MAP, {onStatus: text => statuses.push(text), pollMs: 0}); + expect(statuses.slice(3)).toEqual([ + 'Starting the PDF…', 'Starting the PDF…', 'Starting the PDF…', + 'Waiting for another job to finish…', 'Waiting for another job to finish…', + 'Fetching the PDF…', + ]); + }); + + test('says why the map was refused', async () => { + fakeServer([], {replies: {'POST /map/export': () => reply(400, {error: 'A map of 0x0 pixels can not be exported'})}}); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('A map of 0x0 pixels can not be exported'); + }); + + test('gives the PDF without waiting for the server to remove the export', async () => { + const pdf = new Blob(['%PDF-']); + const calls = fakeServer([{status: 'complete'}], { + // The server never answers. + pdf, replies: {'DELETE /map/export/abc': () => new Promise(() => undefined)}, + }); + expect(await exportMapPdf(MAP, {pollMs: 0})).toBe(pdf); + expect(calls.at(-1)).toBe('DELETE /map/export/abc'); + }); + + test('has the server remove what was sent when the export fails', async () => { + const calls = fakeServer([{status: 'complete'}], { + replies: {'POST /map/export/abc/layer/1': () => Promise.reject(new Error('network'))}, + }); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('Could not reach the server'); + expect(calls.at(-1)).toBe('DELETE /map/export/abc'); + }); + + test('says why the Job failed', async () => { + const calls = fakeServer([{status: 'running', progress: 0}, {status: 'failed', error: 'The layer is 10x10'}]); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('The layer is 10x10'); + expect(calls).not.toContain('GET /map/export/abc/pdf'); + }); + + test('stops before anything is sent when it was already cancelled', async () => { + const calls = fakeServer([{status: 'complete'}]); + await expect(exportMapPdf(MAP, {isCancelled: () => true})).rejects.toBeInstanceOf(ExportCancelled); + expect(calls).toEqual([]); + }); + + test('has the server remove what was sent when it is stopped while layers are sent', async () => { + const calls = fakeServer([{status: 'complete'}]); + // Stopped once the first layer has been sent. + const rejected = exportMapPdf(MAP, {pollMs: 0, isCancelled: () => calls.includes('POST /map/export/abc/layer/0')}); + await expect(rejected).rejects.toBeInstanceOf(ExportCancelled); + expect(calls).toEqual(['POST /map/export', 'POST /map/export/abc/layer/0', 'DELETE /map/export/abc']); + }); + + test('has the server stop the export when it is stopped while the PDF is made', async () => { + const calls = fakeServer([{status: 'running', progress: 10}]); + let polls = 0; + const rejected = exportMapPdf(MAP, { + pollMs: 0, + // Stopped once the Job has been asked about twice. + isCancelled: () => calls.includes('GET /jobs/job-1') && ++polls > 2, + }); + await expect(rejected).rejects.toMatchObject({cancelled: true}); + // The server cancels the Job of an export which is removed. + expect(calls.at(-1)).toBe('DELETE /map/export/abc'); + expect(calls).not.toContain('GET /map/export/abc/pdf'); + }); + + test('is stopped all the same when the server can not be told', async () => { + const calls = fakeServer([{status: 'running', progress: 10}], { + replies: {'DELETE /map/export/abc': () => Promise.reject(new Error('network'))}, + }); + const rejected = exportMapPdf(MAP, {pollMs: 0, isCancelled: () => calls.includes('GET /jobs/job-1')}); + await expect(rejected).rejects.toBeInstanceOf(ExportCancelled); + }); + + test('is stopped in the middle of a request, which the server may never answer', async () => { + const abort = new AbortController(); + let signals = []; + const calls = fakeServer([{status: 'complete'}], { + replies: { + // A layer which is never taken: the request ends when it is stopped, as fetch's does. + 'POST /map/export/abc/layer/1': () => new Promise((resolve, reject) => { + signals = global.fetch.mock.calls.map(([, init]) => init?.signal); + abort.signal.addEventListener( + 'abort', () => reject(Object.assign(new Error('aborted'), {name: 'AbortError'}))); + abort.abort(); + }), + }, + }); + const rejected = exportMapPdf(MAP, {pollMs: 0, signal: abort.signal}); + await expect(rejected).rejects.toBeInstanceOf(ExportCancelled); + // Every request of the export could be stopped; the one which removes it can not. + expect(signals).toEqual([abort.signal, abort.signal, abort.signal]); + expect(calls.at(-1)).toBe('DELETE /map/export/abc'); + expect(global.fetch.mock.calls.at(-1)[1].signal).toBeUndefined(); + }); + + test.each([ + ['the Job is asked after', 'GET /jobs/job-1'], + ['the PDF is fetched', 'GET /map/export/abc/pdf'], + ])('is stopped in the middle of a request while %s', async (_, request) => { + const abort = new AbortController(); + const calls = fakeServer([{status: 'complete'}], { + replies: { + [request]: () => new Promise((resolve, reject) => { + abort.signal.addEventListener( + 'abort', () => reject(Object.assign(new Error('aborted'), {name: 'AbortError'}))); + abort.abort(); + }), + }, + }); + await expect(exportMapPdf(MAP, {pollMs: 0, signal: abort.signal})).rejects.toBeInstanceOf(ExportCancelled); + // Every request up to the one which was stopped, and that one, could be stopped. + const stoppable = global.fetch.mock.calls.slice(0, -1).map(([, init]) => init?.signal); + expect(stoppable).toHaveLength(calls.indexOf(request) + 1); + expect(stoppable.every(signal => signal === abort.signal)).toBe(true); + // It is not asked again once it is stopped; the server is only told to remove the export. + expect(calls.filter(call => call === request)).toHaveLength(1); + expect(calls.at(-1)).toBe('DELETE /map/export/abc'); + }); + + test('a Job which someone else cancelled is a cancelled export', async () => { + fakeServer([{status: 'cancelled'}]); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toBeInstanceOf(ExportCancelled); + }); + + test('keeps asking when the server does not answer for a moment', async () => { + let failures = 3; + const calls = fakeServer([{status: 'complete'}], { + replies: { + 'GET /jobs/job-1': () => { + if (failures-- > 0) return Promise.reject(new Error('network')); + return reply(200, {job: {status: 'complete'}}); + }, + }, + }); + await exportMapPdf(MAP, {pollMs: 0}); + expect(calls.filter(i => i === 'GET /jobs/job-1')).toHaveLength(4); + }); + + test('asks for the PDF again when the request is lost, and not when it is refused', async () => { + const pdf = new Blob(['%PDF-']); + let lost = 3; + let calls = fakeServer([{status: 'complete'}], { + replies: { + 'GET /map/export/abc/pdf': () => { + if (lost === 3) { lost--; return Promise.reject(new Error('network')); } + if (lost-- > 0) return reply(502, {error: 'Bad gateway'}); + return reply(200, pdf); + }, + }, + }); + expect(await exportMapPdf(MAP, {pollMs: 0})).toBe(pdf); + expect(calls.filter(i => i === 'GET /map/export/abc/pdf')).toHaveLength(4); + // The map was drawn, sent and made into a PDF once. + expect(calls.filter(i => i === 'POST /map/export/abc/start')).toHaveLength(1); + + calls = fakeServer([{status: 'complete'}], { + replies: {'GET /map/export/abc/pdf': () => reply(409, {error: 'The PDF is gone'})}, + }); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('The PDF is gone'); + expect(calls.filter(i => i === 'GET /map/export/abc/pdf')).toHaveLength(1); + + calls = fakeServer([{status: 'complete'}], { + replies: {'GET /map/export/abc/pdf': () => Promise.reject(new Error('network'))}, + }); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('Could not fetch the PDF'); + expect(calls.filter(i => i === 'GET /map/export/abc/pdf')).toHaveLength(10); + }); + + test('gives up when the server stops answering, or has forgotten the Job', async () => { + fakeServer([], {replies: {'GET /jobs/job-1': () => Promise.reject(new Error('network'))}}); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('Could not reach the server'); + expect(global.fetch.mock.calls.filter(([url]) => String(url).endsWith('/jobs/job-1'))).toHaveLength(10); + + fakeServer([], {replies: {'GET /jobs/job-1': () => reply(404, {error: 'unknown'})}}); + await expect(exportMapPdf(MAP, {pollMs: 0})).rejects.toThrow('The server forgot the export'); + }); +}); diff --git a/app/src/api.js b/app/src/api.js index 2c9e741f..969eb4d6 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1758,6 +1758,158 @@ export async function setMapDefaultLocation(lat, lon, zoom) { } } +// Make a PDF of a map from the images of its layers ({name, blob}, from the bottom up). Returns the +// PDF (a Blob); throws an Error which says what went wrong. +// `bounds` is the west, south, east and north edges of the images, in degrees. +// What exportMapPdf throws when the person exporting stopped it: not a failure to be told of. +export class ExportCancelled extends Error { + constructor() { + super('The export was cancelled'); + this.name = 'ExportCancelled'; + this.cancelled = true; + } +} + +// How many times in a row the server may not answer, while the PDF of an export is made or fetched, +// before the export is given up on: the API is busy while it makes a PDF, and a Raspberry Pi more so. +const EXPORT_POLL_FAILURES = 10; + +// Make the PDF of a map from the images of its layers (see modules/map/export_job.py). The PDF is a +// minute or two of work on a Raspberry Pi, so it is a Job: the map is described, the image of each +// layer is sent, the Job is started and watched, and the PDF is fetched when the Job is done. +// +// onStatus(text): told how far along it is. isCancelled(): asked, between steps, whether to stop; +// signal: an AbortSignal which stops it at once, in the middle of a request (an image of a layer +// is tens of megabytes, and a request to a server which has stopped answering never ends); +// the server is then told to stop the export and remove what was sent. Rejects with +// ExportCancelled when it was stopped. +// +// However it ends, the server is told to remove what it has of the export: the images are a hundred +// megabytes and the PDF tens, on a disk which may be an SD card. +export async function exportMapPdf( + {width, height, dpi, title, bounds, layers, sheet}, + {onStatus = () => undefined, isCancelled = () => false, signal = undefined, pollMs = 1000} = {}, +) { + const exports = `${API_URI}/map/export`; + let exportId = null; + // Best effort: the server removes old exports itself, later. An export which is stopped while + // it is being described has no id yet to remove it by: what the server made of it is one small + // file, which it removes with the old ones. + const remove = async () => { + if (exportId) await fetch(`${exports}/${exportId}`, {method: 'DELETE'}).catch(() => undefined); + }; + try { + return await sendAndFetch(); + } catch (e) { + // A request which was stopped because the export was is not a failure to be told of. + throw signal?.aborted ? new ExportCancelled() : e; + } finally { + // Not waited for: a server which is slow to answer is not to keep a PDF from who made it. + remove(); + } + + async function sendAndFetch() { + const send = async (url, init, fallback) => { + let response; + try { + response = await fetch(url, {...init, signal}); + } catch (e) { + throw new Error('Could not reach the server'); + } + if (!response.ok) throw new Error(await getErrorMessage(response, fallback)); + return response; + }; + const pause = () => new Promise(resolve => setTimeout(resolve, pollMs)); + // Stopped by the person exporting: removing the export cancels its Job. + const stopIfCancelled = async () => { + if (isCancelled() || signal?.aborted) throw new ExportCancelled(); + }; + + await stopIfCancelled(); + onStatus('Sending the map…'); + const created = await send(exports, { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({width, height, dpi, title, bounds, sheet, layers: layers.map(i => i.name)}), + }, 'Could not export the map'); + exportId = (await created.json()).export_id; + + // One layer a request: together they are more than a request may be, and more than a slow + // connection sends in the time a request is given. + for (const [index, layer] of layers.entries()) { + await stopIfCancelled(); + onStatus(`Sending ${layer.name} (${index + 1} of ${layers.length})…`); + await send(`${exports}/${exportId}/layer/${index}`, {method: 'POST', body: layer.blob}, + `Could not send ${layer.name}`); + } + + await stopIfCancelled(); + const started = await send(`${exports}/${exportId}/start`, {method: 'POST'}, 'Could not make the PDF'); + const {job_id: jobId} = await started.json(); + + let failures = 0; + let waited = 0; + while (true) { + await stopIfCancelled(); + let job; + try { + const response = await fetch(`${API_URI}/jobs/${jobId}`, {signal}); + if (response.status === 404) throw Object.assign(new Error('The server forgot the export'), {final: true}); + if (!response.ok) throw new Error('Could not ask how the PDF is coming'); + job = (await response.json()).job; + failures = 0; + } catch (e) { + if (e.final || ++failures >= EXPORT_POLL_FAILURES) { + throw e.final ? e : new Error('Could not reach the server'); + } + } + if (job?.status === 'complete') break; + if (job?.status === 'failed') throw new Error(job.error || 'The PDF could not be made'); + if (job?.status === 'cancelled') throw new ExportCancelled(); + if (job?.status === 'pending') { + // A Job is taken up within a moment, unless another (a video being transcoded) is in its way. + onStatus(++waited > 3 ? 'Waiting for another job to finish…' : 'Starting the PDF…'); + } else if (job) { + onStatus(`Making the PDF (${Math.round(job.progress || 0)}%)…`); + } + await pause(); + } + + // The PDF is made, and kept for an hour. A request which is lost now is asked again: not to be + // the reason all of it is done again. + onStatus('Fetching the PDF…'); + for (failures = 0; ; ) { + await stopIfCancelled(); + let response; + try { + response = await fetch(`${exports}/${exportId}/pdf`, {signal}); + } catch (e) { + response = null; + } + if (response?.ok) return await response.blob(); + // What the server refuses, it will refuse again. + if (response && response.status < 500) { + throw new Error(await getErrorMessage(response, 'Could not fetch the PDF')); + } + if (++failures >= EXPORT_POLL_FAILURES) throw new Error('Could not fetch the PDF'); + await pause(); + } + } +} + +// What the USGS calls a quadrangle: {name, state, exact}, or null where it has no name (most of the +// world), and when it can not be asked. A map is exported without its name as well as with it. +export async function getMapQuadName({west, south, east, north}) { + try { + const response = await apiGet( + `${API_URI}/map/quad?west=${west}&south=${south}&east=${east}&north=${north}`); + if (!response.ok) return null; + return (await response.json()).quad || null; + } catch (e) { + return null; + } +} + export async function searchMap(query, limit = 12, offset = 0, lat = null, lon = null) { let url = `${API_URI}/map/search?q=${encodeURIComponent(query)}&limit=${limit}&offset=${offset}`; if (lat !== null && lon !== null) { diff --git a/app/src/components/MapQuadPicker.js b/app/src/components/MapQuadPicker.js new file mode 100644 index 00000000..ac98c4b9 --- /dev/null +++ b/app/src/components/MapQuadPicker.js @@ -0,0 +1,263 @@ +import React, {useEffect, useRef, useState} from "react"; +import {Button, Checkbox, Group, Radio} from "./ui"; +import {EXPORT_DPI, EXPORT_DPIS} from "./mapExport"; +import {EXPORT_LOOKS, PRINT_LOOK} from "./mapPrintStyle"; +import { + EMPTY_FEATURES, + QUAD_MIN_ZOOM, + quadAt, + quadGridLines, + quadLabel, + quadPolygon, + quadSize, + quadTitle, +} from "./mapQuads"; + +// What the ids of the picker's layers and sources start with: they are of the app, not of the map. +export const QUAD_LAYER_PREFIX = "quad-"; +const GRID_SOURCE = "quad-grid"; +const SELECTED_SOURCE = "quad-selected"; +// The picker's own layers, in the order they are drawn (all above the map's). +const QUAD_LAYERS = [ + { + id: "quad-grid-lines", type: "line", source: GRID_SOURCE, + paint: {"line-color": "#e8590c", "line-width": 1.5, "line-opacity": 0.8}, + }, + { + id: "quad-selected-fill", type: "fill", source: SELECTED_SOURCE, + paint: {"fill-color": "#e8590c", "fill-opacity": 0.2}, + }, + { + id: "quad-selected-outline", type: "line", source: SELECTED_SOURCE, + paint: {"line-color": "#e8590c", "line-width": 3.5}, + }, +]; + +export function formatDistance(meters, scaleUnit) { + return scaleUnit === "imperial" + ? `${(meters / 1609.344).toFixed(1)} mi` + : `${(meters / 1000).toFixed(1)} km`; +} + +export function formatSheet(millimeters, scaleUnit) { + return scaleUnit === "imperial" + ? `${(millimeters / 25.4).toFixed(1)} in` + : `${(millimeters / 10).toFixed(1)} cm`; +} + +function selectedFeatures(quad) { + return quad ? {type: "FeatureCollection", features: [quadPolygon(quad)]} : EMPTY_FEATURES; +} + +// Draws the grid of 1:24,000 quadrangles over the map while the user chooses one to export. +// +// The grid and the chosen quadrangle are layers of the map, which a change of its style removes; they +// are put back whenever the style has loaded. +// +// onExport(quad, {sheet, dpi, name}): make the PDF; `sheet` is whether the map is to be on a sheet, +// with a margin, a grid and scale bars, `dpi` is how sharp it is to be, and `name` is what the USGS +// calls the quadrangle ({name, state, exact}), or null. +// +// fetchName(quad): asks for that name; it resolves to null where there is none. +// +// look, onLook(look): the look the map is exported in (see mapPrintStyle.js), and what is told when +// another is chosen. The map is shown in it, so what is seen is what is exported. +// +// While the PDF is being made `status` says how far along it is, and nothing else can be chosen; +// Cancel then stops it (onCancel()) and does not close the picker. `error` says why the last one +// could not be made. +export default function MapQuadPicker({ + map, scaleUnit, initialQuad = null, onExport, onCancel, onClose, fetchName, status = null, error = null, + look = null, onLook = null, +}) { + const busy = status !== null; + const busyRef = useRef(busy); + busyRef.current = busy; + const [selected, setSelected] = useState(initialQuad); + const selectedRef = useRef(initialQuad); + // Whether the map is zoomed in far enough for the grid to be drawn. + const [gridShown, setGridShown] = useState(true); + const [sheet, setSheet] = useState(true); + const [dpi, setDpi] = useState(EXPORT_DPI); + // What the chosen quadrangle is called: {id, name} once it is known, for the quadrangle it is of. + const [named, setNamed] = useState(null); + const name = selected && named?.id === selected.id ? named.name : null; + + useEffect(() => { + if (!selected || !fetchName) return; + let current = true; + Promise.resolve(fetchName(selected)) + .then(found => current && setNamed({id: selected.id, name: found || null})) + .catch(() => current && setNamed({id: selected.id, name: null})); + // An answer for a quadrangle which is no longer the chosen one is not used. + return () => { + current = false; + }; + }, [selected, fetchName]); + + useEffect(() => { + if (!map) return; + + const drawGrid = () => { + const source = map.getSource(GRID_SOURCE); + if (!source) return; + const bounds = map.getBounds(); + const grid = map.getZoom() >= QUAD_MIN_ZOOM + ? quadGridLines({ + west: bounds.getWest(), south: bounds.getSouth(), + east: bounds.getEast(), north: bounds.getNorth(), + }) + : null; + source.setData(grid || EMPTY_FEATURES); + setGridShown(grid !== null); + }; + + const drawSelected = () => { + const source = map.getSource(SELECTED_SOURCE); + if (source) source.setData(selectedFeatures(selectedRef.current)); + }; + + const addLayers = () => { + if (!map.getSource(GRID_SOURCE)) map.addSource(GRID_SOURCE, {type: "geojson", data: EMPTY_FEATURES}); + if (!map.getSource(SELECTED_SOURCE)) { + map.addSource(SELECTED_SOURCE, {type: "geojson", data: EMPTY_FEATURES}); + } + for (const layer of QUAD_LAYERS) { + if (!map.getLayer(layer.id)) map.addLayer(layer); + } + drawGrid(); + drawSelected(); + }; + + const choose = (e) => { + // The quadrangle being exported stays the chosen one. + if (busyRef.current) return; + const quad = quadAt(e.lngLat.lng, e.lngLat.lat); + // Choosing the chosen quadrangle again lets go of it. + const next = quad && selectedRef.current?.id === quad.id ? null : quad; + selectedRef.current = next; + setSelected(next); + drawSelected(); + }; + + addLayers(); + map.on("style.load", addLayers); + map.on("moveend", drawGrid); + map.on("click", choose); + + return () => { + map.off("style.load", addLayers); + map.off("moveend", drawGrid); + map.off("click", choose); + try { + for (const layer of [...QUAD_LAYERS].reverse()) { + if (map.getLayer(layer.id)) map.removeLayer(layer.id); + } + for (const source of [GRID_SOURCE, SELECTED_SOURCE]) { + if (map.getSource(source)) map.removeSource(source); + } + } catch (e) { + // The map itself is gone, and its layers with it. + } + }; + }, [map]); + + const size = selected ? quadSize(selected) : null; + + // Part of the app's interface over the map, like the layer control: plain, token-colored chrome. + const panelStyle = { + position: "absolute", + bottom: "2.5rem", + left: "50%", + transform: "translateX(-50%)", + zIndex: 1000, + background: "var(--panel)", + border: "1px solid var(--border)", + color: "var(--text)", + padding: "0.625rem 0.75rem", + fontSize: "0.8125rem", + width: "max-content", + maxWidth: "calc(100% - 1.25rem)", + }; + + return
+
Export a 1:24,000 map
+ {selected + ?
+ {name &&
{quadTitle(name)}
} +
{quadLabel(selected)}
+
+ {formatDistance(size.widthMeters, scaleUnit)} × {formatDistance(size.heightMeters, scaleUnit)} + {" on the ground, "} + {formatSheet(size.sheetWidthMm, scaleUnit)} × {formatSheet(size.sheetHeightMm, scaleUnit)} + {" printed"} +
+
+ :
+ {gridShown + ? "Click a quadrangle on the map to choose it." + : "Zoom in to see the quadrangles."} +
} + setSheet(e.currentTarget.checked)} + /> + setDpi(Number(value))} + > + + {EXPORT_DPIS.map(value => )} + + + {look && onLook && + + {EXPORT_LOOKS.map(({value, label}) => )} + + } + {look === PRINT_LOOK && onLook &&
+ Its colors can be changed under Layers. +
} + {status &&
{status}
} + {error && !status &&
+ {error} +
} +
+ + +
+
; +} diff --git a/app/src/components/MapQuadPicker.test.js b/app/src/components/MapQuadPicker.test.js new file mode 100644 index 00000000..1badf474 --- /dev/null +++ b/app/src/components/MapQuadPicker.test.js @@ -0,0 +1,315 @@ +import React from 'react'; +import {act, fireEvent, screen, waitFor} from '@testing-library/react'; +import {renderUI} from '../test-utils'; +import MapQuadPicker, {formatDistance, formatSheet} from './MapQuadPicker'; +import {quadAt} from './mapQuads'; + +// As much of a MapLibre map as the picker uses. +class FakeMap { + constructor({zoom = 12, bounds = {west: -105.175, south: 40.26, east: -104.925, north: 40.4}} = {}) { + this.zoom = zoom; + this.bounds = bounds; + this.sources = {}; + this.layers = []; + this.handlers = {}; + } + + on(event, handler) { + (this.handlers[event] = this.handlers[event] || []).push(handler); + } + + off(event, handler) { + this.handlers[event] = (this.handlers[event] || []).filter(i => i !== handler); + } + + fire(event, data) { + for (const handler of [...(this.handlers[event] || [])]) handler(data); + } + + addSource(id, source) { + this.sources[id] = {data: source.data, setData(data) { this.data = data; }}; + } + + getSource(id) { + return this.sources[id]; + } + + removeSource(id) { + delete this.sources[id]; + } + + addLayer(layer) { + this.layers.push(layer); + } + + getLayer(id) { + return this.layers.find(i => i.id === id); + } + + removeLayer(id) { + this.layers = this.layers.filter(i => i.id !== id); + } + + getZoom() { + return this.zoom; + } + + getBounds() { + const {west, south, east, north} = this.bounds; + return {getWest: () => west, getSouth: () => south, getEast: () => east, getNorth: () => north}; + } + + // What a style change does: every layer and source is gone, then the new style has loaded. + changeStyle() { + this.sources = {}; + this.layers = []; + this.fire('style.load'); + } +} + +const click = (map, lng, lat) => act(() => map.fire('click', {lngLat: {lng, lat}})); +const gridLines = map => map.getSource('quad-grid').data.features[0]?.geometry.coordinates || []; +const selectedIds = map => map.getSource('quad-selected').data.features.map(f => f.properties.id); +const BERTHOUD = quadAt(-105.0595, 40.31); + +function renderPicker(map, props = {}) { + const onClose = jest.fn(); + const result = renderUI(); + return {...result, onClose}; +} + +describe('choosing a quadrangle to export', () => { + test('the grid is drawn over the view, above the layers of the map', () => { + const map = new FakeMap(); + map.layers.push({id: 'roads'}); + renderPicker(map); + + expect(gridLines(map)).toHaveLength(7); + expect(map.layers.map(l => l.id)) + .toEqual(['roads', 'quad-grid-lines', 'quad-selected-fill', 'quad-selected-outline']); + expect(screen.getByText('Click a quadrangle on the map to choose it.')).toBeInTheDocument(); + expect(screen.getByRole('button', {name: 'Export PDF'})).toBeDisabled(); + }); + + test('the grid follows the map', () => { + const map = new FakeMap(); + renderPicker(map); + map.bounds = {west: -105.175, south: 40.26, east: -104.925, north: 40.6}; + act(() => map.fire('moveend')); + expect(gridLines(map)).toHaveLength(8); + }); + + test('there is no grid when zoomed out, and the user is told to zoom in', () => { + const map = new FakeMap({zoom: 5}); + renderPicker(map); + expect(gridLines(map)).toEqual([]); + expect(screen.getByText('Zoom in to see the quadrangles.')).toBeInTheDocument(); + + map.zoom = 9; + act(() => map.fire('moveend')); + expect(gridLines(map)).toHaveLength(7); + expect(screen.getByText('Click a quadrangle on the map to choose it.')).toBeInTheDocument(); + }); + + test('a click chooses the quadrangle under it, and says what it is', () => { + const map = new FakeMap(); + renderPicker(map); + click(map, -105.0595, 40.31); + + expect(selectedIds(map)).toEqual([BERTHOUD.id]); + expect(screen.getByTestId('quad-label')).toHaveTextContent('40°15′N 105°07′30″W'); + expect(screen.getByTestId('quad-size')) + .toHaveTextContent('10.6 km × 13.9 km on the ground, 44.3 cm × 57.8 cm printed'); + + // Another click chooses another; there is one at a time. + click(map, -104.925, 40.4); + expect(selectedIds(map)).toEqual([quadAt(-104.925, 40.4).id]); + expect(screen.getByTestId('quad-label')).toHaveTextContent('40°22′30″N 105°00′W'); + }); + + test('a click on the chosen quadrangle lets go of it', () => { + const map = new FakeMap(); + renderPicker(map); + click(map, -105.0595, 40.31); + click(map, -105.075, 40.3); + expect(selectedIds(map)).toEqual([]); + expect(screen.queryByTestId('quad-label')).not.toBeInTheDocument(); + }); + + test('can start with a quadrangle already chosen', () => { + const map = new FakeMap(); + renderPicker(map, {initialQuad: BERTHOUD}); + expect(selectedIds(map)).toEqual([BERTHOUD.id]); + expect(screen.getByTestId('quad-label')).toHaveTextContent('40°15′N 105°07′30″W'); + }); + + test('the size is in the units of the map', () => { + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, scaleUnit: 'imperial'}); + expect(screen.getByTestId('quad-size')) + .toHaveTextContent('6.6 mi × 8.6 mi on the ground, 17.4 in × 22.8 in printed'); + expect(formatDistance(1609.344, 'imperial')).toBe('1.0 mi'); + expect(formatSheet(254, 'metric')).toBe('25.4 cm'); + }); + + test('the grid and the choice are put back when the style of the map changes', () => { + const map = new FakeMap(); + renderPicker(map); + click(map, -105.0595, 40.31); + + act(() => map.changeStyle()); + expect(map.layers.map(l => l.id)).toEqual(['quad-grid-lines', 'quad-selected-fill', 'quad-selected-outline']); + expect(gridLines(map)).toHaveLength(7); + expect(selectedIds(map)).toEqual([BERTHOUD.id]); + }); + + test('exports the chosen quadrangle', () => { + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport}); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: true, dpi: 300, name: null}); + }); + + test('while the PDF is being made, Cancel stops it and the picker stays', () => { + const onCancel = jest.fn(); + const {onClose} = renderPicker(new FakeMap(), { + initialQuad: BERTHOUD, onExport: jest.fn(), onCancel, status: 'Making the PDF (40%)…', + }); + const cancel = screen.getByRole('button', {name: 'Cancel'}); + expect(cancel).toBeEnabled(); + fireEvent.click(cancel); + expect(onCancel).toHaveBeenCalledTimes(1); + expect(onClose).not.toHaveBeenCalled(); + }); + + test('when nothing is being made, Cancel closes the picker', () => { + const onCancel = jest.fn(); + const {onClose} = renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport: jest.fn(), onCancel}); + fireEvent.click(screen.getByRole('button', {name: 'Cancel'})); + expect(onClose).toHaveBeenCalledTimes(1); + expect(onCancel).not.toHaveBeenCalled(); + }); + + test('exports the map alone when the sheet is turned off', () => { + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport}); + const sheet = screen.getByRole('checkbox', {name: 'Margins, grid and scale bars'}); + expect(sheet).toBeChecked(); + fireEvent.click(sheet); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: false, dpi: 300, name: null}); + }); + + test('exports a sharper map when one is asked for', () => { + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport}); + expect(screen.getByRole('radio', {name: '300 DPI'})).toBeChecked(); + fireEvent.click(screen.getByRole('radio', {name: '400 DPI'})); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: true, dpi: 400, name: null}); + }); + + test('the look the map is exported in is chosen, and not while a map is exported', () => { + const onLook = jest.fn(); + const {unmount} = renderPicker(new FakeMap(), {initialQuad: BERTHOUD, look: 'print', onLook}); + expect(screen.getByRole('radio', {name: 'Print'})).toBeChecked(); + fireEvent.click(screen.getByRole('radio', {name: 'Dark'})); + expect(onLook).toHaveBeenCalledWith('dark'); + unmount(); + + renderPicker(new FakeMap(), { + initialQuad: BERTHOUD, look: 'dark', onLook, status: 'Drawing Water (1 of 12)…', + }); + expect(screen.getByRole('radio', {name: 'Dark'})).toBeChecked(); + expect(screen.getByRole('radio', {name: 'Light'})).toBeDisabled(); + }); + + test('there is no look to choose where none is given', () => { + renderPicker(new FakeMap(), {initialQuad: BERTHOUD}); + expect(screen.queryByRole('radio', {name: 'Print'})).toBeNull(); + }); + + test('says what the USGS calls the quadrangle, and exports it by that name', async () => { + const named = {name: 'Berthoud', state: 'Colorado', exact: true}; + const fetchName = jest.fn().mockResolvedValue(named); + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport, fetchName}); + expect(await screen.findByTestId('quad-name')).toHaveTextContent('Berthoud, Colorado'); + expect(fetchName).toHaveBeenCalledWith(BERTHOUD); + // Where it is, is still said. + expect(screen.getByTestId('quad-label')).toHaveTextContent('40°15′N 105°07′30″W'); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: true, dpi: 300, name: named}); + }); + + test('a quadrangle the USGS has no name for goes by where it is', async () => { + const fetchName = jest.fn().mockResolvedValue(null); + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport, fetchName}); + await waitFor(() => expect(fetchName).toHaveBeenCalled()); + expect(screen.queryByTestId('quad-name')).toBeNull(); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: true, dpi: 300, name: null}); + }); + + test('a name which can not be asked for is no name', async () => { + const fetchName = jest.fn().mockRejectedValue(new Error('offline')); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, fetchName}); + await waitFor(() => expect(fetchName).toHaveBeenCalled()); + expect(screen.queryByTestId('quad-name')).toBeNull(); + expect(screen.getByTestId('quad-label')).toBeInTheDocument(); + }); + + test('the name of one quadrangle is not shown for another', async () => { + let answer; + const fetchName = jest.fn() + .mockImplementationOnce(() => new Promise(resolve => { + answer = resolve; + })) + .mockResolvedValue({name: 'Windsor', state: 'Colorado', exact: true}); + const map = new FakeMap(); + renderPicker(map, {initialQuad: BERTHOUD, fetchName}); + // Another quadrangle is chosen before the first is named. + act(() => map.fire('click', {lngLat: {lng: -104.925, lat: 40.4}})); + expect(await screen.findByTestId('quad-name')).toHaveTextContent('Windsor, Colorado'); + await act(async () => answer({name: 'Berthoud', state: 'Colorado', exact: true})); + expect(screen.getByTestId('quad-name')).toHaveTextContent('Windsor, Colorado'); + }); + + test('while the PDF is being made it says how far along it is, and the choice stays', () => { + const map = new FakeMap(); + const onExport = jest.fn(); + renderPicker(map, {initialQuad: BERTHOUD, onExport, status: 'Drawing Water (2 of 13)…'}); + + expect(screen.getByRole('status')).toHaveTextContent('Drawing Water (2 of 13)…'); + expect(screen.getByRole('button', {name: 'Export PDF'})).toBeDisabled(); + // An export which can not be stopped has no way to stop it. + expect(screen.getByRole('button', {name: 'Cancel'})).toBeDisabled(); + + // A click on the map does not choose another quadrangle. + click(map, -104.925, 40.4); + expect(selectedIds(map)).toEqual([BERTHOUD.id]); + expect(screen.getByTestId('quad-label')).toHaveTextContent('40°15′N 105°07′30″W'); + }); + + test('says why the PDF could not be made, and it can be tried again', () => { + const onExport = jest.fn(); + renderPicker(new FakeMap(), {initialQuad: BERTHOUD, onExport, error: 'Could not reach the server'}); + expect(screen.getByRole('alert')).toHaveTextContent('Could not reach the server'); + fireEvent.click(screen.getByRole('button', {name: 'Export PDF'})); + expect(onExport).toHaveBeenCalledWith(BERTHOUD, {sheet: true, dpi: 300, name: null}); + expect(screen.getByRole('button', {name: 'Close'})).toBeEnabled(); + }); + + test('leaves nothing on the map when it is closed', () => { + const map = new FakeMap(); + map.layers.push({id: 'roads'}); + const {onClose, unmount} = renderPicker(map); + fireEvent.click(screen.getByRole('button', {name: 'Cancel'})); + expect(onClose).toHaveBeenCalled(); + + unmount(); + expect(map.layers.map(l => l.id)).toEqual(['roads']); + expect(map.sources).toEqual({}); + expect(Object.values(map.handlers).flat()).toEqual([]); + }); +}); diff --git a/app/src/components/MapViewer.js b/app/src/components/MapViewer.js index e9aec996..b6de88b1 100644 --- a/app/src/components/MapViewer.js +++ b/app/src/components/MapViewer.js @@ -5,8 +5,23 @@ import "maplibre-gl/dist/maplibre-gl.css"; import {Protocol} from "pmtiles"; import layers from "protomaps-themes-base"; import mlcontour from "@acalcutt/maplibre-contour-pmtiles"; -import {addMapPin, deleteMapPin, getMapFiles, getMapPins, searchMap, setMapDefaultLocation} from "../api"; +import { + addMapPin, + deleteMapPin, + exportMapPdf, + getMapQuadName, + getMapFiles, + getMapPins, + searchMap, + setMapDefaultLocation, +} from "../api"; import {MAP_VIEWER_URI} from "./Vars"; +import {readTileBounds, readVectorTile} from "./mapTiles"; +import { + lookStyle, PRINT_LOOK, PRINT_SWATCHES, printContours, printLayers, savedExportLook, savedPrintColors, + saveExportLook, savePrintColors, +} from "./mapPrintStyle"; +import {exportLegend} from "./mapExportLegend"; import {Button, Checkbox, Header, Icon, Panel, TextInput, Toggle} from "./ui"; import {SettingsContext, ThemeContext} from "../contexts/contexts"; import {mapFlavor, mapSprite} from "../themes/names"; @@ -14,11 +29,13 @@ import {poiPopupHtml} from "./mapPoiPopup"; import {solarCalculatorPath} from "./calculators/solar"; import { addOverlaysToStyle, + labelsOverGround, buildOverlayLayers, isOverlayEnabled, isOverlayLayerEnabled, isOverlayLayerId, isStyledOverlay, + overlaySourceId, loadOverlayState, mediaUrl, overlayColor, @@ -33,6 +50,23 @@ import { setOverlayLayerEnabled, } from "./mapOverlays"; import {addMissingOverlayIcon, loadOverlayStyles, OVERLAY_ICONS_SPRITE_ID} from "./mapOverlayStyle"; +import MapQuadPicker, {QUAD_LAYER_PREFIX} from "./MapQuadPicker"; +import {contourInterval, contourThresholds, DEFAULT_TERRAIN_MAX_ZOOM, terrainMaxZoom} from "../map-shared/terrain.js"; +import {quadAt} from "./mapQuads"; +import { + EXPORT_DPI, + exportFileName, + exportFrame, + exportGroups, + exportContours, + exportSheet, + exportTitle, + maxCanvasSide, + maxExportPixels, + renderExport, + sourcesOutside, + saveBlob, +} from "./mapExport"; // Layer groups matching the standalone viewer. const LAYER_GROUPS = { @@ -68,13 +102,16 @@ const SKIP_SECONDARY = new Set(["background", "earth"]); // Set up contour DEM source (must be called once before buildStyle). let demSource = null; let terrainUrl = null; -function initContourSource(path) { - terrainUrl = mediaUrl(path); +// How deep the terrain file goes: contours are drawn from as deep as it does. +let terrainZoom = DEFAULT_TERRAIN_MAX_ZOOM; +function initContourSource(file) { + terrainUrl = mediaUrl(file.path); if (demSource) return; + terrainZoom = terrainMaxZoom(file); demSource = new mlcontour.DemSource({ url: `pmtiles://${window.location.origin}${terrainUrl}`, encoding: "terrarium", - maxzoom: 8, + maxzoom: terrainZoom, worker: true, }); demSource.setupMaplibre(maplibregl); @@ -86,16 +123,19 @@ function initContourSource(path) { // `sprite` is deliberately separate from `flavor`: we ship two sprite sets (light, dark) // and the monochrome themes draw a `black` basemap, which has no sprites of its own. // overlays: the map files which are not a basemap; overlayState: which of them (and of their -// layers) the user turned on. +// layers) the user turned on. printColors: the colors a person chose for the printed map. function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = "metric", - sprite = "light", overlays = [], overlayState = {}) { + sprite = "light", overlays = [], overlayState = {}, printColors = {}) { const styleSources = {}; + // The contour lines of the printed map, in the colors a person chose for it. + const contours = printContours(printColors); const allLayers = []; for (let i = 0; i < sources.length; i++) { const {name, url} = sources[i]; styleSources[name] = {type: "vector", url}; - const fileLayers = layers(name, flavor); + // The printed map is not a flavor a basemap has: it is the map's own (see mapPrintStyle.js). + const fileLayers = flavor === PRINT_LOOK ? printLayers(name, printColors) : layers(name, flavor); if (i === 0) { // Primary source: use layer IDs as-is. allLayers.push(...fileLayers); @@ -108,6 +148,9 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " } } + // The labels of every basemap file are over the ground of every file. + allLayers.splice(0, allLayers.length, ...labelsOverGround(allLayers)); + // Add terrain sources and layers if available. if (hasTerrain && demSource) { // Read saved visibility to set initial layer state correctly. @@ -146,27 +189,14 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " elevationKey: "ele", levelKey: "level", multiplier: isImperial ? 3.28084 : 1, // meters to feet conversion - thresholds: isImperial ? { - // [minor, major] contour intervals in feet per zoom level. - 4: [1000, 5000], - 5: [500, 2500], - 6: [200, 1000], - 7: [200, 1000], - 8: [100, 500], - } : { - // [minor, major] contour intervals in meters per zoom level. - 4: [500, 2500], - 5: [200, 1000], - 6: [100, 500], - 7: [100, 500], - 8: [50, 200], - }, + // [minor, major] contour intervals per zoom level, as fine as the terrain file can bear. + thresholds: contourThresholds(isImperial, terrainZoom), }); const elevUnit = isImperial ? "ft" : "m"; styleSources["contours"] = { type: "vector", tiles: [contourUrl], - maxzoom: 8, + maxzoom: terrainZoom, }; allLayers.push( @@ -177,7 +207,8 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " "source-layer": "contours", filter: ["!=", ["get", "level"], 1], layout: {visibility: contourVis}, - paint: {"line-color": "rgba(150,100,50,0.3)", "line-width": 0.5}, + paint: flavor === PRINT_LOOK + ? contours.minor : {"line-color": "rgba(150,100,50,0.3)", "line-width": 0.5}, }, { id: "contour-lines-major", @@ -186,7 +217,8 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " "source-layer": "contours", filter: ["==", ["get", "level"], 1], layout: {visibility: contourVis}, - paint: {"line-color": "rgba(150,100,50,0.5)", "line-width": 1}, + paint: flavor === PRINT_LOOK + ? contours.major : {"line-color": "rgba(150,100,50,0.5)", "line-width": 1}, }, { id: "contour-labels", @@ -206,6 +238,7 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = " "text-color": flavor === "light" ? "rgba(150,100,50,0.8)" : "rgba(210,180,140,0.9)", "text-halo-color": flavor === "light" ? "rgba(255,255,255,0.8)" : "rgba(0,0,0,0.8)", "text-halo-width": 1, + ...(flavor === PRINT_LOOK ? contours.label : {}), }, }, ); @@ -327,10 +360,29 @@ function OverlayControl({file, label, state, onToggleFile, onToggleLayer, onTogg ; } +// The colors of the printed map which are of a group of the layer control, each to be chosen. +// colors: what was chosen ({swatch id: "#rrggbb"}); onChange(colors): told what is chosen now. +function PrintSwatches({group, colors, onChange}) { + const swatches = PRINT_SWATCHES.filter(swatch => swatch.group === group); + if (!swatches.length) return null; + return + {swatches.map(swatch => onChange({...colors, [swatch.id]: e.target.value})} + style={{width: "1.25rem", height: "1.25rem", padding: 0, border: "1px solid var(--border)", + background: "none", cursor: "pointer"}} + />)} + ; +} + function LayerControl({ map, scaleUnit, onScaleUnitChange, visibilityRef, overlays = [], overlayState = {}, onToggleOverlay, onToggleOverlayLayer, - onToggleAllOverlayLayers, + onToggleAllOverlayLayers, printColors = null, onPrintColors = null, }) { const [visibility, setVisibility] = useState(() => { const v = getInitialVisibility(); @@ -410,14 +462,27 @@ function LayerControl({ {!collapsed &&
{Object.keys(LAYER_GROUPS).map(name => -
+
toggleGroup(name, e.currentTarget.checked)} /> + {printColors && onPrintColors && }
)} + {printColors && onPrintColors &&
+ Colors are of the printed map.{" "} + {Object.keys(printColors).length > 0 && } +
} {overlays.length > 0 &&
Overlays
@@ -659,11 +724,37 @@ export default function MapViewer() { const [addingPin, setAddingPin] = useState(null); // {lat, lon} when pin dialog is open const markersRef = useRef([]); const pinsVisibleRef = useRef(true); + // Choosing a quadrangle to export: null, or {quad} (the quadrangle to start with, if any). While + // choosing, a click on the map chooses, so the ref tells the map's own click handlers to stand by. + const [pickingQuad, setPickingQuad] = useState(null); + const pickingQuadRef = useRef(false); + // The look a map is exported in. While a quadrangle is chosen the map is shown in it: what is + // seen is what the PDF will be. + const [exportLook, setExportLook] = useState(savedExportLook); + const handleExportLook = useCallback(look => { + setExportLook(look); + saveExportLook(look); + }, []); + // The colors a person chose for the printed map: {swatch id: "#rrggbb"}. + const [printColors, setPrintColors] = useState(savedPrintColors); + const handlePrintColors = useCallback(colors => setPrintColors(savePrintColors(colors)), []); + // Exporting the chosen quadrangle: how far along it is, or why it could not be done. + const [exportStatus, setExportStatus] = useState(null); + // Whether the person exporting has asked for the export which is being made to stop. + const exportCancelledRef = useRef(false); + // Stops the request an export is in the middle of. + const exportAbortRef = useRef(null); + // Whether a map is being exported, for what is asked outside of React (the map's own buttons). + const exportingRef = useRef(false); + const [exportError, setExportError] = useState(null); const [scaleUnit, setScaleUnit] = useState(() => { try { return localStorage.getItem(STORAGE_KEY_SCALE) || "metric"; } catch (e) { return "metric"; } }); const [searchParams, setSearchParams] = useSearchParams(); const {theme} = useContext(ThemeContext); + const {flavor: lookFlavor, sprite: lookSprite} = lookStyle(pickingQuad ? exportLook : null); + // The map is shown as it is printed: its colors can be chosen. + const printing = lookFlavor === PRINT_LOOK; const {settings, fetchSettings} = useContext(SettingsContext); // Use URL params if provided, otherwise fall back to saved default location, then 0,0,z2. @@ -690,7 +781,7 @@ export default function MapViewer() { // Fetch user's PMTiles files first to determine what base layer is needed. let hasTerrain = false; - let terrainPath = null; + let terrainFile = null; let overlayFiles = []; try { const data = await getMapFiles(); @@ -700,7 +791,7 @@ export default function MapViewer() { overlayFiles = await loadOverlayStyles(foundOverlays); if (terrain) { hasTerrain = true; - terrainPath = terrain.path; + terrainFile = terrain; } if (planet) { @@ -734,7 +825,7 @@ export default function MapViewer() { // Initialize contour source if terrain data is available. if (hasTerrain) { - initContourSource(terrainPath); + initContourSource(terrainFile); } try { @@ -785,6 +876,28 @@ export default function MapViewer() { }; map.addControl(pinControl, "top-right"); + // Choose a quadrangle to export. + const exportButton = document.createElement("button"); + exportButton.type = "button"; + exportButton.title = "Export a 1:24,000 map"; + exportButton.setAttribute("aria-label", "Export a 1:24,000 map"); + exportButton.textContent = "🖨️"; + exportButton.style.cssText = pinToggle.style.cssText; + exportButton.addEventListener("click", () => { + // Not while a map is being exported: what is said of it, and its Cancel, are there. + if (destroyed || exportingRef.current) return; + setPickingQuad(picking => (picking ? null : {quad: null})); + }); + map.addControl({ + onAdd: () => { + const container = document.createElement("div"); + container.className = "maplibregl-ctrl maplibregl-ctrl-group"; + container.appendChild(exportButton); + return container; + }, + onRemove: () => {}, + }, "top-right"); + // Right-click context menu (desktop). map.on("contextmenu", (e) => { if (destroyed) return; @@ -813,7 +926,7 @@ export default function MapViewer() { const featuresAt = (point, layers) => layers.length ? map.queryRenderedFeatures(point, {layers}) : []; map.on("click", (e) => { - if (destroyed) return; + if (destroyed || pickingQuadRef.current) return; const html = overlayPopupHtml(featuresAt(e.point, overlayLayerIds())) || poiPopupHtml(featuresAt(e.point, poiLayerIds()), e.lngLat); if (!html) return; @@ -824,6 +937,10 @@ export default function MapViewer() { }); map.on("mousemove", (e) => { if (destroyed) return; + if (pickingQuadRef.current) { + map.getCanvas().style.cursor = "crosshair"; + return; + } const hit = featuresAt(e.point, [...overlayLayerIds(), ...poiLayerIds()]).length; map.getCanvas().style.cursor = hit ? "pointer" : ""; }); @@ -885,6 +1002,9 @@ export default function MapViewer() { return () => { destroyed = true; + // An export of this map stops with it: nobody is here to be given the PDF. + exportCancelledRef.current = true; + exportAbortRef.current?.abort(); if (map) { map.remove(); } @@ -903,23 +1023,22 @@ export default function MapViewer() { }; }, []); // eslint-disable-line react-hooks/exhaustive-deps - // Rebuild style when theme changes. + // Rebuild the style when the theme changes, and when the look the map is shown in does. useEffect(() => { const map = mapRef.current; if (!map || !mapReady) return; - const center = map.getCenter(); - const zoom = map.getZoom(); + // What the user turned off is off in the new style too. Listened for before the style is + // set: a style which only differs from the one the map has is in place, and has said so, + // before `setStyle` returns. The map stays where it is: a style of ours says nothing of that. + const showAsChosen = () => applyGroupVisibility(map, layerVisibilityRef.current); + map.once("style.load", showAsChosen); // The overlays are part of the style, as the user last set them. - map.setStyle(buildStyle(activeFilesRef.current, mapFlavor(theme), hasTerrainRef.current, - scaleUnit, mapSprite(theme), overlaysRef.current, overlayStateRef.current)); - map.once("style.load", () => { - map.setCenter(center); - map.setZoom(zoom); - // Re-apply layer visibility state after style rebuild. - applyGroupVisibility(map, layerVisibilityRef.current); - }); - }, [theme, mapReady, scaleUnit]); + // In the look a map is exported in while one is chosen; as the theme has it otherwise. + map.setStyle(buildStyle(activeFilesRef.current, lookFlavor || mapFlavor(theme), hasTerrainRef.current, + scaleUnit, lookSprite || mapSprite(theme), overlaysRef.current, overlayStateRef.current, printColors)); + return () => map.off("style.load", showAsChosen); + }, [theme, lookFlavor, lookSprite, mapReady, scaleUnit, printColors]); // Remember what the user chose to see of an overlay, and show or hide its layers to match. const changeOverlayState = useCallback((file, next) => { @@ -973,6 +1092,98 @@ export default function MapViewer() { setContextMenu(null); }, [contextMenu]); + useEffect(() => { + pickingQuadRef.current = pickingQuad !== null; + const map = mapRef.current; + if (map) map.getCanvas().style.cursor = pickingQuad ? "crosshair" : ""; + }, [pickingQuad]); + + const handleExportQuad = useCallback(() => { + if (!contextMenu) return; + // Start with the quadrangle which was clicked; not while another is being exported. + if (!exportingRef.current) setPickingQuad({quad: quadAt(contextMenu.lon, contextMenu.lat)}); + setContextMenu(null); + }, [contextMenu]); + + // Make a PDF of a quadrangle: the layers which are on, drawn as they are on the map. + const handleExportPdf = useCallback(async (quad, {sheet = true, dpi = EXPORT_DPI, name = null} = {}) => { + const map = mapRef.current; + if (!map) return; + exportCancelledRef.current = false; + exportingRef.current = true; + const abort = new AbortController(); + exportAbortRef.current = abort; + // The unit the map measures in is the unit its contour lines are apart in. + const imperial = scaleUnit === "imperial"; + setExportError(null); + setExportStatus("Preparing…"); + try { + const style = map.getStyle(); + const overlayLabels = Object.fromEntries(overlaysRef.current.map( + file => [overlaySourceId(file), overlayLabel(file, overlaysRef.current)])); + // A map file which is of somewhere else is not drawn. + const outside = await sourcesOutside( + style, [quad.west, quad.south, quad.east, quad.north], readTileBounds); + const groups = exportGroups( + style.layers.filter(layer => !layer.id.startsWith(QUAD_LAYER_PREFIX) && !outside.has(layer.source)), + {layerGroups: LAYER_GROUPS, overlayLabels}); + if (!groups.length) throw new Error("No layer of the map is turned on"); + // How sharp the map may be depends on how many images it is, which is not known until + // the groups have been looked at: one with nothing in it is no image. + const maxSide = maxCanvasSide(); + const frameFor = count => exportFrame(quad, {dpi, maxSide, maxPixels: maxExportPixels(count)}); + // The zoom of the map is the same however many images it is. + const frame = frameFor(groups.length); + const rendered = await renderExport({ + MapClass: maplibregl.Map, + style, + frameFor, + groups, + ignoredPrefixes: [QUAD_LAYER_PREFIX], + readTile: readVectorTile, + setup: exportMap => exportMap.on("styleimagemissing", e => addMissingOverlayIcon(exportMap, e.id)), + // Stopped between two layers, as soon as the person exporting asks. + isCancelled: () => exportCancelledRef.current, + onLooking: () => setExportStatus("Looking at the map…"), + // The map is less sharp than was asked for when this browser, or this many layers, can not be. + onProgress: (name, index, count, drawn) => setExportStatus( + `Drawing ${name} (${index + 1} of ${count})` + + `${Math.round(drawn.dpi) < dpi ? ` at ${Math.round(drawn.dpi)} DPI` : ""}…`), + }); + const pdf = await exportMapPdf({ + ...rendered, + title: exportTitle(quad, name), + // With a sheet the map has a margin, a grid and scale bars; without, it is the map alone. + sheet: sheet ? exportSheet(quad, map, groups, new Date(), name, exportContours( + groups, LAYER_GROUPS["Contours"], contourInterval(imperial, terrainZoom, frame.zoom), imperial, + ), exportLegend(style, groups, frame.zoom)) : undefined, + }, { + // Once it is being stopped, that is what is said. + onStatus: text => !exportCancelledRef.current && setExportStatus(text), + isCancelled: () => exportCancelledRef.current, + signal: abort.signal, + }); + // Stopped while the PDF was on its way: it is not wanted. + if (!exportCancelledRef.current) saveBlob(pdf, exportFileName(quad, name)); + } catch (e) { + // Stopped by the person exporting: nothing went wrong. + if (!e?.cancelled) { + console.error("Map export failed:", e); + setExportError(e?.message || "The map could not be exported"); + } + } finally { + exportingRef.current = false; + setExportStatus(null); + } + }, [scaleUnit]); + + // Stop the export which is being made. It stops between two steps, and the server stops making the PDF. + const handleCancelExport = useCallback(() => { + exportCancelledRef.current = true; + exportAbortRef.current?.abort(); + setExportStatus("Stopping…"); + }, []); + const handleExportPng = useCallback(() => { const map = mapRef.current; if (!map) return; @@ -1099,6 +1310,8 @@ export default function MapViewer() { scaleUnit={scaleUnit} onScaleUnitChange={handleScaleUnitChange} visibilityRef={layerVisibilityRef} + printColors={printing ? printColors : null} + onPrintColors={handlePrintColors} overlays={overlays} overlayState={overlayState} onToggleOverlay={handleToggleOverlay} @@ -1143,6 +1356,14 @@ export default function MapViewer() { > Export as PNG
+
e.target.style.background = "#f0f0f0"} + onMouseLeave={e => e.target.style.background = "transparent"} + onClick={handleExportQuad} + > + Export a 1:24,000 map +
e.target.style.background = "#f0f0f0"} @@ -1168,6 +1389,23 @@ export default function MapViewer() { Set as Default View
} + {mapReady && pickingQuad && { + setExportError(null); + setPickingQuad(null); + }} + />} {addingPin && { constructor(options) { this.options = options; this.style = options.style; + this.center = {lat: 0, lng: 0}; + this.moves = []; + this.listeners = []; + this.controls = []; constructed.push(this); } @@ -28,7 +32,13 @@ jest.mock('maplibre-gl', () => { return this; } - once() { + once(event, callback) { + this.listeners.push({event, callback}); + return this; + } + + off(event, callback) { + this.listeners = this.listeners.filter(i => i.event !== event || i.callback !== callback); return this; } @@ -38,6 +48,11 @@ jest.mock('maplibre-gl', () => { setStyle(style) { this.style = style; + // MapLibre puts a style which only differs from the one it has in place, and says so, + // before this returns. + const waiting = this.listeners.filter(i => i.event === 'style.load'); + this.listeners = this.listeners.filter(i => i.event !== 'style.load'); + waiting.forEach(i => i.callback()); } getLayer(id) { @@ -58,20 +73,26 @@ jest.mock('maplibre-gl', () => { } getCenter() { - return {lat: 0, lng: 0}; + return this.center; } getZoom() { return 2; } - setCenter() { + setCenter(center) { + this.moves.push(center); + this.center = center; } setZoom() { } - addControl() { + addControl(control) { + // A control of the viewer's own (not one of MapLibre's) is put on the page. + if (typeof control?.onAdd === 'function') { + this.controls.push(global.document.body.appendChild(control.onAdd(this))); + } return this; } @@ -79,6 +100,7 @@ jest.mock('maplibre-gl', () => { } remove() { + this.controls.forEach(control => control.remove()); } } @@ -108,7 +130,16 @@ jest.mock('protomaps-themes-base', () => ({ {id: 'water', type: 'fill', source}, {id: 'places_locality', type: 'symbol', source}, ], + // The printed map: the same layers, in colors of its own. + namedTheme: () => ({}), + layersWithCustomTheme: (source) => [ + {id: 'earth', type: 'fill', source, paint: {'fill-color': 'print'}}, + {id: 'water', type: 'fill', source}, + {id: 'places_locality', type: 'symbol', source}, + ], })); +// Choosing a quadrangle draws on the map, which is not what is looked at here. +jest.mock('./MapQuadPicker', () => ({onClose}) => ); // Contours are generated in the browser from the terrain file; only the URL of that source matters here. jest.mock('@acalcutt/maplibre-contour-pmtiles', () => ({ __esModule: true, @@ -407,3 +438,54 @@ describe('the text of the map', () => { } }); }); + +describe('the look the map is exported in', () => { + const earthColor = map => map.getLayer('earth').paint?.['fill-color']; + + test('is how the map is shown while a quadrangle is chosen, where it is and with what is off still off', async () => { + const map = await renderViewer([PLANET]); + await click(screen.getByLabelText('Water')); + expect(map.visibility('water')).toBe('none'); + // The map has been moved since it was made. + map.center = {lat: 40.3, lng: -105.06}; + + await click(screen.getByTitle('Export a 1:24,000 map')); + expect(earthColor(map)).toBe('print'); + expect(map.visibility('water')).toBe('none'); + // Moved again, while the quadrangle is chosen. + map.center = {lat: 61.2, lng: -149.9}; + + await click(screen.getByText('Close the picker')); + expect(earthColor(map)).toBeUndefined(); + expect(map.visibility('water')).toBe('none'); + // Changing the look never moves the map. + expect(map.moves).toEqual([]); + expect(map.center).toEqual({lat: 61.2, lng: -149.9}); + }); + + test('has colors which are chosen where its layers are, while a quadrangle is chosen', async () => { + const map = await renderViewer([PLANET]); + // The map on the screen has no colors to choose. + expect(screen.queryByLabelText('Water: color when printed')).not.toBeInTheDocument(); + + await click(screen.getByTitle('Export a 1:24,000 map')); + const water = screen.getByLabelText('Water: color when printed'); + expect(water).toHaveValue('#a9d4f2'); + expect(screen.queryByText('Reset colors')).not.toBeInTheDocument(); + await act(async () => { + fireEvent.change(water, {target: {value: '#ff00ff'}}); + }); + expect(screen.getByLabelText('Water: color when printed')).toHaveValue('#ff00ff'); + expect(JSON.parse(localStorage.getItem('wrolpi-map-print-colors'))).toEqual({water: '#ff00ff'}); + // The map was drawn again, where it was. + expect(earthColor(map)).toBe('print'); + expect(map.moves).toEqual([]); + + await click(screen.getByText('Reset colors')); + expect(screen.getByLabelText('Water: color when printed')).toHaveValue('#a9d4f2'); + expect(localStorage.getItem('wrolpi-map-print-colors')).toBeNull(); + + await click(screen.getByText('Close the picker')); + expect(screen.queryByLabelText('Water: color when printed')).not.toBeInTheDocument(); + }); +}); diff --git a/app/src/components/mapExport.js b/app/src/components/mapExport.js new file mode 100644 index 00000000..d61cd261 --- /dev/null +++ b/app/src/components/mapExport.js @@ -0,0 +1,739 @@ +// Exporting a quadrangle of the map as a PDF. +// +// The browser draws the map, because nothing else draws it as the user sees it. A second map, off +// the screen and the size of the printed sheet, is drawn once for each layer the user has turned on, +// with only that layer showing; the API puts the images in a PDF, each in a layer of its own, and +// (when it is asked to) on a sheet: with a margin, a grid, scale bars and notes around the map. + +import {QUAD_DEGREES, QUAD_SCALE, quadLabel, quadSize, quadTitle} from "./mapQuads"; +import {isOverlayLabelLayer, isOverlayLayerId} from "./mapOverlays"; + +export const EXPORT_DPI = 300; +// How sharp a map may be asked for. More is four times the work for twice the sharpness: a +// quadrangle at 600 is more than a browser will draw, or a Raspberry Pi will make a PDF of. +export const EXPORT_DPIS = [300, 400]; +// The most the API makes a PDF of: the pixels of one layer, and of all the layers of a map together +// (MAX_PIXELS and MAX_TOTAL_PIXELS in modules/map/export.py). +const MAX_LAYER_PIXELS = 64_000_000; +const MAX_TOTAL_PIXELS = 1_000_000_000; +// The map's style is made for a screen, where its sizes (of text, of lines) are in CSS pixels. This +// is how many of those are an inch of paper: fewer is larger text and less detail. At 150 a 12 pixel +// label prints at about 6 points, which is what a topographic map uses, and a quadrangle is drawn at +// about zoom 13 to 14. +export const EXPORT_CSS_DPI = 150; +// No browser makes a canvas larger than this, and few need to be asked for more. +const MAX_CANVAS_SIDE = 16384; +const FALLBACK_CANVAS_SIDE = 4096; +const MILLIMETERS_PER_INCH = 25.4; +const TILE_SIZE = 512; +const RENDER_TIMEOUT_MS = 120_000; +// How often it is asked whether the map has been too long in drawing. +const RENDER_WATCH_MS = 1000; +// How sharp the map is on which the groups are looked at, to see which have anything in them: +// half as many pixels each way as there are CSS pixels. Anything drawn is still drawn. +const LOOKING_PIXEL_RATIO = 0.5; +// How many rows of a canvas are looked at at once. +const BLANK_BAND_ROWS = 256; + +export const BACKGROUND_GROUP = "Background"; + +// The largest canvas (in pixels a side) this browser will draw a map on. +export function maxCanvasSide() { + try { + const canvas = document.createElement("canvas"); + const gl = canvas.getContext("webgl2") || canvas.getContext("webgl"); + if (!gl) return FALLBACK_CANVAS_SIDE; + const viewport = gl.getParameter(gl.MAX_VIEWPORT_DIMS) || []; + const side = Math.min(gl.getParameter(gl.MAX_TEXTURE_SIZE), viewport[0], viewport[1], MAX_CANVAS_SIDE); + gl.getExtension("WEBGL_lose_context")?.loseContext(); + return Number.isFinite(side) && side > 0 ? side : FALLBACK_CANVAS_SIDE; + } catch (e) { + return FALLBACK_CANVAS_SIDE; + } +} + +// The most pixels each layer of a map of `layerCount` layers may be. +export function maxExportPixels(layerCount) { + return Math.min(MAX_LAYER_PIXELS, Math.floor(MAX_TOTAL_PIXELS / Math.max(1, layerCount))); +} + +function mercatorY(latitude) { + return Math.log(Math.tan(Math.PI / 4 + latitude * Math.PI / 360)); +} + +function latitudeOfMercatorY(y) { + return (2 * Math.atan(Math.exp(y)) - Math.PI / 2) * 180 / Math.PI; +} + +// How to draw a quadrangle at 1:24,000: the size of the map to make (in CSS pixels), where to put it +// (center, zoom), and how many real pixels each CSS pixel is. +// +// The map is Web Mercator, in which a quadrangle is exactly a rectangle. It is as wide as the sheet +// is at the quadrangle's middle, and as tall as Mercator makes that. +// +// The map is as sharp as was asked for (`dpi`), or as sharp as it can be: no side of it longer than +// `maxSide` (the browser's largest canvas), and no more of it than `maxPixels` (what the API takes). +export function exportFrame(quad, { + dpi = EXPORT_DPI, cssDpi = EXPORT_CSS_DPI, maxSide = MAX_CANVAS_SIDE, maxPixels = Infinity, +} = {}) { + const widthInches = quadSize(quad).sheetWidthMm / MILLIMETERS_PER_INCH; + const cssWidth = Math.round(widthInches * cssDpi); + // The width of the whole world at the zoom where the quadrangle is `cssWidth` wide. + const world = cssWidth * 360 / QUAD_DEGREES; + const [south, north] = [mercatorY(quad.south), mercatorY(quad.north)]; + const cssHeight = Math.round((north - south) / (2 * Math.PI) * world); + + // A hair under what the pixels allow: each side of the canvas is rounded to a whole pixel. + const pixelRatio = Math.min( + dpi / cssDpi, + maxSide / Math.max(cssWidth, cssHeight), + Math.sqrt(maxPixels / (cssWidth * cssHeight)) * 0.999); + return { + cssWidth, + cssHeight, + pixelRatio, + dpi: pixelRatio * cssDpi, + zoom: Math.log2(world / TILE_SIZE), + bounds: [quad.west, quad.south, quad.east, quad.north], + center: [(quad.west + quad.east) / 2, latitudeOfMercatorY((south + north) / 2)], + }; +} + +// The name of a basemap layer without the source it is of ("oregon.pmtiles:water" is "water"). +function basemapLayerName(layerId) { + return layerId.includes(":") ? layerId.split(":").slice(1).join(":") : layerId; +} + +// The kinds of layer which are drawn over what is under them without hiding it: a line, a name. +const THIN_LAYER_TYPES = new Set(["line", "symbol", "circle"]); + +// The layers of the PDF: the visible layers of the map's style, gathered as the layer control +// gathers them, from the bottom up. Each is {name, ids, raster}. An overlay is one layer, or two +// when it has both ground and labels (see orderOverlayLayers). +// +// A layer of the PDF is drawn as images, and an image is in one place among the others. The layers +// of a group are not always together in the style: the parks of Land Cover are under the water and +// its piers are over it, and a second basemap file has all of its layers over the first's. So a +// group is as many images as it must be, each with the name of the group (they are one layer of the +// PDF): a part of it which fills an area stays where it is, because moved over what was above it, +// it would hide that; a part which is only lines or names is drawn with the group's next part. +// +// styleLayers: the layers of the map's style, as the map has them now. +// layerGroups: {name: [part of a layer id, ...]}, the basemap groups of the layer control. +// overlayLabels: {source id: label} of the overlay files. +export function exportGroups(styleLayers, {layerGroups = {}, overlayLabels = {}} = {}) { + const groupNames = Object.keys(layerGroups); + // What each visible layer is of: {key, name, overlay}. + const groupOf = layer => { + if (isOverlayLayerId(layer.id)) { + // An overlay is drawn in two places: its ground under the basemap's labels, and its own + // labels over them. Each is a layer of the PDF. + const part = isOverlayLabelLayer(layer) ? "labels" : "ground"; + return { + key: `overlay ${part} ${layer.source}`, name: overlayLabels[layer.source] || layer.source, + overlay: {source: layer.source, part}, + }; + } + const name = basemapLayerName(layer.id); + // A layer in more than one group (a water label) is of the last, which is the labels. + const group = [...groupNames].reverse().find(g => layerGroups[g].some(part => name.includes(part))); + // What is in no group is the ground everything else is drawn on. + return group ? {key: `group ${group}`, name: group} : {key: "background", name: BACKGROUND_GROUP}; + }; + + // The style from the bottom up, as runs of layers which are of one group and are together. + const runs = []; + for (const layer of styleLayers) { + if (layer.layout?.visibility === "none") continue; + const group = groupOf(layer); + let run = runs[runs.length - 1]; + if (run?.key !== group.key) { + run = {...group, ids: [], raster: true, thin: true}; + runs.push(run); + } + run.ids.push(layer.id); + run.raster = run.raster && layer.type === "raster"; + run.thin = run.thin && THIN_LAYER_TYPES.has(layer.type); + } + // A run of lines is drawn with the next run of its group, when there is one. + const waiting = new Map(); + const placed = []; + for (const [index, run] of runs.entries()) { + const ids = [...(waiting.get(run.key) || []), ...run.ids]; + waiting.delete(run.key); + if (run.thin && runs.slice(index + 1).some(later => later.key === run.key)) { + waiting.set(run.key, ids); + } else { + placed.push({...run, ids}); + } + } + + // The labels of an overlay are named as its labels when its ground is a layer too. + const grounds = new Set(placed.filter(g => g.overlay?.part === "ground").map(g => g.overlay.source)); + // Two groups of one name are told apart; the images of one group have one name. + const used = new Set(); + const names = new Map(); + return placed.map(({key, name, ids, raster, overlay}) => { + if (!names.has(key)) { + if (overlay?.part === "labels" && grounds.has(overlay.source)) name = `${name} (labels)`; + let unique = name; + for (let n = 2; used.has(unique); n++) unique = `${name} (${n})`; + used.add(unique); + names.set(key, unique); + } + return {name: names.get(key), ids, raster}; + }); +} + +// The sources of a style whose map file has nothing of an area: a map of another state. Their +// layers are not drawn, each of which would be an image of nothing, counted against how sharp the +// map may be. A source is only left out when its file says where it is of, and that is elsewhere. +// +// bounds: the west, south, east and north edges of the area. +// readBounds(url): the edges of the map file a source has as its `url`, or null (see mapTiles.js). +export async function sourcesOutside(style, bounds, readBounds) { + const [west, south, east, north] = bounds; + const outside = new Set(); + await Promise.all(Object.entries(style.sources || {}).map(async ([id, source]) => { + if (typeof source.url !== "string" || !source.url.startsWith("pmtiles://")) return; + try { + const edges = await readBounds(source.url); + if (!edges) return; + const [fileWest, fileSouth, fileEast, fileNorth] = edges; + if (fileEast <= west || fileWest >= east || fileNorth <= south || fileSouth >= north) outside.add(id); + } catch (e) { + // A file which can not be asked is drawn, as it was going to be. + console.warn("Could not read where a map file is of:", source.url, e); + } + })); + return outside; +} + +// A basemap's tiles name its small roads from this zoom on. A quadrangle is drawn at about zoom 13, +// from tiles in which the streets of a town are lines with no names. +export const EXPORT_DETAIL_ZOOM = 14; +// The layers of a basemap which name roads. +const ROAD_LABELS = "roads_labels"; +const ROAD_NAMES_SOURCE = "export-road-names:"; +// More room (ems) than the name of a road is long: a name is one line. +const ROAD_NAME_MAX_EMS = 1000; +// Which way the name of a road is turned (degrees, clockwise), in what is said of the road. +const NAME_ANGLE = "export:angle"; +const ROAD_TEXT_SIZE = 12; +// About how wide a letter is, in its own height. +const LETTER_WIDTH = 0.55; +// A long road is named again each time it has gone this far (CSS pixels: 2.7 inches of a sheet). +const NAME_EVERY = 400; +// Two names of one road are never nearer than this (CSS pixels). +const SAME_NAME_APART = 150; +// The most tiles names are read from. A quadrangle is 20 to 40 of them, and more only where it is a +// sliver near a pole (455 at 85 degrees); more than this is not a quadrangle. +const MAX_DETAIL_TILES = 512; + +// The layers of `style` which name roads, but not on a map drawn at `zoom`: their style keeps the +// names for a zoom at which a street is inches long on a screen. On a sheet a road is found by its +// name. +export function unnamedRoadLayers(style, zoom) { + return (style.layers || []).filter(layer => layer.type === "symbol" && layer["source-layer"] + && String(layer.id).includes(ROAD_LABELS) && layer.minzoom > zoom); +} + +// The tiles of `zoom` which have part of `bounds` (west, south, east, north) in them: [{z, x, y}]. +export function tilesOfBounds([west, south, east, north], zoom) { + const count = 2 ** zoom; + const column = longitude => (longitude + 180) / 360 * count; + const row = latitude => (1 - mercatorY(latitude) / Math.PI) / 2 * count; + const clamp = n => Math.min(count - 1, Math.max(0, n)); + // A tile whose edge is the edge of the bounds has nothing of them in it. + const [left, right] = [clamp(Math.floor(column(west))), clamp(Math.ceil(column(east)) - 1)]; + const [top, bottom] = [clamp(Math.floor(row(north))), clamp(Math.ceil(row(south)) - 1)]; + const tiles = []; + for (let x = left; x <= right; x++) { + for (let y = top; y <= bottom; y++) tiles.push({z: zoom, x, y}); + } + return tiles; +} + +// The roads of one layer of a tile which have a name, as GeoJSON features with all that the tile says +// of them: what a layer of the style draws a name from is whatever its style asks for. +// +// tile: a tile as @mapbox/vector-tile reads it. +export function namedRoads(tile, sourceLayer, {z, x, y}) { + const layer = tile?.layers?.[sourceLayer]; + const features = []; + for (let i = 0; layer && i < layer.length; i++) { + const feature = layer.feature(i); + if (!Object.keys(feature.properties).some(key => key === "name" || key.startsWith("name:"))) continue; + const {geometry, properties} = feature.toGeoJSON(x, y, z); + features.push({type: "Feature", geometry, properties}); + } + return features; +} + +// What two pieces of one road have in common: its name, and what kind of road it is. The layers +// which name roads each name some kinds of them, so a piece of another kind may not be named at all. +function roadOf(properties) { + return `${properties.kind ?? ""}\n${nameOf(properties)}`; +} + +function nameOf(properties) { + return String(properties.name ?? Object.entries(properties).find(([key]) => key.startsWith("name:"))?.[1] ?? ""); +} + +// Where a line (of points, with the length of each of its parts) is when it has gone `along`. +function pointAlong(points, lengths, along) { + let i = 0; + while (i < lengths.length - 1 && along > lengths[i]) along -= lengths[i++]; + const part = lengths[i] ? Math.min(1, Math.max(0, along / lengths[i])) : 0; + const [from, to] = [points[i], points[i + 1]]; + return [from[0] + (to[0] - from[0]) * part, from[1] + (to[1] - from[1]) * part]; +} + +// Where named roads (of namedRoads) are named on a map drawn at `zoom` with text of `textSize` CSS +// pixels: a GeoJSON FeatureCollection of points, each with all that is said of its road, and which +// way the road goes there (NAME_ANGLE). +// +// A map writes a name along a road only where the road is longer than the name and bends little, +// and at the scale of a sheet most streets of a town are shorter than their names. So the names +// are placed here, as a map maker places them: over the middle of the road, turned as the road goes +// under the name, and over its ends when it is short; again every NAME_EVERY of a long road. +// +// A road is in pieces where two tiles meet, and where what is said of it changes. The longest +// pieces are named first, and a piece is not named near where its road already is. +export function placeRoadNames(features, zoom, textSize = ROAD_TEXT_SIZE) { + const world = TILE_SIZE * 2 ** zoom; + const pixel = ([longitude, latitude]) => [ + (longitude + 180) / 360 * world, (1 - mercatorY(latitude) / Math.PI) / 2 * world]; + const pieces = []; + for (const {geometry, properties} of features) { + const parts = geometry.type === "MultiLineString" ? geometry.coordinates + : geometry.type === "LineString" ? [geometry.coordinates] : []; + for (const coordinates of parts) { + if (coordinates.length < 2) continue; + const pixels = coordinates.map(pixel); + const lengths = pixels.slice(1).map((point, i) => Math.hypot(point[0] - pixels[i][0], point[1] - pixels[i][1])); + const length = lengths.reduce((sum, n) => sum + n, 0); + if (length) pieces.push({coordinates, pixels, lengths, length, properties}); + } + } + const named = new Map(); + const points = []; + for (const {coordinates, pixels, lengths, length, properties} of pieces.sort((a, b) => b.length - a.length)) { + const road = roadOf(properties); + if (!named.has(road)) named.set(road, []); + const places = named.get(road); + const width = nameOf(properties).length * textSize * LETTER_WIDTH; + const count = Math.max(1, Math.floor(length / NAME_EVERY)); + for (let n = 0; n < count; n++) { + const along = (n + 0.5) * length / count; + const at = pointAlong(pixels, lengths, along); + if (places.some(other => Math.hypot(other[0] - at[0], other[1] - at[1]) < SAME_NAME_APART)) continue; + places.push(at); + // From where the name begins to where it ends, and never upside down. + const first = pointAlong(pixels, lengths, Math.max(0, along - width / 2)); + const last = pointAlong(pixels, lengths, Math.min(length, along + width / 2)); + let angle = Math.atan2(last[1] - first[1], last[0] - first[0]) * 180 / Math.PI; + if (angle > 90) angle -= 180; + if (angle <= -90) angle += 180; + points.push({ + type: "Feature", + geometry: {type: "Point", coordinates: pointAlong(coordinates, lengths, along)}, + properties: {...properties, [NAME_ANGLE]: Math.round(angle * 10) / 10}, + }); + } + } + return {type: "FeatureCollection", features: points}; +} + +// The names of the roads of the map which a map drawn for `frame` would not have: {source id: where +// they are named (see placeRoadNames)}, for exportStyle. They are read from tiles of EXPORT_DETAIL_ZOOM. +// +// readTile(url, {z, x, y}): resolves to a tile of the source with that URL (as @mapbox/vector-tile +// reads it), or to nothing when the source has no such tile. +// +// They are read wherever a layer would not name roads, whatever tiles the map itself is drawn from: +// nearer the equator a quadrangle is drawn at a little over zoom 14, from tiles which have the names, +// but its streets are as short there as anywhere. +// +// isCancelled(): asked before each source is read; when it is so no more is read. +// +// A source whose names can not be read is left out, and the map is exported without them. +export async function loadRoadNames(style, frame, readTile, isCancelled) { + const names = {}; + // Each source once: the layers which name its roads are of one layer of its tiles. + const unnamed = new Map(unnamedRoadLayers(style, frame.zoom).map(layer => [layer.source, layer])); + if (!readTile || !unnamed.size) return names; + const tiles = tilesOfBounds(frame.bounds, EXPORT_DETAIL_ZOOM); + if (tiles.length > MAX_DETAIL_TILES) return names; + for (const [sourceId, layer] of unnamed) { + if (isCancelled && isCancelled()) break; + const sourceLayer = layer["source-layer"]; + const textSize = Number.isFinite(layer.layout?.["text-size"]) ? layer.layout["text-size"] : ROAD_TEXT_SIZE; + const url = style.sources?.[sourceId]?.url; + if (!url) continue; + try { + const read = await Promise.all(tiles.map(async tile => namedRoads(await readTile(url, tile), sourceLayer, tile))); + const features = read.flat(); + if (features.length) names[sourceId] = placeRoadNames(features, frame.zoom, textSize); + } catch (e) { + console.warn(`The road names of ${sourceId} could not be read`, e); + } + } + return names; +} + +// A copy of the map's style in which nothing is drawn, for the map which is exported from; the +// layers are shown a group at a time. Layers which are of the app and not of the map (those whose +// id starts with one of `ignoredPrefixes`) are not in it. +// +// roadNames: what loadRoadNames gave for a map drawn at `zoom`. A layer which would not name roads +// at that zoom names those instead, in its own style, where placeRoadNames put the names. +export function exportStyle(style, ignoredPrefixes = [], {zoom = null, roadNames = {}} = {}) { + const ignored = id => ignoredPrefixes.some(prefix => String(id).startsWith(prefix)); + const sources = Object.fromEntries(Object.entries(style.sources || {}).filter(([id]) => !ignored(id))); + for (const [id, data] of Object.entries(roadNames)) { + if (sources[id]) sources[ROAD_NAMES_SOURCE + id] = {type: "geojson", data}; + } + const unnamed = new Set(zoom === null ? [] : unnamedRoadLayers(style, zoom).map(layer => layer.id)); + const layers = (style.layers || []) + .filter(layer => !ignored(layer.id)) + .map(layer => { + if (!unnamed.has(layer.id) || !sources[ROAD_NAMES_SOURCE + layer.source]) { + const hidden = {...layer, layout: {...layer.layout, visibility: "none"}}; + // What a basemap draws from a zoom a little deeper than the map is drawn at (a + // stream, a service road) is on a map of this scale, and is in the tiles it is + // drawn from: it is drawn. Not names, of which there would be too many. + if (zoom !== null && layer.type !== "symbol" && layer.minzoom > zoom + && layer.minzoom <= EXPORT_DETAIL_ZOOM) { + hidden.minzoom = Math.floor(zoom); + } + return hidden; + } + const {minzoom, "source-layer": sourceLayer, ...named} = layer; + return { + ...named, + source: ROAD_NAMES_SOURCE + layer.source, + layout: { + ...layer.layout, + "symbol-placement": "point", + "text-rotate": ["get", NAME_ANGLE], + "text-rotation-alignment": "map", + // A name along a line is one line; a name at a point is broken into several + // unless it is told it has the room. + "text-max-width": ROAD_NAME_MAX_EMS, + visibility: "none", + }, + }; + }); + return {...style, sources, layers}; +} + +// The map's arithmetic can put the edge of the world a hair past where it is (180.00000000000125 +// degrees). Such an edge is the edge of the world; one further past it is of a map which crosses the +// antimeridian, and is left as it is. +const LONGITUDE_SLIVER = 1e-8; + +function edgeLongitude(longitude) { + if (Math.abs(longitude) > 180 && Math.abs(longitude) - 180 < LONGITUDE_SLIVER) return Math.sign(longitude) * 180; + return longitude; +} + +// Only the time the page can be seen is counted: a browser does not draw a page which can not be +// seen (another tab is in front of it), and the map is not slow for that. +function once(map, event, timeoutMs = RENDER_TIMEOUT_MS) { + return new Promise((resolve, reject) => { + let seenMs = 0; + const timer = setInterval(() => { + if (typeof document !== "undefined" && document.hidden) return; + seenMs += RENDER_WATCH_MS; + if (seenMs >= timeoutMs) { + clearInterval(timer); + reject(new Error("The map took too long to draw")); + } + }, RENDER_WATCH_MS); + map.once(event, () => { + clearInterval(timer); + resolve(); + }); + }); +} + +function canvasBlob(canvas, type, quality) { + return new Promise((resolve, reject) => canvas.toBlob( + blob => (blob ? resolve(blob) : reject(new Error("The map could not be made into an image"))), + type, quality)); +} + +// Whether a canvas has no transparent part. It is reduced first: looking at every pixel of a sheet +// is slow, and a part too small to survive the reduction is too small to matter. Its four edges are +// looked at by themselves: a photograph which ends a little inside the map leaves a strip along an +// edge, which is narrow enough to be lost in the reduction and wide enough to be seen. +export function isOpaque(canvas) { + const small = document.createElement("canvas"); + small.width = 64; + small.height = Math.max(1, Math.round(64 * canvas.height / canvas.width)); + const context = small.getContext("2d"); + if (!context) return false; + const {width, height} = canvas; + const parts = [ + [0, 0, width, height], + [0, 0, width, 1], [0, height - 1, width, 1], [0, 0, 1, height], [width - 1, 0, 1, height], + ]; + for (const [x, y, w, h] of parts) { + context.clearRect(0, 0, small.width, small.height); + context.drawImage(canvas, x, y, w, h, 0, 0, small.width, small.height); + const {data} = context.getImageData(0, 0, small.width, small.height); + for (let i = 3; i < data.length; i += 4) { + if (data[i] !== 255) return false; + } + } + return true; +} + +// Whether nothing is drawn on a canvas: every pixel of it is clear. Every pixel is looked at (a +// lone well is a few of them), a band of rows at a time so that no more is in memory than a band. +export function isBlankCanvas(canvas) { + const band = document.createElement("canvas"); + band.width = canvas.width; + band.height = Math.min(BLANK_BAND_ROWS, canvas.height); + const context = band.getContext("2d", {willReadFrequently: true}); + // Not known to be blank, so it is drawn. + if (!context) return false; + for (let top = 0; top < canvas.height; top += band.height) { + const rows = Math.min(band.height, canvas.height - top); + context.clearRect(0, 0, band.width, band.height); + context.drawImage(canvas, 0, top, canvas.width, rows, 0, 0, canvas.width, rows); + // Only whether a pixel is seen at all: one which is not may still have a trace of a color. + const {data} = context.getImageData(0, 0, canvas.width, rows); + for (let i = 3; i < data.length; i += 4) { + if (data[i] !== 0) return false; + } + } + return true; +} + +// Whether the browser has stopped drawing on a map's canvas (it does when it has too many, or is +// short of memory): such a canvas reads as clear. +function contextLost(canvas) { + if (typeof canvas.getContext !== "function") return false; + const context = canvas.getContext("webgl2") || canvas.getContext("webgl"); + return Boolean(context && typeof context.isContextLost === "function" && context.isContextLost()); +} + +// Draw each group of layers of `style` on a map of its own, and return the images: +// {width, height, dpi, bounds, layers: [{name, blob}]}. `bounds` is the west, south, east and north +// edges of the images (degrees), which is what says where the PDF is on the Earth: they are read +// from the map which was drawn, so they are of the picture and not of what was asked for. +// +// MapClass: maplibregl.Map. setup(map): called with the new map before it draws anything. +// readTile: how to read a tile of a source (see loadRoadNames); without it small roads have no names. +// onProgress(name, index, count, frame): called before each group is drawn, with the frame it is +// drawn in. isCancelled(): asked before each group whether to stop; when it is so nothing more is +// drawn, and this rejects with an error whose `cancelled` is true. +// +// frame: how the map is drawn (see exportFrame). Or frameFor(count): the frame of a map of that +// many images. How sharp a map may be depends on how many images it is, and a group in which +// nothing is drawn (a basemap file with no runway in the quadrangle) is no image: so the groups are +// first looked at on a small map, onLooking() is told, and only those with something in them are +// drawn, as sharp as that many may be. isBlank(canvas): whether nothing is drawn on a canvas. +export async function renderExport({ + MapClass, style, frame, frameFor, groups, setup, onProgress, onLooking, isCancelled, readTile, + ignoredPrefixes = [], isBlank = isBlankCanvas, +}) { + const cancelled = () => Object.assign(new Error("The export was cancelled"), {cancelled: true}); + // The size, the middle and the zoom of the map are the same however many images it is. + if (frameFor) frame = frameFor(groups.length); + const roadNames = await loadRoadNames(style, frame, readTile, isCancelled); + // Reading the names is the first of the work, and may be the slowest of it. + if (isCancelled && isCancelled()) throw cancelled(); + const container = document.createElement("div"); + // Off the screen, but laid out and drawn as any element is. + container.style.cssText = `position:fixed;left:-100000px;top:0;width:${frame.cssWidth}px;` + + `height:${frame.cssHeight}px;pointer-events:none;`; + document.body.appendChild(container); + + let map; + try { + const exported = exportStyle(style, ignoredPrefixes, {zoom: frame.zoom, roadNames}); + // A map which draws as sharp as `pixelRatio` says. A map is made as sharp as it is to be: + // a browser lets a canvas be larger when it is made than it lets one be made larger later + // (some 33 million pixels, less than a quadrangle at 400 DPI). + const makeMap = async pixelRatio => { + const side = Math.ceil(Math.max(frame.cssWidth, frame.cssHeight) * pixelRatio); + map = new MapClass({ + container, + style: exported, + center: frame.center, + zoom: frame.zoom, + interactive: false, + attributionControl: false, + pixelRatio, + maxCanvasSize: [side, side], + // The canvas is read after it is drawn, so what was drawn must still be in it. + canvasContextAttributes: {preserveDrawingBuffer: true}, + // A label is all there at once: nothing is caught half faded in. + fadeDuration: 0, + }); + if (setup) setup(map); + await once(map, "load"); + return map.getCanvas(); + }; + let canvas = await makeMap(frameFor ? Math.min(LOOKING_PIXEL_RATIO, frame.pixelRatio) : frame.pixelRatio); + // Draw a group alone, have `drawn()` look at the canvas, and hide the group again. + const draw = async (group, drawn) => { + if (isCancelled && isCancelled()) { + throw cancelled(); + } + const show = visibility => { + for (const id of group.ids) { + if (map.getLayer(id)) map.setLayoutProperty(id, "visibility", visibility); + } + }; + show("visible"); + const idle = once(map, "idle"); + map.triggerRepaint(); + await idle; + const result = await drawn(); + show("none"); + return result; + }; + + if (frameFor) { + if (onLooking) onLooking(); + const seen = []; + for (const group of groups) { + const blank = await draw(group, () => isBlank(canvas)); + // A canvas the browser has stopped drawing on is clear, whatever was to be drawn: + // that is not a group with nothing in it, to be left out and not missed. + if (blank && contextLost(canvas)) throw new Error("The browser stopped drawing the map"); + if (!blank) seen.push(group); + } + if (!seen.length) throw new Error("Nothing is drawn in any layer of the map"); + // Stopped while the groups were looked at: the map to export from is not made. + if (isCancelled && isCancelled()) throw cancelled(); + groups = seen; + frame = frameFor(groups.length); + // The small map is done with; the map which is exported from is made in its place. + map.remove(); + map = null; + canvas = await makeMap(frame.pixelRatio); + } + + const layers = []; + for (const [index, group] of groups.entries()) { + // A group is not said to be drawn when it will not be. + if (isCancelled && isCancelled()) throw cancelled(); + if (onProgress) onProgress(group.name, index, groups.length, frame); + const blob = await draw(group, async () => { + // A photograph with no gaps is far smaller as a JPEG; anything else needs to be seen through. + return group.raster && isOpaque(canvas) + ? await canvasBlob(canvas, "image/jpeg", 0.9) + : await canvasBlob(canvas, "image/png"); + }); + layers.push({name: group.name, blob}); + } + const edges = map.getBounds(); + return { + width: canvas.width, + height: canvas.height, + bounds: [edgeLongitude(edges.getWest()), edges.getSouth(), edgeLongitude(edges.getEast()), edges.getNorth()], + dpi: canvas.width / frame.cssWidth * (frame.dpi / frame.pixelRatio), + layers, + }; + } finally { + if (map) map.remove(); + container.remove(); + } +} + +// Where the data of the exported layers is from: what each of their sources says of itself, once +// each, as text. +// +// map: the map on the screen, whose sources have been loaded (a source may only learn its +// attribution from its file). +export function exportCredits(map, groups) { + const style = map.getStyle(); + const exported = new Set(groups.flatMap(group => group.ids)); + const credits = []; + for (const layer of style.layers || []) { + if (!layer.source || !exported.has(layer.id)) continue; + const attribution = map.getSource(layer.source)?.attribution || style.sources?.[layer.source]?.attribution; + if (!attribution) continue; + // An attribution is HTML: links, mostly. One may name many sources, with a bar between them. + const html = new DOMParser().parseFromString(String(attribution), "text/html"); + for (const part of (html.body.textContent || "").split("|")) { + const text = part.replace(/\s+/g, " ").trim(); + if (text && !credits.includes(text)) credits.push(text); + } + } + return credits; +} + +// The day, as the person exporting the map has it: 2026-11-15. +export function exportDate(now = new Date()) { + const pad = value => String(value).padStart(2, "0"); + return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`; +} + +// What is written on the sheet a quadrangle is printed on. `named` is what the USGS calls the +// quadrangle ({name, state, exact}), when it calls it anything: else it goes by where it is. +// +// contours: {interval, unit} ("feet" or "meters"): how far apart the contour lines of the exported +// map are, when it has them. +// +// legend: the lines of the map as they are drawn (of exportLegend), when it has any. +export function exportSheet(quad, map, groups, now = new Date(), named = null, contours = null, legend = null) { + return { + name: quadTitle(named) || quadLabel(quad), + credits: exportCredits(map, groups), + date: exportDate(now), + ...(contours ? {contours} : {}), + ...(legend ? {legend} : {}), + }; +} + +// How far apart the contour lines of an exported map are, for exportSheet; null when no layer of +// `contourLayers` (parts of ids, as the layer control has them) is among the `groups` which are +// exported, or no line is drawn at the zoom. +// +// interval: [minor, major] at the zoom the map is exported at (see contourInterval). +export function exportContours(groups, contourLayers, interval, imperial) { + const exported = groups.some(group => group.ids.some(id => contourLayers.some(part => id.includes(part)))); + if (!exported || !interval) return null; + return {interval: interval[0], unit: imperial ? "feet" : "meters"}; +} + +// "N40.250_W105.125": where a quadrangle's southwest corner is, in a form a file can be named. +function quadFileStem(quad) { + const part = (value, positive, negative) => `${value < 0 ? negative : positive}${Math.abs(value).toFixed(3)}`; + return `${part(quad.south, "N", "S")}_${part(quad.west, "E", "W")}`; +} + +// A name as a part of a file's: "Berthoud". +function fileNamePart(name) { + // An accent is left off its letter (Cañon is Canon), not made a gap in the name. + return String(name).normalize("NFKD").replace(/\p{M}+/gu, "").replace(/[^A-Za-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 60); +} + +export function exportFileName(quad, named = null) { + const name = named?.name ? fileNamePart(named.name) : ""; + return `wrolpi-map_${QUAD_SCALE / 1000}k_${name ? `${name}_` : ""}${quadFileStem(quad)}.pdf`; +} + +export function exportTitle(quad, named = null) { + return `1:${QUAD_SCALE.toLocaleString("en-US")} ${quadTitle(named) || quadLabel(quad)}`; +} + +// Give the user a file to keep. +export function saveBlob(blob, filename) { + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + a.style.display = "none"; + document.body.appendChild(a); + a.click(); + a.remove(); + // Not at once: a large file is still being read when the click returns. + setTimeout(() => URL.revokeObjectURL(url), 60_000); +} diff --git a/app/src/components/mapExport.test.js b/app/src/components/mapExport.test.js new file mode 100644 index 00000000..12853ef4 --- /dev/null +++ b/app/src/components/mapExport.test.js @@ -0,0 +1,1123 @@ +import { + BACKGROUND_GROUP, + exportFileName, + exportCredits, + exportDate, + exportContours, + exportFrame, + exportGroups, + isBlankCanvas, + exportSheet, + exportStyle, + exportTitle, + loadRoadNames, + namedRoads, + placeRoadNames, + tilesOfBounds, + unnamedRoadLayers, + maxExportPixels, + renderExport, + sourcesOutside, +} from './mapExport'; +import {quadAt} from './mapQuads'; + +const ANCHORAGE = quadAt(-149.93, 61.19); +const BERTHOUD = quadAt(-105.0595, 40.31); +const BERTHOUD_BOUNDS = [BERTHOUD.west, BERTHOUD.south, BERTHOUD.east, BERTHOUD.north]; + +describe('how a quadrangle is drawn', () => { + test('is the size of a 1:24,000 sheet, at the sharpness asked for', () => { + const frame = exportFrame(ANCHORAGE); + // 11.0 inches wide at 150 CSS pixels an inch, and twice that many real pixels (300 DPI). + expect(frame.cssWidth).toBe(1654); + expect(frame.cssHeight).toBe(3432); + expect(frame.pixelRatio).toBe(2); + expect(frame.dpi).toBe(300); + expect(frame.zoom).toBeCloseTo(13.18, 2); + + // Wider nearer the equator, and no taller. + const south = exportFrame(BERTHOUD); + expect(south.cssWidth).toBe(2615); + expect(Math.abs(south.cssHeight - frame.cssHeight)).toBeLessThan(15); + expect(south.zoom).toBeCloseTo(13.84, 2); + }); + + test('is exactly the quadrangle: its edges are the edges of the map', () => { + const {cssWidth, cssHeight, zoom, center} = exportFrame(ANCHORAGE); + // Web Mercator, as the map draws it: pixels from the center to a point. + const world = 512 * 2 ** zoom; + const x = lng => (lng + 180) / 360 * world; + const y = lat => (1 - Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360)) / Math.PI) / 2 * world; + expect(x(ANCHORAGE.east) - x(ANCHORAGE.west)).toBeCloseTo(cssWidth, 6); + expect(x(center[0]) - x(ANCHORAGE.west)).toBeCloseTo(cssWidth / 2, 6); + expect(y(ANCHORAGE.south) - y(center[1])).toBeCloseTo(y(center[1]) - y(ANCHORAGE.north), 6); + // The height is whole pixels, so it is within half of one of the quadrangle's. + expect(Math.abs((y(ANCHORAGE.south) - y(ANCHORAGE.north)) - cssHeight)).toBeLessThanOrEqual(0.5); + }); + + test('is sharper when that is asked for, and no larger on paper', () => { + const frame = exportFrame(ANCHORAGE, {dpi: 400}); + expect(frame.dpi).toBe(400); + expect(frame.pixelRatio).toBeCloseTo(400 / 150, 10); + // The same sheet, the same text and detail: only more pixels. + expect(frame).toMatchObject({cssWidth: 1654, cssHeight: 3432}); + expect(frame.zoom).toBe(exportFrame(ANCHORAGE).zoom); + }); + + test('is less sharp when it would be more pixels than the API makes a PDF of', () => { + // A quadrangle at the equator is square, and at 400 DPI is 83 megapixels. + const equator = quadAt(0.05, 0.05); + const asked = exportFrame(equator, {dpi: 400}); + expect(asked.cssWidth * asked.cssHeight * asked.pixelRatio ** 2).toBeGreaterThan(80e6); + + const frame = exportFrame(equator, {dpi: 400, maxPixels: maxExportPixels(1)}); + const pixels = Math.round(frame.cssWidth * frame.pixelRatio) * Math.round(frame.cssHeight * frame.pixelRatio); + expect(pixels).toBeLessThanOrEqual(64e6); + expect(pixels).toBeGreaterThan(63.5e6); + expect(frame.dpi).toBeGreaterThan(345); + expect(frame.dpi).toBeLessThan(355); + + // With many layers on, each may be less. + expect(maxExportPixels(1)).toBe(64e6); + expect(maxExportPixels(15)).toBe(64e6); + expect(maxExportPixels(25)).toBe(40e6); + const many = exportFrame(equator, {maxPixels: maxExportPixels(25)}); + expect(many.dpi).toBeLessThan(300); + expect(25 * many.cssWidth * many.cssHeight * many.pixelRatio ** 2).toBeLessThanOrEqual(1e9); + // A map which is within what the API takes is as sharp as was asked for. + expect(exportFrame(ANCHORAGE, {dpi: 400, maxPixels: maxExportPixels(13)}).dpi).toBe(400); + }); + + test('is less sharp where the browser can not make a canvas that large', () => { + const frame = exportFrame(ANCHORAGE, {maxSide: 4096}); + expect(frame.cssWidth).toBe(1654); + expect(frame.cssHeight * frame.pixelRatio).toBeCloseTo(4096, 6); + expect(frame.dpi).toBeCloseTo(179, 0); + }); +}); + +const LAYER_GROUPS = { + 'Hillshade': ['hillshade'], + 'Water': ['water'], + 'Buildings': ['buildings'], + 'Roads - Minor': ['roads_minor', 'roads_other'], + 'Labels': ['roads_labels', 'places_', 'water_label'], +}; +const hidden = {layout: {visibility: 'none'}}; +const STYLE_LAYERS = [ + {id: 'background', type: 'background'}, + {id: 'earth', type: 'fill'}, + {id: 'water', type: 'fill'}, + {id: 'hillshade', type: 'hillshade', ...hidden}, + {id: 'buildings', type: 'fill'}, + {id: 'roads_minor_casing', type: 'line'}, + {id: 'roads_minor', type: 'line'}, + // A second basemap file's layers carry its name. + {id: 'oregon.pmtiles:water', type: 'fill'}, + {id: 'oregon.pmtiles:roads_minor', type: 'line'}, + {id: 'overlay:satellite.pmtiles:raster', type: 'raster', source: 'overlay:satellite.pmtiles'}, + // The ground of an overlay is under the basemap's labels, and its own labels are over them. + {id: 'overlay:extras/features.pmtiles:water:fill', type: 'fill', source: 'overlay:extras/features.pmtiles', ...hidden}, + {id: 'overlay:extras/features.pmtiles:lines:line', type: 'line', source: 'overlay:extras/features.pmtiles'}, + {id: 'water_label_ocean', type: 'symbol'}, + {id: 'places_locality', type: 'symbol'}, + {id: 'overlay:extras/features.pmtiles:towers:circle', type: 'circle', source: 'overlay:extras/features.pmtiles'}, + {id: 'overlay:extras/features.pmtiles:towers:names', type: 'symbol', source: 'overlay:extras/features.pmtiles'}, + {id: 'overlay:off.pmtiles:raster', type: 'raster', source: 'overlay:off.pmtiles', ...hidden}, +]; +const OVERLAY_LABELS = { + 'overlay:satellite.pmtiles': 'Satellite imagery', + 'overlay:extras/features.pmtiles': 'Infrastructure', +}; + +describe('the layers of the PDF', () => { + const groups = exportGroups(STYLE_LAYERS, {layerGroups: LAYER_GROUPS, overlayLabels: OVERLAY_LABELS}); + + test('are the layers which are on, as the layer control names them, from the bottom up', () => { + // The water of a second basemap file is over the buildings of the first, as it is on the map: + // Water is two images, of one name. + expect(groups.map(g => g.name)).toEqual([ + BACKGROUND_GROUP, 'Water', 'Buildings', 'Water', 'Roads - Minor', 'Satellite imagery', 'Infrastructure', + 'Labels', 'Infrastructure (labels)', + ]); + }); + + test('a group whose layers are apart is drawn where each part which fills an area is', () => { + const layers = [ + {id: 'background', type: 'background'}, + {id: 'landuse_park', type: 'fill'}, + {id: 'roads_runway', type: 'line'}, + {id: 'water', type: 'fill'}, + {id: 'water_stream', type: 'line'}, + {id: 'landuse_pier', type: 'fill'}, + {id: 'roads_pier', type: 'line'}, + {id: 'roads_minor_casing', type: 'line'}, + {id: 'roads_highway_casing', type: 'line'}, + {id: 'roads_minor', type: 'line'}, + {id: 'roads_highway', type: 'line'}, + {id: 'roads_rail', type: 'line'}, + ]; + const drawn = exportGroups(layers, {layerGroups: { + 'Water': ['water'], 'Land Cover': ['landuse_park', 'landuse_pier'], + 'Roads - Highway': ['roads_highway'], 'Roads - Minor': ['roads_minor'], + 'Rail & Runways': ['roads_rail', 'roads_runway'], + }}).map(g => [g.name, g.ids]); + expect(drawn).toEqual([ + [BACKGROUND_GROUP, ['background']], + // A park is under the water of its lake, and a pier is over it. + ['Land Cover', ['landuse_park']], + ['Water', ['water', 'water_stream']], + ['Land Cover', ['landuse_pier']], + // What is in no group is drawn where it is, with the name of the ground. + [BACKGROUND_GROUP, ['roads_pier']], + // Lines hide nothing: those of a group are drawn together, where the last of them are. + ['Roads - Minor', ['roads_minor_casing', 'roads_minor']], + ['Roads - Highway', ['roads_highway_casing', 'roads_highway']], + ['Rail & Runways', ['roads_runway', 'roads_rail']], + ]); + }); + + test('each has the layers of the style which are of it', () => { + const ids = {}; + for (const group of groups) ids[group.name] = [...(ids[group.name] || []), ...group.ids]; + // What is in no group is the ground the rest is drawn on. + expect(ids[BACKGROUND_GROUP]).toEqual(['background', 'earth']); + // Of every basemap file. + expect(ids['Water']).toEqual(['water', 'oregon.pmtiles:water']); + expect(ids['Roads - Minor']).toEqual(['roads_minor_casing', 'roads_minor', 'oregon.pmtiles:roads_minor']); + // A water label is a label. + expect(ids['Labels']).toEqual(['water_label_ocean', 'places_locality']); + // An overlay's layers are of the overlay, whatever they are named, and only those which are on. + expect(ids['Infrastructure']).toEqual(['overlay:extras/features.pmtiles:lines:line']); + // Its labels are a layer of their own, because the basemap's labels are between the two. + expect(ids['Infrastructure (labels)']).toEqual([ + 'overlay:extras/features.pmtiles:towers:circle', 'overlay:extras/features.pmtiles:towers:names', + ]); + }); + + test('a layer which is off is not exported', () => { + expect(groups.map(g => g.name)).not.toContain('Hillshade'); + expect(groups.flatMap(g => g.ids)).not.toContain('overlay:off.pmtiles:raster'); + }); + + test('says which are photographs', () => { + expect(groups.filter(g => g.raster).map(g => g.name)).toEqual(['Satellite imagery']); + }); + + test('an overlay with no label goes by its source, and two of one name are told apart', () => { + const names = exportGroups([ + {id: 'water', type: 'fill'}, + {id: 'overlay:a.pmtiles:x:line', type: 'line', source: 'overlay:a.pmtiles'}, + {id: 'overlay:b.pmtiles:x:line', type: 'line', source: 'overlay:b.pmtiles'}, + {id: 'overlay:c.pmtiles:x:line', type: 'line', source: 'overlay:c.pmtiles'}, + ], { + layerGroups: LAYER_GROUPS, + overlayLabels: {'overlay:a.pmtiles': 'Water', 'overlay:b.pmtiles': 'Water'}, + }).map(g => g.name); + expect(names).toEqual(['Water', 'Water (2)', 'Water (3)', 'overlay:c.pmtiles']); + }); + + test('an overlay which is only ground, or only labels, is one layer by its own name', () => { + const names = layers => exportGroups(layers, { + layerGroups: LAYER_GROUPS, overlayLabels: {'overlay:a.pmtiles': 'Towers'}, + }).map(g => g.name); + expect(names([ + {id: 'places_locality', type: 'symbol'}, + {id: 'overlay:a.pmtiles:x:icons', type: 'symbol', source: 'overlay:a.pmtiles'}, + ])).toEqual(['Labels', 'Towers']); + expect(names([ + {id: 'overlay:a.pmtiles:x:line', type: 'line', source: 'overlay:a.pmtiles'}, + {id: 'places_locality', type: 'symbol'}, + // Its labels are turned off. + {id: 'overlay:a.pmtiles:x:icons', type: 'symbol', source: 'overlay:a.pmtiles', ...hidden}, + ])).toEqual(['Towers', 'Labels']); + }); + + test('there are none when nothing is on', () => { + expect(exportGroups([{id: 'water', type: 'fill', ...hidden}], {layerGroups: LAYER_GROUPS})).toEqual([]); + }); +}); + +describe('the style which is exported from', () => { + test('draws nothing to begin with, and has nothing of the app in it', () => { + const style = { + version: 8, + glyphs: '/fonts', + sources: {basemap: {type: 'vector'}, 'quad-grid': {type: 'geojson'}}, + layers: [ + {id: 'water', type: 'fill', source: 'basemap'}, + {id: 'places', type: 'symbol', source: 'basemap', layout: {'text-size': 12}}, + {id: 'quad-grid-lines', type: 'line', source: 'quad-grid'}, + ], + }; + const exported = exportStyle(style, ['quad-']); + expect(exported.glyphs).toBe('/fonts'); + expect(Object.keys(exported.sources)).toEqual(['basemap']); + expect(exported.layers).toEqual([ + {id: 'water', type: 'fill', source: 'basemap', layout: {visibility: 'none'}}, + {id: 'places', type: 'symbol', source: 'basemap', layout: {'text-size': 12, visibility: 'none'}}, + ]); + // The map's own style is as it was. + expect(style.layers[0].layout).toBeUndefined(); + }); + + test('a layer which would not name roads names the roads which were read for it', () => { + const roadNames = {'a.pmtiles': POINTS}; + const exported = exportStyle(TOWN, [], {zoom: 13.2, roadNames}); + expect(exported.sources['export-road-names:a.pmtiles']).toEqual({type: 'geojson', data: POINTS}); + expect(exported.sources['a.pmtiles']).toBe(TOWN.sources['a.pmtiles']); + + expect(exported.layers.map(l => l.id)).toEqual(TOWN.layers.map(l => l.id)); + const [, minor, major, address] = exported.layers; + // In its own style, at whatever zoom: each name where it was put, turned as its road is. + expect(minor).toEqual({ + id: 'roads_labels_minor', type: 'symbol', source: 'export-road-names:a.pmtiles', + filter: ['in', 'kind', 'minor_road'], + layout: { + 'symbol-placement': 'point', 'text-rotate': ['get', 'export:angle'], 'text-max-width': 1000, + 'text-rotation-alignment': 'map', 'text-size': 12, visibility: 'none', + }, + }); + // A layer which names roads at this zoom, and one which is not of roads, are as they were. + expect(major).toMatchObject({source: 'a.pmtiles', 'source-layer': 'roads', minzoom: 11}); + expect(address).toMatchObject({source: 'a.pmtiles', minzoom: 18}); + // The map's own style is as it was. + expect(TOWN.layers[1]).toMatchObject({source: 'a.pmtiles', minzoom: 15}); + expect(TOWN.layers[1].layout['symbol-placement']).toBe('line'); + }); + + test('a layer whose names could not be read is drawn as the map draws it', () => { + const exported = exportStyle(TOWN, [], {zoom: 13.2, roadNames: {}}); + expect(exported.layers.map(l => l.id)).toEqual(TOWN.layers.map(l => l.id)); + expect(exported.layers[1]).toMatchObject({source: 'a.pmtiles', minzoom: 15}); + }); +}); + +// A town: its small roads are named from zoom 15, its large ones from 11. +const TOWN = { + version: 8, + sources: {'a.pmtiles': {type: 'vector', url: 'pmtiles://a'}}, + layers: [ + {id: 'roads_minor', type: 'line', source: 'a.pmtiles', 'source-layer': 'roads'}, + { + id: 'roads_labels_minor', type: 'symbol', source: 'a.pmtiles', 'source-layer': 'roads', minzoom: 15, + filter: ['in', 'kind', 'minor_road'], layout: {'symbol-placement': 'line', 'text-size': 12}, + }, + {id: 'roads_labels_major', type: 'symbol', source: 'a.pmtiles', 'source-layer': 'roads', minzoom: 11}, + {id: 'address_label', type: 'symbol', source: 'a.pmtiles', 'source-layer': 'pois', minzoom: 18}, + ], +}; +const POINTS = {type: 'FeatureCollection', features: []}; + +// A tile as @mapbox/vector-tile reads it, of roads which go east from the given longitude. +function fakeTile(roads) { + return { + layers: { + roads: { + length: roads.length, + feature: i => ({ + properties: roads[i].properties, + toGeoJSON: (x, y, z) => ({ + type: 'Feature', id: 7, properties: roads[i].properties, + geometry: {type: 'LineString', coordinates: roads[i].coordinates, tile: [z, x, y]}, + }), + }), + }, + }, + }; +} + +describe('the names of small roads', () => { + test('are of the layers which name roads, but not at the zoom of the sheet', () => { + expect(unnamedRoadLayers(TOWN, 13.2).map(l => l.id)).toEqual(['roads_labels_minor']); + expect(unnamedRoadLayers(TOWN, 10).map(l => l.id)).toEqual(['roads_labels_minor', 'roads_labels_major']); + expect(unnamedRoadLayers(TOWN, 15)).toEqual([]); + expect(unnamedRoadLayers({}, 13)).toEqual([]); + }); + + test('are read from the tiles which the quadrangle is in', () => { + // One tile of zoom 1 is a quarter of the world. + expect(tilesOfBounds([-90, 10, -80, 20], 1)).toEqual([{z: 1, x: 0, y: 0}]); + expect(tilesOfBounds([-10, -10, 10, 10], 1)).toEqual([ + {z: 1, x: 0, y: 0}, {z: 1, x: 0, y: 1}, {z: 1, x: 1, y: 0}, {z: 1, x: 1, y: 1}]); + // A tile which only touches the edge has nothing of the quadrangle in it. + expect(tilesOfBounds([-180, 0, 0, 85.0511287798], 1)).toEqual([{z: 1, x: 0, y: 0}]); + // A quadrangle is a few tiles of zoom 14 wide, each of them once. + const tiles = tilesOfBounds([ANCHORAGE.west, ANCHORAGE.south, ANCHORAGE.east, ANCHORAGE.north], 14); + expect(new Set(tiles.map(t => t.x)).size).toBeLessThanOrEqual(7); + expect(tiles.every(t => t.z === 14)).toBe(true); + expect(tiles.length).toBe(new Set(tiles.map(t => `${t.x}/${t.y}`)).size); + }); + + test('are the roads of a tile which have a name, with all that is said of them', () => { + const tile = fakeTile([ + {properties: {kind: 'minor_road', name: 'Elm Street', ref: '7'}, coordinates: [[0, 0], [1, 0]]}, + {properties: {kind: 'minor_road'}, coordinates: [[0, 1], [1, 1]]}, + {properties: {kind: 'path', 'name:en': 'Ridge Trail'}, coordinates: [[0, 2], [1, 2]]}, + ]); + const roads = namedRoads(tile, 'roads', {z: 14, x: 3, y: 5}); + expect(roads.map(r => r.properties)).toEqual([ + {kind: 'minor_road', name: 'Elm Street', ref: '7'}, {kind: 'path', 'name:en': 'Ridge Trail'}]); + // Where the tile says they are, as the tile reader makes that of the tile's place. + expect(roads[0].geometry).toMatchObject({type: 'LineString', coordinates: [[0, 0], [1, 0]], tile: [14, 3, 5]}); + expect(roads[0]).not.toHaveProperty('id'); + // No such tile, or no such layer in it. + expect(namedRoads(null, 'roads', {z: 14, x: 3, y: 5})).toEqual([]); + expect(namedRoads(tile, 'streets', {z: 14, x: 3, y: 5})).toEqual([]); + }); + + // At zoom 13 the world is 4,194,304 pixels around: a degree of longitude is 11,651 of them. + const piece = (name, from, to, kind = 'minor_road') => ({ + type: 'Feature', properties: {name, kind}, + geometry: {type: 'LineString', coordinates: [[from, 0], [to, 0]]}, + }); + const places = (roads, zoom = 13) => placeRoadNames(roads, zoom).features + .map(f => [f.properties.name, Math.round(f.geometry.coordinates[0] * 1e6) / 1e6]); + + test('a road is named over its middle, however short it is', () => { + const {type, features} = placeRoadNames([piece('Elm Street', 0, 0.01), piece('Oak Street', 1, 1.0004)], 13); + expect(type).toBe('FeatureCollection'); + expect(features).toEqual([{ + type: 'Feature', geometry: {type: 'Point', coordinates: [expect.closeTo(0.005, 9), 0]}, + properties: {kind: 'minor_road', name: 'Elm Street', 'export:angle': 0}, + }, { + // 5 pixels of road under 66 pixels of name. + type: 'Feature', geometry: {type: 'Point', coordinates: [expect.closeTo(1.0002, 9), 0]}, + properties: {kind: 'minor_road', name: 'Oak Street', 'export:angle': 0}, + }]); + }); + + test('a long road is named again each time it has gone far enough', () => { + // 0.1 degrees is 1,165 pixels: two names, each in the middle of half of it. + expect(places([piece('Elm Street', 0, 0.1)])).toEqual([['Elm Street', 0.025], ['Elm Street', 0.075]]); + // 699 pixels is not twice as far. + expect(places([piece('Elm Street', 0, 0.06)])).toEqual([['Elm Street', 0.03]]); + }); + + test('a name is turned as the road goes under it, and never upside down', () => { + const going = (to, from = [0, 0]) => ({ + type: 'Feature', properties: {name: `${from} to ${to}`}, + geometry: {type: 'LineString', coordinates: [from, to]}, + }); + const angles = roads => placeRoadNames(roads, 13).features.map(f => f.properties['export:angle']); + // East and west are level; north and south are on their side; a map's y is down. + expect(angles([going([0.003, 0])])).toEqual([0]); + expect(angles([going([0, 0], [0.003, 0])])).toEqual([0]); + expect(angles([going([0, 0.003])])).toEqual([90]); + expect(angles([going([0, -0.003])])).toEqual([90]); + expect(angles([going([0.002, 0.002])])).toEqual([-45]); + expect(angles([going([-0.002, -0.002])])).toEqual([-45]); + expect(angles([going([0.002, -0.002])])).toEqual([45]); + // A road which turns a corner far from its middle is named as it goes at its middle. + const corner = { + type: 'Feature', properties: {name: 'Elm'}, + geometry: {type: 'LineString', coordinates: [[0, 0], [0.05, 0], [0.05, 0.002]]}, + }; + expect(angles([corner])).toEqual([0]); + }); + + test('a road in pieces is not named twice in one place', () => { + // A street cut by the edge of a tile: most of it, and a stub at its far end. + expect(places([piece('Elm Street', 0.02, 0.022), piece('Elm Street', 0, 0.02)], 13.2)) + .toEqual([['Elm Street', 0.01]]); + // Short pieces end to end: the longest is the one which is named. + expect(places([piece('Oak Street', 0, 0.002), piece('Oak Street', 0.002, 0.005)])) + .toEqual([['Oak Street', 0.0035]]); + // Another road beside it has its own name, and the same name a long way off is another road. + expect(places([piece('Oak Street', 0, 0.005), piece('Ash Street', 0.002, 0.0045), piece('Oak Street', 0.1, 0.102)])) + .toEqual([['Oak Street', 0.0025], ['Ash Street', 0.00325], ['Oak Street', 0.101]]); + }); + + test('a piece of another kind of road does not take the name', () => { + // The layer which names small roads does not draw the large one, however long its piece is. + const {features} = placeRoadNames( + [piece('Main Street', 0, 0.004, 'major_road'), piece('Main Street', 0.004, 0.0069, 'minor_road')], 13); + expect(features.map(f => f.properties.kind)).toEqual(['major_road', 'minor_road']); + }); + + test('the lines of a road of many lines are each a piece of it; what is not a line is not one', () => { + const {features} = placeRoadNames([ + { + type: 'Feature', properties: {name: 'Elm Street'}, + geometry: {type: 'MultiLineString', coordinates: [[[0, 0], [0.01, 0]], [[0, 1], [0.003, 1]], [[5, 5]]]}, + }, + {type: 'Feature', properties: {name: 'Elm Square'}, geometry: {type: 'Point', coordinates: [0, 0]}}, + ], 13); + expect(features.map(f => f.geometry.coordinates)).toEqual([ + [expect.closeTo(0.005, 9), 0], [expect.closeTo(0.0015, 9), 1]]); + }); + + const FRAME = {zoom: 13.2, bounds: [-10, -10, 10, 10]}; + + test('are read from each source whose roads would have no names, a tile at a time', async () => { + const readTile = jest.fn(async (url, {x, y}) => (x === 8191 && y === 8191 + ? fakeTile([{properties: {kind: 'minor_road', name: 'Elm Street'}, coordinates: [[0, 0], [0.01, 0]]}]) + : null)); + const frame = {zoom: 13.2, bounds: [-0.01, 0.001, -0.001, 0.01]}; + const names = await loadRoadNames(TOWN, frame, readTile); + expect(readTile).toHaveBeenCalledTimes(1); + expect(readTile).toHaveBeenCalledWith('pmtiles://a', {z: 14, x: 8191, y: 8191}); + expect(Object.keys(names)).toEqual(['a.pmtiles']); + expect(names['a.pmtiles'].features.map(f => f.properties.name)).toEqual(['Elm Street']); + }); + + test('are not read when there is no need, or no way', async () => { + const readTile = jest.fn(async () => null); + // Nothing reads tiles. + expect(await loadRoadNames(TOWN, FRAME, undefined)).toEqual({}); + // The map is drawn at a zoom at which its style names every road. + expect(await loadRoadNames(TOWN, {...FRAME, zoom: 15.1}, readTile)).toEqual({}); + // No layer of the map names roads. + expect(await loadRoadNames({...TOWN, layers: [TOWN.layers[0]]}, FRAME, readTile)).toEqual({}); + // More tiles than a quadrangle is. + expect(await loadRoadNames(TOWN, {zoom: 13.2, bounds: [-10, -10, 10, 10]}, readTile)).toEqual({}); + expect(readTile).not.toHaveBeenCalled(); + // No tile of the place has a named road. + expect(await loadRoadNames(TOWN, {zoom: 13.2, bounds: [-0.01, 0.001, -0.001, 0.01]}, readTile)).toEqual({}); + }); + + test('are read nearer the equator too, where a quadrangle is drawn at a little over zoom 14', async () => { + // The style still keeps the names of small roads for zoom 15 there. + const readTile = jest.fn(async () => fakeTile([ + {properties: {kind: 'minor_road', name: 'Elm Street'}, coordinates: [[-80.95, 25.05], [-80.9499, 25.05]]}, + ])); + const frame = {zoom: 14.09, bounds: [-80.9501, 25.0499, -80.9499, 25.0501]}; + const names = await loadRoadNames(TOWN, frame, readTile); + expect(readTile).toHaveBeenCalledWith('pmtiles://a', expect.objectContaining({z: 14})); + expect(names['a.pmtiles'].features.map(f => f.properties.name)).toEqual(['Elm Street']); + // And the layer draws them. + const exported = exportStyle(TOWN, [], {zoom: frame.zoom, roadNames: names}); + expect(exported.layers[1]).not.toHaveProperty('minzoom'); + expect(exported.layers[1].source).toBe('export-road-names:a.pmtiles'); + }); + + test('no more is read once the export is cancelled', async () => { + const two = { + ...TOWN, + sources: {...TOWN.sources, 'b.pmtiles': {type: 'vector', url: 'pmtiles://b'}}, + layers: [...TOWN.layers, {...TOWN.layers[1], id: 'b.pmtiles:roads_labels_minor', source: 'b.pmtiles'}], + }; + let cancelled = false; + const readTile = jest.fn(async () => { + cancelled = true; + return null; + }); + await loadRoadNames(two, {zoom: 13.2, bounds: [-0.01, 0.001, -0.001, 0.01]}, readTile, () => cancelled); + expect(readTile).toHaveBeenCalledTimes(1); + expect(readTile).toHaveBeenCalledWith('pmtiles://a', expect.anything()); + }); + + test('a source which can not be read is exported without them', async () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const readTile = jest.fn(async () => { + throw new Error('no such file'); + }); + expect(await loadRoadNames(TOWN, {zoom: 13.2, bounds: [-0.01, 0.001, -0.001, 0.01]}, readTile)).toEqual({}); + expect(warn).toHaveBeenCalledTimes(1); + warn.mockRestore(); + }); +}); + +// As much of a MapLibre map as an export uses. It records which layers were visible each time +// its canvas was read. +function fakeMapClass(record, {transparent = true, canvas = () => ({})} = {}) { + return class FakeMap { + constructor(options) { + record.options = options; + record.maps = (record.maps || 0) + 1; + record.pixelRatios = [...(record.pixelRatios || []), options.pixelRatio]; + record.sides = [...(record.sides || []), options.maxCanvasSize?.[0]]; + this.visible = new Set(options.style.layers + .filter(l => l.layout.visibility !== 'none').map(l => l.id)); + this.layerIds = new Set(options.style.layers.map(l => l.id)); + this.canvas = { + ...canvas(this), + width: Math.floor(parseInt(options.container.style.width, 10) * options.pixelRatio), + height: Math.floor(parseInt(options.container.style.height, 10) * options.pixelRatio), + toBlob: (callback, type) => { + record.captures.push({type, visible: [...this.visible]}); + callback(record.failBlob ? null : {type, layers: [...this.visible]}); + }, + }; + } + + on(event, handler) { + record.listeners = [...(record.listeners || []), event]; + } + + once(event, handler) { + if (event === 'idle' && record.neverIdle) return; + Promise.resolve().then(handler); + } + + getCanvas() { + return this.canvas; + } + + // The edges of what this map draws (not quite those of the quadrangle it was asked for). + getBounds() { + const [west, south, east, north] = record.bounds || [-150, 61.12499878, -149.875, 61.25000121]; + return {getWest: () => west, getSouth: () => south, getEast: () => east, getNorth: () => north}; + } + + getLayer(id) { + return this.layerIds.has(id) ? {id} : undefined; + } + + setLayoutProperty(id, name, value) { + if (value === 'visible') this.visible.add(id); else this.visible.delete(id); + } + + triggerRepaint() { + } + + remove() { + record.removed = true; + record.removals = (record.removals || 0) + 1; + } + }; +} + +describe('drawing the layers', () => { + const style = { + version: 8, + sources: {basemap: {type: 'vector'}}, + layers: [ + {id: 'water', type: 'fill', source: 'basemap'}, + {id: 'roads_minor', type: 'line', source: 'basemap'}, + {id: 'places', type: 'symbol', source: 'basemap'}, + ], + }; + const groups = [ + {name: 'Water', ids: ['water'], raster: false}, + {name: 'Roads - Minor', ids: ['roads_minor', 'gone'], raster: false}, + {name: 'Labels', ids: ['places'], raster: false}, + ]; + const frame = exportFrame(ANCHORAGE); + let getContext; + + beforeEach(() => { + // The reduced copy which says whether an image has gaps: here, all of it is gaps. + getContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(() => ({ + clearRect: jest.fn(), + drawImage: jest.fn(), + getImageData: () => ({data: new Uint8ClampedArray([0, 0, 0, 0])}), + })); + }); + afterEach(() => getContext.mockRestore()); + + test('each group is drawn alone, on a map the size of the sheet', async () => { + const record = {captures: []}; + const progress = []; + const setup = jest.fn(); + const result = await renderExport({ + MapClass: fakeMapClass(record), style, frame, groups, setup, + onProgress: (name, index, count) => progress.push(`${name} ${index + 1}/${count}`), + }); + + expect(record.captures).toEqual([ + {type: 'image/png', visible: ['water']}, + {type: 'image/png', visible: ['roads_minor']}, + {type: 'image/png', visible: ['places']}, + ]); + expect(result.layers.map(l => l.name)).toEqual(['Water', 'Roads - Minor', 'Labels']); + expect(result.layers[1].blob.layers).toEqual(['roads_minor']); + expect(result).toMatchObject({width: 3308, height: 6864, dpi: 300}); + // Where the picture is, as the map which drew it has it. + expect(result.bounds).toEqual([-150, 61.12499878, -149.875, 61.25000121]); + expect(progress).toEqual(['Water 1/3', 'Roads - Minor 2/3', 'Labels 3/3']); + + expect(record.options).toMatchObject({ + center: frame.center, zoom: frame.zoom, pixelRatio: 2, interactive: false, + fadeDuration: 0, maxCanvasSize: [6864, 6864], + // Where this MapLibre reads it from; as an option of the map itself it is ignored. + canvasContextAttributes: {preserveDrawingBuffer: true}, + }); + expect(record.options).not.toHaveProperty('preserveDrawingBuffer'); + expect(record.options.container.style.width).toBe('1654px'); + expect(record.options.container.style.height).toBe('3432px'); + expect(setup).toHaveBeenCalledTimes(1); + }); + + test('the names of small roads are read, and drawn with the layer they are of', async () => { + const record = {captures: []}; + const readTile = jest.fn(async () => fakeTile([ + {properties: {kind: 'minor_road', name: 'Elm Street'}, coordinates: [[-149.9, 61.2], [-149.9001, 61.2]]}, + ])); + const town = {...TOWN, layers: TOWN.layers.map(l => ({...l, layout: {...l.layout, visibility: 'visible'}}))}; + await renderExport({ + MapClass: fakeMapClass(record), style: town, frame, readTile, + groups: [{name: 'Labels', ids: ['roads_labels_minor', 'roads_labels_major']}], + }); + expect(readTile).toHaveBeenCalledWith('pmtiles://a', expect.objectContaining({z: 14})); + expect(record.options.style.sources['export-road-names:a.pmtiles'].data.features).toHaveLength(1); + expect(record.options.style.layers[1].source).toBe('export-road-names:a.pmtiles'); + expect(record.captures).toEqual([{type: 'image/png', visible: ['roads_labels_minor', 'roads_labels_major']}]); + }); + + test('an edge a hair past the edge of the world is the edge of the world', async () => { + // What the map's arithmetic makes of the last quadrangle before the antimeridian. + const east = await renderExport({ + MapClass: fakeMapClass({captures: [], bounds: [179.875, 0, 180.00000000000125, 0.125]}), + style, frame, groups, + }); + expect(east.bounds).toEqual([179.875, 0, 180, 0.125]); + + const west = await renderExport({ + MapClass: fakeMapClass({captures: [], bounds: [-180.00000000000125, 0, -179.875, 0.125]}), + style, frame, groups, + }); + expect(west.bounds).toEqual([-180, 0, -179.875, 0.125]); + + // A map which is really across the antimeridian is left as it is, for the API to refuse. + const across = await renderExport({ + MapClass: fakeMapClass({captures: [], bounds: [179.9, 0, 180.1, 0.125]}), style, frame, groups, + }); + expect(across.bounds).toEqual([179.9, 0, 180.1, 0.125]); + }); + + test('nothing is left behind, whether it works or not', async () => { + const record = {captures: []}; + await renderExport({MapClass: fakeMapClass(record), style, frame, groups}); + expect(record.removed).toBe(true); + expect(record.options.container.isConnected).toBe(false); + + const failed = {captures: [], failBlob: true}; + await expect(renderExport({MapClass: fakeMapClass(failed), style, frame, groups})) + .rejects.toThrow('could not be made into an image'); + expect(failed.removed).toBe(true); + expect(failed.options.container.isConnected).toBe(false); + }); + + test('a photograph with no gaps is a JPEG, and one with gaps is not', async () => { + const photo = [{name: 'Satellite', ids: ['water'], raster: true}]; + const gaps = {captures: []}; + await renderExport({MapClass: fakeMapClass(gaps), style, frame, groups: photo}); + expect(gaps.captures[0].type).toBe('image/png'); + + const opaque = new Uint8ClampedArray([9, 9, 9, 255, 9, 9, 9, 255]); + getContext.mockImplementation(() => ({ + clearRect: jest.fn(), drawImage: jest.fn(), getImageData: () => ({data: opaque}), + })); + const whole = {captures: []}; + await renderExport({MapClass: fakeMapClass(whole), style, frame, groups: photo}); + expect(whole.captures[0].type).toBe('image/jpeg'); + + // A photograph which ends a little inside the map: all of it reduced has no gap to see, and + // one of its edges, looked at by itself, is all gap. + const looks = []; + getContext.mockImplementation(() => ({ + clearRect: jest.fn(), + drawImage: (canvas, x, y, width, height) => looks.push([x, y, width, height]), + getImageData: () => ({data: looks.length === 5 ? new Uint8ClampedArray([0, 0, 0, 0]) : opaque}), + })); + const strip = {captures: []}; + await renderExport({MapClass: fakeMapClass(strip), style, frame, groups: photo}); + expect(strip.captures[0].type).toBe('image/png'); + // All of it, then its top, bottom, left and right edges. + const [[, , width, height]] = looks; + expect(looks).toEqual([ + [0, 0, width, height], + [0, 0, width, 1], [0, height - 1, width, 1], [0, 0, 1, height], [width - 1, 0, 1, height], + ]); + // What is not a photograph is never a JPEG, gaps or none. + const drawn = {captures: []}; + await renderExport({MapClass: fakeMapClass(drawn), style, frame, groups: groups.slice(0, 1)}); + expect(drawn.captures[0].type).toBe('image/png'); + }); + + describe('when how sharp the map may be depends on how many images it is', () => { + // A map of fewer images may be sharper. + const frameFor = count => ({...frame, pixelRatio: count <= 2 ? 2 : 1, dpi: count <= 2 ? 300 : 150}); + // Nothing is drawn in the roads of this quadrangle. + const blankRoads = canvas => canvas.visible().join() === 'roads_minor'; + const looking = record => fakeMapClass(record, { + canvas: map => ({visible: () => [...map.visible]}), + }); + + test('a group with nothing in it is not drawn, sent or counted', async () => { + const record = {captures: []}; + const progress = []; + const lookings = []; + const rendered = await renderExport({ + MapClass: looking(record), style, frameFor, groups, isBlank: blankRoads, + onLooking: () => lookings.push(record.captures.length), + onProgress: (name, index, count, drawn) => progress.push([name, index, count, drawn.dpi]), + }); + // Looked at first, on a small map; told once, before anything is drawn to keep. + expect(lookings).toEqual([0]); + // Two of the three groups have something in them, so the map is as sharp as two may be. + // It is a map of its own, made that sharp: a canvas is not made larger once it is made. + expect(record.pixelRatios).toEqual([0.5, 2]); + expect(record.maps).toBe(2); + expect(record.removals).toBe(2); + expect(rendered.layers.map(layer => layer.name)).toEqual(['Water', 'Labels']); + expect(progress).toEqual([['Water', 0, 2, 300], ['Labels', 1, 2, 300]]); + expect(rendered.dpi).toBe(300); + expect(rendered.width).toBe(frame.cssWidth * 2); + // Each canvas is allowed to be as large as its map is. + const side = Math.max(frame.cssWidth, frame.cssHeight); + expect(record.sides).toEqual([Math.ceil(side * 0.5), Math.ceil(side * 2)]); + }); + + test('every group is drawn when all have something in them', async () => { + const record = {captures: []}; + const rendered = await renderExport({ + MapClass: looking(record), style, frameFor, groups, isBlank: () => false, + }); + expect(rendered.layers.map(layer => layer.name)).toEqual(groups.map(group => group.name)); + expect(record.pixelRatios).toEqual([0.5, 1]); + expect(rendered.dpi).toBe(150); + }); + + test('a map with nothing in any group is not exported', async () => { + const record = {captures: []}; + await expect(renderExport({ + MapClass: looking(record), style, frameFor, groups, isBlank: () => true, + })).rejects.toThrow('Nothing is drawn in any layer of the map'); + expect(record.captures).toEqual([]); + expect(record.removed).toBe(true); + }); + + test('a canvas the browser stopped drawing on is not a group with nothing in it', async () => { + const record = {captures: []}; + const lost = fakeMapClass(record, { + canvas: () => ({getContext: kind => (kind === 'webgl2' ? {isContextLost: () => true} : null)}), + }); + await expect(renderExport({MapClass: lost, style, frameFor, groups, isBlank: () => true})) + .rejects.toThrow('The browser stopped drawing the map'); + expect(record.captures).toEqual([]); + expect(record.removed).toBe(true); + }); + + test('is stopped after the last group is looked at, before the map to export from is made', async () => { + const record = {captures: []}; + let looked = 0; + await expect(renderExport({ + MapClass: looking(record), style, frameFor, groups, isBlank: () => !++looked, + // Stopped while the last group is looked at. + isCancelled: () => looked >= groups.length, + })).rejects.toMatchObject({cancelled: true}); + expect(looked).toBe(groups.length); + expect(record.maps).toBe(1); + expect(record.captures).toEqual([]); + }); + + test('is stopped while the groups are looked at', async () => { + const record = {captures: []}; + let looked = 0; + await expect(renderExport({ + MapClass: looking(record), style, frameFor, groups, isBlank: () => !++looked, + isCancelled: () => looked >= 1, + })).rejects.toMatchObject({cancelled: true}); + expect(looked).toBe(1); + expect(record.captures).toEqual([]); + }); + }); + + test('stops before a map is made when it is cancelled while the names of roads are read', async () => { + const record = {captures: []}; + let cancelled = false; + const readTile = jest.fn(async () => { + cancelled = true; + return null; + }); + await expect(renderExport({ + MapClass: fakeMapClass(record), style: TOWN, frame, readTile, isCancelled: () => cancelled, + groups: [{name: 'Labels', ids: ['roads_labels_minor']}], + })).rejects.toMatchObject({cancelled: true}); + expect(record.maps).toBeUndefined(); + }); + + test('stops between two groups when it is cancelled, and leaves nothing behind', async () => { + const record = {captures: []}; + const progress = []; + const rendering = renderExport({ + MapClass: fakeMapClass(record), style, frame, groups, + onProgress: name => progress.push(name), + // Cancelled while the first group is drawn. + isCancelled: () => record.captures.length >= 1, + }); + await expect(rendering).rejects.toMatchObject({cancelled: true}); + // The second group was not drawn, nor said to be. + expect(record.captures).toHaveLength(1); + expect(progress).toEqual(['Water']); + expect(record.removed).toBe(true); + }); + + test('a map which never finishes drawing is given up on', async () => { + jest.useFakeTimers(); + try { + const record = {captures: [], neverIdle: true}; + const rendering = renderExport({MapClass: fakeMapClass(record), style, frame, groups}); + const failure = expect(rendering).rejects.toThrow('took too long'); + // Once the map has loaded and is waiting to be drawn, the time runs out. + for (let i = 0; i < 20; i++) await Promise.resolve(); + // The time the page can not be seen (another tab is in front) is not counted: nothing + // is drawn then. + const hidden = jest.spyOn(document, 'hidden', 'get').mockReturnValue(true); + jest.advanceTimersByTime(600_000); + hidden.mockRestore(); + jest.advanceTimersByTime(119_000); + expect(record.removed).toBeFalsy(); + jest.advanceTimersByTime(2_000); + await failure; + expect(record.removed).toBe(true); + } finally { + jest.useRealTimers(); + } + }); +}); + +describe('what is written on the sheet', () => { + const style = { + sources: { + basemap: {type: 'vector'}, + satellite: {type: 'raster', attribution: 'Imagery © Somebody'}, + 'overlay:towers': {type: 'vector', attribution: 'Towers by Somebody Else'}, + unused: {type: 'vector', attribution: 'Not on this map'}, + }, + layers: [ + {id: 'background', type: 'background'}, + {id: 'water', type: 'fill', source: 'basemap'}, + {id: 'roads', type: 'line', source: 'basemap'}, + {id: 'satellite', type: 'raster', source: 'satellite'}, + {id: 'overlay:towers:points', type: 'circle', source: 'overlay:towers'}, + {id: 'hidden', type: 'fill', source: 'unused'}, + ], + }; + // The basemap learns where it is from when its file is read. + const loaded = {basemap: {attribution: '© OpenStreetMap contributors'}}; + const map = {getStyle: () => style, getSource: id => loaded[id]}; + const groups = [ + {name: 'Background', ids: ['background']}, + {name: 'Water', ids: ['water']}, + {name: 'Roads', ids: ['roads']}, + {name: 'Satellite', ids: ['satellite']}, + {name: 'Towers', ids: ['overlay:towers:points']}, + ]; + + test('says where the data of the exported layers is from, once each, as text', () => { + expect(exportCredits(map, groups)).toEqual([ + '© OpenStreetMap contributors', 'Imagery © Somebody', 'Towers by Somebody Else', + ]); + expect(exportCredits(map, groups.slice(0, 1))).toEqual([]); + expect(exportCredits(map, [])).toEqual([]); + }); + + test('an attribution which names many sources is each of them', () => { + const many = { + getStyle: () => ({ + sources: {a: {type: 'vector'}, b: {type: 'vector'}}, + layers: [{id: 'a', type: 'line', source: 'a'}, {id: 'b', type: 'line', source: 'b'}], + }), + getSource: id => ({ + a: {attribution: '© OpenStreetMap contributors | © OpenMapTiles | Somebody'}, + b: {attribution: 'Somebody | Somebody Else | '}, + })[id], + }; + expect(exportCredits(many, [{name: 'A', ids: ['a', 'b']}])).toEqual([ + '© OpenStreetMap contributors', '© OpenMapTiles', 'Somebody', 'Somebody Else', + ]); + }); + + test('says the day as the person exporting has it', () => { + expect(exportDate(new Date(2026, 0, 5, 23, 59))).toBe('2026-01-05'); + expect(exportDate(new Date(2026, 10, 15))).toBe('2026-11-15'); + }); + + test('is the name of the quadrangle, its credits and the day', () => { + expect(exportSheet(ANCHORAGE, map, groups.slice(1, 2), new Date(2026, 10, 15))).toEqual({ + name: '61°07′30″N 150°00′W', credits: ['© OpenStreetMap contributors'], date: '2026-11-15', + }); + }); + + test('says how far apart the contour lines are, when the map has them', () => { + const contours = {interval: 40, unit: 'feet'}; + const sheet = exportSheet(ANCHORAGE, map, groups.slice(1, 2), new Date(2026, 10, 15), null, contours); + expect(sheet.contours).toEqual(contours); + expect(exportSheet(ANCHORAGE, map, groups.slice(1, 2), new Date(2026, 10, 15), null, null)) + .not.toHaveProperty('contours'); + }); + + test('has the legend of the map, when the map has one', () => { + const legend = {ground: [1, 1, 1], lines: [{label: 'Highway', color: [0, 0, 0], width: 1}]}; + const sheet = exportSheet(ANCHORAGE, map, groups.slice(1, 2), new Date(2026, 10, 15), null, null, legend); + expect(sheet.legend).toEqual(legend); + expect(exportSheet(ANCHORAGE, map, groups.slice(1, 2), new Date(2026, 10, 15))).not.toHaveProperty('legend'); + }); +}); + +describe('how far apart the contour lines of an exported map are', () => { + const exported = [ + {name: 'Water', ids: ['water']}, + {name: 'Contours', ids: ['contour-lines', 'contour-lines-major', 'contour-labels']}, + ]; + const parts = ['contour-lines', 'contour-labels']; + + test('is the least of the two intervals, in the unit the map measures in', () => { + expect(exportContours(exported, parts, [40, 200], true)).toEqual({interval: 40, unit: 'feet'}); + expect(exportContours(exported, parts, [20, 100], false)).toEqual({interval: 20, unit: 'meters'}); + // The darker lines alone are still contour lines. + expect(exportContours([{name: 'Contours', ids: ['contour-lines-major']}], parts, [40, 200], true)) + .toEqual({interval: 40, unit: 'feet'}); + }); + + test('is nothing when the contours are not exported, or are not drawn', () => { + expect(exportContours(exported.slice(0, 1), parts, [40, 200], true)).toBeNull(); + expect(exportContours([], parts, [40, 200], true)).toBeNull(); + expect(exportContours(exported, parts, null, true)).toBeNull(); + }); +}); + +describe('what the PDF is called', () => { + test('says where it is of', () => { + expect(exportFileName(ANCHORAGE)).toBe('wrolpi-map_24k_N61.125_W150.000.pdf'); + expect(exportFileName(quadAt(151.21, -33.87))).toBe('wrolpi-map_24k_S33.875_E151.125.pdf'); + expect(exportTitle(ANCHORAGE)).toBe('1:24,000 61°07′30″N 150°00′W'); + }); + + test('says what the USGS calls it, when it calls it anything', () => { + const berthoud = {name: 'Berthoud', state: 'Colorado', exact: true}; + const anchorage = {name: 'Anchorage A-8 NW', state: 'Alaska', exact: false}; + expect(exportFileName(BERTHOUD, berthoud)).toBe('wrolpi-map_24k_Berthoud_N40.250_W105.125.pdf'); + expect(exportFileName(ANCHORAGE, anchorage)).toBe('wrolpi-map_24k_Anchorage_A_8_NW_N61.125_W150.000.pdf'); + expect(exportFileName(BERTHOUD, {name: '../Ni‘ihau: “North”?'})).toBe( + 'wrolpi-map_24k_Ni_ihau_North_N40.250_W105.125.pdf'); + // An accent is left off its letter. + expect(exportFileName(BERTHOUD, {name: 'Cañon City'})).toBe('wrolpi-map_24k_Canon_City_N40.250_W105.125.pdf'); + expect(exportFileName(BERTHOUD, {name: '///'})).toBe('wrolpi-map_24k_N40.250_W105.125.pdf'); + expect(exportTitle(BERTHOUD, berthoud)).toBe('1:24,000 Berthoud, Colorado'); + // A quadrangle of the grid which is not all of the named one is a part of it. + expect(exportTitle(ANCHORAGE, anchorage)).toBe('1:24,000 Part of Anchorage A-8 NW, Alaska'); + const map = {getStyle: () => ({layers: [], sources: {}}), getSource: () => undefined}; + expect(exportSheet(BERTHOUD, map, [], new Date(2026, 10, 15), berthoud)).toEqual({ + name: 'Berthoud, Colorado', credits: [], date: '2026-11-15', + }); + expect(exportSheet(BERTHOUD, map, [], new Date(2026, 10, 15), null).name).toBe('40°15′N 105°07′30″W'); + }); +}); + +describe('what a basemap draws from a deeper zoom', () => { + const style = { + sources: {a: {type: 'vector', url: 'pmtiles://a'}}, + layers: [ + {id: 'water_river', type: 'line', source: 'a', minzoom: 9}, + {id: 'water_stream', type: 'line', source: 'a', minzoom: 14}, + {id: 'roads_taxiway', type: 'line', source: 'a', minzoom: 13}, + {id: 'buildings_3d', type: 'fill-extrusion', source: 'a', minzoom: 15}, + {id: 'pois', type: 'symbol', source: 'a', minzoom: 14}, + ], + }; + const minzooms = zoom => Object.fromEntries( + exportStyle(style, [], {zoom}).layers.map(layer => [layer.id, layer.minzoom])); + + test('is drawn on a quadrangle, which is drawn a little short of there', () => { + // A quadrangle at 40 degrees. + expect(minzooms(13.85)).toEqual({ + water_river: 9, water_stream: 13, roads_taxiway: 13, buildings_3d: 15, pois: 14, + }); + // Far north, a quadrangle is drawn from tiles two zooms short. + expect(minzooms(12.7)).toMatchObject({water_stream: 12, roads_taxiway: 12, pois: 14}); + }); + + test('is as it was where the map is drawn that deep, and when the zoom is not said', () => { + expect(minzooms(14.2)).toMatchObject({water_stream: 14, roads_taxiway: 13}); + expect(minzooms(null)).toMatchObject({water_stream: 14}); + }); +}); + +describe('the map files which are of somewhere else', () => { + const style = { + sources: { + 'alaska.pmtiles': {type: 'vector', url: 'pmtiles:///media/alaska.pmtiles'}, + 'colorado.pmtiles': {type: 'vector', url: 'pmtiles:///media/colorado.pmtiles'}, + 'beside.pmtiles': {type: 'vector', url: 'pmtiles:///media/beside.pmtiles'}, + 'unsaid.pmtiles': {type: 'vector', url: 'pmtiles:///media/unsaid.pmtiles'}, + 'broken.pmtiles': {type: 'vector', url: 'pmtiles:///media/broken.pmtiles'}, + 'terrain': {type: 'raster-dem', url: 'contours://terrain'}, + 'pins': {type: 'geojson', data: {}}, + }, + }; + const edges = { + 'pmtiles:///media/alaska.pmtiles': [-180, 51, -129, 72], + 'pmtiles:///media/colorado.pmtiles': [-109.1, 36.9, -102, 41.1], + // It ends where the quadrangle begins. + 'pmtiles:///media/beside.pmtiles': [-106, 40, -105.125, 41], + 'pmtiles:///media/unsaid.pmtiles': null, + }; + const asked = []; + const readBounds = async url => { + asked.push(url); + if (url.includes('broken')) throw new Error('No such file'); + return edges[url]; + }; + + test('are those whose file says it is of elsewhere, and no other', async () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined); + try { + const outside = await sourcesOutside(style, BERTHOUD_BOUNDS, readBounds); + expect([...outside].sort()).toEqual(['alaska.pmtiles', 'beside.pmtiles']); + // Only a map file is asked. + expect(asked).toHaveLength(5); + } finally { + warn.mockRestore(); + } + }); + + test('are none of a style with no sources', async () => { + expect((await sourcesOutside({}, BERTHOUD_BOUNDS, readBounds)).size).toBe(0); + }); +}); + +describe('whether nothing is drawn on a canvas', () => { + let getContext; + afterEach(() => getContext.mockRestore()); + + // A canvas of `rows` rows, with something drawn in the rows of `drawn`. + const look = (rows, drawn = []) => { + const bands = []; + getContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(() => { + let top = 0; + return { + clearRect: jest.fn(), + drawImage: (canvas, x, y) => { + top = y; + }, + getImageData: (x, y, width, height) => { + bands.push([top, height]); + const data = new Uint8ClampedArray(width * height * 4); + // One pixel, barely there. + for (const row of drawn) if (row >= top && row < top + height) data[(row - top) * width * 4 + 3] = 1; + return {data}; + }, + }; + }); + return {blank: isBlankCanvas({width: 4, height: rows}), bands}; + }; + + test('every row is looked at, a band at a time', () => { + expect(look(600)).toEqual({blank: true, bands: [[0, 256], [256, 256], [512, 88]]}); + }); + + test('one faint pixel is something drawn, and no more is looked at', () => { + expect(look(600, [599]).blank).toBe(false); + expect(look(600, [300])).toEqual({blank: false, bands: [[0, 256], [256, 256]]}); + }); + + test('a pixel which is not seen is nothing drawn, whatever color it has a trace of', () => { + getContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(() => ({ + clearRect: jest.fn(), + drawImage: jest.fn(), + // Red, green and blue, and no alpha: what a map leaves where it drew nothing. + getImageData: (x, y, width, height) => ({data: new Uint8ClampedArray(width * height * 4).fill(1) + .map((value, index) => (index % 4 === 3 ? 0 : value))}), + })); + expect(isBlankCanvas({width: 4, height: 4})).toBe(true); + }); + + test('a canvas which can not be looked at is not taken to be blank', () => { + getContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(() => null); + expect(isBlankCanvas({width: 4, height: 4})).toBe(false); + }); +}); diff --git a/app/src/components/mapExportLegend.js b/app/src/components/mapExportLegend.js new file mode 100644 index 00000000..9bb88a9a --- /dev/null +++ b/app/src/components/mapExportLegend.js @@ -0,0 +1,117 @@ +// The legend of an exported map: what its lines are, each as the map draws it. +// +// The map's style says how a road is drawn at each zoom, and a sheet is drawn at one. The lines are +// described here, where the style can be read, and drawn on the sheet by the API. + +import {expression, latest} from "@maplibre/maplibre-gl-style-spec"; +import {EXPORT_CSS_DPI} from "./mapExport"; + +const POINTS_PER_INCH = 72; +const WHITE = [1, 1, 1]; +// The thinnest line (points) a legend has. A style brings a line in from nothing as the map is +// zoomed in, and a line of a few hundredths of a point is not one anybody sees on a sheet. +export const LEGEND_LEAST_WIDTH = 0.1; + +// The lines a legend may have, from the top: what each is called, the layer of the basemap (or of +// the contours) which draws it, and the layers which may draw its edges under it. +export const LEGEND_LINES = [ + {label: "Highway", line: "roads_highway", casings: ["roads_highway_casing_late", "roads_highway_casing_early"]}, + {label: "Major road", line: "roads_major", casings: ["roads_major_casing_late", "roads_major_casing_early"]}, + {label: "Minor road", line: "roads_minor", casings: ["roads_minor_casing"]}, + {label: "Path or track", line: "roads_other"}, + {label: "Railway", line: "roads_rail"}, + {label: "Boundary", line: "boundaries"}, + {label: "Index contour", line: "contour-lines-major"}, + {label: "Contour", line: "contour-lines"}, +]; + +// The name of a basemap layer without the source it is of ("oregon.pmtiles:water" is "water"). +function layerName(layerId) { + const id = String(layerId); + return id.includes(":") ? id.split(":").slice(1).join(":") : id; +} + +// What a property of a layer is at a zoom; the style's own default when the layer does not say. +function valueAt(layer, group, property, zoom) { + const spec = latest[`${group}_${layer.type}`]?.[property]; + if (!spec) return undefined; + const value = layer[group]?.[property] ?? spec.default; + if (value === undefined) return undefined; + try { + return expression.normalizePropertyExpression(value, spec).evaluate({zoom}); + } catch (e) { + return undefined; + } +} + +// A color of the style as red, green and blue from 0 to 1, and how opaque it is. +function colorParts(color) { + if (!color || !Number.isFinite(color.a) || color.a <= 0) return null; + // The style keeps a color multiplied by its opacity. + const part = n => Math.round(Math.min(1, Math.max(0, n / color.a)) * 1000) / 1000; + return {color: [part(color.r), part(color.g), part(color.b)], opacity: color.a}; +} + +function drawnAt(layer, zoom) { + return (layer.minzoom === undefined || zoom >= layer.minzoom) && (layer.maxzoom === undefined || zoom < layer.maxzoom); +} + +const round = n => Math.round(n * 100) / 100; + +// The legend of a map which is exported: {ground, lines}, for the sheet (see `Sheet.legend` in +// modules/map/sheet.py), or null when the map has none of the lines a legend is of. +// +// style: the map's style. groups: the layers which are exported (of exportGroups). zoom: the zoom +// the map is exported at. cssDpi: how many CSS pixels of the style are an inch of the sheet. +// +// A line is in the legend when its layer is exported and draws a line which can be seen at that +// zoom: a path which the style only draws from zoom 14 is not on a sheet drawn at 13, nor in its +// legend, and at 14.2 it is too thin to be on either. +export function exportLegend(style, groups, zoom, cssDpi = EXPORT_CSS_DPI) { + const exported = new Set(groups.flatMap(group => group.ids)); + const layers = (style.layers || []).filter(layer => exported.has(layer.id) && drawnAt(layer, zoom)); + const points = POINTS_PER_INCH / cssDpi; + const find = name => layers.find(layer => layer.type === "line" && layerName(layer.id) === name); + + const lines = []; + for (const {label, line, casings = []} of LEGEND_LINES) { + const layer = find(line); + if (!layer) continue; + const width = valueAt(layer, "paint", "line-width", zoom); + const parts = colorParts(valueAt(layer, "paint", "line-color", zoom)); + if (!parts || !(width * points >= LEGEND_LEAST_WIDTH)) continue; + const entry = { + label, + color: parts.color, + width: round(width * points), + opacity: round(parts.opacity * (valueAt(layer, "paint", "line-opacity", zoom) ?? 1)), + }; + // The lengths of a dash are in widths of its line. + const dash = valueAt(layer, "paint", "line-dasharray", zoom); + if (Array.isArray(dash) && dash.length > 1) entry.dash = dash.map(n => round(n * width * points)); + // The edges of a road are a wider line under it, drawn as two lines with a gap between them. + const casing = casings.map(find).find(Boolean); + if (casing) { + const edge = valueAt(casing, "paint", "line-width", zoom); + const gap = valueAt(casing, "paint", "line-gap-width", zoom) ?? 0; + const color = colorParts(valueAt(casing, "paint", "line-color", zoom)); + if (color && edge > 0) { + entry.casing_color = color.color; + entry.casing_width = round((gap + 2 * edge) * points); + } + } + lines.push(entry); + } + if (!lines.length) return null; + + // The land the lines are on: the topmost of the layers which color all of it. + let ground = WHITE; + for (const layer of layers) { + const name = layerName(layer.id); + const color = layer.type === "background" ? valueAt(layer, "paint", "background-color", zoom) + : layer.type === "fill" && name === "earth" ? valueAt(layer, "paint", "fill-color", zoom) : null; + const parts = colorParts(color); + if (parts) ground = parts.color; + } + return {ground, lines}; +} diff --git a/app/src/components/mapExportLegend.test.js b/app/src/components/mapExportLegend.test.js new file mode 100644 index 00000000..78c62d3c --- /dev/null +++ b/app/src/components/mapExportLegend.test.js @@ -0,0 +1,133 @@ +import {exportLegend, LEGEND_LEAST_WIDTH, LEGEND_LINES} from './mapExportLegend'; + +// A basemap as the map's style has it: roads with edges under them, at widths which change with +// the zoom; and the contours of the terrain. +const grows = (...stops) => ['interpolate', ['exponential', 1.6], ['zoom'], ...stops]; +const STYLE = { + layers: [ + {id: 'background', type: 'background', paint: {'background-color': '#cccccc'}}, + {id: 'earth', type: 'fill', paint: {'fill-color': '#e2dfda'}}, + {id: 'roads_minor_casing', type: 'line', paint: { + 'line-color': '#e0e0e0', 'line-gap-width': grows(11, 0, 12.5, 0.5, 15, 2), 'line-width': grows(12, 0, 12.5, 1), + }}, + {id: 'roads_highway_casing_late', type: 'line', minzoom: 12, paint: { + 'line-color': '#e0e0e0', 'line-gap-width': 2, 'line-width': 1, + }}, + {id: 'roads_highway_casing_early', type: 'line', maxzoom: 12, paint: { + 'line-color': '#000000', 'line-gap-width': 1, 'line-width': 0.5, + }}, + {id: 'roads_other', type: 'line', paint: {'line-color': '#ebebeb', 'line-dasharray': [3, 1], 'line-width': grows(14, 0, 20, 7)}}, + {id: 'roads_minor', type: 'line', paint: { + 'line-color': grows(11, '#ebebeb', 16, '#ffffff'), 'line-width': grows(11, 0, 12.5, 0.5, 15, 2), + }}, + {id: 'roads_highway', type: 'line', paint: {'line-color': '#ffffff', 'line-width': 2}}, + {id: 'roads_rail', type: 'line', paint: { + 'line-dasharray': [0.3, 0.75], 'line-opacity': 0.5, 'line-color': '#a7b1b3', 'line-width': 1, + }}, + {id: 'boundaries', type: 'line', paint: { + 'line-color': '#adadad', 'line-width': 0.4, 'line-dasharray': ['step', ['zoom'], ['literal', [2]], 4, ['literal', [2, 1]]], + }}, + {id: 'roads_labels_minor', type: 'symbol'}, + {id: 'contour-lines', type: 'line', paint: {'line-color': 'rgba(150,100,50,0.3)', 'line-width': 0.5}}, + {id: 'contour-lines-major', type: 'line', paint: {'line-color': 'rgba(150,100,50,0.5)', 'line-width': 1}}, + ], +}; +const ALL = [{name: 'Everything', ids: STYLE.layers.map(layer => layer.id)}]; +const only = (...ids) => [{name: 'Some', ids}]; +const labels = legend => legend.lines.map(line => line.label); + +describe('the legend of an exported map', () => { + test('is of the lines which are exported and drawn at the zoom of the sheet, from the top', () => { + const legend = exportLegend(STYLE, ALL, 13.2); + // A path is drawn only from zoom 14: it is not on this sheet. + expect(labels(legend)).toEqual(['Highway', 'Minor road', 'Railway', 'Boundary', 'Index contour', 'Contour']); + expect(labels(exportLegend(STYLE, ALL, 15))).toContain('Path or track'); + // In the order they are listed, whatever order the style has them in. + const order = LEGEND_LINES.map(line => line.label); + expect(labels(legend)).toEqual(order.filter(label => labels(legend).includes(label))); + }); + + test('a line is as wide on the sheet as the map draws it: 150 pixels of the style are an inch', () => { + const {lines} = exportLegend(STYLE, ALL, 13.2); + const highway = lines.find(line => line.label === 'Highway'); + // 2 pixels are 0.96 points; its edges are a pixel each side of it. + expect(highway).toEqual({ + label: 'Highway', color: [1, 1, 1], width: 0.96, opacity: 1, + casing_color: [0.878, 0.878, 0.878], casing_width: 1.92, + }); + // At another scale of the style it is as much wider. + expect(exportLegend(STYLE, ALL, 13.2, 75).lines[0].width).toBe(1.92); + }); + + test('a color and a width which change with the zoom are those of the zoom', () => { + const at = zoom => exportLegend(STYLE, ALL, zoom).lines.find(line => line.label === 'Minor road'); + expect(at(13.2).width).toBeGreaterThan(at(12.6).width); + expect(at(13.2).width).toBeLessThan(at(15).width); + expect(at(15).width).toBe(0.96); + expect(at(12.6).color[0]).toBeLessThan(at(15.9).color[0]); + // The edges of a road are those of the layer which draws them at that zoom. + const highway = zoom => exportLegend(STYLE, ALL, zoom).lines[0]; + expect(highway(13).casing_color).toEqual([0.878, 0.878, 0.878]); + expect(highway(11).casing_color).toEqual([0, 0, 0]); + expect(highway(11).casing_width).toBe(0.96); + }); + + test('a broken line has its dashes, and a faint one how faint it is', () => { + const {lines} = exportLegend(STYLE, ALL, 13.2); + const line = label => lines.find(i => i.label === label); + // The dashes of a style are in widths of the line: 0.3 and 0.75 of 0.48 points. + expect(line('Railway')).toMatchObject({dash: [0.14, 0.36], opacity: 0.5, width: 0.48}); + expect(line('Boundary').dash).toEqual([0.38, 0.19]); + // A color which can be seen through is that color, and how much of it. + expect(line('Contour')).toEqual({label: 'Contour', color: [0.588, 0.392, 0.196], width: 0.24, opacity: 0.3}); + expect(line('Index contour')).toMatchObject({width: 0.48, opacity: 0.5}); + expect(line('Highway')).not.toHaveProperty('dash'); + }); + + test('is on the color of the land which is exported', () => { + expect(exportLegend(STYLE, ALL, 13.2).ground).toEqual([0.886, 0.875, 0.855]); + // Without the land the lines are on the color under it, and without that on paper. + expect(exportLegend(STYLE, only('background', 'roads_highway'), 13.2).ground).toEqual([0.8, 0.8, 0.8]); + expect(exportLegend(STYLE, only('roads_highway'), 13.2).ground).toEqual([1, 1, 1]); + }); + + test('has only what is exported', () => { + const legend = exportLegend(STYLE, only('roads_highway', 'contour-lines'), 13.2); + expect(labels(legend)).toEqual(['Highway', 'Contour']); + // The road without its edges. + expect(legend.lines[0]).not.toHaveProperty('casing_color'); + // A map with none of the lines has no legend. + expect(exportLegend(STYLE, only('earth', 'roads_labels_minor'), 13.2)).toBeNull(); + expect(exportLegend(STYLE, [], 13.2)).toBeNull(); + expect(exportLegend({}, ALL, 13.2)).toBeNull(); + }); + + test('the layers of a second basemap file are the same lines', () => { + const style = {layers: STYLE.layers.map(layer => ({...layer, id: `b.pmtiles:${layer.id}`}))}; + const groups = [{name: 'Everything', ids: style.layers.map(layer => layer.id)}]; + expect(labels(exportLegend(style, groups, 13.2))).toEqual(labels(exportLegend(STYLE, ALL, 13.2))); + }); + + test('a line too thin to be seen is not in the legend', () => { + // As the basemap draws a path: from nothing at zoom 14, where a quadrangle near the equator is drawn. + const path = {id: 'roads_other', type: 'line', paint: {'line-color': '#ebebeb', 'line-width': grows(14, 0, 20, 7)}}; + const style = {layers: [path, {id: 'roads_highway', type: 'line', paint: {'line-width': 2}}]}; + const groups = only('roads_other', 'roads_highway'); + expect(labels(exportLegend(style, groups, 14.2))).toEqual(['Highway']); + // Once it is a tenth of a point wide it is. + expect(labels(exportLegend(style, groups, 15.5))).toEqual(['Highway', 'Path or track']); + const least = {layers: [{id: 'roads_highway', type: 'line', paint: {'line-width': LEGEND_LEAST_WIDTH * 150 / 72}}]}; + expect(exportLegend(least, only('roads_highway'), 13).lines[0].width).toBe(LEGEND_LEAST_WIDTH); + const less = {layers: [{id: 'roads_highway', type: 'line', paint: {'line-width': 0.2}}]}; + expect(exportLegend(less, only('roads_highway'), 13)).toBeNull(); + }); + + test('a layer which says nothing of its line is drawn as a style draws one', () => { + const style = {layers: [{id: 'roads_highway', type: 'line'}]}; + expect(exportLegend(style, only('roads_highway'), 13).lines).toEqual([ + {label: 'Highway', color: [0, 0, 0], width: 0.48, opacity: 1}]); + // What can not be made a color or a width of is left out. + const broken = {layers: [{id: 'roads_highway', type: 'line', paint: {'line-color': ['nonsense'], 'line-width': 1}}]}; + expect(exportLegend(broken, only('roads_highway'), 13)).toBeNull(); + }); +}); diff --git a/app/src/components/mapOverlays.test.js b/app/src/components/mapOverlays.test.js index 6679f1a1..a230a16f 100644 --- a/app/src/components/mapOverlays.test.js +++ b/app/src/components/mapOverlays.test.js @@ -1,6 +1,8 @@ -import {addOverlaysToMap, orderOverlayLayers} from '../map-shared/overlays.js'; +import {addOverlaysToMap, isOverlayLabelLayer, orderOverlayLayers} from '../map-shared/overlays.js'; import { addOverlaysToStyle, + basemapLabelsStart, + labelsOverGround, buildOverlayLayers, buildOverlaySource, isBasemapLayer, @@ -240,7 +242,7 @@ describe('the style of an overlay', () => { expect(visibility(setOverlayEnabled(state, FEATURES, true))).toEqual(['visible', 'none', 'visible']); }); - test('a raster overlay goes under the basemap labels, vector overlays on top', () => { + test('the ground of an overlay goes under the basemap labels, and what it marks goes on top', () => { const sources = {basemap: {}}; const layers = [ {id: 'earth', type: 'fill'}, {id: 'roads', type: 'line'}, @@ -250,19 +252,114 @@ describe('the style of an overlay', () => { expect(Object.keys(sources)).toEqual(['basemap', 'overlay:extras/features.pmtiles', 'overlay:satellite.pmtiles']); expect(layers.map(l => l.id)).toEqual([ 'earth', 'roads', + // A picture under an area under a line: none of them over a name. 'overlay:satellite.pmtiles:raster', - 'places', 'pois', - // Fills under lines under points. 'overlay:extras/features.pmtiles:flood_zones:fill', 'overlay:extras/features.pmtiles:power_lines:line', + 'places', 'pois', 'overlay:extras/features.pmtiles:towers:circle', ]); }); - test('a raster overlay is drawn last when the basemap has no labels', () => { + test('the ground of an overlay is over the ground of every basemap file', () => { + // A map of two basemap files: the labels of the first are under the ground of the second. + const layers = [ + {id: 'earth', type: 'fill'}, {id: 'roads', type: 'line'}, {id: 'places', type: 'symbol'}, + {id: 'second.pmtiles:water', type: 'fill'}, {id: 'second.pmtiles:roads', type: 'line'}, + {id: 'second.pmtiles:places', type: 'symbol'}, {id: 'second.pmtiles:pois', type: 'symbol'}, + ]; + expect(basemapLabelsStart(layers)).toBe(5); + addOverlaysToStyle({}, layers, [FEATURES, SATELLITE], {}); + expect(layers.map(l => l.id)).toEqual([ + 'earth', 'roads', 'places', + 'second.pmtiles:water', 'second.pmtiles:roads', + // Over the land, water and roads of both files; under the labels which are over them. + 'overlay:satellite.pmtiles:raster', + 'overlay:extras/features.pmtiles:flood_zones:fill', + 'overlay:extras/features.pmtiles:power_lines:line', + 'second.pmtiles:places', 'second.pmtiles:pois', + 'overlay:extras/features.pmtiles:towers:circle', + ]); + }); + + test('the ground of an overlay is under the basemap labels when the map has contours', () => { + const layers = [ + {id: 'earth', type: 'fill'}, {id: 'places', type: 'symbol'}, + {id: 'second.pmtiles:water', type: 'fill'}, {id: 'second.pmtiles:places', type: 'symbol'}, + {id: 'contour-lines', type: 'line', source: 'contours'}, + {id: 'contour-labels', type: 'symbol', source: 'contours'}, + ]; + addOverlaysToStyle({}, layers, [FEATURES], {}); + expect(layers.map(l => l.id)).toEqual([ + 'earth', 'places', 'second.pmtiles:water', + 'overlay:extras/features.pmtiles:flood_zones:fill', + 'overlay:extras/features.pmtiles:power_lines:line', + 'second.pmtiles:places', + 'contour-lines', 'contour-labels', + 'overlay:extras/features.pmtiles:towers:circle', + ]); + }); + + test('the labels of every basemap file go over the ground of every file', () => { + const layers = [ + {id: 'earth', type: 'fill'}, {id: 'roads', type: 'line'}, {id: 'places', type: 'symbol'}, + {id: 'second.pmtiles:water', type: 'fill'}, {id: 'second.pmtiles:places', type: 'symbol'}, + {id: 'third.pmtiles:roads', type: 'line'}, {id: 'third.pmtiles:pois', type: 'symbol'}, + ]; + const ordered = labelsOverGround(layers); + expect(ordered.map(l => l.id)).toEqual([ + 'earth', 'roads', 'second.pmtiles:water', 'third.pmtiles:roads', + 'places', 'second.pmtiles:places', 'third.pmtiles:pois', + ]); + // The same layers, and what was given is not changed. + expect(ordered).toHaveLength(layers.length); + expect(layers[2].id).toBe('places'); + expect(labelsOverGround(undefined)).toEqual([]); + + // An overlay then goes under the labels of all three files, and over the ground of all three. + addOverlaysToStyle({}, ordered, [FEATURES], {}); + expect(ordered.map(l => l.id)).toEqual([ + 'earth', 'roads', 'second.pmtiles:water', 'third.pmtiles:roads', + 'overlay:extras/features.pmtiles:flood_zones:fill', + 'overlay:extras/features.pmtiles:power_lines:line', + 'places', 'second.pmtiles:places', 'third.pmtiles:pois', + 'overlay:extras/features.pmtiles:towers:circle', + ]); + }); + + test('where the labels of a basemap begin', () => { + const symbol = id => ({id, type: 'symbol'}); + const line = id => ({id, type: 'line'}); + expect(basemapLabelsStart([])).toBe(0); + expect(basemapLabelsStart(undefined)).toBe(0); + expect(basemapLabelsStart([line('roads')])).toBe(1); + expect(basemapLabelsStart([symbol('places')])).toBe(0); + // The contour lines are drawn after the basemap's labels, and are not of the basemap: what goes + // under the labels does not go under the contours instead, which would be over every label. + const contours = [ + {id: 'contour-lines', type: 'line', source: 'contours'}, + {id: 'contour-labels', type: 'symbol', source: 'contours'}, + ]; + expect(basemapLabelsStart([line('roads'), symbol('places'), symbol('pois'), ...contours])).toBe(1); + // With no label over the basemap's ground, it is over that ground and under the contours. + expect(basemapLabelsStart([line('roads'), ...contours])).toBe(1); + // An overlay which is already on the map is not of the basemap. + expect(basemapLabelsStart([ + line('roads'), {id: 'overlay:a.pmtiles:x:line', type: 'line'}, symbol('places'), + {id: 'overlay:a.pmtiles:x:circle', type: 'circle'}, + ])).toBe(2); + }); + + test('an overlay is drawn last when the basemap has no labels', () => { const layers = [{id: 'earth', type: 'fill'}]; - addOverlaysToStyle({}, layers, [SATELLITE], {}); - expect(layers.map(l => l.id)).toEqual(['earth', 'overlay:satellite.pmtiles:raster']); + addOverlaysToStyle({}, layers, [SATELLITE, FEATURES], {}); + expect(layers.map(l => l.id)).toEqual([ + 'earth', + 'overlay:satellite.pmtiles:raster', + 'overlay:extras/features.pmtiles:flood_zones:fill', + 'overlay:extras/features.pmtiles:power_lines:line', + 'overlay:extras/features.pmtiles:towers:circle', + ]); }); }); @@ -371,8 +468,11 @@ describe('adding overlays to a map a layer at a time', () => { const whole = BASEMAP.map(l => ({...l})); addOverlaysToStyle(sources, whole, [FEATURES, SATELLITE], {}); expect(map.layers.map(l => l.id)).toEqual(whole.map(l => l.id)); - // The raster is under the basemap's labels. - expect(map.layers.map(l => l.id).indexOf('overlay:satellite.pmtiles:raster')).toBe(1); + // The raster, and the overlay's areas and lines, are under the basemap's labels; its points are over them. + const ids = map.layers.map(l => l.id); + expect(ids.indexOf('overlay:satellite.pmtiles:raster')).toBe(1); + expect(ids.indexOf('overlay:extras/features.pmtiles:power_lines:line')).toBeLessThan(ids.indexOf('places')); + expect(ids.indexOf('overlay:extras/features.pmtiles:towers:circle')).toBeGreaterThan(ids.indexOf('pois')); }); test('layers are added as the user last chose to see them', () => { @@ -389,10 +489,11 @@ describe('adding overlays to a map a layer at a time', () => { const refused = addOverlaysToMap(map, [FEATURES, other], {}, URLS); expect(refused).toEqual({'extras/features.pmtiles': 1, 'other.pmtiles': 1}); expect(map.layers.map(l => l.id)).toEqual([ - 'earth', 'places', 'pois', + 'earth', 'overlay:extras/features.pmtiles:flood_zones:fill', 'overlay:extras/features.pmtiles:power_lines:line', 'overlay:other.pmtiles:roads:line', + 'places', 'pois', ]); }); @@ -412,23 +513,32 @@ describe('adding overlays to a map a layer at a time', () => { expect(map.layers.map(l => l.id)).toEqual(before); }); - test('an overlay with its own style keeps that style\'s order, under the generic ones', () => { + test('an overlay with its own style keeps that style\'s order, under the generic ones, in each part', () => { const styled = { ...FEATURES, map_path: 'styled.pmtiles', styleLayers: [ {id: 'overlay:styled.pmtiles:towers:icons', type: 'symbol', source: 'overlay:styled.pmtiles', 'source-layer': 'towers'}, {id: 'overlay:styled.pmtiles:flood_zones:area', type: 'fill', source: 'overlay:styled.pmtiles', 'source-layer': 'flood_zones'}, + {id: 'overlay:styled.pmtiles:towers:names', type: 'symbol', source: 'overlay:styled.pmtiles', 'source-layer': 'towers'}, + {id: 'overlay:styled.pmtiles:power_lines:wires', type: 'line', source: 'overlay:styled.pmtiles', 'source-layer': 'power_lines'}, ], }; - const {rasters, vectors} = orderOverlayLayers([FEATURES, styled, SATELLITE], {}); - expect(rasters.map(l => l.id)).toEqual(['overlay:satellite.pmtiles:raster']); - expect(vectors.map(l => l.id)).toEqual([ - 'overlay:styled.pmtiles:towers:icons', + const {under, over} = orderOverlayLayers([FEATURES, styled, SATELLITE], {}); + // What goes under the basemap's labels: no text, icon or point. + expect(under.map(l => l.id)).toEqual([ + 'overlay:satellite.pmtiles:raster', 'overlay:styled.pmtiles:flood_zones:area', + 'overlay:styled.pmtiles:power_lines:wires', 'overlay:extras/features.pmtiles:flood_zones:fill', 'overlay:extras/features.pmtiles:power_lines:line', + ]); + // What goes over them: every label, whichever overlay it is of. + expect(over.map(l => l.id)).toEqual([ + 'overlay:styled.pmtiles:towers:icons', + 'overlay:styled.pmtiles:towers:names', 'overlay:extras/features.pmtiles:towers:circle', ]); + expect([...under, ...over].every(l => isOverlayLabelLayer(l) === over.includes(l))).toBe(true); }); }); diff --git a/app/src/components/mapPrintStyle.js b/app/src/components/mapPrintStyle.js new file mode 100644 index 00000000..703f7f27 --- /dev/null +++ b/app/src/components/mapPrintStyle.js @@ -0,0 +1,353 @@ +// The map as it is printed. The light and dark maps are made for a screen, which is lit: their +// roads are white on pale gray and their streams half a pixel of pale blue, which is nothing on +// paper. The printed map is drawn on white, with dark lines and names, in the colors a topographic +// sheet has: black for what was built, blue for water, green for what grows, brown for the land. + +import {layersWithCustomTheme, namedTheme} from "protomaps-themes-base"; + +// What the map is called where a look is chosen and remembered. +export const PRINT_LOOK = "print"; +// The looks a map may be exported in: the printed map, and the two a screen has. +export const EXPORT_LOOKS = [ + {value: PRINT_LOOK, label: "Print"}, + {value: "light", label: "Light"}, + {value: "dark", label: "Dark"}, +]; +const STORAGE_KEY_LOOK = "wrolpi-map-export-look"; +const STORAGE_KEY_COLORS = "wrolpi-map-print-colors"; + +const BLACK = "#1a1a1a"; +const CASING = "#4d4d4d"; +const WHITE = "#ffffff"; +const WATER = "#a9d4f2"; +// Water which is a line (a stream) or a name is darker than water which is an area: a thin pale +// line is not seen. +const WATER_LINE = "#2f7fc4"; +const WATER_NAME = "#1b5fa3"; +const HIGHWAY = "#e2725b"; +const MAJOR = "#f2c14e"; +const TUNNEL = "#e6e6e6"; +const TUNNEL_CASING = "#9a9a9a"; + +// The colors of the printed map which are not the light map's. A basemap is drawn in some seventy +// colors (see `namedTheme`), and these are every one which is seen at the scale of a quadrangle. +export const PRINT_COLORS = { + background: WHITE, + earth: WHITE, + park_a: "#dcefd0", + park_b: "#c2e2b0", + wood_a: "#d3ebc6", + wood_b: "#b5dca4", + scrub_a: "#e3edcd", + scrub_b: "#cfe0b3", + hospital: "#f3e3e0", + school: "#f1ead9", + industrial: "#e6e4ec", + pedestrian: "#efefef", + glacier: "#eef7fd", + sand: "#f4ecd2", + beach: "#f4ecd2", + aerodrome: "#e7e7ee", + runway: "#a9a9b3", + zoo: "#d6ecd6", + military: "#eedddd", + water: WATER, + pier: "#d9d9d9", + buildings: "#6e6e6e", + + tunnel_other_casing: TUNNEL_CASING, + tunnel_minor_casing: TUNNEL_CASING, + tunnel_link_casing: TUNNEL_CASING, + tunnel_major_casing: TUNNEL_CASING, + tunnel_highway_casing: TUNNEL_CASING, + tunnel_other: TUNNEL, + tunnel_minor: TUNNEL, + tunnel_link: TUNNEL, + tunnel_major: TUNNEL, + tunnel_highway: TUNNEL, + + minor_service_casing: CASING, + minor_casing: CASING, + link_casing: CASING, + major_casing_late: BLACK, + major_casing_early: BLACK, + highway_casing_late: BLACK, + highway_casing_early: BLACK, + other: CASING, + minor_service: WHITE, + minor_a: WHITE, + minor_b: WHITE, + link: MAJOR, + major: MAJOR, + highway: HIGHWAY, + + bridges_other_casing: CASING, + bridges_minor_casing: CASING, + bridges_link_casing: CASING, + bridges_major_casing: BLACK, + bridges_highway_casing: BLACK, + bridges_other: CASING, + bridges_minor: WHITE, + bridges_link: MAJOR, + bridges_major: MAJOR, + bridges_highway: HIGHWAY, + + railway: BLACK, + boundaries: "#5b4b8a", + + waterway_label: WATER_NAME, + ocean_label: WATER_NAME, + peak_label: "#6b4423", + roads_label_minor: BLACK, + roads_label_minor_halo: WHITE, + roads_label_major: BLACK, + roads_label_major_halo: WHITE, + subplace_label: "#333333", + subplace_label_halo: WHITE, + city_label: "#000000", + city_label_halo: WHITE, + state_label: "#555555", + state_label_halo: WHITE, + country_label: "#555555", + address_label: "#444444", + address_label_halo: WHITE, + pois: { + blue: "#12648a", + green: "#17643a", + lapis: "#24449c", + pink: "#b02f85", + red: "#b5304f", + slategray: "#4b3f69", + tangerine: "#9a4e03", + turquoise: "#00808c", + }, + landcover: { + grassland: "rgba(228, 242, 216, 1)", + barren: "rgba(248, 241, 222, 1)", + urban_area: "rgba(240, 240, 240, 1)", + farmland: "rgba(234, 243, 216, 1)", + glacier: "rgba(238, 247, 253, 1)", + scrub: "rgba(229, 238, 208, 1)", + forest: "rgba(205, 232, 194, 1)", + }, +}; + +// What a color does not say of a layer of the printed map: how wide its line is, and how much of +// it is seen. {layer id: paint which is the layer's on paper}. +export const PRINT_PAINT = { + // A stream is half a pixel on a screen, and a river is the color of a lake. + water_stream: {"line-color": WATER_LINE, "line-width": 1.2}, + water_river: {"line-color": WATER_LINE}, + // A path is not drawn on a screen until the map is deeper than a quadrangle is drawn. + roads_other: {"line-width": 1.2, "line-dasharray": [4, 2]}, + roads_bridges_other: {"line-width": 1.2, "line-dasharray": [4, 2]}, + // A basemap draws a ramp on a bridge in the color of a minor road on one, not in a ramp's. + roads_bridges_link: {"line-color": MAJOR}, + roads_rail: {"line-opacity": 1}, + boundaries: {"line-width": 1.2}, + boundaries_country: {"line-width": 1.6}, + buildings: {"fill-opacity": 0.75}, +}; + +// The contour lines and their heights, which are not of a basemap: brown, and dark enough to follow. +export const PRINT_CONTOURS = { + minor: {"line-color": "rgba(140, 85, 35, 0.55)", "line-width": 0.6}, + major: {"line-color": "rgba(140, 85, 35, 0.9)", "line-width": 1.1}, + label: {"text-color": "rgba(110, 65, 25, 1)", "text-halo-color": "rgba(255, 255, 255, 0.9)"}, +}; + +const HEX_COLOR = /^#[0-9a-f]{6}$/i; + +function channels(hex) { + return [1, 3, 5].map(at => parseInt(hex.slice(at, at + 2), 16)); +} + +// A color part of the way (0 to 1) to another: `mix("#336699", "#ffffff", 0.4)` is paler. +export function mix(hex, toward, part) { + const to = channels(toward); + return "#" + channels(hex) + .map((value, index) => Math.round(value + (to[index] - value) * part).toString(16).padStart(2, "0")) + .join(""); +} + +const paler = (hex, part) => mix(hex, WHITE, part); +const darker = (hex, part) => mix(hex, "#000000", part); + +function rgba(hex, alpha) { + return `rgba(${channels(hex).join(", ")}, ${alpha})`; +} + +// The colors of the printed map which a person may choose, in the order they are offered. Each is +// of a group of the layer control (`group`), is called `label` there, and is `color` until another +// is chosen. A basemap has several colors where a person chooses one (a park and a wood are both +// what grows), so a chosen color says what the others are: +// colors(color): the colors of the basemap (see PRINT_COLORS) it stands for. +// paint(color): the paint of layers (see PRINT_PAINT) which is of that color. +// contours(color): the paint of the contour lines and their heights (see PRINT_CONTOURS). +// The edge of a road is not chosen: it is what makes a road a line on paper, and stays dark. +export const PRINT_SWATCHES = [ + { + id: "water", group: "Water", label: "Water", color: WATER, + colors: color => ({water: color, waterway_label: darker(color, 0.55), ocean_label: darker(color, 0.55)}), + // A line of water is darker than an area of it: a thin pale line is not seen. + paint: color => ({ + water_stream: {"line-color": darker(color, 0.4)}, + water_river: {"line-color": darker(color, 0.4)}, + }), + }, + { + id: "vegetation", group: "Land Cover", label: "Parks and woods", color: "#c2e2b0", + colors: color => ({ + park_a: paler(color, 0.4), park_b: color, + wood_a: paler(color, 0.25), wood_b: darker(color, 0.06), + scrub_a: paler(color, 0.5), scrub_b: paler(color, 0.2), + zoo: paler(color, 0.35), + }), + }, + { + id: "built", group: "Land Cover", label: "Schools, hospitals and industry", color: "#e6e4ec", + colors: color => ({ + hospital: color, school: color, industrial: color, military: color, + pedestrian: paler(color, 0.3), aerodrome: color, + }), + }, + { + id: "bare", group: "Land Cover", label: "Sand and ice", color: "#f4ecd2", + colors: color => ({sand: color, beach: color, glacier: paler(color, 0.5)}), + }, + { + id: "buildings", group: "Buildings", label: "Buildings", color: "#6e6e6e", + colors: color => ({buildings: color}), + }, + { + id: "highway", group: "Roads - Highway", label: "Highways", color: HIGHWAY, + colors: color => ({highway: color, bridges_highway: color}), + }, + { + id: "major", group: "Roads - Major", label: "Major roads", color: MAJOR, + colors: color => ({major: color, link: color, bridges_major: color}), + // A ramp on a bridge, which a basemap does not draw in a color of its own. + paint: color => ({roads_bridges_link: {"line-color": color}}), + }, + { + id: "minor", group: "Roads - Minor", label: "Minor roads", color: WHITE, + colors: color => ({minor_service: color, minor_a: color, minor_b: color, bridges_minor: color}), + }, + { + id: "rail", group: "Rail & Runways", label: "Railways", color: BLACK, + colors: color => ({railway: color}), + }, + { + id: "boundaries", group: "Boundaries", label: "Boundaries", color: "#5b4b8a", + colors: color => ({boundaries: color}), + }, + { + id: "contours", group: "Contours", label: "Contours", color: "#8c5523", + contours: color => ({ + minor: {"line-color": rgba(color, 0.55)}, + major: {"line-color": rgba(color, 0.9)}, + label: {"text-color": darker(color, 0.2)}, + }), + }, + { + id: "names", group: "Labels", label: "Names", color: BLACK, + colors: color => ({ + roads_label_minor: color, roads_label_major: color, city_label: color, subplace_label: color, + address_label: color, state_label: paler(color, 0.3), country_label: paler(color, 0.3), + }), + }, + { + id: "halo", group: "Labels", label: "Around names", color: WHITE, + colors: color => ({ + roads_label_minor_halo: color, roads_label_major_halo: color, city_label_halo: color, + subplace_label_halo: color, state_label_halo: color, address_label_halo: color, + }), + contours: color => ({label: {"text-halo-color": rgba(color, 0.9)}}), + }, +]; + +// What was chosen ({swatch id: "#rrggbb"}) with all that is not a choice left out: a color of no +// swatch, a color which is not one, and a color which is the swatch's own. +export function chosenColors(chosen) { + const colors = {}; + for (const {id, color} of PRINT_SWATCHES) { + const value = chosen?.[id]; + if (typeof value === "string" && HEX_COLOR.test(value) && value.toLowerCase() !== color.toLowerCase()) { + colors[id] = value.toLowerCase(); + } + } + return colors; +} + +// Each kind of thing a swatch says, with what was chosen over what the printed map has. +function withChosen(kind, picked, base) { + const result = {...base}; + const colors = chosenColors(picked); + for (const swatch of PRINT_SWATCHES) { + const color = colors[swatch.id]; + if (!color || !swatch[kind]) continue; + for (const [key, value] of Object.entries(swatch[kind](color))) { + result[key] = typeof value === "object" ? {...result[key], ...value} : value; + } + } + return result; +} + +// Every color of the printed map. colors: what a person chose ({swatch id: "#rrggbb"}). +export function printTheme(colors = {}) { + return withChosen("colors", colors, {...namedTheme("light"), ...PRINT_COLORS}); +} + +// The paint of the contour lines and their heights on the printed map. +export function printContours(colors = {}) { + return withChosen("contours", colors, PRINT_CONTOURS); +} + +// The layers of a basemap file (a source of the map's style) as they are printed. +export function printLayers(source, colors = {}, lang = undefined) { + const paints = withChosen("paint", colors, PRINT_PAINT); + return layersWithCustomTheme(source, printTheme(colors), lang).map(layer => { + const paint = paints[layer.id]; + return paint ? {...layer, paint: {...layer.paint, ...paint}} : layer; + }); +} + +// The colors a person chose for the printed map, in this browser. +export function savedPrintColors() { + try { + return chosenColors(JSON.parse(localStorage.getItem(STORAGE_KEY_COLORS))); + } catch (e) { + return {}; + } +} + +// Remember what was chosen; with nothing chosen, nothing is kept. +export function savePrintColors(colors) { + const kept = chosenColors(colors); + try { + if (Object.keys(kept).length) localStorage.setItem(STORAGE_KEY_COLORS, JSON.stringify(kept)); + else localStorage.removeItem(STORAGE_KEY_COLORS); + } catch (e) { /* ignore */ } + return kept; +} + +// The look a map was last exported in, in this browser: the printed map until another is chosen. +export function savedExportLook() { + try { + const saved = localStorage.getItem(STORAGE_KEY_LOOK); + if (EXPORT_LOOKS.some(look => look.value === saved)) return saved; + } catch (e) { /* ignore */ } + return PRINT_LOOK; +} + +export function saveExportLook(look) { + try { localStorage.setItem(STORAGE_KEY_LOOK, look); } catch (e) { /* ignore */ } +} + +// The flavor and the sprite of the map's style for a look: {flavor, sprite}. Nothing for no look, +// which is the map as the screen has it. +export function lookStyle(look) { + if (look === PRINT_LOOK) return {flavor: PRINT_LOOK, sprite: "light"}; + if (look === "light" || look === "dark") return {flavor: look, sprite: look}; + return {}; +} diff --git a/app/src/components/mapPrintStyle.test.js b/app/src/components/mapPrintStyle.test.js new file mode 100644 index 00000000..8b0ca202 --- /dev/null +++ b/app/src/components/mapPrintStyle.test.js @@ -0,0 +1,151 @@ +import layers, {namedTheme} from 'protomaps-themes-base'; +import { + chosenColors, EXPORT_LOOKS, lookStyle, mix, PRINT_COLORS, PRINT_CONTOURS, PRINT_LOOK, PRINT_PAINT, PRINT_SWATCHES, + printContours, printLayers, printTheme, savedExportLook, savedPrintColors, saveExportLook, savePrintColors, +} from './mapPrintStyle'; + +describe('the printed map', () => { + const printed = printLayers('a.pmtiles'); + const byId = Object.fromEntries(printed.map(layer => [layer.id, layer])); + + test('has every layer the light map has, of the same source', () => { + const light = layers('a.pmtiles', 'light'); + expect(printed.map(layer => layer.id)).toEqual(light.map(layer => layer.id)); + expect(new Set(printed.map(layer => layer.source).filter(Boolean))).toEqual(new Set(['a.pmtiles'])); + }); + + test('is drawn on white, in dark lines', () => { + expect(byId.background.paint['background-color']).toBe('#ffffff'); + expect(byId.earth.paint['fill-color']).toBe('#ffffff'); + // The edge of a road is what is seen of it on paper. + expect(byId.roads_minor_casing.paint['line-color']).toBe('#4d4d4d'); + expect(byId.roads_highway.paint['line-color']).toBe(PRINT_COLORS.highway); + expect(byId.roads_labels_minor.paint['text-color']).toBe('#1a1a1a'); + expect(byId.roads_labels_minor.paint['text-halo-color']).toBe('#ffffff'); + }); + + test('has lines which are wide enough to see', () => { + // A stream is not the color of a lake, nor half a pixel. + expect(byId.water_stream.paint['line-width']).toBeGreaterThan(1); + expect(byId.water_stream.paint['line-color']).not.toBe(byId.water.paint['fill-color']); + // A path is drawn however deep the map is, and is still a broken line. + expect(byId.roads_other.paint['line-width']).toBe(1.2); + expect(byId.roads_other.paint['line-dasharray']).toEqual([4, 2]); + expect(byId.roads_rail.paint['line-opacity']).toBe(1); + }); + + test('changes only layers a basemap has, and colors it has', () => { + for (const id of Object.keys(PRINT_PAINT)) expect(Object.keys(byId)).toContain(id); + const light = namedTheme('light'); + for (const color of Object.keys(PRINT_COLORS)) expect(Object.keys(light)).toContain(color); + expect(Object.keys(printTheme()).sort()).toEqual(Object.keys(light).sort()); + expect(Object.keys(PRINT_COLORS.pois).sort()).toEqual(Object.keys(light.pois).sort()); + expect(Object.keys(PRINT_COLORS.landcover).sort()).toEqual(Object.keys(light.landcover).sort()); + }); +}); + +describe('the look a map is exported in', () => { + beforeEach(() => localStorage.clear()); + + test('is the printed map until another is chosen, and is remembered', () => { + expect(savedExportLook()).toBe(PRINT_LOOK); + saveExportLook('dark'); + expect(savedExportLook()).toBe('dark'); + // What is not a look is not remembered as one. + localStorage.setItem('wrolpi-map-export-look', 'sepia'); + expect(savedExportLook()).toBe(PRINT_LOOK); + expect(EXPORT_LOOKS.map(look => look.value)).toEqual([PRINT_LOOK, 'light', 'dark']); + }); + + test('says how the map is drawn', () => { + // The printed map has the icons of the light one. + expect(lookStyle(PRINT_LOOK)).toEqual({flavor: PRINT_LOOK, sprite: 'light'}); + expect(lookStyle('light')).toEqual({flavor: 'light', sprite: 'light'}); + expect(lookStyle('dark')).toEqual({flavor: 'dark', sprite: 'dark'}); + // With no look chosen, the map is as the screen has it: nothing is said. + expect(lookStyle(null)).toEqual({}); + }); +}); + +describe('the colors a person chooses for the printed map', () => { + beforeEach(() => localStorage.clear()); + + test('each is a color the printed map has until another is chosen', () => { + const theme = printTheme(); + const paints = Object.fromEntries(printLayers('a.pmtiles').map(layer => [layer.id, layer.paint])); + for (const swatch of PRINT_SWATCHES) { + // Choosing the color it has changes nothing: what it stands for is what the map has. + const same = {[swatch.id]: swatch.color}; + expect(chosenColors(same)).toEqual({}); + // Every color and layer it speaks of is one a basemap has. + for (const key of Object.keys(swatch.colors?.(swatch.color) || {})) expect(Object.keys(theme)).toContain(key); + for (const id of Object.keys(swatch.paint?.(swatch.color) || {})) expect(Object.keys(paints)).toContain(id); + for (const key of Object.keys(swatch.contours?.(swatch.color) || {})) { + expect(Object.keys(PRINT_CONTOURS)).toContain(key); + } + } + // No two have one name, and none speaks of a color another does. + expect(new Set(PRINT_SWATCHES.map(swatch => swatch.id)).size).toBe(PRINT_SWATCHES.length); + const spoken = PRINT_SWATCHES.flatMap(swatch => Object.keys(swatch.colors?.(swatch.color) || {})); + expect(new Set(spoken).size).toBe(spoken.length); + }); + + test('a chosen color is the color of all it stands for', () => { + const byId = Object.fromEntries( + printLayers('a.pmtiles', {water: '#ff00ff', minor: '#00ff00'}).map(layer => [layer.id, layer])); + expect(byId.water.paint['fill-color']).toBe('#ff00ff'); + // A line of water is darker than an area of it, and as wide as the printed map has it. + expect(byId.water_stream.paint['line-color']).toBe(mix('#ff00ff', '#000000', 0.4)); + expect(byId.water_stream.paint['line-width']).toBe(PRINT_PAINT.water_stream['line-width']); + expect(JSON.stringify(byId.roads_minor.paint['line-color'])).toContain('#00ff00'); + // The edge of a road is not chosen. + expect(byId.roads_bridges_link.paint['line-color']).toBe(PRINT_COLORS.major); + expect(byId.roads_minor_casing.paint['line-color']).toBe('#4d4d4d'); + // What was not chosen is as it was. + expect(byId.roads_highway.paint['line-color']).toBe(PRINT_COLORS.highway); + }); + + test('a ramp is the color of a major road, on a bridge as off one', () => { + const ramps = colors => { + const byId = Object.fromEntries(printLayers('a.pmtiles', colors).map(layer => [layer.id, layer])); + return [byId.roads_link.paint['line-color'], byId.roads_bridges_link.paint['line-color']]; + }; + expect(ramps({})).toEqual([PRINT_COLORS.major, PRINT_COLORS.major]); + expect(ramps({major: '#123456'})).toEqual(['#123456', '#123456']); + // The color of a minor road is not a ramp's. + expect(ramps({minor: '#00ff00'})).toEqual([PRINT_COLORS.major, PRINT_COLORS.major]); + }); + + test('several colors of a basemap are shades of the one chosen', () => { + const theme = printTheme({vegetation: '#008000'}); + expect(theme.park_b).toBe('#008000'); + expect(theme.park_a).toBe(mix('#008000', '#ffffff', 0.4)); + expect(theme.wood_b).not.toBe(theme.park_b); + expect(mix('#000000', '#ffffff', 0.5)).toBe('#808080'); + }); + + test('contours are the color chosen for them, and their heights have the halo of names', () => { + expect(printContours()).toEqual(PRINT_CONTOURS); + const contours = printContours({contours: '#102030', halo: '#fffacd'}); + expect(contours.minor).toEqual({'line-color': 'rgba(16, 32, 48, 0.55)', 'line-width': 0.6}); + expect(contours.major['line-color']).toBe('rgba(16, 32, 48, 0.9)'); + expect(contours.label['text-halo-color']).toBe('rgba(255, 250, 205, 0.9)'); + }); + + test('what is not a choice is not kept', () => { + expect(chosenColors({water: '#FF00FF', sky: '#0000ff', minor: 'green', highway: 7, major: '#fff'})) + .toEqual({water: '#ff00ff'}); + expect(chosenColors(null)).toEqual({}); + }); + + test('are remembered in the browser, and forgotten when there are none', () => { + expect(savedPrintColors()).toEqual({}); + expect(savePrintColors({water: '#ff00ff', sky: '#0000ff'})).toEqual({water: '#ff00ff'}); + expect(savedPrintColors()).toEqual({water: '#ff00ff'}); + savePrintColors({}); + expect(localStorage.getItem('wrolpi-map-print-colors')).toBeNull(); + // What was kept is not what this keeps. + localStorage.setItem('wrolpi-map-print-colors', 'not JSON'); + expect(savedPrintColors()).toEqual({}); + }); +}); diff --git a/app/src/components/mapQuads.js b/app/src/components/mapQuads.js new file mode 100644 index 00000000..43fba0cf --- /dev/null +++ b/app/src/components/mapQuads.js @@ -0,0 +1,128 @@ +// The areas a map can be exported by: the 7.5-minute quadrangles of a 1:24,000 topographic map. +// +// A quadrangle is 7.5 minutes of latitude by 7.5 minutes of longitude, its edges on whole multiples +// of that. The grid is arithmetic, so it needs no data and covers the whole world. + +export const QUAD_DEGREES = 0.125; +export const QUAD_SCALE = 24000; +// The grid is not drawn below this zoom: its cells would be too small to choose between. +export const QUAD_MIN_ZOOM = 8; + +const QUADS_PER_DEGREE = 1 / QUAD_DEGREES; +// Web Mercator draws no further north or south than this. +const MAX_LATITUDE = 85; +const MAX_GRID_LINES = 400; + +function wrapLongitude(lng) { + return ((lng + 180) % 360 + 360) % 360 - 180; +} + +function quadOf(row, col) { + return { + id: `${row}:${col}`, + row, + col, + west: col * QUAD_DEGREES, + south: row * QUAD_DEGREES, + east: (col + 1) * QUAD_DEGREES, + north: (row + 1) * QUAD_DEGREES, + }; +} + +// The quadrangle a point is in, or null where there is none (off the map, or not a place). +export function quadAt(lng, lat) { + if (!Number.isFinite(lng) || !Number.isFinite(lat) || Math.abs(lat) >= MAX_LATITUDE) return null; + return quadOf(Math.floor(lat * QUADS_PER_DEGREE), Math.floor(wrapLongitude(lng) * QUADS_PER_DEGREE)); +} + +// The outline of a quadrangle, as GeoJSON. +export function quadPolygon(quad) { + const {west, south, east, north} = quad; + return { + type: "Feature", + properties: {id: quad.id}, + geometry: { + type: "Polygon", + coordinates: [[[west, south], [east, south], [east, north], [west, north], [west, south]]], + }, + }; +} + +export const EMPTY_FEATURES = {type: "FeatureCollection", features: []}; + +// The lines of the grid across `bounds` ({west, south, east, north}, as the map gives them; east may +// be past 180), as GeoJSON. Null when there would be too many lines to be of use. +export function quadGridLines(bounds) { + const south = Math.max(bounds.south, -MAX_LATITUDE); + const north = Math.min(bounds.north, MAX_LATITUDE); + const firstRow = Math.floor(south * QUADS_PER_DEGREE); + const lastRow = Math.ceil(north * QUADS_PER_DEGREE); + const firstCol = Math.floor(bounds.west * QUADS_PER_DEGREE); + const lastCol = Math.ceil(bounds.east * QUADS_PER_DEGREE); + if (!(lastRow > firstRow) || !(lastCol > firstCol)) return EMPTY_FEATURES; + if ((lastRow - firstRow) + (lastCol - firstCol) > MAX_GRID_LINES) return null; + + const [west, east] = [firstCol * QUAD_DEGREES, lastCol * QUAD_DEGREES]; + const [bottom, top] = [firstRow * QUAD_DEGREES, lastRow * QUAD_DEGREES]; + const lines = []; + for (let row = firstRow; row <= lastRow; row++) { + lines.push([[west, row * QUAD_DEGREES], [east, row * QUAD_DEGREES]]); + } + for (let col = firstCol; col <= lastCol; col++) { + lines.push([[col * QUAD_DEGREES, bottom], [col * QUAD_DEGREES, top]]); + } + return { + type: "FeatureCollection", + features: [{type: "Feature", properties: {}, geometry: {type: "MultiLineString", coordinates: lines}}], + }; +} + +// 37.875 -> 37°52′30″ +function degreesMinutesSeconds(value) { + const seconds = Math.round(Math.abs(value) * 3600); + const d = Math.floor(seconds / 3600); + const m = Math.floor((seconds % 3600) / 60); + const s = seconds % 60; + const pad = n => String(n).padStart(2, "0"); + return s ? `${d}°${pad(m)}′${pad(s)}″` : `${d}°${pad(m)}′`; +} + +export function formatLatitude(lat) { + return `${degreesMinutesSeconds(lat)}${lat < 0 ? "S" : "N"}`; +} + +export function formatLongitude(lng) { + return `${degreesMinutesSeconds(lng)}${lng < 0 ? "W" : "E"}`; +} + +// What to call a quadrangle: where its southwest corner is. +export function quadLabel(quad) { + return `${formatLatitude(quad.south)} ${formatLongitude(quad.west)}`; +} + +// What the USGS calls a quadrangle, from what the API says of it ({name, state, exact}): "Berthoud, +// Colorado". A quadrangle of the grid which is not all of the named one (they are wider in +// Alaska) is a part of it. +export function quadTitle(named) { + if (!named?.name) return null; + const title = named.state ? `${named.name}, ${named.state}` : named.name; + return named.exact === false ? `Part of ${title}` : title; +} + +// How far it is across and up a quadrangle on the ground (meters, along its middle), and on a sheet +// printed at 1:24,000 (millimeters). +export function quadSize(quad) { + const latitude = (quad.south + quad.north) / 2 * Math.PI / 180; + // The length of a degree on the WGS84 ellipsoid. + const metersPerDegreeLatitude = 111132.92 - 559.82 * Math.cos(2 * latitude) + 1.175 * Math.cos(4 * latitude); + const metersPerDegreeLongitude = 111412.84 * Math.cos(latitude) - 93.5 * Math.cos(3 * latitude) + + 0.118 * Math.cos(5 * latitude); + const widthMeters = metersPerDegreeLongitude * QUAD_DEGREES; + const heightMeters = metersPerDegreeLatitude * QUAD_DEGREES; + return { + widthMeters, + heightMeters, + sheetWidthMm: widthMeters * 1000 / QUAD_SCALE, + sheetHeightMm: heightMeters * 1000 / QUAD_SCALE, + }; +} diff --git a/app/src/components/mapQuads.test.js b/app/src/components/mapQuads.test.js new file mode 100644 index 00000000..3bf461f1 --- /dev/null +++ b/app/src/components/mapQuads.test.js @@ -0,0 +1,111 @@ +import { + EMPTY_FEATURES, + formatLatitude, + formatLongitude, + quadAt, + quadGridLines, + quadLabel, + quadPolygon, + quadSize, +} from './mapQuads'; + +describe('the quadrangle a point is in', () => { + test('has its edges on whole multiples of 7.5 minutes', () => { + expect(quadAt(-105.0595, 40.31)).toEqual({ + id: '322:-841', row: 322, col: -841, west: -105.125, south: 40.25, east: -105, north: 40.375, + }); + // South and east of the origin. + expect(quadAt(151.21, -33.87)).toMatchObject({west: 151.125, south: -33.875, east: 151.25, north: -33.75}); + }); + + test('a point on an edge belongs to the quadrangle north and east of it', () => { + expect(quadAt(-105.125, 40.25)).toMatchObject({west: -105.125, south: 40.25}); + expect(quadAt(-105.1250001, 40.2499999)).toMatchObject({west: -105.25, south: 40.125}); + }); + + test('is the same wherever around the world the map has been dragged to', () => { + expect(quadAt(-105.0595 + 360, 40.31)).toEqual(quadAt(-105.0595, 40.31)); + expect(quadAt(-105.0595 - 720, 40.31)).toEqual(quadAt(-105.0595, 40.31)); + expect(quadAt(180, 0)).toMatchObject({west: -180, east: -179.875}); + }); + + test('is nothing off the map', () => { + expect(quadAt(0, 86)).toBeNull(); + expect(quadAt(0, -85)).toBeNull(); + expect(quadAt(NaN, 0)).toBeNull(); + expect(quadAt(0, undefined)).toBeNull(); + }); + + test('has an outline which closes', () => { + const ring = quadPolygon(quadAt(-105.0595, 40.31)).geometry.coordinates[0]; + expect(ring).toEqual([[-105.125, 40.25], [-105, 40.25], [-105, 40.375], [-105.125, 40.375], [-105.125, 40.25]]); + }); +}); + +describe('the grid across the map', () => { + const lines = bounds => quadGridLines(bounds).features[0].geometry.coordinates; + + test('covers the view, out to the edges of the quadrangles it shows part of', () => { + const grid = lines({west: -105.175, south: 40.26, east: -104.925, north: 40.4}); + // Parallels at 40.25, 40.375, 40.5; meridians at -105.25, -105.125, -105, -104.875. + expect(grid).toHaveLength(7); + expect(grid.slice(0, 3)).toEqual([ + [[-105.25, 40.25], [-104.875, 40.25]], + [[-105.25, 40.375], [-104.875, 40.375]], + [[-105.25, 40.5], [-104.875, 40.5]], + ]); + expect(grid[3]).toEqual([[-105.25, 40.25], [-105.25, 40.5]]); + expect(grid[6]).toEqual([[-104.875, 40.25], [-104.875, 40.5]]); + }); + + test('is not made when the view is of too many quadrangles', () => { + expect(quadGridLines({west: -125, south: 25, east: -66, north: 50})).toBeNull(); + }); + + test('stops where the map does', () => { + const grid = lines({west: 0, south: 84.9, east: 0.1, north: 89}); + expect(Math.max(...grid.flat().map(point => point[1]))).toBe(85); + }); + + test('is empty for a view of nothing', () => { + expect(quadGridLines({west: 0, south: 88, east: 1, north: 89})).toBe(EMPTY_FEATURES); + }); +}); + +describe('what a quadrangle is called', () => { + test('is where its southwest corner is', () => { + expect(quadLabel(quadAt(-105.0595, 40.31))).toBe('40°15′N 105°07′30″W'); + expect(quadLabel(quadAt(-104.925, 40.4))).toBe('40°22′30″N 105°00′W'); + expect(quadLabel(quadAt(151.21, -33.87))).toBe('33°52′30″S 151°07′30″E'); + }); + + test('a coordinate is written in degrees, minutes and seconds', () => { + expect(formatLatitude(0)).toBe('0°00′N'); + expect(formatLatitude(-0.125)).toBe('0°07′30″S'); + expect(formatLongitude(-179.875)).toBe('179°52′30″W'); + }); +}); + +describe('the size of a quadrangle', () => { + test('is narrower further from the equator, and about as tall everywhere', () => { + const equator = quadSize(quadAt(0, 0)); + const colorado = quadSize(quadAt(-105.0595, 40.31)); + const alaska = quadSize(quadAt(-150, 64.8)); + expect(equator.widthMeters).toBeGreaterThan(colorado.widthMeters); + expect(colorado.widthMeters).toBeGreaterThan(alaska.widthMeters); + for (const size of [equator, colorado, alaska]) { + expect(size.heightMeters).toBeGreaterThan(13800); + expect(size.heightMeters).toBeLessThan(14000); + } + }); + + test('is that of a 1:24,000 sheet', () => { + // A quadrangle at 40 degrees north is about 10.6 km by 13.9 km: about 17.4 by 22.8 inches of paper. + const {widthMeters, heightMeters, sheetWidthMm, sheetHeightMm} = quadSize(quadAt(-105.0595, 40.31)); + expect(widthMeters / 1000).toBeCloseTo(10.62, 1); + expect(heightMeters / 1000).toBeCloseTo(13.88, 1); + expect(sheetWidthMm / 25.4).toBeCloseTo(17.4, 0); + expect(sheetHeightMm / 25.4).toBeCloseTo(22.8, 0); + expect(sheetWidthMm).toBeCloseTo(widthMeters / 24, 6); + }); +}); diff --git a/app/src/components/mapTiles.js b/app/src/components/mapTiles.js new file mode 100644 index 00000000..7e3addf1 --- /dev/null +++ b/app/src/components/mapTiles.js @@ -0,0 +1,29 @@ +// Reading the tiles of a map file, as the map does not: one at a time, for what is in them. + +import {PMTiles} from "pmtiles"; +import {VectorTile} from "@mapbox/vector-tile"; +import Pbf from "pbf"; + +const PROTOCOL = "pmtiles://"; +const archives = new Map(); + +function archive(url) { + const address = url.startsWith(PROTOCOL) ? url.slice(PROTOCOL.length) : url; + if (!archives.has(address)) archives.set(address, new PMTiles(address)); + return archives.get(address); +} + +// A tile of the vector map file which a source of the map's style has as its `url`, or null when +// the file has no such tile (it is not of that place, or does not go that deep). +export async function readVectorTile(url, {z, x, y}) { + const tile = await archive(url).getZxy(z, x, y); + return tile?.data ? new VectorTile(new Pbf(new Uint8Array(tile.data))) : null; +} + +// The west, south, east and north edges (degrees) of what a map file is of, as the file says; null +// when it does not say. +export async function readTileBounds(url) { + const {minLon, minLat, maxLon, maxLat} = await archive(url).getHeader(); + const edges = [minLon, minLat, maxLon, maxLat]; + return edges.every(Number.isFinite) && minLon < maxLon && minLat < maxLat ? edges : null; +} diff --git a/app/src/components/mapTiles.test.js b/app/src/components/mapTiles.test.js new file mode 100644 index 00000000..fb863f2d --- /dev/null +++ b/app/src/components/mapTiles.test.js @@ -0,0 +1,51 @@ +import {readTileBounds, readVectorTile} from './mapTiles'; + +const mockOpened = []; +const mockTiles = {}; +const mockHeaders = {}; + +jest.mock('pmtiles', () => ({ + PMTiles: class { + constructor(address) { + this.address = address; + mockOpened.push(address); + } + + async getHeader() { + return mockHeaders[this.address] || {}; + } + + async getZxy(z, x, y) { + return mockTiles[`${this.address} ${z}/${x}/${y}`]; + } + }, +})); + +describe('reading a tile of a map file', () => { + test('is of the file a source of the map has as its URL, which is opened once', async () => { + // A tile with nothing in it. + mockTiles['https://example.com/a.pmtiles 14/3/5'] = {data: new ArrayBuffer(0)}; + const tile = await readVectorTile('pmtiles://https://example.com/a.pmtiles', {z: 14, x: 3, y: 5}); + expect(tile.layers).toEqual({}); + await readVectorTile('pmtiles://https://example.com/a.pmtiles', {z: 14, x: 3, y: 6}); + expect(mockOpened).toEqual(['https://example.com/a.pmtiles']); + }); + + test('is nothing when the file has no such tile', async () => { + expect(await readVectorTile('pmtiles:///media/b.pmtiles', {z: 14, x: 0, y: 0})).toBeNull(); + expect(mockOpened).toContain('/media/b.pmtiles'); + }); +}); + +describe('the edges of a map file', () => { + test('are what the file says: west, south, east and north', async () => { + mockHeaders['/media/c.pmtiles'] = {minLon: -150, minLat: 61, maxLon: -149, maxLat: 62, maxZoom: 15}; + expect(await readTileBounds('pmtiles:///media/c.pmtiles')).toEqual([-150, 61, -149, 62]); + }); + + test('are not known when the file does not say', async () => { + mockHeaders['/media/d.pmtiles'] = {minLon: 0, minLat: 0, maxLon: 0, maxLat: 0}; + expect(await readTileBounds('pmtiles:///media/d.pmtiles')).toBeNull(); + expect(await readTileBounds('pmtiles:///media/e.pmtiles')).toBeNull(); + }); +}); diff --git a/app/src/map-shared/map-shared.test.js b/app/src/map-shared/map-shared.test.js index 2ef4e845..3ccd79c0 100644 --- a/app/src/map-shared/map-shared.test.js +++ b/app/src/map-shared/map-shared.test.js @@ -2,15 +2,17 @@ const fs = require('fs'); const path = require('path'); // The standalone map viewer (modules/map/static/index.html) has no build step: it loads copies of -// the shared overlay modules as they are. The copies must be the modules. +// the shared map modules as they are. The copies must be the modules. const SHARED = __dirname; const COPIES = path.join(__dirname, '../../../modules/map/static/js'); const MODULES = fs.readdirSync(SHARED).filter(name => name.endsWith('.js') && !name.endsWith('.test.js')); describe('the shared map modules', () => { - test('are the two the standalone viewer loads', () => { - expect(MODULES.sort()).toEqual(['overlayStyle.js', 'overlays.js']); + test('are those the standalone viewer loads', () => { + expect(MODULES.sort()).toEqual(['overlayStyle.js', 'overlays.js', 'terrain.js']); + const viewer = fs.readFileSync(path.join(COPIES, '../index.html'), 'utf8'); + for (const name of MODULES) expect(viewer).toContain(`import("/js/${name}")`); }); test.each(MODULES)('%s is copied to the standalone viewer as it is (run `npm run sync:map-shared`)', (name) => { diff --git a/app/src/map-shared/overlays.js b/app/src/map-shared/overlays.js index 05b47006..640a740d 100644 --- a/app/src/map-shared/overlays.js +++ b/app/src/map-shared/overlays.js @@ -252,10 +252,17 @@ export function buildOverlayLayers(file, state) { return styleLayers; } -// The style layers of every overlay, in the order they are drawn. A raster overlay goes under the -// basemap's labels (`rasters`) so the map stays readable; vector overlays go on top (`vectors`): an -// overlay with its own style in that style's order, then the generic ones, fills under lines under -// points. +// Whether a layer of an overlay is of its labels: its text, its icons, and the points it marks. The +// rest of an overlay is its ground: areas, lines, pictures. +export function isOverlayLabelLayer(layer) { + return layer?.type === "symbol" || layer?.type === "circle"; +} + +// The style layers of every overlay, in the order they are drawn. Labels are always on top: the +// ground of every overlay goes under the basemap's labels (`under`), so an area an overlay fills +// does not cover the name of what is in it, and the labels of every overlay go over them (`over`). +// In each, a raster is lowest, then an overlay with its own style in that style's order, then the +// generic ones, fills under lines under points. export function orderOverlayLayers(overlays, state) { const rasters = []; const styled = []; @@ -271,7 +278,49 @@ export function orderOverlayLayers(overlays, state) { else generic[layer.type].push(layer); } } - return {rasters, vectors: [...styled, ...generic.fill, ...generic.line, ...generic.circle]}; + const vectors = [...styled, ...generic.fill, ...generic.line, ...generic.circle]; + return { + under: [...rasters, ...vectors.filter(layer => !isOverlayLabelLayer(layer))], + over: vectors.filter(isOverlayLabelLayer), + }; +} + +// The layers of a basemap with every label over all of its ground: the ground in the order it was +// in, then the labels in the order they were in. +// +// A map of several basemap files is their layers one file after another, so the labels of each file +// but the last are under the land, the water and the roads of the files after it, and under +// anything which is put under "the labels". Labels are always on top. +export function labelsOverGround(layers) { + layers = layers || []; + return [...layers.filter(l => l.type !== "symbol"), ...layers.filter(l => l.type === "symbol")]; +} + +// The source of the contour lines both viewers draw from the terrain. They are added to a style +// after the basemap's labels, lines and labels both, and are not of the basemap. +const CONTOUR_SOURCE = "contours"; + +// Where the basemap's labels begin: the index of the first of the labels which are over all of the +// basemap's ground. Where no label is over its ground, the index after the last of its ground. +// +// Not the map's first label: a map of several basemap files has the labels of the first file under +// the ground of the next, and an overlay put there would be under the land and the water of every +// file after the first. Nor before the last layer which is not a label: the contour lines are +// drawn after the basemap's labels, and an overlay put under them would be over every label. +export function basemapLabelsStart(layers) { + layers = layers || []; + const ofBasemap = layer => !isOverlayLayerId(layer.id) && layer.source !== CONTOUR_SOURCE; + let lastGround = -1; + for (let i = layers.length - 1; i >= 0; i--) { + if (ofBasemap(layers[i]) && layers[i].type !== "symbol") { + lastGround = i; + break; + } + } + for (let i = lastGround + 1; i < layers.length; i++) { + if (ofBasemap(layers[i]) && layers[i].type === "symbol") return i; + } + return lastGround + 1; } // Add the overlays to a style's sources and layers (the basemap's are already there). @@ -279,10 +328,9 @@ export function addOverlaysToStyle(styleSources, allLayers, overlays, state, url for (const file of overlays || []) { styleSources[overlaySourceId(file)] = buildOverlaySource(file, urls); } - const {rasters, vectors} = orderOverlayLayers(overlays, state); - const firstLabel = allLayers.findIndex(l => l.type === "symbol"); - allLayers.splice(firstLabel === -1 ? allLayers.length : firstLabel, 0, ...rasters); - allLayers.push(...vectors); + const {under, over} = orderOverlayLayers(overlays, state); + allLayers.splice(basemapLabelsStart(allLayers), 0, ...under); + allLayers.push(...over); } // Add the overlays to a map whose style has loaded, a layer at a time. For a viewer which cannot @@ -295,8 +343,9 @@ export function addOverlaysToMap(map, overlays, state, urls) { const id = overlaySourceId(file); if (!map.getSource(id)) map.addSource(id, buildOverlaySource(file, urls)); } - const firstLabel = (map.getStyle()?.layers || []).find(l => l.type === "symbol"); - const {rasters, vectors} = orderOverlayLayers(overlays, state); + const mapLayers = map.getStyle()?.layers || []; + const firstLabel = mapLayers[basemapLabelsStart(mapLayers)]; + const {under, over} = orderOverlayLayers(overlays, state); const add = (layer, before) => { if (map.getLayer(layer.id)) return; try { @@ -307,8 +356,8 @@ export function addOverlaysToMap(map, overlays, state, urls) { refused[mapPath] = (refused[mapPath] || 0) + 1; } }; - for (const layer of rasters) add(layer, firstLabel?.id); - for (const layer of vectors) add(layer, undefined); + for (const layer of under) add(layer, firstLabel?.id); + for (const layer of over) add(layer, undefined); return refused; } diff --git a/app/src/map-shared/terrain.js b/app/src/map-shared/terrain.js new file mode 100644 index 00000000..8cdc3715 --- /dev/null +++ b/app/src/map-shared/terrain.js @@ -0,0 +1,59 @@ +// How the map reads a terrain (elevation) file: how deep into it to look, and how far apart the +// contour lines are. Shared by the app's map viewer and the standalone viewer, which loads a copy +// of this file as it is: no imports from the app, no dependencies. + +// How deep the terrain file which WROLPi offers goes, and what a file is taken to be when it does +// not say. +export const DEFAULT_TERRAIN_MAX_ZOOM = 8; +// No terrain file is read deeper than this. +const DEEPEST_TERRAIN_ZOOM = 15; + +// How far apart the contour lines are at each zoom of the map: [minor, major], in the unit the map +// measures in. A zoom between two of these uses the one below it. +// +// A 1:24,000 sheet has a line every 40 feet (or 20 meters), which is what the deepest have: finer +// lines than that are more than the terrain says, and more than a hillside has room for. +const CONTOUR_FEET = { + 4: [1000, 5000], 5: [500, 2500], 6: [200, 1000], 7: [200, 1000], 8: [100, 500], + 9: [100, 500], 10: [100, 500], 11: [40, 200], 12: [40, 200], +}; +const CONTOUR_METERS = { + 4: [500, 2500], 5: [200, 1000], 6: [100, 500], 7: [100, 500], 8: [50, 200], + 9: [50, 200], 10: [50, 200], 11: [20, 100], 12: [20, 100], +}; + +// How deep a terrain file goes: the deepest zoom it has tiles of. `file` is as the API lists it. +export function terrainMaxZoom(file) { + const zoom = Number(file && file.max_zoom); + if (!Number.isInteger(zoom) || zoom < 1) return DEFAULT_TERRAIN_MAX_ZOOM; + return Math.min(zoom, DEEPEST_TERRAIN_ZOOM); +} + +// The contour intervals for a terrain file which goes as deep as `maxZoom`. +// +// Only those the file can bear: lines 40 feet apart drawn from terrain too coarse for them are +// drawn where no line is. A map zoomed in past the file's depth keeps the file's finest interval. +export function contourThresholds(imperial, maxZoom) { + const all = imperial ? CONTOUR_FEET : CONTOUR_METERS; + const deepest = Number.isFinite(maxZoom) ? maxZoom : DEFAULT_TERRAIN_MAX_ZOOM; + const lowest = Math.min(...Object.keys(all).map(Number)); + const thresholds = {}; + for (const [zoom, intervals] of Object.entries(all)) { + // A file shallower than every interval still has contours: the coarsest. + if (Number(zoom) <= deepest || Number(zoom) === lowest) thresholds[zoom] = intervals; + } + return thresholds; +} + +// How far apart the contour lines are on a map drawn at `zoom` from a terrain file which goes as +// deep as `maxZoom`: [minor, major], or null when no line is drawn at that zoom. +// +// The lines of a map are made a tile at a time: for tiles of the map's zoom, or of the file's +// deepest when the map is zoomed in past it, with the interval of the nearest zoom below. +export function contourInterval(imperial, maxZoom, zoom) { + const thresholds = contourThresholds(imperial, maxZoom); + const deepest = Number.isFinite(maxZoom) ? maxZoom : DEFAULT_TERRAIN_MAX_ZOOM; + const tileZoom = Math.min(Math.floor(zoom), deepest); + const zooms = Object.keys(thresholds).map(Number).filter(i => i <= tileZoom); + return zooms.length ? thresholds[Math.max(...zooms)] : null; +} diff --git a/app/src/map-shared/terrain.test.js b/app/src/map-shared/terrain.test.js new file mode 100644 index 00000000..fdb23502 --- /dev/null +++ b/app/src/map-shared/terrain.test.js @@ -0,0 +1,96 @@ +import {contourInterval, contourThresholds, DEFAULT_TERRAIN_MAX_ZOOM, terrainMaxZoom} from './terrain.js'; + +describe('how deep a terrain file is read', () => { + test('as deep as the file goes', () => { + expect(terrainMaxZoom({max_zoom: 8})).toBe(8); + expect(terrainMaxZoom({max_zoom: 12})).toBe(12); + expect(terrainMaxZoom({max_zoom: 5})).toBe(5); + }); + + test('as the terrain WROLPi offers, when the file does not say', () => { + expect(DEFAULT_TERRAIN_MAX_ZOOM).toBe(8); + const unsaid = [undefined, null, {}, {max_zoom: null}, {max_zoom: 'deep'}, {max_zoom: 0}, {max_zoom: 9.5}]; + for (const file of unsaid) { + expect(terrainMaxZoom(file)).toBe(8); + } + }); + + test('and no deeper than a map goes', () => { + expect(terrainMaxZoom({max_zoom: 22})).toBe(15); + }); +}); + +describe('how far apart the contour lines are', () => { + test('are as they have always been for the terrain WROLPi offers', () => { + expect(contourThresholds(true, 8)).toEqual({ + 4: [1000, 5000], 5: [500, 2500], 6: [200, 1000], 7: [200, 1000], 8: [100, 500], + }); + expect(contourThresholds(false, 8)).toEqual({ + 4: [500, 2500], 5: [200, 1000], 6: [100, 500], 7: [100, 500], 8: [50, 200], + }); + // And when it is not said how deep the file is. + expect(contourThresholds(true)).toEqual(contourThresholds(true, 8)); + expect(contourThresholds(false, undefined)).toEqual(contourThresholds(false, 8)); + }); + + test('are finer for a file which goes deeper: 40 feet, or 20 meters, as a 1:24,000 sheet has', () => { + const feet = contourThresholds(true, 12); + expect(feet[12]).toEqual([40, 200]); + expect(feet[11]).toEqual([40, 200]); + expect(feet[10]).toEqual([100, 500]); + expect(contourThresholds(false, 12)[12]).toEqual([20, 100]); + // What was there for a shallower file is unchanged. + expect(feet).toMatchObject(contourThresholds(true, 8)); + }); + + test('are no finer than the file can bear', () => { + expect(Object.keys(contourThresholds(true, 10)).map(Number)).toEqual([4, 5, 6, 7, 8, 9, 10]); + // A file deeper than any interval keeps the finest. + expect(contourThresholds(true, 15)).toEqual(contourThresholds(true, 12)); + // A file shallower than every interval still has contours. + expect(contourThresholds(true, 2)).toEqual({4: [1000, 5000]}); + expect(contourThresholds(false, 4)).toEqual({4: [500, 2500]}); + }); + + test('never have a minor line which is not a part of a major one', () => { + for (const imperial of [true, false]) { + for (const [minor, major] of Object.values(contourThresholds(imperial, 15))) { + expect(major % minor).toBe(0); + expect(major).toBeGreaterThan(minor); + } + } + }); +}); + +describe('how far apart the contour lines of a map at one zoom are', () => { + test('is the interval of the tiles the lines are made for', () => { + // A sheet is drawn at about zoom 13, from a file which goes to 12: tiles of zoom 12. + expect(contourInterval(true, 12, 13.2)).toEqual([40, 200]); + expect(contourInterval(false, 12, 13.2)).toEqual([20, 100]); + // The file which WROLPi offers goes to zoom 8, whatever the map is zoomed to. + expect(contourInterval(true, 8, 13.2)).toEqual([100, 500]); + expect(contourInterval(false, DEFAULT_TERRAIN_MAX_ZOOM, 14.9)).toEqual([50, 200]); + // A file deeper than the finest interval has the finest. + expect(contourInterval(true, 15, 14.2)).toEqual([40, 200]); + // A map zoomed out has the interval of its zoom. + expect(contourInterval(true, 12, 6.9)).toEqual([200, 1000]); + expect(contourInterval(true, 12, 10.99)).toEqual([100, 500]); + expect(contourInterval(true, 12, 11)).toEqual([40, 200]); + }); + + test('is the same as the lines which are drawn: the interval of the nearest zoom below', () => { + for (const [maxZoom, zoom] of [[8, 13.2], [12, 13.2], [12, 9.5], [3, 13], [undefined, 13]]) { + const thresholds = contourThresholds(true, maxZoom); + const tile = Math.min(Math.floor(zoom), maxZoom ?? DEFAULT_TERRAIN_MAX_ZOOM); + const below = Math.max(...Object.keys(thresholds).map(Number).filter(i => i <= tile)); + expect(contourInterval(true, maxZoom, zoom)).toEqual(thresholds[below] ?? null); + } + }); + + test('is nothing where no line is drawn', () => { + expect(contourInterval(true, 12, 3.9)).toBeNull(); + expect(contourInterval(false, 8, 0)).toBeNull(); + // A file shallower than every interval has lines only from the zoom of the coarsest. + expect(contourInterval(true, 3, 13)).toBeNull(); + }); +}); diff --git a/docker/help/wrolpi-help b/docker/help/wrolpi-help index deda0b7c..349a8018 160000 --- a/docker/help/wrolpi-help +++ b/docker/help/wrolpi-help @@ -1 +1 @@ -Subproject commit deda0b7ce7640835016b73512ee0c88e9f7f2059 +Subproject commit 349a8018130aaa743ebcd7447ac6fb961226f11a diff --git a/modules/map/api.py b/modules/map/api.py index 44a549ca..8db720d0 100644 --- a/modules/map/api.py +++ b/modules/map/api.py @@ -1,11 +1,13 @@ +import asyncio from http import HTTPStatus from sanic import Request, response, Blueprint from sanic_ext import validate from sanic_ext.extensions.openapi import openapi -from modules.map import lib, schema, search +from modules.map import export, export_job, lib, quads, schema, search from modules.map.pins import get_map_pins_config +from modules.map.sheet import legend_as_sent from wrolpi.api_utils import json_response from wrolpi.common import wrol_mode_check from wrolpi.events import Events @@ -175,3 +177,144 @@ async def rebuild_single_search_index(_: Request, filename: str): if started: return json_response({'message': f'Search index rebuild started for {filename}'}, HTTPStatus.ACCEPTED) return json_response({'error': 'File not found'}, HTTPStatus.NOT_FOUND) + + +@map_bp.get('/quad') +@openapi.description('The name of the USGS quadrangle of an area of the map') +async def get_quad(request: Request): + """What the USGS calls the quadrangle between `west`, `south`, `east` and `north` (degrees): `quad` is its + `name`, its `state`, and whether it is `exact`ly that area (and not a larger quadrangle which has the middle + of it); or null where there is no name, which is most of the world.""" + try: + west, south, east, north = (float(request.args.get(i)) for i in ('west', 'south', 'east', 'north')) + except (TypeError, ValueError): + return response.json({'error': 'The edges of the area are four numbers'}, HTTPStatus.BAD_REQUEST) + if not (-180 <= west < east <= 180 and -90 <= south < north <= 90): + # Not so of a number which is not one, either. + return response.json({'error': 'The edges are not those of an area of the map'}, HTTPStatus.BAD_REQUEST) + # The index is read from its file the first time, which is not for the event loop. + name = await asyncio.to_thread(quads.quad_name, west, south, east, north) + return json_response({'quad': name.__json__() if name else None}) + + +# How much may be written on the sheet of an exported map. +SHEET_TEXT_LENGTH = 300 +SHEET_CREDITS = 40 + + +def _export_text(value) -> str: + """What was sent as text of an exported map (its title, the name of a layer), as it can be written in a PDF: + no longer than a line of a sheet, and with nothing in it which is not a character.""" + text = str(value or '').replace('\x00', '') + return text.encode('utf-8', 'replace').decode('utf-8')[:SHEET_TEXT_LENGTH] + + +def _sheet_contours(contours) -> dict: + """How far apart the contour lines of a map are, as the browser says it, for `export.Sheet`. A map with no + contour lines says nothing; what is said is written on the sheet only when it is an interval (see + `modules.map.sheet`).""" + if not isinstance(contours, dict): + return {} + interval = contours.get('interval') + if isinstance(interval, bool) or not isinstance(interval, (int, float)): + return {} + return dict(contour_interval=float(interval), contour_unit=str(contours.get('unit') or '')[:SHEET_TEXT_LENGTH]) + + +def _export_error(e: Exception): + """The response for an export which could not go on.""" + if isinstance(e, export_job.ExportNotFound): + return response.json({'error': str(e)}, HTTPStatus.NOT_FOUND) + if isinstance(e, export_job.ExportNotReady): + return response.json({'error': str(e)}, HTTPStatus.CONFLICT) + if isinstance(e, export_job.ExportStorageError): + return response.json({'error': str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR) + return response.json({'error': str(e)}, HTTPStatus.BAD_REQUEST) + + +EXPORT_ERRORS = (export.InvalidExport, export_job.ExportNotFound, export_job.ExportNotReady, + export_job.ExportStorageError) + + +@map_bp.post('/export') +@openapi.description('Begin the export of a map as a PDF') +async def post_export(request: Request): + """The browser draws the map, one image for each layer; the API puts them in a PDF, each in a layer of its own. + That takes a minute or two on a Raspberry Pi, so it is a Job: + + 1. This request describes the map, and is answered with an `export_id`. + 2. `POST /api/map/export//layer/` sends the image of each layer. + 3. `POST /api/map/export//start` is answered with a `job_id`, to watch at `/api/jobs/`. + 4. `GET /api/map/export//pdf` is the PDF, once the Job is complete. + + `DELETE /api/map/export/` stops an export at any of those steps, and removes what was sent. + + The JSON body: `width` and `height` in pixels, `dpi`, `title`, `layers`: the name of each image from the bottom + up (images of one name are one layer of the PDF), `bounds`: the west, south, east and north edges of the images in degrees, with which the PDF says where it + is, and `sheet`: with which the map is put on a sheet with a margin and a grid; `name`, `credits` and `date` + are written on it, `contours` (`{"interval": 40, "unit": "feet"}`, or "meters") when the map has contour + lines, and `legend`: the lines of the map as they are drawn (see `modules.map.sheet.Sheet`).""" + try: + meta = request.json + width, height, dpi = int(meta['width']), int(meta['height']), float(meta['dpi']) + names = [_export_text(i) for i in meta['layers']] + title = _export_text(meta.get('title')) or None + bounds = meta.get('bounds') + sheet = meta.get('sheet') + if sheet is not None: + sheet = export.Sheet( + name=_export_text(sheet.get('name')) or None, + credits=[_export_text(i) for i in (sheet.get('credits') or [])[:SHEET_CREDITS]], + date=_export_text(sheet.get('date')) or None, + **_sheet_contours(sheet.get('contours')), + legend=legend_as_sent(sheet.get('legend')), + ) + except Exception: # noqa Whatever is made of a body which is not the description of a map. + return response.json({'error': 'The description of the map is not valid'}, HTTPStatus.BAD_REQUEST) + + try: + # Old exports are removed from the disk here, which is not for the event loop. + export_id = await asyncio.to_thread(export_job.create_export, names, width, height, dpi, title, bounds, sheet) + except EXPORT_ERRORS as e: + return _export_error(e) + return json_response({'export_id': export_id}, HTTPStatus.CREATED) + + +@map_bp.post('/export//layer/') +@openapi.description('Send the image (PNG or JPEG) of one layer of a map which is being exported') +async def post_export_layer(request: Request, export_id: str, index: int): + try: + await asyncio.to_thread(export_job.save_layer, export_id, index, request.body) + except EXPORT_ERRORS as e: + return _export_error(e) + return response.empty() + + +@map_bp.post('/export//start') +@openapi.description('Queue the Job which makes the PDF of a map, all of whose layers have been sent') +async def post_export_start(_: Request, export_id: str): + try: + job_id = await asyncio.to_thread(export_job.start_export, export_id) + except EXPORT_ERRORS as e: + return _export_error(e) + return json_response({'job_id': job_id}, HTTPStatus.ACCEPTED) + + +@map_bp.delete('/export/') +@openapi.description('Stop the export of a map: cancel its Job, if it has one, and remove what was sent') +async def delete_export(_: Request, export_id: str): + try: + await asyncio.to_thread(export_job.remove_export, export_id) + except EXPORT_ERRORS as e: + return _export_error(e) + return response.empty() + + +@map_bp.get('/export//pdf') +@openapi.description('The PDF of an exported map, once the Job which makes it is complete') +async def get_export_pdf(_: Request, export_id: str): + try: + pdf = await asyncio.to_thread(export_job.get_export_pdf, export_id) + except EXPORT_ERRORS as e: + return _export_error(e) + return await response.file(pdf, mime_type='application/pdf', filename='map.pdf') diff --git a/modules/map/conftest.py b/modules/map/conftest.py index 58546d05..10d0b806 100644 --- a/modules/map/conftest.py +++ b/modules/map/conftest.py @@ -49,3 +49,12 @@ def make_pmtiles(test_directory) -> Callable[..., Path]: return path return _ + + +@pytest.fixture(autouse=True) +def map_export_root(tmp_path, monkeypatch) -> Path: + """Exports of the map are kept in a directory of the test, not of the machine.""" + from modules.map import export_job + root = tmp_path / 'map-export' + monkeypatch.setattr(export_job, 'EXPORT_ROOT', root) + return root diff --git a/modules/map/export.py b/modules/map/export.py new file mode 100644 index 00000000..b17a793d --- /dev/null +++ b/modules/map/export.py @@ -0,0 +1,295 @@ +"""Build the PDF of an exported map. + +The map is drawn by the browser, which is the only thing that draws it as the user sees it: one image for each +layer the user has turned on, all of the same area and size. Here they become one page, each image in a PDF +layer (an "optional content group") of its own, so a reader can show and hide them as the map can. + +The page is also told where on the Earth it is (a "geospatial PDF", ISO 32000-2 section 12.10), so a reader which +knows how can show coordinates, measure, and put a GPS position on the map. + +The map can be put on a sheet (see `modules.map.sheet`): a margin around it with what a printed map has there, and +a grid over it. +""" +import io +import math +import pathlib +import zlib +from dataclasses import dataclass +from typing import Callable, List, Optional, Sequence + +from PIL import Image + +from modules.map.sheet import Sheet, draw_sheet, plan_sheet +from wrolpi.common import logger + +logger = logger.getChild(__name__) + +__all__ = ['ExportLayer', 'InvalidExport', 'Sheet', 'build_layered_pdf', 'check_export', 'geospatial_viewport', + 'MAX_LAYERS', 'MAX_SIDE', 'MAX_PIXELS', 'MAX_TOTAL_PIXELS', 'MIN_DPI', 'MAX_DPI'] + +# What the browser may ask for. Every image is decoded, so what an export costs is its pixels, whatever the size +# of the upload (an image of one color is a few kilobytes however large it is). +MAX_LAYERS = 64 +MAX_SIDE = 16384 +# The pixels of one layer, which are in memory together (a few bytes each) while it is put in the PDF. A 1:24,000 +# quadrangle at 300 DPI is 23 to 47 megapixels, more the nearer it is to the equator; at 400 DPI, 40 to 83. The +# browser draws a map less sharp than it was asked to rather than send more than this. +MAX_PIXELS = 64_000_000 +# The pixels of every layer of a map together, which is the work an export is. A quadrangle of 40 megapixels with +# 24 layers on is about 960 million. +MAX_TOTAL_PIXELS = 1_000_000_000 +# How hard an image is compressed in the PDF (zlib, 1 to 9). Less is a few seconds faster and a much larger file. +COMPRESSION_LEVEL = 6 +MIN_DPI = 36 +MAX_DPI = 1200 +IMAGE_FORMATS = ('PNG', 'JPEG') + +POINTS_PER_INCH = 72 + + +# The map is drawn in Web Mercator, in which the area between two meridians and two parallels is a rectangle: the +# page. Its four corners, in latitude and longitude, are all a reader needs to know of where it is. +# +# What a reader is told of the projection is not Web Mercator, which Adobe Acrobat does not know (it says every +# place is at 0, 0), but Mercator on the WGS 84 ellipsoid, which every reader knows. Web Mercator is Mercator on +# a sphere: across a page of a map the two differ by a stretch from north to south, which the corners say, and +# what is left is under three centimeters on the ground across a quadrangle, at any latitude. It grows with the +# square of how tall the page is (a third of a meter for half a degree, more than a meter for a degree), so a +# map much taller than a quadrangle wants more than its corners said. (To say only that the page is in +# latitude and longitude would be read too, and be up to 7 meters out at 61 degrees on a quadrangle: a degree +# of latitude is not as tall at the top of a Mercator page as at the bottom.) +# +# The words are Esri's, as GDAL writes them in a PDF: Acrobat reads those. +PAGE_PROJECTION_EPSG = 3395 +PAGE_PROJECTION_WKT = ( + 'PROJCS["WGS_1984_World_Mercator",' + 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],' + 'PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],' + 'PROJECTION["Mercator"],' + 'PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],' + 'PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],' + 'UNIT["Meter",1.0]]' +) +# Web Mercator has no poles: this is as far north or south as it goes. +MAX_LATITUDE = 85.06 +# The browser's arithmetic can put the edge of the world a hair past where it is (180.00000000000125 degrees). An +# edge within this of 180 degrees is at 180 degrees; one further past it is of a map which crosses the antimeridian. +LONGITUDE_SLIVER = 1e-8 + + +class InvalidExport(ValueError): + """The layers sent can not be made into a map.""" + + +@dataclass +class ExportLayer: + # What the layer is called in the reader's list of layers. + name: str + # A PNG or JPEG image, or the file it is in. The images of a map are a hundred megabytes together, so those + # in files are read one at a time, when each is needed. + image: Optional[bytes] = None + path: Optional[pathlib.Path] = None + + def read(self) -> bytes: + return self.image if self.image is not None else pathlib.Path(self.path).read_bytes() + + +def _is_blank(image: Image.Image) -> bool: + """True when nothing was drawn: every pixel of the image is transparent.""" + if image.mode in ('RGBA', 'LA'): + return image.getchannel('A').getextrema()[1] == 0 + if image.mode == 'P' and 'transparency' in image.info: + return image.convert('RGBA').getchannel('A').getextrema()[1] == 0 + return False + + +def _check_layer(layer: ExportLayer, width: int, height: int) -> bool: + """Raises InvalidExport when the layer is not an image of the map. Returns whether there is anything in it.""" + try: + image = Image.open(io.BytesIO(layer.read())) + if image.format not in IMAGE_FORMATS: + raise InvalidExport(f'The layer {layer.name!r} is not a PNG or JPEG image') + if image.size != (width, height): + raise InvalidExport(f'The layer {layer.name!r} is {image.size[0]}x{image.size[1]},' + f' but the map is {width}x{height}') + return not _is_blank(image) + except InvalidExport: + raise + except Exception as e: + # Whatever Pillow makes of something which is not an image. + raise InvalidExport(f'The layer {layer.name!r} is not a readable image') from e + + +def _edge_longitude(longitude: float) -> float: + """A longitude which is a rounding error past the edge of the world, put on it. It is not wrapped: the east + edge of the last map before the antimeridian is at 180 degrees, not at -180.""" + if 180 < abs(longitude) < 180 + LONGITUDE_SLIVER: + return math.copysign(180.0, longitude) + return longitude + + +def _check_bounds(bounds: Sequence[float]) -> List[float]: + """The west, south, east and north edges of a map, in degrees. Raises InvalidExport when they are not.""" + try: + west, south, east, north = (float(i) for i in bounds) + except (TypeError, ValueError) as e: + raise InvalidExport('The edges of the map are not four numbers') from e + west, east = _edge_longitude(west), _edge_longitude(east) + edges = [west, south, east, north] + if not all(math.isfinite(i) for i in edges) or not (-180 <= west < east <= 180) \ + or not (-MAX_LATITUDE <= south < north <= MAX_LATITUDE): + raise InvalidExport(f'A map with the edges {edges} can not be exported') + return edges + + +def _pdf_string(text: str) -> str: + """A string as it is written in a PDF: as it is when it is ASCII, else in UTF-16.""" + if text.isascii(): + return '(' + text.replace('\\', '\\\\').replace('(', '\\(').replace(')', '\\)') + ')' + return '<' + (b'\xfe\xff' + text.encode('utf-16-be')).hex().upper() + '>' + + +def geospatial_viewport(width: float, height: float, bounds: Sequence[float], name: str = None, + origin: Sequence[float] = (0, 0)) -> str: + """The value of a page's `/VP`: a viewport which covers the map (`width` by `height` points, its lower left + corner at `origin`: so far right of, and up from, the page's), and says it is the map between `bounds` (west, + south, east, north; degrees of WGS 84), in the projection a reader is told of (see PAGE_PROJECTION_WKT). + + A point of the map is given as a part of the way across and up it (0 to 1, from the lower left), and the + corners are listed as GDAL and the readers which follow it expect: upper left, lower left, lower right, + upper right. The coordinates of each are its latitude, then its longitude. + """ + west, south, east, north = _check_bounds(bounds) + corners = '0 1 0 0 1 0 1 1' + points = ' '.join(f'{lat:.9f} {lon:.9f}' for lat, lon in ((north, west), (south, west), (south, east), + (north, east))) + name = f' /Name {_pdf_string(name)}' if name else '' + return ( + f'[<< /Type /Viewport{name}' + f' /BBox [{origin[0]:.3f} {origin[1]:.3f} {origin[0] + width:.3f} {origin[1] + height:.3f}]' + f' /Measure << /Type /Measure /Subtype /GEO' + f' /Bounds [{corners}] /GPTS [{points}] /LPTS [{corners}]' + f' /GCS << /Type /PROJCS /EPSG {PAGE_PROJECTION_EPSG} /WKT {_pdf_string(PAGE_PROJECTION_WKT)} >>' + f' /PDU [/M /SQM /DEG] >> >>]' + ) + + +def _compress_stream(doc, xref: int): + """Compress the stream of an object of the PDF, when it is not already.""" + if doc.xref_get_key(xref, 'Filter')[0] != 'null': + return + doc.update_stream(xref, zlib.compress(doc.xref_stream_raw(xref), COMPRESSION_LEVEL), compress=False) + doc.xref_set_key(xref, 'Filter', '/FlateDecode') + + +def _insert_layer(doc, page, rect, layer: ExportLayer, groups: dict): + """Put the image of a layer in a rectangle of the page, in the PDF layer of its name: its own, unless an + image before it had the name. (`groups` is the PDF layer of each name so far.) A layer of the map may be + several images, with other layers drawn between them. + + PyMuPDF keeps the pixels of an image uncompressed until the PDF is written: about 90 MB for each layer of a + quadrangle, all of them at once. They are compressed here instead, so only one layer's are ever in memory.""" + if layer.name not in groups: + groups[layer.name] = doc.add_ocg(layer.name, on=True) + xref = page.insert_image(rect, stream=layer.read(), oc=groups[layer.name]) + _compress_stream(doc, xref) + # The transparency of a PNG is an image of its own. + kind, mask = doc.xref_get_key(xref, 'SMask') + if kind == 'xref': + _compress_stream(doc, int(mask.split()[0])) + + +def check_export(layer_count: int, width: int, height: int, dpi: float, bounds: Optional[Sequence[float]] = None, + sheet: Optional[Sheet] = None) -> Optional[List[float]]: + """Raises InvalidExport when a map of `layer_count` images of `width` by `height` pixels can not be made into a + PDF. No image is read: this is what can be said of a map before its images are sent. Returns the `bounds` + as they are used.""" + if layer_count < 1: + raise InvalidExport('There are no layers to export') + if layer_count > MAX_LAYERS: + raise InvalidExport(f'A map of more than {MAX_LAYERS} layers can not be exported') + if not (0 < width <= MAX_SIDE and 0 < height <= MAX_SIDE) or width * height > MAX_PIXELS: + raise InvalidExport(f'A map of {width}x{height} pixels can not be exported') + if layer_count * width * height > MAX_TOTAL_PIXELS: + raise InvalidExport(f'A map of {layer_count} layers of {width}x{height} pixels is too large to export;' + f' turn some layers off') + if not MIN_DPI <= dpi <= MAX_DPI: + raise InvalidExport(f'A map can not be exported at {dpi} DPI') + if bounds is not None: + return _check_bounds(bounds) + if sheet is not None: + raise InvalidExport('The edges of the map are needed to put it on a sheet') + return None + + +def build_layered_pdf(layers: List[ExportLayer], width: int, height: int, dpi: float, title: str = None, + bounds: Optional[Sequence[float]] = None, sheet: Optional[Sheet] = None, + progress: Optional[Callable[[int, int], None]] = None) -> bytes: + """A one page PDF of the layers, the first at the bottom, each in a PDF layer of its own. + + A layer in which nothing was drawn is left out. + + @param layers: The images of the map, all `width` by `height` pixels. + @param dpi: How many of those pixels are an inch of the page. + @param bounds: The west, south, east and north edges of the images, in degrees. With them the PDF says where + it is on the Earth; the images must be Web Mercator, and all of the map between those edges. + @param sheet: With this the map is on a sheet, which says this of it. The sheet needs `bounds`. + @param progress: Called with how many steps are done, and how many there are, before each step. A PDF takes + a minute or two on a Raspberry Pi, which is long to wait with nothing said; and it may raise to stop the + work. + @raise InvalidExport: When the layers are not of one map. + """ + # Before any image is decoded. + bounds = check_export(len(layers), width, height, dpi, bounds, sheet) + + # Each layer is looked at, and then each which has anything in it is put in the PDF: two steps a layer. + steps = 2 * len(layers) + + def step(done: int): + if progress: + progress(done, steps) + + drawn = [] + for index, layer in enumerate(layers): + step(index) + if _check_layer(layer, width, height): + drawn.append(layer) + if not drawn: + raise InvalidExport('Nothing is drawn in any layer of the map') + + # Not imported at module level: pymupdf also imports onnxruntime (~2s on a Pi 4), which the API must not pay + # for at startup. + import pymupdf + + doc = pymupdf.open() + try: + map_width, map_height = width * POINTS_PER_INCH / dpi, height * POINTS_PER_INCH / dpi + if sheet is not None: + plan = plan_sheet(map_width, map_height, bounds, sheet) + page = doc.new_page(width=plan.width, height=plan.height) + rect = pymupdf.Rect(plan.map_rect) + else: + plan = None + page = doc.new_page(width=map_width, height=map_height) + rect = page.rect + groups = {} + for index, layer in enumerate(drawn): + # A layer which was left out is a step done. + step(len(layers) + (len(layers) - len(drawn)) + index) + _insert_layer(doc, page, rect, layer, groups) + step(steps) + if plan is not None: + draw_sheet(doc, page, plan) + # Every step is done; what is told may still stop the work, here and before the PDF is written. + step(steps) + if bounds is not None: + # A PDF measures up from the bottom of the page. + origin = (rect.x0, page.rect.height - rect.y1) + doc.xref_set_key(page.xref, 'VP', geospatial_viewport(rect.width, rect.height, bounds, title, origin)) + doc.set_metadata(dict(title=title or 'Map', creator='WROLPi', producer='WROLPi')) + step(steps) + pdf = doc.tobytes(garbage=3) + step(steps) + return pdf + finally: + doc.close() diff --git a/modules/map/export_job.py b/modules/map/export_job.py new file mode 100644 index 00000000..4b272b1c --- /dev/null +++ b/modules/map/export_job.py @@ -0,0 +1,321 @@ +"""Making the PDF of an exported map as a Job. + +A PDF of a map is a minute or two of work on a Raspberry Pi: too long for a request to wait on. So the browser +describes the map and is given the id of an export; it sends the image of each layer, one a request (together they +are a hundred megabytes, more than one request may be); then it asks for the PDF to be made, and is given the id of +a Job (see `wrolpi.jobs`). It watches the Job, and when the Job is done it fetches the PDF. + +An export is a directory, which holds what was said of the map, the images until the PDF is made of them, and then +the PDF. It is on a disk, not in memory. It is removed when the person exporting stops it, when its Job is +cancelled, or when it is old; never while its Job is waiting or running. +""" +import asyncio +import json +import os +import pathlib +import re +import shutil +import stat +import threading +import time +import uuid +from typing import List, Optional + +from modules.map import export +from wrolpi.common import logger +from wrolpi.errors import UnknownJob +from wrolpi.jobs import (CANCELLED, COMPLETE, PENDING, RUNNING, cancel_job, get_current_job, get_job, + register_job) + +logger = logger.getChild(__name__) + +__all__ = ['ExportNotFound', 'ExportNotReady', 'ExportStorageError', 'create_export', 'save_layer', 'start_export', + 'get_export_pdf', 'remove_export', 'remove_old_exports', 'map_export_pdf_job', 'EXPORT_ROOT', 'EXPORT_KEEP_SECONDS', 'MAX_LAYER_BYTES'] + +# Where exports are kept. Not /tmp, which is memory on a Raspberry Pi. /var/tmp is a directory anyone on the +# machine may make things in, so this one is only used when it is a real directory of this user's (see `_root`). +EXPORT_ROOT = pathlib.Path('/var/tmp/wrolpi-map-export') +# An export which is this old is removed: its PDF was fetched long ago, or never will be. +EXPORT_KEEP_SECONDS = 60 * 60 +# The most one image of a layer may be. A layer of a quadrangle at 400 DPI is 20 to 30 MB as a PNG. +MAX_LAYER_BYTES = 64 * 1024 * 1024 +JOB_NAME = 'map_export_pdf' +META_FILE = 'meta.json' +PDF_FILE = 'map.pdf' +EXPORT_ID = re.compile(r'^[0-9a-f]{32}$') +PNG_MAGIC, JPEG_MAGIC = b'\x89PNG\r\n\x1a\n', b'\xff\xd8\xff' + + +class ExportNotFound(Exception): + """There is no such export: it was never made, or it was old and has been removed.""" + + +class ExportNotReady(Exception): + """The export is not as far along as it was asked to be.""" + + +class ExportStorageError(Exception): + """Where exports are kept can not be used.""" + + +class ExportCancelled(Exception): + """The Job which was making the PDF was cancelled.""" + + +def _is_own_directory(path: pathlib.Path) -> bool: + """Whether a path is a directory itself (not a link to one) which this user owns.""" + try: + status = os.lstat(path) + except OSError: + return False + return stat.S_ISDIR(status.st_mode) and status.st_uid == os.getuid() + + +def _root(create: bool = False) -> Optional[pathlib.Path]: + """Where exports are kept, or None when there is no such directory (and it is not to be made). + + It is made for this user alone: the images of a map are whatever the user has on their map. + + @raise ExportStorageError: When what is there is not a directory of this user's: a link there, which another + user of the machine may have made, would have exports written (and old ones removed) wherever it leads.""" + if create: + try: + os.mkdir(EXPORT_ROOT, 0o700) + except FileExistsError: + pass + except OSError as e: + raise ExportStorageError(f'The directory of map exports can not be made: {EXPORT_ROOT}') from e + if not os.path.lexists(EXPORT_ROOT): + return None + if not _is_own_directory(EXPORT_ROOT): + raise ExportStorageError(f'The directory of map exports is not a directory of this user: {EXPORT_ROOT}') + if create and stat.S_IMODE(os.lstat(EXPORT_ROOT).st_mode) != 0o700: + # One which was made before it was made for this user alone. + os.chmod(EXPORT_ROOT, 0o700) + return EXPORT_ROOT + + +def _directory(export_id: str) -> pathlib.Path: + """Where an export is. The id is what `create_export` gave, and nothing else: it becomes part of a path.""" + if not EXPORT_ID.match(str(export_id)): + raise ExportNotFound(f'There is no export {export_id!r}') + root = _root() + directory = root / export_id if root else None + if directory is None or not _is_own_directory(directory) or not (directory / META_FILE).is_file(): + raise ExportNotFound(f'There is no export {export_id!r}') + return directory + + +def _read_meta(directory: pathlib.Path) -> dict: + return json.loads((directory / META_FILE).read_text()) + + +def _write_whole(path: pathlib.Path, contents: bytes): + """Write a file so that whoever reads it reads all of it, or what was there before: never a part.""" + partial = path.with_name(f'{path.name}.{uuid.uuid4().hex}.part') + try: + partial.write_bytes(contents) + os.replace(partial, path) + finally: + partial.unlink(missing_ok=True) + + +def _write_meta(directory: pathlib.Path, meta: dict): + _write_whole(directory / META_FILE, json.dumps(meta).encode()) + + +def _layer_file(directory: pathlib.Path, index: int) -> pathlib.Path: + return directory / f'layer-{index:03d}' + + +def _job_status(meta: dict) -> Optional[str]: + """The status of the Job of an export, or None when it has none: it was not started, or the API has + restarted since and has forgotten the Job.""" + if not meta.get('job_id'): + return None + try: + return get_job(meta['job_id'])['status'] + except UnknownJob: + return None + + +def remove_old_exports(now: float = None): + """Remove the exports which are done with. An export is a hundred megabytes until its PDF is made, and tens + after. + + An export whose Job is waiting or running is kept however old it is: its images are what the Job is to work + from, and it may wait behind another Job for longer than an export is kept. One whose Job was cancelled is + removed at once. Any other is removed when it is old. Only what is an export is touched.""" + now = time.time() if now is None else now + root = _root() + if root is None: + return + for directory in root.iterdir(): + try: + if not EXPORT_ID.match(directory.name) or not _is_own_directory(directory): + continue + try: + status = _job_status(_read_meta(directory)) + except (OSError, ValueError): + # No description which can be read: not an export any Job can use. + status = None + if status in (PENDING, RUNNING): + continue + if status == CANCELLED or now - directory.stat().st_mtime > EXPORT_KEEP_SECONDS: + shutil.rmtree(directory, ignore_errors=True) + except OSError as e: + logger.warning(f'Could not remove the old map export {directory}', exc_info=e) + + +def remove_export(export_id: str): + """Stop an export, and remove it: the person exporting does not want it. + + Its Job is cancelled when it is waiting or running. A running Job stops between two layers and removes the + images itself; until then they are in use, so what is left of the export is removed later, with the old ones.""" + directory = _directory(export_id) + meta = _read_meta(directory) + status = _job_status(meta) + if status in (PENDING, RUNNING): + cancel_job(meta['job_id']) + if status != RUNNING: + shutil.rmtree(directory, ignore_errors=True) + + +def create_export(layers: List[str], width: int, height: int, dpi: float, title: Optional[str] = None, + bounds=None, sheet: Optional[export.Sheet] = None) -> str: + """Begin an export of a map of these layers (their names, from the bottom up). Returns its id. + + @raise InvalidExport: When such a map can not be exported; no image has been sent yet.""" + bounds = export.check_export(len(layers), width, height, dpi, bounds, sheet) + root = _root(create=True) + remove_old_exports() + export_id = uuid.uuid4().hex + directory = root / export_id + os.mkdir(directory, 0o700) + _write_meta(directory, dict( + layers=[str(i) for i in layers], width=width, height=height, dpi=dpi, title=title, bounds=bounds, + sheet=dict(name=sheet.name, credits=list(sheet.credits), date=sheet.date, + contour_interval=sheet.contour_interval, contour_unit=sheet.contour_unit, + legend=sheet.legend) if sheet else None, + job_id=None, + )) + return export_id + + +def save_layer(export_id: str, index: int, image: bytes): + """Keep the image of one layer of an export. + + @raise InvalidExport: When it is not an image of a layer of that export.""" + directory = _directory(export_id) + meta = _read_meta(directory) + if meta['job_id']: + raise export.InvalidExport('The PDF of this map is already being made') + if not 0 <= index < len(meta['layers']): + raise export.InvalidExport(f'The map has {len(meta["layers"])} layers; there is no layer {index}') + if not image or len(image) > MAX_LAYER_BYTES: + raise export.InvalidExport(f'The image of the layer {meta["layers"][index]!r} is empty, or is too large') + if not image.startswith((PNG_MAGIC, JPEG_MAGIC)): + raise export.InvalidExport(f'The layer {meta["layers"][index]!r} is not a PNG or JPEG image') + _write_whole(_layer_file(directory, index), image) + if not (directory / META_FILE).is_file(): + # The export was removed while this image was on its way: the image is not left behind it. + shutil.rmtree(directory, ignore_errors=True) + raise ExportNotFound(f'There is no export {export_id!r}') + + +def start_export(export_id: str) -> str: + """Queue the Job which makes the PDF of an export, all of whose images have been sent. Returns the Job's id; + asked again, the same Job's.""" + directory = _directory(export_id) + meta = _read_meta(directory) + if meta['job_id']: + return meta['job_id'] + missing = [name for index, name in enumerate(meta['layers']) if not _layer_file(directory, index).is_file()] + if missing: + raise ExportNotReady(f'The images of {len(missing)} layers have not been sent: {", ".join(missing[:5])}') + job_id = map_export_pdf_job.enqueue(description=f'Map PDF: {meta["title"] or "a map"}', export_id=export_id) + meta['job_id'] = job_id + _write_meta(directory, meta) + return job_id + + +def get_export_pdf(export_id: str) -> pathlib.Path: + """The PDF of an export. + + @raise ExportNotReady: When it has not been made (yet, or because the Job failed or was cancelled).""" + directory = _directory(export_id) + pdf = directory / PDF_FILE + if not pdf.is_file(): + job_id = _read_meta(directory).get('job_id') + status = get_job(job_id)['status'] if job_id else 'not started' + raise ExportNotReady(f'The PDF has not been made (the job is {status})' if status != COMPLETE + else 'The PDF is gone') + return pdf + + +def _make_pdf(directory: pathlib.Path, job, stop: threading.Event) -> dict: + """Make the PDF of an export from its images, which are removed whether or not it could be made. + + The work stops between two layers when the Job is cancelled, and when `stop` is set: the Job was ended by + something other than its being cancelled (the API is stopping), which the Job itself does not say.""" + meta = _read_meta(directory) + layers = [export.ExportLayer(name=name, path=_layer_file(directory, index)) + for index, name in enumerate(meta['layers'])] + sheet = export.Sheet(**meta['sheet']) if meta['sheet'] else None + + def stopped() -> bool: + return stop.is_set() or job.cancel_requested() + + def progress(done: int, steps: int): + # Between two layers is where the work can be stopped. + if stopped(): + raise ExportCancelled() + job.set_progress(100 * done / steps) + + try: + pdf = export.build_layered_pdf(layers, meta['width'], meta['height'], meta['dpi'], meta['title'], + meta['bounds'], sheet, progress=progress) + # Under another name until all of it is written: a PDF which is there is a whole one. + partial = directory / f'{PDF_FILE}.part' + partial.write_bytes(pdf) + if stopped(): + partial.unlink(missing_ok=True) + raise ExportCancelled() + partial.rename(directory / PDF_FILE) + return dict(export_id=directory.name, size=len(pdf)) + finally: + for layer in layers: + layer.path.unlink(missing_ok=True) + + +@register_job(JOB_NAME) +async def map_export_pdf_job(export_id: str): + """Make the PDF of an export. The work is in a thread: this process has other things to keep doing. + + A Job which is cancelled is not over until that thread has stopped: Jobs are run one at a time so that two of + them do not work a Raspberry Pi at once, and the next is not to start beside the work of this one.""" + directory = _directory(export_id) + job = get_current_job() + job.log(f'Making the PDF of the map export {export_id}') + # Old exports are removed here too: an export may be the last for a long while. + await asyncio.to_thread(remove_old_exports) + stop = threading.Event() + work = asyncio.ensure_future(asyncio.to_thread(_make_pdf, directory, job, stop)) + try: + # Shielded: cancelling this Job must not let go of the thread, which can not be stopped from here. + result = await asyncio.shield(work) + except asyncio.CancelledError: + # The thread is told to stop, which it does between two layers. Wait for it to: an API which is stopping + # waits for its threads, and is not to wait for all of a PDF. + stop.set() + await asyncio.wait({work}) + if not work.cancelled(): + work.exception() + raise + except ExportCancelled: + # The work saw that the Job was cancelled before the Job's runner did. The runner is who ends a cancelled + # Job (it looks every second), so wait for it to. + await asyncio.Event().wait() + raise + job.log(f'The PDF is {result["size"]} bytes') + return result diff --git a/modules/map/magnetic.py b/modules/map/magnetic.py new file mode 100644 index 00000000..4a00fc17 --- /dev/null +++ b/modules/map/magnetic.py @@ -0,0 +1,121 @@ +"""Where a compass points: the World Magnetic Model. + +A compass does not point at the north a map is drawn to, and how far off it is depends on where it is and when. +The model is the Earth's magnetic field as a sum of spherical harmonics, each of which changes at a steady rate +for the five years the model is good for. + +`wmm/WMM2025.COF` is the coefficient file of the World Magnetic Model 2025, as it is published by the NOAA +National Centers for Environmental Information and the British Geological Survey +(https://www.ncei.noaa.gov/products/world-magnetic-model). It is a work of the U.S. Government, in the public +domain. It is good from 2025.0 until 2030.0, when its successor is published: put that file beside it, and name +it in MODEL_PATH. +""" +import math +import pathlib +from dataclasses import dataclass +from functools import lru_cache +from typing import List, Optional, Tuple + +__all__ = ['MagneticModel', 'load_model', 'declination', 'MODEL_PATH'] + +MODEL_PATH = pathlib.Path(__file__).parent / 'wmm' / 'WMM2025.COF' +# How many years a model is good for, from its epoch. +MODEL_YEARS = 5 + +# The WGS 84 ellipsoid (kilometers), and the radius of the sphere the model is of. +WGS84_A = 6378.137 +WGS84_F = 1 / 298.257223563 +WGS84_E2 = WGS84_F * (2 - WGS84_F) +MODEL_RADIUS = 6371.2 + + +@dataclass +class MagneticModel: + # The decimal year the coefficients are of. + epoch: float + name: str + # (n, m, g, h, the change of g in a year, the change of h in a year): nanoteslas. + coefficients: List[Tuple[int, int, float, float, float, float]] + + @property + def degree(self) -> int: + return max(i[0] for i in self.coefficients) + + def covers(self, year: float) -> bool: + return self.epoch <= year < self.epoch + MODEL_YEARS + + def field(self, latitude: float, longitude: float, year: float, + altitude: float = 0) -> Tuple[float, float, float]: + """The magnetic field (nanoteslas north, east and down) at a place (degrees of WGS 84; `altitude` is + kilometers above the ellipsoid) in a decimal year.""" + phi, lam = math.radians(latitude), math.radians(longitude) + # The place as the sphere has it: how far from the center of the Earth, and at what angle from its axis. + curvature = WGS84_A / math.sqrt(1 - WGS84_E2 * math.sin(phi) ** 2) + p = (curvature + altitude) * math.cos(phi) + z = (curvature * (1 - WGS84_E2) + altitude) * math.sin(phi) + r = math.hypot(p, z) + geocentric = math.asin(z / r) + cos_t, sin_t = math.sin(geocentric), math.cos(geocentric) + + # The Schmidt semi-normalized Legendre functions of the angle from the axis, and their derivatives. + size = self.degree + 1 + legendre = [[0.0] * size for _ in range(size)] + derivative = [[0.0] * size for _ in range(size)] + legendre[0][0] = 1.0 + for n in range(1, size): + for m in range(n + 1): + if n == m: + k = 1.0 if n == 1 else math.sqrt((2 * n - 1) / (2 * n)) + legendre[n][n] = k * sin_t * legendre[n - 1][n - 1] + derivative[n][n] = k * (sin_t * derivative[n - 1][n - 1] + cos_t * legendre[n - 1][n - 1]) + else: + k = math.sqrt(n * n - m * m) + before = math.sqrt((n - 1) ** 2 - m * m) if n > 1 and m <= n - 2 else 0.0 + legendre[n][m] = ((2 * n - 1) * cos_t * legendre[n - 1][m] + - before * (legendre[n - 2][m] if n > 1 else 0.0)) / k + derivative[n][m] = ((2 * n - 1) * (cos_t * derivative[n - 1][m] - sin_t * legendre[n - 1][m]) + - before * (derivative[n - 2][m] if n > 1 else 0.0)) / k + + years = year - self.epoch + north = east = down = 0.0 + for n, m, g, h, g_rate, h_rate in self.coefficients: + g, h = g + years * g_rate, h + years * h_rate + ratio = (MODEL_RADIUS / r) ** (n + 2) + cos_m, sin_m = math.cos(m * lam), math.sin(m * lam) + north += ratio * (g * cos_m + h * sin_m) * derivative[n][m] + east += ratio * m * (g * sin_m - h * cos_m) * legendre[n][m] / sin_t + down -= ratio * (n + 1) * (g * cos_m + h * sin_m) * legendre[n][m] + + # From the sphere's north and down to the ellipsoid's. + turn = geocentric - phi + return (north * math.cos(turn) - down * math.sin(turn), east, + north * math.sin(turn) + down * math.cos(turn)) + + def declination(self, latitude: float, longitude: float, year: float, altitude: float = 0) -> float: + """How far east of true north (degrees) a compass points at a place in a decimal year.""" + north, east, _ = self.field(latitude, longitude, year, altitude) + return math.degrees(math.atan2(east, north)) + + +@lru_cache(maxsize=None) +def load_model(path: pathlib.Path = MODEL_PATH) -> MagneticModel: + """Read a model's coefficient file: a line of its epoch and name, then a line for each harmonic.""" + lines = path.read_text().splitlines() + epoch, name = lines[0].split()[:2] + coefficients = [] + for line in lines[1:]: + parts = line.split() + if len(parts) != 6: + # The lines of nines which end the file. + continue + coefficients.append((int(parts[0]), int(parts[1]), *(float(i) for i in parts[2:]))) + return MagneticModel(epoch=float(epoch), name=name, coefficients=coefficients) + + +def declination(latitude: float, longitude: float, year: float) -> Optional[float]: + """How far east of true north (degrees) a compass points at a place on the ground in a decimal year, or None + in a year the model is not of: it is not known, and a guess is worse than nothing.""" + model = load_model() + if not model.covers(year): + return None + return model.declination(latitude, longitude, year) diff --git a/modules/map/quads.py b/modules/map/quads.py new file mode 100644 index 00000000..b01c15e8 --- /dev/null +++ b/modules/map/quads.py @@ -0,0 +1,117 @@ +"""What a quadrangle of the map is called. + +The U.S. Geological Survey names each of its 7.5 minute quadrangles, after a town or a mountain or a lake in it +("Berthoud", Colorado). `quads/usgs_7_5_minute.tsv.gz` is the index of those names, from the Map Indices of +The National Map (public domain); `wrolpi/scripts/build_map_quad_names.py` makes it. + +Most of those quadrangles are 7.5 minutes each way, with their edges on whole multiples of 7.5 minutes. Not all: +in Alaska a quadrangle is wider the further north it is, and around some islands they are moved to fit the land. +So a place is looked for in the quadrangle which has it, and whether that quadrangle is the one asked about is +said too. +""" +import bisect +import gzip +import pathlib +from dataclasses import dataclass +from functools import lru_cache +from typing import Dict, List, Optional, Tuple + +__all__ = ['QuadName', 'quad_name', 'load_index', 'INDEX_PATH'] + +INDEX_PATH = pathlib.Path(__file__).parent / 'quads' / 'usgs_7_5_minute.tsv.gz' +SECONDS_PER_DEGREE = 3600 +# No quadrangle is taller than this (seconds of arc): how far south of a place its quadrangle may start. +TALLEST = 450 +# Edges this near (seconds of arc) are the same edge. +SAME_EDGE = 1 +# A place this near (seconds of arc) past the outer edge of a quadrangle is on that edge: the edges the map gives +# are a hair from where they are. +OUTER_EDGE = 0.01 +# A place is where it is to the nearest this much (seconds of arc; 3 meters): the edges the map gives are a hair +# from where they are, and the middle of an area is often on the edge two quadrangles share. +NEAREST = 0.1 + + +@dataclass +class QuadName: + name: str + state: str + # Whether the named quadrangle is the area which was asked about, and not a larger or a moved one which has + # the middle of it. + exact: bool + + def __json__(self) -> dict: + return dict(name=self.name, state=self.state, exact=self.exact) + + +# The quadrangles which start at one latitude: their west edges in order, and (west, east, north, name, state) of +# each, in the same order. +Row = Tuple[List[int], List[Tuple[int, int, int, str, str]]] + + +@lru_cache(maxsize=None) +def load_index(path: pathlib.Path = INDEX_PATH) -> Tuple[List[int], Dict[int, str]]: + """The index: the latitudes (seconds of arc) quadrangles start at, in order, and the lines of the quadrangles + which start at each. + + The lines are kept as they are written (3 MB; as numbers and names they are 17 MB in each process of the API), + and are not read until a name is asked for.""" + rows: Dict[int, List[str]] = {} + with gzip.open(path, 'rt', encoding='utf-8') as fh: + for line in fh: + rows.setdefault(int(line[:line.index('\t')]), []).append(line) + return sorted(rows), {south: ''.join(lines) for south, lines in rows.items()} + + +@lru_cache(maxsize=32) +def _row(south: int) -> Row: + """The quadrangles which start at a latitude. Those asked about one after another are near each other, so + the few rows last read are kept.""" + wests, cells = [], [] + for line in load_index()[1][south].splitlines(): + _, west, east, north, name, state = line.split('\t') + # The lines are in order, so these are. + wests.append(int(west)) + cells.append((int(west), int(east), int(north), name, state)) + return wests, cells + + +def _find(x: float, y: float, outer: float = 0) -> Optional[Tuple[int, int, int, int, str, str]]: + """The quadrangle (west, south, east, north, name, state) which has a place (seconds of arc), or None. + + A place on an edge two quadrangles share is in the one east, or north, of it: a quadrangle has its west and + south edges, and not its east and north ones. With `outer` it has those too, and that much (seconds of arc) + past them.""" + souths, _ = load_index() + # The rows which may have the place, from the nearest to start south of it. + last = bisect.bisect_right(souths, y) + for row_south in reversed(souths[max(0, last - 8):last]): + if row_south < y - TALLEST * 2: + break + wests, cells = _row(row_south) + index = bisect.bisect_right(wests, x) - 1 + # Quadrangles moved to fit an island lie over their neighbors a little, so the one before is looked at too. + for cell_west, cell_east, cell_north, name, state in cells[max(0, index - 1):index + 1][::-1]: + if outer: + within = x <= cell_east + outer and y <= cell_north + outer + else: + within = x < cell_east and y < cell_north + if within and cell_west <= x and row_south <= y: + return cell_west, row_south, cell_east, cell_north, name, state + return None + + +def quad_name(west: float, south: float, east: float, north: float) -> Optional[QuadName]: + """What the USGS calls the quadrangle between these edges (degrees), or None where it has none: most of the + world. + + It is the quadrangle which has the middle of the area. Where the middle is on the east or north edge of a + quadrangle with none beyond it (an area half of which is the last quadrangle of the land), it is that one.""" + x = round((west + east) / 2 * SECONDS_PER_DEGREE / NEAREST) * NEAREST + y = round((south + north) / 2 * SECONDS_PER_DEGREE / NEAREST) * NEAREST + found = _find(x, y) or _find(x, y, outer=OUTER_EDGE) + if found is None: + return None + edges = [round(i * SECONDS_PER_DEGREE) for i in (west, south, east, north)] + exact = all(abs(a - b) <= SAME_EDGE for a, b in zip(edges, found[:4])) + return QuadName(name=found[4], state=found[5], exact=exact) diff --git a/modules/map/quads/usgs_7_5_minute.tsv.gz b/modules/map/quads/usgs_7_5_minute.tsv.gz new file mode 100644 index 00000000..564e656b Binary files /dev/null and b/modules/map/quads/usgs_7_5_minute.tsv.gz differ diff --git a/modules/map/sheet.py b/modules/map/sheet.py new file mode 100644 index 00000000..7a9a492f --- /dev/null +++ b/modules/map/sheet.py @@ -0,0 +1,871 @@ +"""The sheet an exported map is printed on: what a topographic map has around and over the map itself. + +Around the map is a margin with a title, the latitude and longitude of the map's corners and of ticks along its +edges, scale bars, a diagram of the three norths (true, grid and magnetic), and notes (the projection, the grid, +where a compass points, where the map's data is from). Over the map is a grid of +1,000 meter squares (Universal Transverse Mercator), which is how a position is read from, or found on, paper. + +All of it is drawn as lines and text, in PDF layers of its own which a reader can turn off. +""" +import dataclasses +import datetime +import math +from dataclasses import dataclass, field +from typing import List, Optional, Sequence, Tuple + +from modules.map import magnetic, quads + +__all__ = ['Sheet', 'SheetPlan', 'plan_sheet', 'draw_sheet', 'to_utm', 'from_utm', 'utm_zone', 'map_scale', + 'format_angle', 'graticule_interval', 'SHEET_LAYER', 'GRID_LAYER', 'LegendLine', 'legend_lines', + 'legend_as_sent', 'adjoining_quads', 'mils'] + +# The names of the sheet's PDF layers. +SHEET_LAYER = 'Sheet' +GRID_LAYER = 'UTM grid' + +# The WGS 84 ellipsoid. +WGS84_A = 6378137.0 +WGS84_F = 1 / 298.257223563 +WGS84_E2 = WGS84_F * (2 - WGS84_F) + +# Universal Transverse Mercator. +UTM_SCALE = 0.9996 +UTM_FALSE_EASTING = 500_000.0 +UTM_FALSE_NORTHING_SOUTH = 10_000_000.0 +# Where UTM is used: nearer the poles than this, positions are given another way. +UTM_SOUTH, UTM_NORTH = -80.0, 84.0 +GRID_METERS = 1000 +# More lines than this across a map are too many to read; the squares are made ten times larger until there are +# fewer. +GRID_MAX_LINES = 60 + +POINTS_PER_INCH = 72 +METERS_PER_POINT = 0.0254 / POINTS_PER_INCH +METERS_PER_MILE = 1609.344 +METERS_PER_FOOT = 0.3048 + +# The sizes of the sheet, in points. +MARGIN = 54 +TOP_MARGIN = 64 +FONT = 'helv' +BOLD = 'hebo' +TITLE_SIZE = 13 +# Between two lines of the title, and the least there is between the name and the scale on one line. +TITLE_LEADING = 18 +TITLE_GAP = 18 +LABEL_SIZE = 6.5 +NOTE_SIZE = 7 +NOTE_LEADING = 9.5 +BAR_HEIGHT = 4 +BAR_SPACING = 27 +# The bars: kilometers, meters, miles and feet. +BAR_COUNT = 4 +# The room under the bars for how far apart the contour lines are. +CONTOUR_LEADING = 16 +# The units contour lines are apart in, as they are written. +CONTOUR_UNITS = dict(feet='FEET', meters='METERS') +# The diagram of the quadrangles around the map: how large each of its nine squares is, how much room the +# diagram and the names beside it take under the bars, and the narrowest map which has room for it. +ADJOINING_SQUARE = 12 +ADJOINING_HEIGHT = 58 +ADJOINING_MIN_WIDTH = 280 +ADJOINING_COLUMN = 150 +# How many maps over to look for the next quadrangle: the widest of the USGS is under four maps wide. +ADJOINING_STEPS = 4 +ADJOINING_GRAY = (0.75, 0.75, 0.75) +GRAY = (0.6, 0.6, 0.6) +# Where the eight are from the map, in the order they are numbered: (columns east, rows north). +ADJOINING = ((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)) +# The legend: the most lines it has, how long what each is called may be, how wide and tall the room of one is, +# how long its piece of line is, and the least and the most a line is drawn wide (points): a line thinner than +# the least is not one which is seen on the map, and is left out. +LEGEND_LINES = 12 +LEGEND_LABEL = 40 +LEGEND_ITEM_WIDTH = 118 +LEGEND_ITEM_HEIGHT = 13 +LEGEND_SAMPLE = 30 +LEGEND_WIDTHS = (0.1, 5) +LEGEND_DASHES = 6 +WHITE = (1, 1, 1) +# A compass is marked in degrees, or in mils: 6,400 of them around. +MILS_PER_DEGREE = 6400 / 360 +# From the bottom of the map to the first scale bar, and from the last note to the bottom of the page. +BARS_OFFSET = 52 +BOTTOM_PADDING = 36 +TICK_LENGTH = 7 +CROSS_LENGTH = 5 +BLACK = (0, 0, 0) +GRID_COLOR = (0.1, 0.2, 0.55) + +# The spacings a map's ticks of latitude and longitude may have, in seconds of arc. +GRATICULE_INTERVALS = (36000, 18000, 3600, 1800, 900, 450, 300, 150, 60, 30, 15, 5, 1) + + +@dataclass +class LegendLine: + """A kind of line of the map (a road, a boundary), as it is drawn there: for the legend.""" + label: str + # Red, green and blue, each from 0 to 1. + color: Tuple[float, float, float] + # How wide it is, in points. + width: float + opacity: float = 1 + # The lengths of its dashes and of the gaps between them, in points; nothing when it is unbroken. + dash: Tuple[float, ...] = () + # The line under it which is its edges: its color, and how wide it is, edges and all. + casing_color: Optional[Tuple[float, float, float]] = None + casing_width: float = 0 + + +@dataclass +class Sheet: + """What the browser says of a map, to be written around it.""" + # What the map is called. + name: Optional[str] = None + # Where the data of the map is from. + credits: Sequence[str] = () + # When the map was made, as it is to be written. When it is a day (2026-11-15), the sheet says where a + # compass points on that day. + date: Optional[str] = None + # How far apart the map's contour lines are, and in what (one of CONTOUR_UNITS); None when it has none. + contour_interval: Optional[float] = None + contour_unit: Optional[str] = None + # The lines of the map as the browser drew them, for the legend: `{"ground": [r, g, b], "lines": [{"label", + # "color", "width", "opacity", "dash", "casing_color", "casing_width"}, ...]}` (see `LegendLine`; `ground` + # is the color of the land they are drawn on). It is whatever was sent: `legend_lines` makes lines of it. + legend: Optional[dict] = None + + +@dataclass +class SheetPlan: + """Where everything on a sheet goes. Lengths are points; `y` is down the page from its top.""" + width: float + height: float + # The rectangle of the page the map is in: left, top, right, bottom. + map_rect: Tuple[float, float, float, float] + # The west, south, east and north edges of the map. + bounds: Tuple[float, float, float, float] + # How many times larger the ground is than the map, at the middle of the map. + scale: float + name: Optional[str] = None + notes: List[str] = field(default_factory=list) + # The UTM zone the map's grid is of, or None where there is no grid. + zone: Optional[int] = None + # How far east of true north (degrees) the grid's north, and a compass, point at the middle of the map. + # None where there is no grid, and when it is not known where a compass points. + grid_north: Optional[float] = None + magnetic_north: Optional[float] = None + # How far apart the contour lines are, as it is written under the scale bars; None when there are none. + contours: Optional[str] = None + # What the USGS calls the eight quadrangles around the map, in the order of ADJOINING (None for one it has + # no name for); None when it has a name for none of them, and when the sheet has no room to say. + adjoining: Optional[List[Optional[str]]] = None + # The lines of the legend, and the color of the land they are drawn on. + legend: List[LegendLine] = field(default_factory=list) + legend_ground: Tuple[float, float, float] = WHITE + + @property + def south_hemisphere(self) -> bool: + return (self.bounds[1] + self.bounds[3]) / 2 < 0 + + def point(self, latitude: float, longitude: float) -> Tuple[float, float]: + """Where on the page a place is. The map is Web Mercator.""" + x0, y0, x1, y1 = self.map_rect + west, south, east, north = self.bounds + top, bottom = _mercator_y(north), _mercator_y(south) + return (x0 + (longitude - west) / (east - west) * (x1 - x0), + y0 + (top - _mercator_y(latitude)) / (top - bottom) * (y1 - y0)) + + +def _mercator_y(latitude: float) -> float: + return math.log(math.tan(math.pi / 4 + math.radians(latitude) / 2)) + + +def _middle_latitude(south: float, north: float) -> float: + """The latitude half way up a Web Mercator map.""" + y = (_mercator_y(south) + _mercator_y(north)) / 2 + return math.degrees(2 * math.atan(math.exp(y)) - math.pi / 2) + + +def map_scale(width: float, bounds: Sequence[float]) -> float: + """How many times larger the ground is than a map `width` points wide, at the middle of the map.""" + west, south, east, north = bounds + latitude = math.radians(_middle_latitude(south, north)) + meters_per_degree = math.radians(1) * WGS84_A * math.cos(latitude) \ + / math.sqrt(1 - WGS84_E2 * math.sin(latitude) ** 2) + return meters_per_degree * (east - west) / (width * METERS_PER_POINT) + + +# Universal Transverse Mercator, by the series of Krüger (as Karney gives them), which is good to well under a +# millimeter within a zone. +_N = WGS84_F / (2 - WGS84_F) +_RADIUS = WGS84_A / (1 + _N) * (1 + _N ** 2 / 4 + _N ** 4 / 64) +_ALPHA = (_N / 2 - 2 * _N ** 2 / 3 + 5 * _N ** 3 / 16, 13 * _N ** 2 / 48 - 3 * _N ** 3 / 5, 61 * _N ** 3 / 240) +_BETA = (_N / 2 - 2 * _N ** 2 / 3 + 37 * _N ** 3 / 96, _N ** 2 / 48 + _N ** 3 / 15, 17 * _N ** 3 / 480) +_DELTA = (2 * _N - 2 * _N ** 2 / 3 - 2 * _N ** 3, 7 * _N ** 2 / 3 - 8 * _N ** 3 / 5, 56 * _N ** 3 / 15) + + +def utm_zone(longitude: float) -> int: + """The UTM zone (1 to 60) a longitude is in. The zones are six degrees wide, everywhere: those which are + made wider around Norway and Svalbard are not.""" + return min(60, int((longitude + 180) // 6) + 1) + + +def _central_meridian(zone: int) -> float: + return math.radians(zone * 6 - 183) + + +def to_utm(latitude: float, longitude: float, zone: int, south: bool = False) -> Tuple[float, float]: + """The easting and northing (meters) of a place, in a UTM zone of WGS 84.""" + phi = math.radians(latitude) + lam = math.radians(longitude) - _central_meridian(zone) + c = 2 * math.sqrt(_N) / (1 + _N) + t = math.sinh(math.atanh(math.sin(phi)) - c * math.atanh(c * math.sin(phi))) + xi = math.atan2(t, math.cos(lam)) + eta = math.atanh(math.sin(lam) / math.sqrt(1 + t * t)) + northing = xi + sum(a * math.sin(2 * j * xi) * math.cosh(2 * j * eta) for j, a in enumerate(_ALPHA, 1)) + easting = eta + sum(a * math.cos(2 * j * xi) * math.sinh(2 * j * eta) for j, a in enumerate(_ALPHA, 1)) + return (UTM_FALSE_EASTING + UTM_SCALE * _RADIUS * easting, + (UTM_FALSE_NORTHING_SOUTH if south else 0) + UTM_SCALE * _RADIUS * northing) + + +def from_utm(easting: float, northing: float, zone: int, south: bool = False) -> Tuple[float, float]: + """The latitude and longitude (degrees of WGS 84) of a place in a UTM zone.""" + xi = (northing - (UTM_FALSE_NORTHING_SOUTH if south else 0)) / (UTM_SCALE * _RADIUS) + eta = (easting - UTM_FALSE_EASTING) / (UTM_SCALE * _RADIUS) + xi_ = xi - sum(b * math.sin(2 * j * xi) * math.cosh(2 * j * eta) for j, b in enumerate(_BETA, 1)) + eta_ = eta - sum(b * math.cos(2 * j * xi) * math.sinh(2 * j * eta) for j, b in enumerate(_BETA, 1)) + chi = math.asin(math.sin(xi_) / math.cosh(eta_)) + phi = chi + sum(d * math.sin(2 * j * chi) for j, d in enumerate(_DELTA, 1)) + lam = _central_meridian(zone) + math.atan2(math.sinh(eta_), math.cos(xi_)) + return math.degrees(phi), math.degrees(lam) + + +def format_angle(degrees: float, positive: str = '', negative: str = '', short: bool = False) -> str: + """An angle in degrees, minutes and seconds, as a sheet has it: 61°07'30". The seconds are left out when + there are none. `short` leaves the degrees out too (07'30"), as the ticks between a map's corners do. + + The angle is on the side its seconds are: an edge of a map a hair south of the equator is 0°00', and not + south of anything.""" + seconds = round(abs(degrees) * 3600) + d, m, s = seconds // 3600, seconds // 60 % 60, seconds % 60 + text = f"{m:02d}'" if short else f"{d}°{m:02d}'" + if s: + text += f'{s:02d}"' + return text + (negative if degrees < 0 and seconds else positive if seconds else '') + + +def graticule_interval(span: float) -> float: + """How far apart (degrees) the ticks of latitude and longitude are on a map `span` degrees across: the + largest spacing which divides it in three or more. A 7.5 minute map has a tick every 2.5 minutes.""" + for seconds in GRATICULE_INTERVALS: + if seconds * 3 <= round(span * 3600): + return seconds / 3600 + return GRATICULE_INTERVALS[-1] / 3600 + + +# A tick nearer an edge of the map than this part of the way to the next tick is that edge: the edges of the +# picture the browser draws are a hair outside the map it was asked for (a millionth of a degree). +TICK_CLEARANCE = 0.01 + + +def _multiples(interval: float, low: float, high: float) -> List[float]: + """The multiples of `interval` between `low` and `high`, and not at (or a hair from) either.""" + first = math.floor(low / interval) + values = [] + for i in range(first, first + 100_000): + value = i * interval + if value >= high - interval * TICK_CLEARANCE: + break + if value > low + interval * TICK_CLEARANCE: + values.append(value) + return values + + +def _latin(text: str) -> str: + """Text as the sheet's font can write it.""" + for old, new in (('′', "'"), ('″', '"'), ('’', "'"), ('‘', "'"), ('“', '"'), ('”', '"'), ('–', '-'), + ('—', '-'), (' ', ' ')): + text = text.replace(old, new) + text = ' '.join(text.split()) + return text.encode('latin-1', 'replace').decode('latin-1') + + +def _text_width(text: str, size: float, font: str = FONT) -> float: + # Not imported at module level: see build_layered_pdf. + import pymupdf + return pymupdf.get_text_length(text, fontname=font, fontsize=size) + + +def _wrap(text: str, width: float, size: float) -> List[str]: + """The lines of `text`, none wider than `width`.""" + lines, line = [], '' + for word in text.split(): + longer = f'{line} {word}' if line else word + if line and _text_width(longer, size) > width: + lines.append(line) + line = word + else: + line = longer + return lines + [line] if line else lines + + +def _grid_convergence(latitude: float, longitude: float, zone: int, south: bool) -> float: + """How far east of true north (degrees) the grid's north is at a place.""" + easting, northing = to_utm(latitude, longitude, zone, south) + lat, lon = from_utm(easting, northing + 1000, zone, south) + return math.degrees(math.atan2(math.radians(lon - longitude) * math.cos(math.radians(latitude)), + math.radians(lat - latitude))) + + +def _east_or_west(angle: float) -> str: + """An angle east of north as it is said: 2°34' west. No north is known to better than a minute.""" + return f'{format_angle(_to_the_minute(abs(angle)))} {"west" if angle < 0 else "east"}' + + +def mils(angle: float) -> int: + """An angle (degrees) in the mils a compass may be marked in.""" + return round(abs(angle) * MILS_PER_DEGREE) + + +def _mils(angle: float) -> str: + """An angle in mils, as it is said: "46 mils", and "1 mil".""" + count = mils(angle) + return f'{count} mil' if count == 1 else f'{count} mils' + + +def _north_of_true(angle: float) -> str: + """How far a north is from true north, as it is said: 2°34' west (46 mils).""" + return f'{_east_or_west(angle)} ({_mils(angle)})' + + +def adjoining_quads(bounds: Sequence[float]) -> Optional[List[Optional[str]]]: + """What the USGS calls the quadrangle on each of the eight sides of a map, in the order of ADJOINING. None + when it has a name for none of them: most of the world. + + A quadrangle of the USGS may be larger than the map (in Alaska they are wider), so the area the size of the + map beside it may be more of the quadrangle the map is in. The quadrangle on that side is the next one + which is not: a sheet is never beside itself.""" + west, south, east, north = bounds + width, height = east - west, north - south + own = quads.quad_name(west, south, east, north) + names = [] + for columns, rows in ADJOINING: + name = None + for step in range(1, ADJOINING_STEPS + 1): + name = quads.quad_name(west + step * columns * width, south + step * rows * height, + east + step * columns * width, north + step * rows * height) + if name is None or own is None or (name.name, name.state) != (own.name, own.state): + break + else: + name = None + names.append(_latin(name.name) if name else None) + return names if any(names) else None + + +def _number(value, least: float, most: float) -> Optional[float]: + """A number which was sent, kept between two others; None when it is not a number.""" + if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value): + return None + return min(most, max(least, float(value))) + + +def _color(value) -> Optional[Tuple[float, float, float]]: + """A color which was sent (red, green and blue, each from 0 to 1); None when it is not one.""" + if not isinstance(value, (list, tuple)) or len(value) != 3: + return None + parts = tuple(_number(i, 0, 1) for i in value) + return None if None in parts else parts + + +def legend_lines(legend) -> Tuple[Tuple[float, float, float], List[LegendLine]]: + """The color of the land, and the lines, of the legend which a browser sent (see `Sheet.legend`). What is + not a line (it has no name, no color or no width) is left out, and a line is drawn no wider than a legend + has room for.""" + if not isinstance(legend, dict): + return WHITE, [] + lines = [] + raw = legend.get('lines') + for line in (raw if isinstance(raw, list) else [])[:LEGEND_LINES]: + if not isinstance(line, dict): + continue + label = _latin(str(line.get('label') or '')).strip()[:LEGEND_LABEL] + color, width = _color(line.get('color')), _number(line.get('width'), 0, LEGEND_WIDTHS[1]) + if not label or color is None or width is None or width < LEGEND_WIDTHS[0]: + continue + dash = line.get('dash') + dash = [_number(i, 0.1, LEGEND_SAMPLE) for i in (dash if isinstance(dash, (list, tuple)) else [])[:LEGEND_DASHES]] + opacity = _number(line.get('opacity'), 0.05, 1) + casing_color, casing_width = _color(line.get('casing_color')), _number(line.get('casing_width'), 0, 8) + lines.append(LegendLine( + label=label, color=color, width=width, opacity=1 if opacity is None else opacity, + dash=() if None in dash else tuple(dash), + casing_color=casing_color if casing_width else None, + casing_width=casing_width if casing_color and casing_width else 0)) + return _color(legend.get('ground')) or WHITE, lines + + +def legend_as_sent(legend) -> Optional[dict]: + """The legend which a browser sent with only what `legend_lines` makes of it, as it is sent; None when it + has no lines.""" + ground, lines = legend_lines(legend) + return dict(ground=list(ground), lines=[dataclasses.asdict(line) for line in lines]) if lines else None + + +def _legend_columns(plan: 'SheetPlan') -> int: + """How many lines of the legend go side by side under a map as wide as this one.""" + return max(1, int((plan.map_rect[2] - plan.map_rect[0]) // LEGEND_ITEM_WIDTH)) + + +def _legend_height(plan: 'SheetPlan') -> float: + """The room the legend takes under the map: nothing, when it has no lines.""" + if not plan.legend: + return 0 + rows = math.ceil(len(plan.legend) / _legend_columns(plan)) + return LABEL_SIZE + 8 + rows * LEGEND_ITEM_HEIGHT + 8 + + +def _contours(sheet: 'Sheet') -> Optional[str]: + """How far apart the contour lines are, as it is written: CONTOUR INTERVAL 40 FEET.""" + interval, unit = sheet.contour_interval, CONTOUR_UNITS.get(sheet.contour_unit) + if not unit or not isinstance(interval, (int, float)) or not math.isfinite(interval) or interval <= 0: + return None + return f'CONTOUR INTERVAL {interval:,.10g} {unit}' + + +def _to_the_minute(angle: float) -> float: + return round(angle * 60) / 60 + + +def _decimal_year(date: Optional[str]) -> Optional[float]: + """A day (2026-11-15) as a year and a part of one, or None when it is not a day.""" + try: + day = datetime.date.fromisoformat(str(date).strip()[:10]) + first = datetime.date(day.year, 1, 1) + return day.year + (day - first).days / (datetime.date(day.year + 1, 1, 1) - first).days + except ValueError: + # Not a day, or one with no year after it. + return None + + +def _format_scale(scale: float) -> str: + """1:24,000. A map's scale is not known closer than three figures.""" + digits = max(0, math.floor(math.log10(scale)) - 2) + return f'1:{round(scale / 10 ** digits) * 10 ** digits:,}' + + +def _grid_meters(plan: 'SheetPlan') -> int: + """How far apart the lines of the map's grid are.""" + (low_e, high_e), (low_n, high_n) = _utm_extent(plan) + meters = GRID_METERS + while (high_e - low_e) / meters > GRID_MAX_LINES or (high_n - low_n) / meters > GRID_MAX_LINES: + meters *= 10 + return meters + + +def _utm_extent(plan: 'SheetPlan') -> Tuple[Tuple[float, float], Tuple[float, float]]: + """The least and greatest easting, and northing, on the map.""" + west, south, east, north = plan.bounds + places = [to_utm(lat, lon, plan.zone, plan.south_hemisphere) + for lat in (south, (south + north) / 2, north) for lon in (west, (west + east) / 2, east)] + eastings, northings = [i[0] for i in places], [i[1] for i in places] + return (min(eastings), max(eastings)), (min(northings), max(northings)) + + +def plan_sheet(map_width: float, map_height: float, bounds: Sequence[float], sheet: Sheet) -> SheetPlan: + """Where a map of `map_width` by `map_height` points, between `bounds` (west, south, east, north), goes on a + sheet, and what is written under it.""" + west, south, east, north = bounds + plan = SheetPlan(width=0, height=0, map_rect=(0, 0, 0, 0), bounds=(west, south, east, north), + scale=map_scale(map_width, bounds), name=_latin(sheet.name) if sheet.name else None) + middle = _middle_latitude(south, north), (west + east) / 2 + # By its middle: the edges of a map are a hair outside it, and a map which ends where the zones do has a grid. + if UTM_SOUTH <= middle[0] <= UTM_NORTH: + plan.zone = utm_zone(middle[1]) + + notes = [f'Scale {_format_scale(plan.scale)} at the middle of the map. Projection: Web Mercator.' + f' Datum: WGS 84. True north is up the page.'] + if plan.zone: + plan.grid_north = _grid_convergence(*middle, plan.zone, plan.south_hemisphere) + notes.append( + f'{_grid_meters(plan):,} meter grid: Universal Transverse Mercator, zone {plan.zone}' + f'{"S" if plan.south_hemisphere else "N"}. Grid north is {_north_of_true(plan.grid_north)} of true' + f' north at the middle of the map.') + year = _decimal_year(sheet.date) + plan.magnetic_north = magnetic.declination(*middle, year) if year else None + if plan.magnetic_north is not None: + model = magnetic.load_model() + # The shorter way around: a compass which passes due south goes from 180 west to 180 east. + yearly = (model.declination(*middle, year + 1) - plan.magnetic_north + 180) % 360 - 180 + notes.append( + f'Magnetic north is {_north_of_true(plan.magnetic_north)} of true north at the middle of the map on' + f' {_latin(sheet.date)}, and moves about {_east_or_west(yearly)} each year ({model.name}).') + names = [_latin(str(i)) for i in sheet.credits if str(i).strip()] + if names: + notes.append('Map data: ' + '; '.join(names)) + notes.append(f'Made with WROLPi, {_latin(sheet.date)}.' if sheet.date else 'Made with WROLPi.') + for note in notes: + plan.notes.extend(_wrap(note, map_width, NOTE_SIZE)) + plan.contours = _contours(sheet) + if map_width >= ADJOINING_MIN_WIDTH: + plan.adjoining = adjoining_quads(bounds) + + plan.legend_ground, plan.legend = legend_lines(sheet.legend) + + plan.width = map_width + 2 * MARGIN + plan.map_rect = (MARGIN, TOP_MARGIN, MARGIN + map_width, TOP_MARGIN + map_height) + plan.height = TOP_MARGIN + map_height + BARS_OFFSET + BAR_COUNT * BAR_SPACING \ + + (CONTOUR_LEADING if plan.contours else 0) + (ADJOINING_HEIGHT if plan.adjoining else 0) \ + + _legend_height(plan) + len(plan.notes) * NOTE_LEADING + BOTTOM_PADDING + return plan + + +def _clip(points: Sequence[Tuple[float, float]], rect: Sequence[float]) -> List[List[Tuple[float, float]]]: + """The parts of a line (through `points`) which are in a rectangle.""" + x0, y0, x1, y1 = rect + pieces, piece = [], [] + for (ax, ay), (bx, by) in zip(points, points[1:]): + # Liang-Barsky: the part of the way from a to b which is inside. + start, end = 0.0, 1.0 + dx, dy = bx - ax, by - ay + inside = True + for p, q in ((-dx, ax - x0), (dx, x1 - ax), (-dy, ay - y0), (dy, y1 - ay)): + if p == 0: + inside = inside and q >= 0 + elif p < 0: + start = max(start, q / p) + else: + end = min(end, q / p) + if not inside or start > end: + if piece: + pieces.append(piece) + piece = [] + continue + a, b = (ax + start * dx, ay + start * dy), (ax + end * dx, ay + end * dy) + if not piece or start > 0: + if piece: + pieces.append(piece) + piece = [a] + piece.append(b) + if end < 1: + pieces.append(piece) + piece = [] + if piece: + pieces.append(piece) + return [i for i in pieces if len(i) > 1] + + +class _Pen: + """Draws the lines and text of one PDF layer.""" + + def __init__(self, page, group: int): + self.shape = page.new_shape() + self.group = group + + def line(self, points: Sequence[Tuple[float, float]], width: float = 0.5, color=BLACK, opacity: float = 1, + dash: Sequence[float] = ()): + self.shape.draw_polyline(list(points)) + dashes = f'[{" ".join(f"{i:.2f}" for i in dash)}] 0' if dash else None + self.shape.finish(width=width, color=color, closePath=False, stroke_opacity=opacity, dashes=dashes, + lineCap=0, oc=self.group) + + def box(self, rect: Sequence[float], fill=None, width: float = 0.5, color=BLACK): + self.shape.draw_rect(rect) + self.shape.finish(width=width, color=color, fill=fill, oc=self.group) + + def polygon(self, points: Sequence[Tuple[float, float]], fill=BLACK): + self.shape.draw_polyline(list(points)) + self.shape.finish(width=0.3, color=BLACK, fill=fill, closePath=True, oc=self.group) + + def text(self, x: float, y: float, text: str, size: float = LABEL_SIZE, align: str = 'left', font: str = FONT, + color=BLACK): + """Write `text` with its baseline at `y`: starting at, ending at, or centered on `x`.""" + if align != 'left': + width = _text_width(text, size, font) + x -= width if align == 'right' else width / 2 + self.shape.insert_text((x, y), text, fontsize=size, fontname=font, color=color, oc=self.group) + + def commit(self): + self.shape.commit() + + +def _draw_graticule(pen: _Pen, plan: SheetPlan): + """The edge of the map, the coordinates of its corners, and ticks of latitude and longitude between them.""" + x0, y0, x1, y1 = plan.map_rect + west, south, east, north = plan.bounds + pen.box(plan.map_rect, width=0.75) + + above, below, beside = y0 - 4, y1 + LABEL_SIZE + 3, LABEL_SIZE * 0.35 + # The corners. + for longitude, x, align in ((west, x0, 'left'), (east, x1, 'right')): + label = format_angle(longitude, 'E', 'W') + pen.text(x, above, label, align=align) + pen.text(x, below, label, align=align) + for latitude, y in ((north, y0), (south, y1)): + label = format_angle(latitude, 'N', 'S') + pen.text(x0 - 3, y + beside, label, align='right') + pen.text(x1 + 3, y + beside, label) + + longitudes = _multiples(graticule_interval(east - west), west, east) + latitudes = _multiples(graticule_interval(north - south), south, north) + for longitude in longitudes: + x = plan.point(north, longitude)[0] + pen.line([(x, y0), (x, y0 + TICK_LENGTH)]) + pen.line([(x, y1), (x, y1 - TICK_LENGTH)]) + label = format_angle(longitude, short=True) + pen.text(x, above, label, align='center') + pen.text(x, below, label, align='center') + for latitude in latitudes: + y = plan.point(latitude, west)[1] + pen.line([(x0, y), (x0 + TICK_LENGTH, y)]) + pen.line([(x1, y), (x1 - TICK_LENGTH, y)]) + label = format_angle(latitude, short=True) + pen.text(x0 - 3, y + beside, label, align='right') + pen.text(x1 + 3, y + beside, label) + # A cross where a tick of latitude meets one of longitude. + for latitude in latitudes: + for longitude in longitudes: + x, y = plan.point(latitude, longitude) + pen.line([(x - CROSS_LENGTH, y), (x + CROSS_LENGTH, y)]) + pen.line([(x, y - CROSS_LENGTH), (x, y + CROSS_LENGTH)]) + + +def _graticule_labels(plan: SheetPlan) -> Tuple[List[Tuple[float, float]], List[Tuple[float, float]]]: + """Where along the top and bottom (x), and the sides (y), the margin is taken by a coordinate: each a + (low, high) the grid's labels keep out of.""" + x0, y0, x1, y1 = plan.map_rect + west, south, east, north = plan.bounds + corner = _text_width(format_angle(-179.875, 'E', 'W'), LABEL_SIZE) + 4 + tick = _text_width(format_angle(0.125, short=True), LABEL_SIZE) / 2 + 3 + across = [(x0 - 1, x0 + corner), (x1 - corner, x1 + 1)] + for longitude in _multiples(graticule_interval(east - west), west, east): + x = plan.point(north, longitude)[0] + across.append((x - tick, x + tick)) + up = [(y0 - LABEL_SIZE, y0 + LABEL_SIZE), (y1 - LABEL_SIZE, y1 + LABEL_SIZE)] + for latitude in _multiples(graticule_interval(north - south), south, north): + y = plan.point(latitude, west)[1] + up.append((y - LABEL_SIZE, y + LABEL_SIZE)) + return across, up + + +def _draw_grid(pen: _Pen, plan: SheetPlan): + """The UTM grid over the map, and what each of its lines is in the margin.""" + x0, y0, x1, y1 = plan.map_rect + zone, south = plan.zone, plan.south_hemisphere + meters = _grid_meters(plan) + (low_e, high_e), (low_n, high_n) = _utm_extent(plan) + taken_across, taken_up = _graticule_labels(plan) + beside = LABEL_SIZE * 0.35 + # A line of the grid is not quite straight on the map, so it is drawn through this many places. + steps = 24 + + def free(position: float, half: float, taken: List[Tuple[float, float]]) -> bool: + return all(position + half < low or position - half > high for low, high in taken) + + # On each edge of the map the first line which is named is named in full (339000mE), and the others by their + # kilometers (340). + named = set() + + def label(value: int, edge: str, letter: str) -> str: + return f'{value // 1000}' if edge in named else f'{value}m{letter}' + + for easting in range(math.ceil(low_e / meters) * meters, math.floor(high_e / meters) * meters + 1, meters): + northings = [low_n - meters + i * (high_n - low_n + 2 * meters) / steps for i in range(steps + 1)] + points = [plan.point(*from_utm(easting, northing, zone, south)) for northing in northings] + for piece in _clip(points, plan.map_rect): + pen.line(piece, width=0.4, color=GRID_COLOR, opacity=0.7) + for x, y in (piece[0], piece[-1]): + if abs(y - y0) > 0.01 and abs(y - y1) > 0.01: + continue + edge = 'top' if abs(y - y0) <= 0.01 else 'bottom' + text = label(easting, edge, 'E') + if not free(x, _text_width(text, LABEL_SIZE) / 2 + 2, taken_across): + continue + pen.text(x, y0 - 4 if edge == 'top' else y1 + LABEL_SIZE + 3, text, align='center', + color=GRID_COLOR) + named.add(edge) + + for northing in range(math.ceil(low_n / meters) * meters, math.floor(high_n / meters) * meters + 1, meters): + eastings = [low_e - meters + i * (high_e - low_e + 2 * meters) / steps for i in range(steps + 1)] + points = [plan.point(*from_utm(easting, northing, zone, south)) for easting in eastings] + for piece in _clip(points, plan.map_rect): + pen.line(piece, width=0.4, color=GRID_COLOR, opacity=0.7) + for x, y in (piece[0], piece[-1]): + if abs(x - x0) > 0.01 and abs(x - x1) > 0.01 or not free(y, LABEL_SIZE / 2, taken_up): + continue + edge = 'left' if abs(x - x0) <= 0.01 else 'right' + text = label(northing, edge, 'N') + if edge == 'left': + pen.text(x0 - 3, y + beside, text, align='right', color=GRID_COLOR) + else: + pen.text(x1 + 3, y + beside, text, color=GRID_COLOR) + named.add(edge) + + +def _draw_scale_bar(pen: _Pen, center: float, y: float, unit: float, name: str, most: int, parts: int, + available: float, number=str) -> float: + """A bar which is `unit` points for each unit of distance, with its top at `y`. It has one unit left of its + zero, divided in `parts`, and as many to the right as there is room for, up to `most`. Returns how long the + bar is: nothing, when there is no room for it.""" + count = min(most, int(available // unit) - 1) + if count < 1: + return 0 + left = center - (count + 1) * unit / 2 + zero = left + unit + for i in range(parts): + fill = BLACK if i % 2 == 0 else None + pen.box((left + i * unit / parts, y, left + (i + 1) * unit / parts, y + BAR_HEIGHT), fill=fill, width=0.4) + for i in range(count): + fill = BLACK if i % 2 == 0 else None + pen.box((zero + i * unit, y, zero + (i + 1) * unit, y + BAR_HEIGHT), fill=fill, width=0.4) + pen.text(left, y - 2.5, number(1), align='center') + for i in range(count + 1): + pen.text(zero + i * unit, y - 2.5, number(i), align='center') + pen.text(zero + count * unit + 12, y + BAR_HEIGHT, name) + return (count + 1) * unit + + +# The diagram of the norths: how long the line to each is (points), and the least and the most it is drawn from +# straight up (degrees). The diagram shows which side each north is on, not how far: the angles are written. +NORTH_LENGTHS = dict(true=62, magnetic=50, grid=38) +NORTH_LEAST, NORTH_MOST = 7, 30 +# How wide the diagram is, with what is written beside its lines. +NORTHS_WIDTH = 100 + + +def _north_tip(x: float, y: float, angle: float, length: float) -> Tuple[float, float]: + """Where the line to a north ends: drawn to its side of straight up, but never so near it, or so far from it, + that it can not be told apart.""" + shown = math.radians(math.copysign(min(NORTH_MOST, max(NORTH_LEAST, abs(angle))), angle)) + return x + length * math.sin(shown), y - length * math.cos(shown) + + +def _draw_norths(pen: _Pen, plan: SheetPlan, x: float, y: float): + """Which way true north, the grid's north and a compass's north are from each other, from a point at + (`x`, `y`), with how far each is from true north written under it.""" + top = y - NORTH_LENGTHS['true'] + pen.line([(x, y), (x, top)], width=0.6) + # True north is at a star. + star = [(x + r * math.sin(math.radians(36 * i)), top - 5 - r * math.cos(math.radians(36 * i))) + for i, r in zip(range(10), (5, 2) * 5)] + pen.polygon(star) + below = y + LABEL_SIZE + 4 + for name, angle, length in (('MN', plan.magnetic_north, NORTH_LENGTHS['magnetic']), + ('GN', plan.grid_north, NORTH_LENGTHS['grid'])): + if angle is None: + continue + tip_x, tip_y = _north_tip(x, y, angle, length) + pen.line([(x, y), (tip_x, tip_y)], width=0.6) + east = angle >= 0 + pen.text(tip_x + (3 if east else -3), tip_y + 2, name, align='left' if east else 'right') + pen.text(x, below, f'{name} {format_angle(_to_the_minute(abs(angle)))} {"E" if east else "W"}' + f' {_mils(angle).upper()}', align='center') + below += LABEL_SIZE + 2 + + +def _draw_adjoining(pen: _Pen, plan: SheetPlan, x: float, y: float): + """The quadrangles around the map: nine squares with the map the middle one, each of the others with a + number, and beside them what each number is called. Its top left is at (`x`, `y`).""" + pen.text(x, y + LABEL_SIZE, 'ADJOINING QUADRANGLES') + top = y + LABEL_SIZE + 5 + side = ADJOINING_SQUARE + pen.box((x + side, top + side, x + 2 * side, top + 2 * side), fill=ADJOINING_GRAY, width=0.4) + for i in range(4): + pen.line([(x + i * side, top), (x + i * side, top + 3 * side)], width=0.4) + pen.line([(x, top + i * side), (x + 3 * side, top + i * side)], width=0.4) + # The names are in two columns: as wide as a long name, or as the room beside the squares allows. + left = x + 3 * side + 12 + column = min(ADJOINING_COLUMN, (plan.map_rect[2] - left) / 2) + listed = 0 + for number, ((columns, rows), name) in enumerate(zip(ADJOINING, plan.adjoining), start=1): + if not name: + continue + pen.text(x + (columns + 1.5) * side, top + (1.5 - rows) * side + LABEL_SIZE / 3, str(number), align='center') + while _text_width(f'{number} {name}', LABEL_SIZE) > column - 6 and len(name) > 1: + name = name[:-1] + pen.text(left + (listed // 4) * column, top + LABEL_SIZE + (listed % 4) * (LABEL_SIZE + 2.5), + f'{number} {name}') + listed += 1 + + +def _draw_legend(pen: _Pen, plan: SheetPlan, x: float, y: float): + """What the lines of the map are: a piece of each as the map draws it, on the color of the map's land, with + what it is called beside it. Its top left is at (`x`, `y`).""" + pen.text(x, y + LABEL_SIZE, 'LEGEND') + top = y + LABEL_SIZE + 8 + columns = _legend_columns(plan) + for index, line in enumerate(plan.legend): + left = x + (index % columns) * LEGEND_ITEM_WIDTH + row = top + (index // columns) * LEGEND_ITEM_HEIGHT + box = (left, row, left + LEGEND_SAMPLE, row + LEGEND_ITEM_HEIGHT - 3) + pen.box(box, fill=plan.legend_ground, width=0.3, color=GRAY) + middle = (box[1] + box[3]) / 2 + ends = [(left + 1, middle), (left + LEGEND_SAMPLE - 1, middle)] + if line.casing_color and line.casing_width > line.width: + pen.line(ends, width=line.casing_width, color=line.casing_color) + pen.line(ends, width=line.width, color=line.color, opacity=line.opacity, dash=line.dash) + # A name longer than the room beside its line is cut to it. + label = line.label + while _text_width(label, LABEL_SIZE) > LEGEND_ITEM_WIDTH - LEGEND_SAMPLE - 10 and len(label) > 1: + label = label[:-1] + pen.text(left + LEGEND_SAMPLE + 5, middle + LABEL_SIZE / 3, label) + + +def _draw_margin(pen: _Pen, plan: SheetPlan): + """The title above the map, and the scale and the notes below it.""" + x0, y0, x1, y1 = plan.map_rect + scale = _format_scale(plan.scale) + title_y = y0 - 24 + if not plan.name: + pen.text(x1, title_y, scale, size=TITLE_SIZE, font=BOLD, align='right') + elif _text_width(plan.name, TITLE_SIZE, BOLD) + _text_width(scale, TITLE_SIZE, BOLD) + TITLE_GAP <= x1 - x0: + pen.text(x0, title_y, plan.name, size=TITLE_SIZE, font=BOLD) + pen.text(x1, title_y, scale, size=TITLE_SIZE, font=BOLD, align='right') + else: + # A map too narrow for both (a quadrangle near a pole is two inches wide) has the scale under the name. + pen.text(x0, title_y - TITLE_LEADING, plan.name, size=TITLE_SIZE, font=BOLD) + pen.text(x0, title_y, scale, size=TITLE_SIZE, font=BOLD) + + center = (x0 + x1) / 2 + y = y1 + BARS_OFFSET + pen.text(center, y - 16, f'SCALE {scale}', size=NOTE_SIZE + 1, align='center') + # The room a bar has: the map's width, less the name of its unit on each side (so it stays centered). + available = (x1 - x0) - 2 * (_text_width('KILOMETERS', LABEL_SIZE) + 14) + meter = 1 / (plan.scale * METERS_PER_POINT) + bars = [ + _draw_scale_bar(pen, center, y, 1000 * meter, 'KILOMETERS', 2, 10, available), + _draw_scale_bar(pen, center, y + BAR_SPACING, 1000 * meter, 'METERS', 2, 2, available, + number=lambda i: str(i * 1000)), + _draw_scale_bar(pen, center, y + 2 * BAR_SPACING, METERS_PER_MILE * meter, 'MILES', 1, 4, available), + _draw_scale_bar(pen, center, y + 3 * BAR_SPACING, 1000 * METERS_PER_FOOT * meter, 'FEET', 7, 2, available, + number=lambda i: str(i * 1000)), + ] + # The norths are left of the bars, where there is room for them. + if (plan.grid_north is not None or plan.magnetic_north is not None) \ + and center - max(bars) / 2 - x0 >= NORTHS_WIDTH + 10: + _draw_norths(pen, plan, x0 + NORTHS_WIDTH / 2, y + (BAR_COUNT - 1) * BAR_SPACING - 12) + + y += BAR_COUNT * BAR_SPACING + if plan.contours: + pen.text(center, y - 4, plan.contours, size=NOTE_SIZE + 1, align='center') + y += CONTOUR_LEADING + if plan.adjoining: + _draw_adjoining(pen, plan, x0, y - NOTE_LEADING) + y += ADJOINING_HEIGHT + if plan.legend: + _draw_legend(pen, plan, x0, y - NOTE_LEADING) + y += _legend_height(plan) + for index, note in enumerate(plan.notes): + pen.text(x0, y + index * NOTE_LEADING, note, size=NOTE_SIZE) + + +def draw_sheet(doc, page, plan: SheetPlan): + """Draw the sheet on the page of a map: the grid over the map, and everything around it.""" + if plan.zone: + grid = _Pen(page, doc.add_ocg(GRID_LAYER, on=True)) + _draw_grid(grid, plan) + grid.commit() + pen = _Pen(page, doc.add_ocg(SHEET_LAYER, on=True)) + _draw_graticule(pen, plan) + _draw_margin(pen, plan) + pen.commit() diff --git a/modules/map/static/index.html b/modules/map/static/index.html index e9125804..e0a5a098 100644 --- a/modules/map/static/index.html +++ b/modules/map/static/index.html @@ -294,6 +294,26 @@ style: function(file) { return dataPath(file.style_map_path); }, }; + // How a terrain file is read (js/terrain.js, shared with the app's map viewer). Until it is + // loaded, and if it can not be, a terrain file is read as the one WROLPi offers is. + var Terrain = { + terrainMaxZoom: function() { return 8; }, + contourThresholds: function(imperial) { + return imperial + ? {4: [1000, 5000], 5: [500, 2500], 6: [200, 1000], 7: [200, 1000], 8: [100, 500]} + : {4: [500, 2500], 5: [200, 1000], 6: [100, 500], 7: [100, 500], 8: [50, 200]}; + }, + }; + var terrainFile = null; + var terrainZoom = 8; + + function loadTerrain() { + if (!hasTerrain) return Promise.resolve(); + return import("/js/terrain.js") + .then(function(module) { Terrain = module; }) + .catch(function(err) { console.error("Deeper terrain is not available:", err); }); + } + function loadOverlays(files) { if (!files.length) return Promise.resolve(); return Promise.all([import("/js/overlays.js"), import("/js/overlayStyle.js")]) @@ -409,6 +429,11 @@ } } + // The labels of every basemap file are over the ground of every file (as labelsOverGround in + // js/overlays.js, which may not have been loaded yet). + allLayers = allLayers.filter(function(l) { return l.type !== "symbol"; }) + .concat(allLayers.filter(function(l) { return l.type === "symbol"; })); + // Terrain (hillshade + contours). if (hasTerrain && demSource) { var savedVis = getSavedVisibility(); @@ -444,13 +469,10 @@ elevationKey: "ele", levelKey: "level", multiplier: isImperial ? 3.28084 : 1, - thresholds: isImperial ? { - 4: [1000, 5000], 5: [500, 2500], 6: [200, 1000], 7: [200, 1000], 8: [100, 500], - } : { - 4: [500, 2500], 5: [200, 1000], 6: [100, 500], 7: [100, 500], 8: [50, 200], - }, + // As fine as the terrain file can bear (js/terrain.js). + thresholds: Terrain.contourThresholds(isImperial, terrainZoom), }); - sources["contours"] = { type: "vector", tiles: [contourUrl], maxzoom: 8 }; + sources["contours"] = { type: "vector", tiles: [contourUrl], maxzoom: terrainZoom }; var textColor = flavor === "dark" ? "rgba(210,180,140,0.9)" : "rgba(150,100,50,0.8)"; var haloColor = flavor === "dark" ? "rgba(0,0,0,0.8)" : "rgba(255,255,255,0.8)"; @@ -954,6 +976,7 @@ : (f.name.startsWith(TERRAIN_PREFIX) && f.name.endsWith('.pmtiles')); if (isTerrain) { terrainFileName = f.mapPath; + terrainFile = f; hasTerrain = true; } else if (f.kind === "overlay") { // Not the basemap schema: offered in the layer panel, drawn as itself. @@ -986,16 +1009,18 @@ .then(function(r) { hasPlanetOverview = r.ok; }) .catch(function() { hasPlanetOverview = false; }) .then(function() { return loadOverlays(overlayCandidates); }) + .then(function() { return loadTerrain(); }) .then(function() { return pmtilesFiles; }); }) .then(function(pmtilesFiles) { // Initialize contour source if terrain available. if (hasTerrain && !demSource) { + terrainZoom = Terrain.terrainMaxZoom(terrainFile); demSource = new mlcontour.DemSource({ url: "pmtiles://" + window.location.origin + dataPath(terrainFileName), encoding: "terrarium", - maxzoom: 8, + maxzoom: terrainZoom, worker: true, }); demSource.setupMaplibre(maplibregl); diff --git a/modules/map/static/js/overlays.js b/modules/map/static/js/overlays.js index 05b47006..640a740d 100644 --- a/modules/map/static/js/overlays.js +++ b/modules/map/static/js/overlays.js @@ -252,10 +252,17 @@ export function buildOverlayLayers(file, state) { return styleLayers; } -// The style layers of every overlay, in the order they are drawn. A raster overlay goes under the -// basemap's labels (`rasters`) so the map stays readable; vector overlays go on top (`vectors`): an -// overlay with its own style in that style's order, then the generic ones, fills under lines under -// points. +// Whether a layer of an overlay is of its labels: its text, its icons, and the points it marks. The +// rest of an overlay is its ground: areas, lines, pictures. +export function isOverlayLabelLayer(layer) { + return layer?.type === "symbol" || layer?.type === "circle"; +} + +// The style layers of every overlay, in the order they are drawn. Labels are always on top: the +// ground of every overlay goes under the basemap's labels (`under`), so an area an overlay fills +// does not cover the name of what is in it, and the labels of every overlay go over them (`over`). +// In each, a raster is lowest, then an overlay with its own style in that style's order, then the +// generic ones, fills under lines under points. export function orderOverlayLayers(overlays, state) { const rasters = []; const styled = []; @@ -271,7 +278,49 @@ export function orderOverlayLayers(overlays, state) { else generic[layer.type].push(layer); } } - return {rasters, vectors: [...styled, ...generic.fill, ...generic.line, ...generic.circle]}; + const vectors = [...styled, ...generic.fill, ...generic.line, ...generic.circle]; + return { + under: [...rasters, ...vectors.filter(layer => !isOverlayLabelLayer(layer))], + over: vectors.filter(isOverlayLabelLayer), + }; +} + +// The layers of a basemap with every label over all of its ground: the ground in the order it was +// in, then the labels in the order they were in. +// +// A map of several basemap files is their layers one file after another, so the labels of each file +// but the last are under the land, the water and the roads of the files after it, and under +// anything which is put under "the labels". Labels are always on top. +export function labelsOverGround(layers) { + layers = layers || []; + return [...layers.filter(l => l.type !== "symbol"), ...layers.filter(l => l.type === "symbol")]; +} + +// The source of the contour lines both viewers draw from the terrain. They are added to a style +// after the basemap's labels, lines and labels both, and are not of the basemap. +const CONTOUR_SOURCE = "contours"; + +// Where the basemap's labels begin: the index of the first of the labels which are over all of the +// basemap's ground. Where no label is over its ground, the index after the last of its ground. +// +// Not the map's first label: a map of several basemap files has the labels of the first file under +// the ground of the next, and an overlay put there would be under the land and the water of every +// file after the first. Nor before the last layer which is not a label: the contour lines are +// drawn after the basemap's labels, and an overlay put under them would be over every label. +export function basemapLabelsStart(layers) { + layers = layers || []; + const ofBasemap = layer => !isOverlayLayerId(layer.id) && layer.source !== CONTOUR_SOURCE; + let lastGround = -1; + for (let i = layers.length - 1; i >= 0; i--) { + if (ofBasemap(layers[i]) && layers[i].type !== "symbol") { + lastGround = i; + break; + } + } + for (let i = lastGround + 1; i < layers.length; i++) { + if (ofBasemap(layers[i]) && layers[i].type === "symbol") return i; + } + return lastGround + 1; } // Add the overlays to a style's sources and layers (the basemap's are already there). @@ -279,10 +328,9 @@ export function addOverlaysToStyle(styleSources, allLayers, overlays, state, url for (const file of overlays || []) { styleSources[overlaySourceId(file)] = buildOverlaySource(file, urls); } - const {rasters, vectors} = orderOverlayLayers(overlays, state); - const firstLabel = allLayers.findIndex(l => l.type === "symbol"); - allLayers.splice(firstLabel === -1 ? allLayers.length : firstLabel, 0, ...rasters); - allLayers.push(...vectors); + const {under, over} = orderOverlayLayers(overlays, state); + allLayers.splice(basemapLabelsStart(allLayers), 0, ...under); + allLayers.push(...over); } // Add the overlays to a map whose style has loaded, a layer at a time. For a viewer which cannot @@ -295,8 +343,9 @@ export function addOverlaysToMap(map, overlays, state, urls) { const id = overlaySourceId(file); if (!map.getSource(id)) map.addSource(id, buildOverlaySource(file, urls)); } - const firstLabel = (map.getStyle()?.layers || []).find(l => l.type === "symbol"); - const {rasters, vectors} = orderOverlayLayers(overlays, state); + const mapLayers = map.getStyle()?.layers || []; + const firstLabel = mapLayers[basemapLabelsStart(mapLayers)]; + const {under, over} = orderOverlayLayers(overlays, state); const add = (layer, before) => { if (map.getLayer(layer.id)) return; try { @@ -307,8 +356,8 @@ export function addOverlaysToMap(map, overlays, state, urls) { refused[mapPath] = (refused[mapPath] || 0) + 1; } }; - for (const layer of rasters) add(layer, firstLabel?.id); - for (const layer of vectors) add(layer, undefined); + for (const layer of under) add(layer, firstLabel?.id); + for (const layer of over) add(layer, undefined); return refused; } diff --git a/modules/map/static/js/terrain.js b/modules/map/static/js/terrain.js new file mode 100644 index 00000000..8cdc3715 --- /dev/null +++ b/modules/map/static/js/terrain.js @@ -0,0 +1,59 @@ +// How the map reads a terrain (elevation) file: how deep into it to look, and how far apart the +// contour lines are. Shared by the app's map viewer and the standalone viewer, which loads a copy +// of this file as it is: no imports from the app, no dependencies. + +// How deep the terrain file which WROLPi offers goes, and what a file is taken to be when it does +// not say. +export const DEFAULT_TERRAIN_MAX_ZOOM = 8; +// No terrain file is read deeper than this. +const DEEPEST_TERRAIN_ZOOM = 15; + +// How far apart the contour lines are at each zoom of the map: [minor, major], in the unit the map +// measures in. A zoom between two of these uses the one below it. +// +// A 1:24,000 sheet has a line every 40 feet (or 20 meters), which is what the deepest have: finer +// lines than that are more than the terrain says, and more than a hillside has room for. +const CONTOUR_FEET = { + 4: [1000, 5000], 5: [500, 2500], 6: [200, 1000], 7: [200, 1000], 8: [100, 500], + 9: [100, 500], 10: [100, 500], 11: [40, 200], 12: [40, 200], +}; +const CONTOUR_METERS = { + 4: [500, 2500], 5: [200, 1000], 6: [100, 500], 7: [100, 500], 8: [50, 200], + 9: [50, 200], 10: [50, 200], 11: [20, 100], 12: [20, 100], +}; + +// How deep a terrain file goes: the deepest zoom it has tiles of. `file` is as the API lists it. +export function terrainMaxZoom(file) { + const zoom = Number(file && file.max_zoom); + if (!Number.isInteger(zoom) || zoom < 1) return DEFAULT_TERRAIN_MAX_ZOOM; + return Math.min(zoom, DEEPEST_TERRAIN_ZOOM); +} + +// The contour intervals for a terrain file which goes as deep as `maxZoom`. +// +// Only those the file can bear: lines 40 feet apart drawn from terrain too coarse for them are +// drawn where no line is. A map zoomed in past the file's depth keeps the file's finest interval. +export function contourThresholds(imperial, maxZoom) { + const all = imperial ? CONTOUR_FEET : CONTOUR_METERS; + const deepest = Number.isFinite(maxZoom) ? maxZoom : DEFAULT_TERRAIN_MAX_ZOOM; + const lowest = Math.min(...Object.keys(all).map(Number)); + const thresholds = {}; + for (const [zoom, intervals] of Object.entries(all)) { + // A file shallower than every interval still has contours: the coarsest. + if (Number(zoom) <= deepest || Number(zoom) === lowest) thresholds[zoom] = intervals; + } + return thresholds; +} + +// How far apart the contour lines are on a map drawn at `zoom` from a terrain file which goes as +// deep as `maxZoom`: [minor, major], or null when no line is drawn at that zoom. +// +// The lines of a map are made a tile at a time: for tiles of the map's zoom, or of the file's +// deepest when the map is zoomed in past it, with the interval of the nearest zoom below. +export function contourInterval(imperial, maxZoom, zoom) { + const thresholds = contourThresholds(imperial, maxZoom); + const deepest = Number.isFinite(maxZoom) ? maxZoom : DEFAULT_TERRAIN_MAX_ZOOM; + const tileZoom = Math.min(Math.floor(zoom), deepest); + const zooms = Object.keys(thresholds).map(Number).filter(i => i <= tileZoom); + return zooms.length ? thresholds[Math.max(...zooms)] : null; +} diff --git a/modules/map/test/test_export.py b/modules/map/test/test_export.py new file mode 100644 index 00000000..a0e7ce3f --- /dev/null +++ b/modules/map/test/test_export.py @@ -0,0 +1,581 @@ +import io +import json +import math +import re +import subprocess +import sys +from http import HTTPStatus +from unittest import mock + +import pymupdf +import pytest +from PIL import Image + +from modules.map import export +from modules.map.export import ExportLayer, InvalidExport, build_layered_pdf +from wrolpi.jobs import wait_for_job + +WIDTH, HEIGHT = 60, 90 + + +def make_image(color=(200, 80, 20, 255), size=(WIDTH, HEIGHT), image_format='PNG', box=None) -> bytes: + """An image of a layer: transparent, with `color` in `box` (all of it by default).""" + image = Image.new('RGBA', size, (0, 0, 0, 0)) + image.paste(color, box or (0, 0, *size)) + if image_format == 'JPEG': + image = image.convert('RGB') + data = io.BytesIO() + image.save(data, image_format) + return data.getvalue() + + +BLANK = make_image(color=(0, 0, 0, 0)) + + +def layer_names(pdf: bytes) -> list: + doc = pymupdf.open(stream=pdf, filetype='pdf') + return [i['name'] for _, i in sorted(doc.get_ocgs().items())] + + +def test_layers_are_pdf_layers(): + """Each image of the map is in a PDF layer of its own, named and in the order given, on one page the size + the map is at its DPI.""" + pdf = build_layered_pdf([ + ExportLayer('Background', make_image((240, 240, 230, 255))), + ExportLayer('Water', make_image((60, 120, 220, 255), box=(0, 0, 30, 90))), + ExportLayer('Satellite', make_image(image_format='JPEG')), + ], WIDTH, HEIGHT, dpi=300, title='40°15′N 105°07′30″W') + + doc = pymupdf.open(stream=pdf, filetype='pdf') + assert doc.page_count == 1 + page = doc[0] + # 60x90 pixels at 300 DPI is 0.2x0.3 inches: 14.4x21.6 points. + assert (round(page.rect.width, 3), round(page.rect.height, 3)) == (14.4, 21.6) + assert layer_names(pdf) == ['Background', 'Water', 'Satellite'] + assert all(i['on'] for i in doc.get_ocgs().values()) + assert doc.metadata['title'] == '40°15′N 105°07′30″W' + + # Every image fills the page, and is the picture it was given. + images = page.get_image_info(xrefs=True) + assert len(images) == 3 + for image in images: + assert (image['width'], image['height']) == (WIDTH, HEIGHT) + assert pymupdf.Rect(image['bbox']) == page.rect + + # The page looks like the layers stacked: water over the background on the left... but the satellite image + # is on top of both, and is all one color. + pixmap = page.get_pixmap(dpi=300) + assert pixmap.pixel(5, 5)[:3] == pixmap.pixel(55, 85)[:3] + + +def test_layers_are_stacked_in_order(): + """The first layer is at the bottom: what is above it covers it, and shows it where it is transparent.""" + pdf = build_layered_pdf([ + ExportLayer('Background', make_image((255, 255, 255, 255))), + ExportLayer('Water', make_image((0, 0, 255, 255), box=(0, 0, 30, 90))), + ], WIDTH, HEIGHT, dpi=72) + pixmap = pymupdf.open(stream=pdf, filetype='pdf')[0].get_pixmap(dpi=72) + assert pixmap.pixel(10, 45) == (0, 0, 255) + assert pixmap.pixel(50, 45) == (255, 255, 255) + + +def test_hidden_layer_is_not_drawn(): + """A layer of the PDF can be turned off, which is the point of there being layers.""" + pdf = build_layered_pdf([ + ExportLayer('Background', make_image((255, 255, 255, 255))), + ExportLayer('Water', make_image((0, 0, 255, 255))), + ], WIDTH, HEIGHT, dpi=72) + doc = pymupdf.open(stream=pdf, filetype='pdf') + assert doc[0].get_pixmap(dpi=72).pixel(10, 45) == (0, 0, 255) + + # As a reader does it: clear the layer's box in the list of layers. + water, = [i['number'] for i in doc.layer_ui_configs() if i['text'] == 'Water'] + doc.set_layer_ui_config(water, action=2) + assert doc[0].get_pixmap(dpi=72).pixel(10, 45) == (255, 255, 255) + + +def test_blank_layer_is_left_out(): + """A layer in which nothing was drawn (no tunnels in this quadrangle) is not a layer of the PDF.""" + pdf = build_layered_pdf([ + ExportLayer('Background', make_image()), + ExportLayer('Roads - Tunnels', BLANK), + ExportLayer('Labels', make_image(box=(1, 1, 2, 2))), + ], WIDTH, HEIGHT, dpi=300) + assert layer_names(pdf) == ['Background', 'Labels'] + + with pytest.raises(InvalidExport, match='Nothing is drawn'): + build_layered_pdf([ExportLayer('Roads - Tunnels', BLANK)], WIDTH, HEIGHT, dpi=300) + + +@pytest.mark.parametrize('layers,width,height,dpi,error', [ + ([], WIDTH, HEIGHT, 300, 'no layers'), + ([ExportLayer('A', make_image())] * (export.MAX_LAYERS + 1), WIDTH, HEIGHT, 300, 'more than'), + # Not the size the map is said to be. + ([ExportLayer('A', make_image(size=(61, 90)))], WIDTH, HEIGHT, 300, '61x90'), + # Not an image, and not a kind of image the map is drawn as. + ([ExportLayer('A', b'not an image')], WIDTH, HEIGHT, 300, 'not a readable image'), + ([ExportLayer('A', make_image(image_format='GIF'))], WIDTH, HEIGHT, 300, 'not a PNG or JPEG'), + # Sizes no map is. + ([ExportLayer('A', make_image())], 0, HEIGHT, 300, '0x90'), + ([ExportLayer('A', make_image())], export.MAX_SIDE + 1, 10, 300, 'can not be exported'), + ([ExportLayer('A', make_image())], 10000, 10000, 300, 'can not be exported'), + # Each layer is a size a map is, but all of them together are more than there is memory for. + ([ExportLayer('A', b'never decoded')] * 30, 6000, 8000, 300, 'too large to export'), + ([ExportLayer('A', make_image())], WIDTH, HEIGHT, 0, 'DPI'), + ([ExportLayer('A', make_image())], WIDTH, HEIGHT, 5000, 'DPI'), +]) +def test_invalid_export(layers, width, height, dpi, error): + with pytest.raises(InvalidExport, match=error): + build_layered_pdf(layers, width, height, dpi) + + +async def api_export(async_client, images, **meta): + """Export a map through the API, as the browser does: describe it, send the image of each layer, have the PDF + made, and fetch it. Returns the response which ended it (the PDF's, or the first to refuse) and the Job's + record (None when it did not get as far as a Job).""" + meta = dict(dict(width=WIDTH, height=HEIGHT, dpi=300, layers=[name for name, _ in images]), **meta) + request, response = await async_client.post('/api/map/export', json=meta) + if response.status_code != HTTPStatus.CREATED: + return response, None + export_id = response.json['export_id'] + for index, (_, image) in enumerate(images): + request, response = await async_client.post(f'/api/map/export/{export_id}/layer/{index}', content=image) + if response.status_code != HTTPStatus.NO_CONTENT: + return response, None + request, response = await async_client.post(f'/api/map/export/{export_id}/start') + if response.status_code != HTTPStatus.ACCEPTED: + return response, None + job = await wait_for_job(response.json['job_id'], timeout=60) + request, response = await async_client.get(f'/api/map/export/{export_id}/pdf') + return response, job + + +@pytest.mark.asyncio +async def test_api_export_pdf(async_client): + """The images of a map's layers are sent, a Job makes the PDF of them, and the PDF is fetched.""" + response, job = await api_export(async_client, [ + ('Background', make_image((240, 240, 230, 255))), + ('Roads - Tunnels', BLANK), + ('Water', make_image((60, 120, 220, 255), box=(0, 0, 30, 90))), + ], title='A quadrangle') + assert job['status'] == 'complete', job + assert job['progress'] == 100 + assert job['description'] == 'Map PDF: A quadrangle' + assert response.status_code == HTTPStatus.OK, response.text + assert response.headers['content-type'] == 'application/pdf' + assert response.content.startswith(b'%PDF-') + assert layer_names(response.content) == ['Background', 'Water'] + assert pymupdf.open(stream=response.content, filetype='pdf').metadata['title'] == 'A quadrangle' + + +@pytest.mark.asyncio +async def test_api_export_pdf_invalid(async_client): + """What is not a map is refused before any image is sent, and why is said.""" + # No description of the map. + request, response = await async_client.post('/api/map/export', content=b'not json') + assert response.status_code == HTTPStatus.BAD_REQUEST + assert 'description of the map' in response.json['error'] + request, response = await async_client.post('/api/map/export', json=['a', 'list']) + assert response.status_code == HTTPStatus.BAD_REQUEST + + # A description which is not of a map. + response, _ = await api_export(async_client, [('A', make_image())], width='wide') + assert response.status_code == HTTPStatus.BAD_REQUEST + + # More than a PDF is made of: said at once, with nothing sent. + response, _ = await api_export(async_client, [('A', make_image())], width=10000, height=10000) + assert response.status_code == HTTPStatus.BAD_REQUEST + assert '10000x10000' in response.json['error'] + response, _ = await api_export(async_client, [], layers=[]) + assert response.status_code == HTTPStatus.BAD_REQUEST + assert 'no layers' in response.json['error'] + + +@pytest.mark.asyncio +async def test_api_export_layers(async_client): + """Only the images of the map's own layers are taken, and the PDF is not made until all of them are sent.""" + meta = dict(width=WIDTH, height=HEIGHT, dpi=300, layers=['A', 'B']) + request, response = await async_client.post('/api/map/export', json=meta) + export_id = response.json['export_id'] + layer, start = f'/api/map/export/{export_id}/layer', f'/api/map/export/{export_id}/start' + + for index, content, error in ( + (2, make_image(), 'no layer 2'), + (0, b'', 'empty'), + (0, b'not an image', 'not a PNG or JPEG'), + (0, make_image(image_format='GIF'), 'not a PNG or JPEG'), + ): + request, response = await async_client.post(f'{layer}/{index}', content=content) + assert response.status_code == HTTPStatus.BAD_REQUEST, error + assert error in response.json['error'] + + # One of two layers. + request, response = await async_client.post(f'{layer}/0', content=make_image()) + assert response.status_code == HTTPStatus.NO_CONTENT + request, response = await async_client.post(start) + assert response.status_code == HTTPStatus.CONFLICT + assert '1 layers have not been sent: B' in response.json['error'] + request, response = await async_client.get(f'/api/map/export/{export_id}/pdf') + assert response.status_code == HTTPStatus.CONFLICT + assert 'not started' in response.json['error'] + + request, response = await async_client.post(f'{layer}/1', content=make_image(image_format='JPEG')) + assert response.status_code == HTTPStatus.NO_CONTENT + request, response = await async_client.post(start) + assert response.status_code == HTTPStatus.ACCEPTED + job_id = response.json['job_id'] + # Asked again, it is the same Job; and no more images are taken. + request, response = await async_client.post(start) + assert response.json['job_id'] == job_id + request, response = await async_client.post(f'{layer}/0', content=make_image()) + assert response.status_code == HTTPStatus.BAD_REQUEST + assert 'already being made' in response.json['error'] + + # Until the Job is done there is no PDF. + request, response = await async_client.get(f'/api/map/export/{export_id}/pdf') + assert response.status_code == HTTPStatus.CONFLICT + assert (await wait_for_job(job_id, timeout=60))['status'] == 'complete' + request, response = await async_client.get(f'/api/map/export/{export_id}/pdf') + assert response.status_code == HTTPStatus.OK + assert layer_names(response.content) == ['A', 'B'] + + +@pytest.mark.asyncio +async def test_api_export_unknown(async_client): + """An export which there is not, and an id which is not one.""" + for export_id in ('0' * 32, 'nothing', '..%2F..%2Fetc'): + for method, url in (('post', f'/api/map/export/{export_id}/layer/0'), + ('post', f'/api/map/export/{export_id}/start'), + ('get', f'/api/map/export/{export_id}/pdf')): + request, response = await getattr(async_client, method)(url) + assert response.status_code == HTTPStatus.NOT_FOUND, url + + +@pytest.mark.asyncio +async def test_api_export_job_fails(async_client): + """An image which is not of the map fails the Job, which says why; there is no PDF.""" + response, job = await api_export(async_client, [('A', make_image()), ('B', make_image(size=(10, 10)))]) + assert job['status'] == 'failed' + assert '10x10' in job['error'] + assert response.status_code == HTTPStatus.CONFLICT + assert 'failed' in response.json['error'] + + +def test_total_pixels_are_limited_before_decoding(): + """The memory an export needs is the pixels of all its layers, which a small upload can be a great many of: + it is refused without any image being opened.""" + with mock.patch('modules.map.export.Image.open') as image_open: + with pytest.raises(InvalidExport, match='too large to export'): + build_layered_pdf([ExportLayer(str(i), BLANK) for i in range(export.MAX_LAYERS)], + export.MAX_SIDE // 4, export.MAX_SIDE // 4, dpi=300) + image_open.assert_not_called() + + # A quadrangle at 300 DPI near the equator, with every layer of a full layer control on, is within it. + assert 24 * 5300 * 6900 <= export.MAX_TOTAL_PIXELS + + +def test_pymupdf_is_not_imported_with_the_module(): + """pymupdf is slow to import, and the API imports this module when it starts.""" + code = 'import sys, modules.map.api; print("pymupdf" in sys.modules)' + result = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True, timeout=120) + assert result.returncode == 0, result.stderr + assert result.stdout.strip().splitlines()[-1] == 'False' + + +def test_images_are_compressed_as_they_are_added(): + """The pixels of a layer are compressed in the PDF when the layer is added, not kept until it is written: + kept, the layers of a quadrangle are more memory than a Raspberry Pi has to spare.""" + doc = pymupdf.open() + page = doc.new_page(width=WIDTH, height=HEIGHT) + export._insert_layer(doc, page, page.rect, ExportLayer('Satellite', make_image(image_format='JPEG')), {}) + export._insert_layer(doc, page, page.rect, ExportLayer('Water', make_image((0, 0, 255, 200), box=(0, 0, 30, 90))), {}) + + streams = [xref for xref in range(1, doc.xref_length()) if doc.xref_is_stream(xref)] + images = [xref for xref in streams if doc.xref_get_key(xref, 'Subtype')[1] == '/Image'] + # The picture and its transparency, and the photograph. + assert len(images) == 3 + assert sorted(doc.xref_get_key(xref, 'Filter')[1] for xref in images) \ + == ['/DCTDecode', '/FlateDecode', '/FlateDecode'] + + # And it is still the picture: blue, mostly opaque, over the left of the photograph. + pixmap = page.get_pixmap(dpi=72, alpha=False) + assert pixmap.pixel(50, 45) != pixmap.pixel(10, 45) + + +# A quadrangle at Anchorage: west, south, east, north. +ANCHORAGE = [-150.0, 61.125, -149.875, 61.25] + + +def read_viewport(pdf: bytes) -> dict: + """What the first page of a PDF says of where it is: its /VP, as the text of each part.""" + doc = pymupdf.open(stream=pdf, filetype='pdf') + page = doc[0] + kind, viewports = doc.xref_get_key(page.xref, 'VP') + assert kind == 'array', f'The page has no /VP: {kind} {viewports}' + + def numbers(key): + return [float(i) for i in re.search(rf'/{key}\s*\[([^]]*)]', viewports).group(1).split()] + + return dict( + text=viewports, + bbox=numbers('BBox'), bounds=numbers('Bounds'), gpts=numbers('GPTS'), lpts=numbers('LPTS'), + page=(page.rect.width, page.rect.height), + ) + + +def test_pdf_says_where_it_is(): + """With the edges of the map, the page has a geospatial viewport: all of the page, with the latitude and + longitude of each of its corners, in a projection every reader knows.""" + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, + title='1:24,000 61°07′30″N 150°00′W', bounds=ANCHORAGE) + viewport = read_viewport(pdf) + + # All of the page. + assert viewport['bbox'] == [0, 0, WIDTH, HEIGHT] + # The corners, from the lower left of the page: upper left, lower left, lower right, upper right... + assert viewport['lpts'] == viewport['bounds'] == [0, 1, 0, 0, 1, 0, 1, 1] + # ...and where each is: latitude, then longitude. + assert viewport['gpts'] == [61.25, -150, 61.125, -150, 61.125, -149.875, 61.25, -149.875] + + text = viewport['text'] + assert '/Type/Viewport' in text.replace(' ', '') and '/Subtype/GEO' in text.replace(' ', '') + # Mercator on the ellipsoid, in Esri's words: Adobe Acrobat puts every place of a page said to be in Web + # Mercator at 0, 0. + assert '/Type/PROJCS' in text.replace(' ', '') and '/EPSG 3395' in text + assert 'PROJCS["WGS_1984_World_Mercator"' in text and 'PROJECTION["Mercator"]' in text + assert '3857' not in text and 'Auxiliary_Sphere' not in text and 'Mercator_1SP' not in text + + # It is still the PDF it was. + assert layer_names(pdf) == ['Background'] + assert pymupdf.open(stream=pdf, filetype='pdf')[0].get_pixmap(dpi=72).pixel(5, 5) == (200, 80, 20) + + +def test_corner_of_page_is_corner_of_map(): + """A reader finds where a point of the page is from the corners: here, as a reader does it, in the + projection the page says it is in.""" + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, bounds=ANCHORAGE) + viewport = read_viewport(pdf) + + def mercator(latitude, longitude): + return (math.radians(longitude) * 6378137, + math.log(math.tan(math.pi / 4 + math.radians(latitude) / 2)) * 6378137) + + def unmercator(x, y): + return math.degrees(2 * math.atan(math.exp(y / 6378137)) - math.pi / 2), math.degrees(x / 6378137) + + lpts, gpts = viewport['lpts'], viewport['gpts'] + corners = {(lpts[i], lpts[i + 1]): mercator(gpts[i], gpts[i + 1]) for i in range(0, 8, 2)} + + def where(across, up): + """The latitude and longitude of a point of the page (0 to 1 from its lower left).""" + (x0, y0), (x1, y1) = corners[(0, 0)], corners[(1, 1)] + return unmercator(x0 + across * (x1 - x0), y0 + up * (y1 - y0)) + + assert where(0, 0) == pytest.approx((61.125, -150)) + assert where(1, 1) == pytest.approx((61.25, -149.875)) + # The middle of the page is the middle of the map in longitude, and north of its middle in latitude: the + # further north, the more of the page a degree is. + latitude, longitude = where(0.5, 0.5) + assert longitude == pytest.approx(-149.9375) + assert 61.1875 < latitude < 61.1877 + # The other two corners are the corners of the same rectangle. + assert corners[(0, 1)] == pytest.approx((corners[(0, 0)][0], corners[(1, 1)][1])) + assert corners[(1, 0)] == pytest.approx((corners[(1, 1)][0], corners[(0, 0)][1])) + + +def test_pdf_without_edges_says_nothing_of_where_it_is(): + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72) + doc = pymupdf.open(stream=pdf, filetype='pdf') + assert doc.xref_get_key(doc[0].xref, 'VP')[0] == 'null' + + +def test_viewport_name(): + """The viewport is named as the map is; a name which is not ASCII is written so a PDF can hold it.""" + viewport = export.geospatial_viewport(10, 10, ANCHORAGE, 'A quadrangle (of a map)') + assert '/Name (A quadrangle \\(of a map\\))' in viewport + assert '/Name' not in export.geospatial_viewport(10, 10, ANCHORAGE) + + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, + title='61°07′30″N', bounds=ANCHORAGE) + name = re.search(r'/Name\s*<([0-9A-Fa-f]+)>', read_viewport(pdf)['text']).group(1) + assert bytes.fromhex(name).decode('utf-16') == '61°07′30″N' + + +@pytest.mark.parametrize('bounds', [ + [-150, 61.125, -149.875], # Three edges. + 'here', + [-150, 61.125, -149.875, 'north'], + [-149.875, 61.125, -150, 61.25], # East of its east. + [-150, 61.25, -149.875, 61.125], # North of its north. + [-150, 61.125, -150, 61.25], # No width. + [-190, 61.125, -149.875, 61.25], # Off the world. + [179.875, 0, 180.00001, 0.125], # Across the antimeridian, if not by much. + [179.9, 0, 180.1, 0.125], + [-180.00001, 0, -179.875, 0.125], + [-150, 61.125, -149.875, 89], # Further north than the map goes. + [-150, float('nan'), -149.875, 61.25], +]) +def test_invalid_bounds(bounds): + with pytest.raises(InvalidExport, match='edges'): + build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, bounds=bounds) + + +@pytest.mark.parametrize('bounds,expected', [ + # The map's own arithmetic puts the edge of the world a hair past where it is. + ([179.875, 0, 180.00000000000125, 0.125], [179.875, 0, 180, 0.125]), + ([-180.00000000000125, 0, -179.875, 0.125], [-180, 0, -179.875, 0.125]), + ([-180, 0, -179.875, 0.125], [-180, 0, -179.875, 0.125]), +]) +def test_bounds_at_the_antimeridian(bounds, expected): + """A map whose edge is the antimeridian is exported, with that edge at 180 degrees and not at -180.""" + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, bounds=bounds) + west, south, east, north = expected + assert read_viewport(pdf)['gpts'] == [north, west, south, west, south, east, north, east] + + +@pytest.mark.asyncio +async def test_api_export_pdf_georeferenced(async_client): + """The edges of the map are sent with its description, and the PDF says where it is.""" + images = [('Background', make_image())] + response, job = await api_export(async_client, images, bounds=ANCHORAGE) + assert response.status_code == HTTPStatus.OK, response.text + assert read_viewport(response.content)['gpts'] == [61.25, -150, 61.125, -150, 61.125, -149.875, 61.25, -149.875] + + # Without them it is a PDF all the same. + response, job = await api_export(async_client, images) + assert response.status_code == HTTPStatus.OK + + # Edges which are not those of a map are refused. + response, job = await api_export(async_client, images, bounds=[0, 0, 0, 0]) + assert response.status_code == HTTPStatus.BAD_REQUEST + assert 'edges' in response.json['error'] + + +@pytest.mark.asyncio +async def test_api_export_text_which_is_not_text(async_client): + """What is sent as the title of a map, or the name of a layer, may have in it what is no character (half of + a pair, a NUL). It is taken out when the export begins: the Job, after every image is sent, is too late to + find that a PDF can not have it.""" + body = json.dumps(dict(width=WIDTH, height=HEIGHT, dpi=300, title='A \ud800 map\x00', layers=['Ro\x00ads \ud800'])) + request, response = await async_client.post('/api/map/export', content=body) + assert response.status_code == HTTPStatus.CREATED, response.json + export_id = response.json['export_id'] + request, response = await async_client.post(f'/api/map/export/{export_id}/layer/0', content=make_image()) + assert response.status_code == HTTPStatus.NO_CONTENT + request, response = await async_client.post(f'/api/map/export/{export_id}/start') + job = await wait_for_job(response.json['job_id'], timeout=60) + assert job['status'] == 'complete', job + request, response = await async_client.get(f'/api/map/export/{export_id}/pdf') + assert layer_names(response.content) == ['Roads ?'] + assert pymupdf.open(stream=response.content).metadata['title'] == 'A ? map' + + +def test_images_of_one_name_are_one_pdf_layer(): + """A layer of the map may be several images, with other layers drawn between them (the parks of the land + are under the water, and its piers are over it). They are one layer of the PDF, and each is drawn where it + was given: turning the layer off hides them all.""" + pdf = build_layered_pdf([ + ExportLayer('Land Cover', make_image((0, 200, 0, 255))), + ExportLayer('Water', make_image((0, 0, 255, 255), box=(0, 0, 30, 90))), + ExportLayer('Land Cover', make_image((90, 90, 90, 255), box=(0, 0, 10, 90))), + ], WIDTH, HEIGHT, dpi=300) + assert layer_names(pdf) == ['Land Cover', 'Water'] + doc = pymupdf.open(stream=pdf, filetype='pdf') + page = doc[0] + assert len(page.get_images()) == 3 + + def color(x): + pixmap = page.get_pixmap(dpi=300) + return pixmap.pixel(x, 45) + + # From the left: the pier over the water, the water over the park, the park. + assert [color(x) for x in (5, 20, 50)] == [(90, 90, 90), (0, 0, 255), (0, 200, 0)] + + +def _wkt_parameter(wkt: str, name: str) -> float: + return float(re.search(rf'PARAMETER\["{name}",([-0-9.]+)]', wkt).group(1)) + + +def _read_as_a_reader_does(viewport: dict, wkt: str, latitude: float, longitude: float): + """Where on the page (across and up, 0 to 1) a reader of the PDF puts a place: it projects the corners it + is given (/GPTS) in the projection it is told of (the WKT: Mercator on its spheroid), and the place, and puts + the place between the corners as they are on the page (/LPTS).""" + assert 'PROJECTION["Mercator"]' in wkt + a, inverse_flattening = [float(i) for i in re.search(r'SPHEROID\["[^"]*",([0-9.]+),([0-9.]+)]', wkt).groups()] + f = 1 / inverse_flattening + e = math.sqrt(f * (2 - f)) + meridian = _wkt_parameter(wkt, 'Central_Meridian') + # Mercator of one standard parallel: the scale is that of the parallel. + parallel = math.radians(_wkt_parameter(wkt, 'Standard_Parallel_1')) + k = math.cos(parallel) / math.sqrt(1 - (e * math.sin(parallel)) ** 2) + + def project(lat, lon): + phi = math.radians(lat) + y = math.log(math.tan(math.pi / 4 + phi / 2) * ((1 - e * math.sin(phi)) / (1 + e * math.sin(phi))) ** (e / 2)) + return (a * k * math.radians(lon - meridian) + _wkt_parameter(wkt, 'False_Easting'), + a * k * y + _wkt_parameter(wkt, 'False_Northing')) + + gpts, lpts = viewport['gpts'], viewport['lpts'] + corners = [(project(gpts[i], gpts[i + 1]), (lpts[i], lpts[i + 1])) for i in range(0, 8, 2)] + # The page is a rectangle of the projection, so three corners say where the rest of it is. + (upper_left, page_ul), (lower_left, page_ll), (lower_right, page_lr), _ = corners + x, y = project(latitude, longitude) + across = (x - lower_left[0]) / (lower_right[0] - lower_left[0]) + up = (y - lower_left[1]) / (upper_left[1] - lower_left[1]) + return (page_ll[0] + across * (page_lr[0] - page_ll[0]), page_ll[1] + up * (page_ul[1] - page_ll[1])) + + +def _drawn(bounds, latitude: float, longitude: float): + """Where on the page (across and up, 0 to 1) the map has a place: it is drawn in Web Mercator.""" + west, south, east, north = bounds + + def y(lat): + return math.log(math.tan(math.pi / 4 + math.radians(lat) / 2)) + + return (longitude - west) / (east - west), (y(latitude) - y(south)) / (y(north) - y(south)) + + +@pytest.mark.parametrize('west,south', [(-78.5, 0), (-100, 20), (-105.125, 40.25), (-149.625, 61.25), (-156.75, 71.25), + (151.125, -33.875), (10, 84.875), (-0.0625, -0.0625)]) +def test_page_is_where_the_projection_it_is_said_to_be_in_puts_it(west, south): + """The page is drawn in Web Mercator (a sphere), and a reader is told it is Mercator on the ellipsoid, with + the latitude and longitude of its corners. A reader puts a place on the page by where that projection has + it between the corners: that is within 5 centimeters on the ground of where the map drew it, anywhere on a + quadrangle, at any latitude. What the reader is told is what the PDF says: its corners and its WKT.""" + bounds = [west, south, west + 0.125, south + 0.125] + pdf = build_layered_pdf([ExportLayer('Background', make_image())], WIDTH, HEIGHT, dpi=72, bounds=bounds) + viewport = read_viewport(pdf) + wkt = re.search(r'/WKT\s*\((.*)\)\s*>>', viewport['text'], re.S).group(1) + + # The page is this wide and this tall on the ground. + height = 6378137 * math.radians(0.125) + width = height * math.cos(math.radians(south + 0.0625)) + worst = 0 + for i in range(21): + for j in range(21): + lat, lon = south + 0.125 * i / 20, west + 0.125 * j / 20 + read = _read_as_a_reader_does(viewport, wkt, lat, lon) + drawn = _drawn(bounds, lat, lon) + worst = max(worst, math.hypot((read[0] - drawn[0]) * width, (read[1] - drawn[1]) * height)) + assert worst < 0.05 + + # The same sum finds a page whose corners are not where they are said to be: here, a second of arc south. + moved = dict(viewport, gpts=[v - 1 / 3600 if index % 2 == 0 else v for index, v in enumerate(viewport['gpts'])]) + middle = (south + 0.0625, west + 0.0625) + off = _read_as_a_reader_does(moved, wkt, *middle)[1] - _drawn(bounds, *middle)[1] + assert abs(off) * height == pytest.approx(31, abs=1) + + +def test_page_taller_than_a_quadrangle_is_less_well_said(): + """What is left between the two projections grows with the square of how tall the page is: centimeters on a + quadrangle, more than a meter on a page a degree tall. (A page that tall is not exported yet.)""" + def worst(tall): + bounds = [-150, 61, -149, 61 + tall] + viewport = dict(gpts=[bounds[3], bounds[0], bounds[1], bounds[0], bounds[1], bounds[2], bounds[3], bounds[2]], + lpts=[0, 1, 0, 0, 1, 0, 1, 1]) + height = 6378137 * math.radians(tall) + return max(abs(_read_as_a_reader_does(viewport, export.PAGE_PROJECTION_WKT, 61 + tall * i / 40, -149.5)[1] + - _drawn(bounds, 61 + tall * i / 40, -149.5)[1]) * height for i in range(41)) + + assert worst(0.125) < 0.05 + assert 1 < worst(1) < 2 diff --git a/modules/map/test/test_export_job.py b/modules/map/test/test_export_job.py new file mode 100644 index 00000000..42641309 --- /dev/null +++ b/modules/map/test/test_export_job.py @@ -0,0 +1,422 @@ +import asyncio +import json +import os +import stat +import threading +import time +from http import HTTPStatus + +import pytest + +from modules.map import export, export_job +from modules.map.test.test_export import make_image +from wrolpi import jobs + +WIDTH, HEIGHT = 60, 90 + + +def new_export(*names, **kwargs) -> str: + return export_job.create_export(list(names), WIDTH, HEIGHT, 300, **kwargs) + + +def test_create_export(map_export_root): + """An export is a directory which holds what was said of the map.""" + export_id = new_export('Background', 'Water', title='A quadrangle', bounds=[-150, 61.125, -149.875, 61.25], + sheet=export.Sheet(name='A quadrangle', credits=['Somebody'], date='2026-11-15')) + assert export_job.EXPORT_ID.match(export_id) + meta = json.loads((map_export_root / export_id / 'meta.json').read_text()) + assert meta['layers'] == ['Background', 'Water'] + assert meta['sheet'] == dict(name='A quadrangle', credits=['Somebody'], date='2026-11-15', + contour_interval=None, contour_unit=None, legend=None) + assert meta['job_id'] is None + + # A map which can not be exported is not begun. + with pytest.raises(export.InvalidExport, match='no layers'): + new_export() + with pytest.raises(export.InvalidExport, match='edges of the map are needed'): + new_export('A', sheet=export.Sheet()) + assert len(list(map_export_root.iterdir())) == 1 + + +@pytest.mark.parametrize('export_id', ['0' * 32, '', 'nothing', '../' + '0' * 29, '0' * 31 + '/', None]) +def test_unknown_export(export_id, map_export_root): + """An id which was not given by an export is of no export, whatever it would be as a path.""" + map_export_root.mkdir() + (map_export_root.parent / 'meta.json').write_text('{}') + with pytest.raises(export_job.ExportNotFound): + export_job.save_layer(export_id, 0, make_image()) + with pytest.raises(export_job.ExportNotFound): + export_job.get_export_pdf(export_id) + + +def age(directory, seconds=export_job.EXPORT_KEEP_SECONDS + 60): + """Make an export look as old as that.""" + then = time.time() - seconds + os.utime(directory, (then, then)) + + +def test_remove_old_exports(map_export_root, monkeypatch): + """An export is removed when it is old; a new export is when that is looked for.""" + old, new = new_export('A'), new_export('A') + export_job.save_layer(old, 0, make_image()) + age(map_export_root / old) + (map_export_root / 'a file which is not an export').write_text('left alone') + + newest = new_export('A') + assert sorted(i.name for i in map_export_root.iterdir() if i.is_dir()) == sorted([new, newest]) + assert (map_export_root / 'a file which is not an export').is_file() + with pytest.raises(export_job.ExportNotFound): + export_job.start_export(old) + + # Nothing to remove, and nowhere to look, are not errors; and looking does not make the directory. + export_job.remove_old_exports() + monkeypatch.setattr(export_job, 'EXPORT_ROOT', map_export_root / 'not made yet') + export_job.remove_old_exports() + assert not (map_export_root / 'not made yet').exists() + + +def test_only_exports_are_removed(map_export_root, tmp_path): + """What is old in the directory of exports, and is not an export, is left: a directory by another name, and a + link by an export's name to a directory somewhere else.""" + new_export('A') + other = map_export_root / 'someone-elses-directory' + other.mkdir() + (other / 'file').write_text('kept') + elsewhere = tmp_path / 'elsewhere' + elsewhere.mkdir() + (elsewhere / 'precious').write_text('kept') + link = map_export_root / ('f' * 32) + link.symlink_to(elsewhere, target_is_directory=True) + for path in (other, elsewhere): + age(path) + + export_job.remove_old_exports() + assert (other / 'file').read_text() == 'kept' + assert (elsewhere / 'precious').read_text() == 'kept' + # And a link is not an export. + with pytest.raises(export_job.ExportNotFound): + export_job.save_layer('f' * 32, 0, make_image()) + + +def test_export_directory_is_this_users(map_export_root, tmp_path, monkeypatch): + """Exports are kept in a directory made for this user alone, and never through a link: anyone may make things + in /var/tmp, and a link there would have exports written, and old ones removed, wherever it leads.""" + export_id = new_export('A') + assert stat.S_IMODE(map_export_root.stat().st_mode) == 0o700 + assert stat.S_IMODE((map_export_root / export_id).stat().st_mode) == 0o700 + + # One which others could read is closed to them when it is next used. + os.chmod(map_export_root, 0o755) + new_export('A') + assert stat.S_IMODE(map_export_root.stat().st_mode) == 0o700 + + # A link where the directory should be. + elsewhere = tmp_path / 'elsewhere' + (elsewhere / ('a' * 32)).mkdir(parents=True) + age(elsewhere / ('a' * 32)) + linked = tmp_path / 'linked-root' + linked.symlink_to(elsewhere, target_is_directory=True) + monkeypatch.setattr(export_job, 'EXPORT_ROOT', linked) + with pytest.raises(export_job.ExportStorageError): + new_export('A') + with pytest.raises(export_job.ExportStorageError): + export_job.remove_old_exports() + with pytest.raises(export_job.ExportStorageError): + export_job.save_layer('a' * 32, 0, make_image()) + # Nothing was made, or removed, where the link leads. + assert [i.name for i in elsewhere.iterdir()] == ['a' * 32] + + # A file where the directory should be. + not_a_directory = tmp_path / 'a-file' + not_a_directory.write_text('') + monkeypatch.setattr(export_job, 'EXPORT_ROOT', not_a_directory) + with pytest.raises(export_job.ExportStorageError): + new_export('A') + + +@pytest.mark.asyncio +async def test_exports_of_waiting_jobs_are_kept(async_client, map_export_root): + """An export whose Job is waiting is kept however old it is: it may wait behind another Job for longer than an + export is kept, and its images are what the Job is to work from. One whose Job was cancelled is removed at + once.""" + waiting, cancelled, finished = new_export('A'), new_export('A'), new_export('A') + for export_id in (waiting, cancelled, finished): + export_job.save_layer(export_id, 0, make_image()) + await jobs.wait_for_job(export_job.start_export(finished), timeout=60) + # Queued, and not run: nothing takes Jobs from the queue in a test until one is waited for. + waiting_job = export_job.start_export(waiting) + jobs.cancel_job(export_job.start_export(cancelled)) + for export_id in (waiting, finished): + age(map_export_root / export_id) + + export_job.remove_old_exports() + assert sorted(i.name for i in map_export_root.iterdir()) == [waiting] + assert (map_export_root / waiting / 'layer-000').is_file() + + # It is still there for its Job, which makes the PDF. + assert (await jobs.wait_for_job(waiting_job, timeout=60))['status'] == 'complete' + assert export_job.get_export_pdf(waiting).is_file() + + +@pytest.mark.asyncio +async def test_remove_export(async_client, map_export_root): + """An export which is stopped is removed, with the images which were sent, whatever step it was at.""" + # Before its PDF was asked for. + export_id = new_export('A', 'B') + export_job.save_layer(export_id, 0, make_image()) + request, response = await async_client.delete(f'/api/map/export/{export_id}') + assert response.status_code == HTTPStatus.NO_CONTENT + assert not (map_export_root / export_id).exists() + request, response = await async_client.delete(f'/api/map/export/{export_id}') + assert response.status_code == HTTPStatus.NOT_FOUND + + # While its Job waited: the Job is cancelled, and never runs. + export_id = new_export('A') + export_job.save_layer(export_id, 0, make_image()) + job_id = export_job.start_export(export_id) + export_job.remove_export(export_id) + assert not (map_export_root / export_id).exists() + assert (await jobs.wait_for_job(job_id, timeout=60))['status'] == 'cancelled' + + # After its PDF was made. + export_id = new_export('A') + export_job.save_layer(export_id, 0, make_image()) + await jobs.wait_for_job(export_job.start_export(export_id), timeout=60) + export_job.remove_export(export_id) + assert not (map_export_root / export_id).exists() + + with pytest.raises(export_job.ExportNotFound): + export_job.remove_export('0' * 32) + + +@pytest.mark.asyncio +async def test_job_makes_the_pdf(async_client, map_export_root): + """The Job makes the PDF from the images, says how far along it is, and removes the images.""" + export_id = new_export('Background', 'Water', title='A quadrangle') + export_job.save_layer(export_id, 0, make_image()) + export_job.save_layer(export_id, 1, make_image((0, 0, 255, 255), image_format='JPEG')) + directory = map_export_root / export_id + assert sorted(i.name for i in directory.iterdir()) == ['layer-000', 'layer-001', 'meta.json'] + + job_id = export_job.start_export(export_id) + assert job_id.startswith('map_export_pdf-') + job = await jobs.wait_for_job(job_id, timeout=60) + assert job['status'] == 'complete', job + assert job['progress'] == 100 + assert job['result']['export_id'] == export_id + + pdf = export_job.get_export_pdf(export_id) + assert pdf.read_bytes().startswith(b'%PDF-') + assert job['result']['size'] == pdf.stat().st_size + # Only what was said of the map, and the PDF, are kept. + assert sorted(i.name for i in directory.iterdir()) == ['map.pdf', 'meta.json'] + + +@pytest.mark.asyncio +async def test_job_progress(async_client, map_export_root, monkeypatch): + """The Job says how far along it is as each layer is looked at and then put in the PDF.""" + seen = [] + real_set_progress = jobs.JobContext.set_progress + + def set_progress(self, percent): + seen.append(percent) + real_set_progress(self, percent) + + monkeypatch.setattr(jobs.JobContext, 'set_progress', set_progress) + export_id = new_export('A', 'B') + for index in range(2): + export_job.save_layer(export_id, index, make_image()) + await jobs.wait_for_job(export_job.start_export(export_id), timeout=60) + # And, with every step done, before and after the PDF is written: when the work may still be stopped. + assert seen == [0, 25, 50, 75, 100, 100, 100] + + +@pytest.mark.asyncio +async def test_job_fails(async_client, map_export_root): + """A map which can not be made into a PDF fails the Job; its images are removed all the same.""" + export_id = new_export('A', 'B') + export_job.save_layer(export_id, 0, make_image()) + export_job.save_layer(export_id, 1, make_image(size=(10, 10))) + job = await jobs.wait_for_job(export_job.start_export(export_id), timeout=60) + assert job['status'] == 'failed' + assert '10x10' in job['error'] + assert sorted(i.name for i in (map_export_root / export_id).iterdir()) == ['meta.json'] + with pytest.raises(export_job.ExportNotReady, match='failed'): + export_job.get_export_pdf(export_id) + + +@pytest.mark.asyncio +async def test_job_cancelled(async_client, map_export_root, monkeypatch): + """A cancelled Job stops between two layers, makes no PDF, and removes the images. It is not over until the + work has stopped: the next Job is not to start beside it.""" + export_id = new_export('A', 'B', 'C') + for index in range(3): + export_job.save_layer(export_id, index, make_image()) + job_id = export_job.start_export(export_id) + + # The work is held while its first layer is looked at. + real_check = export._check_layer + checked, looking, go_on = [], threading.Event(), threading.Event() + + def check_layer(layer, width, height): + checked.append(layer.name) + looking.set() + assert go_on.wait(30) + return real_check(layer, width, height) + + monkeypatch.setattr(export, '_check_layer', check_layer) + waiting = asyncio.ensure_future(jobs.wait_for_job(job_id, timeout=60)) + for _ in range(200): + if looking.is_set(): + break + await asyncio.sleep(0.02) + assert looking.is_set() + + # Cancelled, as from another process. (Not `cancel_job` here: it logs while it holds the Jobs' lock, and + # this test's log is written to the running Job, which takes that lock.) + jobs._update_job(job_id, cancel_requested=True) + await asyncio.sleep(0.5) + # The work has not stopped, so neither has the Job: its images are still there. + assert jobs.get_job(job_id)['status'] == 'running' + assert not waiting.done() + assert (map_export_root / export_id / 'layer-000').is_file() + + go_on.set() + job = await waiting + assert job['status'] == 'cancelled' + # When the Job is over, so is the work: one layer was looked at, and the images are gone. + assert checked == ['A'] + assert sorted(i.name for i in (map_export_root / export_id).iterdir()) == ['meta.json'] + with pytest.raises(export_job.ExportNotReady, match='cancelled'): + export_job.get_export_pdf(export_id) + + +@pytest.mark.asyncio +async def test_job_cancelled_as_the_pdf_is_written(async_client, map_export_root, monkeypatch): + """A Job cancelled after its last layer, while the sheet is drawn or the PDF written, makes no PDF either.""" + export_id = new_export('A') + export_job.save_layer(export_id, 0, make_image()) + job_id = export_job.start_export(export_id) + real_insert = export._insert_layer + + def insert_layer(*args): + real_insert(*args) + jobs._update_job(job_id, cancel_requested=True) + + monkeypatch.setattr(export, '_insert_layer', insert_layer) + job = await jobs.wait_for_job(job_id, timeout=60) + assert job['status'] == 'cancelled' + assert sorted(i.name for i in (map_export_root / export_id).iterdir()) == ['meta.json'] + + +def test_layers_are_read_one_at_a_time(tmp_path): + """A layer in a file is read when it is needed, not when the map is described.""" + path = tmp_path / 'layer' + layer = export.ExportLayer('A', path=path) + path.write_bytes(make_image()) + assert layer.read() == make_image() + assert export.ExportLayer('A', image=b'given').read() == b'given' + pdf = export.build_layered_pdf([layer], WIDTH, HEIGHT, 300) + assert pdf.startswith(b'%PDF-') + + +def test_progress_may_stop_the_work(): + """What is told how far along the PDF is may stop it.""" + calls = [] + + def progress(done, steps): + calls.append((done, steps)) + if done == 3: + raise RuntimeError('enough') + + layers = [export.ExportLayer(str(i), make_image()) for i in range(3)] + with pytest.raises(RuntimeError, match='enough'): + export.build_layered_pdf(layers, WIDTH, HEIGHT, 300, progress=progress) + assert calls == [(0, 6), (1, 6), (2, 6), (3, 6)] + + +class NeverCancelled: + """A Job which was not cancelled, as one is which ends because the API is stopping.""" + + @staticmethod + def cancel_requested() -> bool: + return False + + @staticmethod + def set_progress(_: float): + pass + + +def test_work_stops_when_it_is_told_to(map_export_root): + """The work of a Job which ended without being cancelled (the API is stopping) stops between two layers when + it is told to: the API waits for it, and is not to wait for all of a PDF.""" + export_id = new_export('A', 'B') + for index in range(2): + export_job.save_layer(export_id, index, make_image()) + directory = map_export_root / export_id + stop = threading.Event() + stop.set() + with pytest.raises(export_job.ExportCancelled): + export_job._make_pdf(directory, NeverCancelled, stop) + assert sorted(i.name for i in directory.iterdir()) == ['meta.json'] + # Not told to, it makes the PDF. + export_id = new_export('A') + export_job.save_layer(export_id, 0, make_image()) + export_job._make_pdf(map_export_root / export_id, NeverCancelled, threading.Event()) + assert sorted(i.name for i in (map_export_root / export_id).iterdir()) == ['map.pdf', 'meta.json'] + + +def test_files_of_an_export_are_written_whole(map_export_root, monkeypatch): + """What is said of an export, and each image, is all there or is what was there before: another request + may read it while it is written. A write which fails leaves nothing behind.""" + export_id = new_export('A') + directory = map_export_root / export_id + before = (directory / 'meta.json').read_text() + + def replace(*_): + raise OSError('The disk is full') + + with monkeypatch.context() as patch: + patch.setattr(export_job.os, 'replace', replace) + with pytest.raises(OSError): + export_job.save_layer(export_id, 0, make_image()) + with pytest.raises(OSError): + export_job._write_meta(directory, {'layers': []}) + assert sorted(i.name for i in directory.iterdir()) == ['meta.json'] + assert (directory / 'meta.json').read_text() == before + export_job.save_layer(export_id, 0, make_image()) + assert sorted(i.name for i in directory.iterdir()) == ['layer-000', 'meta.json'] + + +@pytest.mark.asyncio +async def test_job_removes_old_exports(async_client, map_export_root): + """Old exports are removed when a PDF is made, as well as when an export begins: the last export of a day + is not to keep the one before it.""" + old = new_export('A') + export_id = new_export('A') + age(map_export_root / old) + export_job.save_layer(export_id, 0, make_image()) + job = await jobs.wait_for_job(export_job.start_export(export_id), timeout=60) + assert job['status'] == 'complete' + assert sorted(i.name for i in map_export_root.iterdir()) == [export_id] + + +def test_image_which_arrives_after_its_export_is_removed(map_export_root, monkeypatch): + """An export may be removed while the image of a layer is on its way (the person exporting stopped it, and + the request was already here). The image is not kept: nothing would ever use it, or remove it soon.""" + export_id = new_export('A', 'B') + directory = map_export_root / export_id + real_write = export_job._write_whole + + def write_whole(path, contents): + # Removed, as by another request, between this image being let in and being written. + if path.name.startswith('layer-'): + export_job.remove_export(export_id) + path.parent.mkdir() + real_write(path, contents) + + monkeypatch.setattr(export_job, '_write_whole', write_whole) + with pytest.raises(export_job.ExportNotFound): + export_job.save_layer(export_id, 0, make_image()) + assert not directory.exists() + assert list(map_export_root.iterdir()) == [] diff --git a/modules/map/test/test_magnetic.py b/modules/map/test/test_magnetic.py new file mode 100644 index 00000000..11e0d7d6 --- /dev/null +++ b/modules/map/test/test_magnetic.py @@ -0,0 +1,50 @@ +import pytest + +from modules.map import magnetic + +# From the test values published with the model: year, altitude (km), latitude, longitude, declination, and the +# field north, east and down (nT). +TEST_VALUES = [ + (2025.0, 28, 89, -121, -99.77, -255.388723, -1482.460628, 56194.288771), + (2025.0, 48, 80, -96, -29.91, 1875.982280, -1079.269389, 55623.044051), + (2025.0, 54, 82, 87, 54.89, 1324.336929, 1883.428620, 56740.772059), + (2025.0, 65, 43, 93, 0.50, 24299.852822, 210.517066, 50037.923998), + (2025.0, 51, -33, 109, -5.49, 21737.778822, -2090.274098, -52710.003920), + (2025.0, 39, -59, -8, -15.75, 14358.095523, -4049.107540, -24389.086374), + (2025.0, 3, -50, -103, 27.96, 19526.532799, 10362.990980, -31437.562789), + (2025.0, 94, -29, -110, 15.74, 23275.471080, 6559.046509, -19063.605287), + (2025.0, 66, 14, 143, -0.19, 35003.441364, -116.624818, 7966.315182), + (2025.0, 18, 0, 21, 1.29, 29274.811882, 659.800118, -14316.722540), + (2025.5, 6, -36, -137, 20.28, 23781.930678, 8786.698927, -32577.518648), + (2025.5, 63, 26, 81, 0.51, 34802.613433, 308.431881, 30332.056989), +] + + +def test_load_model(): + model = magnetic.load_model() + assert (model.epoch, model.name, model.degree) == (2025.0, 'WMM-2025', 12) + # Every harmonic of degrees 1 to 12. + assert len(model.coefficients) == sum(n + 1 for n in range(1, 13)) + assert model.coefficients[0] == (1, 0, -29351.8, 0.0, 12.0, 0.0) + assert model.coefficients[-1] == (12, 12, -0.7, 0.2, -0.1, -0.1) + + +@pytest.mark.parametrize('year,altitude,latitude,longitude,declination,north,east,down', TEST_VALUES) +def test_field(year, altitude, latitude, longitude, declination, north, east, down): + """The field is what the model's own test values say it is.""" + model = magnetic.load_model() + assert model.field(latitude, longitude, year, altitude) == pytest.approx((north, east, down), abs=0.01) + assert model.declination(latitude, longitude, year, altitude) == pytest.approx(declination, abs=0.006) + + +def test_declination(): + """A compass in Anchorage points well east of north; in Maine, west.""" + assert 13 < magnetic.declination(61.19, -149.94, 2026.87) < 16 + assert -17 < magnetic.declination(45.0, -69.0, 2026.87) < -13 + + +def test_declination_is_not_guessed(): + """There is no declination in a year the model is not of.""" + assert magnetic.declination(61.19, -149.94, 2024.99) is None + assert magnetic.declination(61.19, -149.94, 2030.0) is None + assert magnetic.declination(61.19, -149.94, 2029.99) is not None diff --git a/modules/map/test/test_quads.py b/modules/map/test/test_quads.py new file mode 100644 index 00000000..6dc232c9 --- /dev/null +++ b/modules/map/test/test_quads.py @@ -0,0 +1,112 @@ +from http import HTTPStatus + +import pytest + +from modules.map import quads + + +def test_load_index(): + souths, rows = quads.load_index() + assert souths == sorted(souths) == sorted(rows) + count = 0 + for south in souths: + wests, cells = quads._row(south) + assert wests == sorted(wests) == [i[0] for i in cells] + count += len(cells) + assert count > 65_000 + + +@pytest.mark.parametrize('west,south,name,state', [ + (-105.125, 40.25, 'Berthoud', 'Colorado'), + (-108.5, 37.75, 'South Mountain', 'Colorado'), + (-76.375, 36.375, 'South Mills', 'North Carolina'), + (-95.25, 37.375, 'South Mound', 'Kansas'), +]) +def test_quad_name(west, south, name, state): + """A quadrangle of the grid has the name the USGS gives it.""" + assert quads.quad_name(west, south, west + 0.125, south + 0.125) == quads.QuadName(name, state, True) + # Asked with the edges the map has, which are not quite those. + assert quads.quad_name(west + 1e-7, south - 1e-7, west + 0.125, south + 0.125).exact + + +def test_quad_name_in_alaska(): + """A quadrangle in Alaska is wider than 7.5 minutes, so an area 7.5 minutes each way is a part of one.""" + assert quads.quad_name(-150, 61.125, -149.875, 61.25) == quads.QuadName('Anchorage A-8 NW', 'Alaska', False) + # All of it, by its own edges. + assert quads.quad_name(-150, 61.125, -149.8125, 61.25) == quads.QuadName('Anchorage A-8 NW', 'Alaska', True) + # The area east of the first is the rest of that quadrangle, and the start of the next. + assert quads.quad_name(-149.875, 61.125, -149.75, 61.25).name == 'Anchorage A-8 NE' + + +@pytest.mark.parametrize('west,south,name,state', [ + # A quadrangle 11.25 minutes wide, which ends at the middle of this area: there is none east of it. + (-153.25, 59.0, 'Iliamna A-1 SW', 'Alaska'), + (-151.375, 59.0, 'Seldovia A-4 SW', 'Alaska'), + # A quadrangle moved to fit an island, which ends at the middle of this area: there is none north of it. + (145.625, 18.75, 'Agrigan Island', 'Northern Mariana Islands'), +]) +def test_quad_name_at_an_outer_edge(west, south, name, state): + """An area half of which is a quadrangle, and the other half no quadrangle, is a part of that quadrangle: + its middle is on the quadrangle's edge, not outside it.""" + found = quads.quad_name(west, south, west + 0.125, south + 0.125) + assert found is not None + assert (found.name, found.state, found.exact) == (name, state, False) + + +def test_quad_name_at_a_shared_edge(): + """A place on the edge two quadrangles share is in the one east, or north, of that edge.""" + assert quads.quad_name(-105.25, 40.25, -105, 40.375).name == 'Berthoud' + assert quads.quad_name(-105.125, 40.125, -105, 40.375).name == 'Berthoud' + + +def test_quad_name_at_a_shared_edge_the_map_gives(): + """The edges the map gives are a hair from where they are. An area whose middle is that near the edge two + quadrangles share is in the one east, or north, of that edge, whichever side of it the hair is.""" + assert quads.quad_name(-149.875, 61.125, -149.75, 61.25).name == 'Anchorage A-8 NE' + # What the map gave for those edges. + given = (-149.87500000000014, 61.12499878064878, -149.75000000000023, 61.25000121452442) + assert quads.quad_name(*given).name == 'Anchorage A-8 NE' + assert quads.quad_name(-105.25 - 1e-9, 40.25 - 1e-9, -105, 40.375).name == 'Berthoud' + + +@pytest.mark.parametrize('west,south', [ + (0, 0), + (151.125, -33.875), # Sydney. + (-105.125, 30), # Mexico. + (-40, 40), # The Atlantic. + (-180, 84.875), +]) +def test_quad_without_a_name(west, south): + assert quads.quad_name(west, south, west + 0.125, south + 0.125) is None + + +def test_every_quad_is_found(): + """The middle of each quadrangle of the index is in a quadrangle of that name, or (where two lie over each + other) of its neighbor's.""" + souths, rows = quads.load_index() + missed = 0 + for south in souths[::7]: + wests, cells = quads._row(south) + for west, east, north, name, state in cells[::5]: + found = quads.quad_name(west / 3600, south / 3600, east / 3600, north / 3600) + assert found is not None, (name, state) + missed += found.name != name + assert missed <= 2 + + +@pytest.mark.asyncio +async def test_api_quad_name(async_client): + request, response = await async_client.get('/api/map/quad?west=-105.125&south=40.25&east=-105&north=40.375') + assert response.status_code == HTTPStatus.OK + assert response.json == {'quad': {'name': 'Berthoud', 'state': 'Colorado', 'exact': True}} + + # Somewhere the USGS has no name for is not an error. + request, response = await async_client.get('/api/map/quad?west=0&south=0&east=0.125&north=0.125') + assert response.status_code == HTTPStatus.OK + assert response.json == {'quad': None} + + for query in ('', '?west=-105.125&south=40.25&east=-105', '?west=a&south=b&east=c&north=d', + '?west=-105.125&south=40.25&east=-113&north=40.375', '?west=nan&south=0&east=1&north=1', + '?west=-500&south=0&east=1&north=1'): + request, response = await async_client.get(f'/api/map/quad{query}') + assert response.status_code == HTTPStatus.BAD_REQUEST, query diff --git a/modules/map/test/test_sheet.py b/modules/map/test/test_sheet.py new file mode 100644 index 00000000..e7ae3697 --- /dev/null +++ b/modules/map/test/test_sheet.py @@ -0,0 +1,737 @@ +import json +import math +import re +from http import HTTPStatus + +import pymupdf +import pytest + +from modules.map import export, magnetic, quads, sheet +from modules.map.export import ExportLayer, InvalidExport, Sheet, build_layered_pdf +from modules.map.test.test_export import api_export, layer_names, make_image, read_viewport + +# A quadrangle of Anchorage, and the size of its image: 11 by 22.9 inches of paper at 36 DPI. +ANCHORAGE = [-150, 61.125, -149.875, 61.25] +WIDTH, HEIGHT, DPI = 397, 824, 36 +MAP_WIDTH, MAP_HEIGHT = WIDTH * 72 / DPI, HEIGHT * 72 / DPI +SHEET = Sheet(name='61°07′30″N 150°00′W', credits=['© OpenStreetMap contributors', 'Protomaps'], + date='2026-11-15') + + +def build(bounds=None, sheet_=SHEET, **kwargs) -> bytes: + return build_layered_pdf([ExportLayer('Background', make_image(size=(WIDTH, HEIGHT)))], WIDTH, HEIGHT, DPI, + bounds=bounds or ANCHORAGE, sheet=sheet_, **kwargs) + + +def page_text(pdf: bytes) -> str: + return pymupdf.open(stream=pdf, filetype='pdf')[0].get_text() + + +@pytest.mark.parametrize('latitude,longitude,zone,south,easting,northing', [ + # As PROJ has them (gdaltransform, EPSG:326xx and 327xx). + (61.2181, -149.9003, 6, False, 344247.206470396, 6790536.87132859), + (61.125, -150, 6, False, 338419.895396155, 6780416.03492079), + (40.31, -105.0595, 13, False, 494944.139497354, 4462166.80845969), + (-33.87, 151.21, 56, True, 334435.70614183, 6250816.39780466), +]) +def test_utm(latitude, longitude, zone, south, easting, northing): + """A place has the UTM coordinates PROJ gives it, to a millimeter, and they are of that place.""" + assert sheet.utm_zone(longitude) == zone + assert sheet.to_utm(latitude, longitude, zone, south) == pytest.approx((easting, northing), abs=0.001) + assert sheet.from_utm(easting, northing, zone, south) == pytest.approx((latitude, longitude), abs=1e-8) + + +def test_utm_zone(): + assert [sheet.utm_zone(i) for i in (-180, -174.001, -174, 0, 179.999, 180)] == [1, 1, 2, 31, 60, 60] + + +def test_format_angle(): + assert sheet.format_angle(61.125, 'N', 'S') == '61°07\'30"N' + assert sheet.format_angle(-150, 'E', 'W') == "150°00'W" + assert sheet.format_angle(-33.875, 'N', 'S') == '33°52\'30"S' + assert sheet.format_angle(61.125, short=True) == '07\'30"' + assert sheet.format_angle(-149.9166666667, short=True) == "55'" + # Seconds which round up to a minute. + assert sheet.format_angle(2.9999999) == "3°00'" + + +def test_graticule_interval(): + """A 7.5 minute map has a tick every 2.5 minutes; a larger map has them further apart.""" + assert sheet.graticule_interval(0.125) == pytest.approx(2.5 / 60) + assert sheet.graticule_interval(0.25) == pytest.approx(5 / 60) + assert sheet.graticule_interval(1) == pytest.approx(15 / 60) + assert sheet.graticule_interval(1e-9) == pytest.approx(1 / 3600) + + +def test_map_scale(): + """The quadrangle the browser draws for a 1:24,000 sheet is 1:24,000.""" + assert sheet.map_scale(MAP_WIDTH, ANCHORAGE) == pytest.approx(24000, rel=0.005) + assert sheet.map_scale(MAP_WIDTH * 2, ANCHORAGE) == pytest.approx(12000, rel=0.005) + + +def test_plan(): + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + x0, y0, x1, y1 = plan.map_rect + assert (x1 - x0, y1 - y0) == pytest.approx((MAP_WIDTH, MAP_HEIGHT)) + # There is a margin on every side, and the most of it is under the map. + assert x0 > 36 and y0 > 36 and plan.width - x1 > 36 + assert plan.height - y1 > y0 + assert plan.zone == 6 and not plan.south_hemisphere + # The corners of the map are the corners of its rectangle. + assert plan.point(61.25, -150) == pytest.approx((x0, y0)) + assert plan.point(61.125, -149.875) == pytest.approx((x1, y1)) + notes = ' '.join(plan.notes) + assert 'Scale 1:24,000' in notes + assert 'zone 6N' in notes and 'west (46 mils) of true north' in notes + assert 'Map data: © OpenStreetMap contributors; Protomaps' in notes + assert 'Made with WROLPi, 2026-11-15.' in notes + + +def test_plan_notes_fit_the_map(): + """Notes too long for the map are on more lines, and the page is longer for them.""" + short = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet()) + long = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet(credits=['Somebody who made a map'] * 20)) + assert len(long.notes) > len(short.notes) + assert long.height > short.height + assert all(pymupdf.get_text_length(i, fontsize=sheet.NOTE_SIZE) <= MAP_WIDTH for i in long.notes) + + +class FakePen: + def __init__(self): + self.lines, self.texts = [], [] + + def line(self, points, **kwargs): + self.lines.append(list(points)) + + def text(self, x, y, text, **kwargs): + self.texts.append((x, y, text, kwargs.get('align', 'left'))) + + def box(self, rect, **kwargs): + pass + + +def place_at(plan, x, y): + """The latitude and longitude of a point of the page.""" + x0, y0, x1, y1 = plan.map_rect + west, south, east, north = plan.bounds + top, bottom = (math.log(math.tan(math.pi / 4 + math.radians(i) / 2)) for i in (north, south)) + mercator = top - (y - y0) / (y1 - y0) * (top - bottom) + return math.degrees(2 * math.atan(math.exp(mercator)) - math.pi / 2), west + (x - x0) / (x1 - x0) * (east - west) + + +@pytest.mark.parametrize('bounds,zone,south', [ + (ANCHORAGE, 6, False), + ([151.125, -33.875, 151.25, -33.75], 56, True), + ([0, 0, 0.125, 0.125], 31, False), +]) +def test_grid_lines_are_where_they_say(bounds, zone, south): + """Every line of the grid is on a whole kilometer of UTM along all of it, and is nowhere off the map.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, bounds, Sheet()) + assert (plan.zone, plan.south_hemisphere) == (zone, south) + pen = FakePen() + sheet._draw_grid(pen, plan) + x0, y0, x1, y1 = plan.map_rect + eastings, northings = set(), set() + for line in pen.lines: + places = [sheet.to_utm(*place_at(plan, x, y), zone, south) for x, y in line] + for x, y in line: + assert x0 - 1e-6 <= x <= x1 + 1e-6 and y0 - 1e-6 <= y <= y1 + 1e-6 + spread_e = max(i[0] for i in places) - min(i[0] for i in places) + value = places[0][0] if spread_e < 1 else places[0][1] + assert value == pytest.approx(round(value / 1000) * 1000, abs=0.5) + for easting, northing in places: + assert (easting if spread_e < 1 else northing) == pytest.approx(value, abs=0.5) + (eastings if spread_e < 1 else northings).add(round(value)) + # A quadrangle is several kilometers each way, and every kilometer has its line. + for values in (eastings, northings): + assert len(values) >= 5 + assert sorted(values) == list(range(min(values), max(values) + 1, 1000)) + + # Each line is named in the margin: the first in full, the others by their kilometers. + labels = [text for _, _, text, _ in pen.texts] + for letter in ('mE', 'mN'): + assert len([i for i in labels if i.endswith(letter)]) == 2, labels + assert len([i for i in labels if i.isdigit()]) >= 12, labels + # And where the line meets the edge of the map. + for x, y, text, align in pen.texts: + if align == 'center': + edge = y0 if y < y0 else y1 + meters = int(text[:-2]) if text.endswith('mE') else int(text) * 1000 + assert sheet.to_utm(*place_at(plan, x, edge), zone, south)[0] == pytest.approx(meters, abs=0.5) + + +def test_grid_labels_keep_clear_of_coordinates(): + """A line of the grid which meets the edge where a coordinate is written is not named there.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet()) + pen = FakePen() + sheet._draw_grid(pen, plan) + across, up = sheet._graticule_labels(plan) + for x, y, text, align in pen.texts: + if align == 'center': + assert all(not low < x < high for low, high in across), text + else: + assert all(not low < y - sheet.LABEL_SIZE * 0.35 < high for low, high in up), text + + +def test_no_tick_at_a_corner(): + """The edges of the picture the browser draws are a hair outside the quadrangle's. The corner is still the + corner: it is not also a tick, written over the corner's own coordinate.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, [-149.87500012, 61.12499878, -149.74999988, 61.25000121], + Sheet()) + pen = FakePen() + sheet._draw_graticule(pen, plan) + labels = sorted(text for _, _, text, _ in pen.texts) + assert labels == sorted(2 * ["10'", '12\'30"', "50'", '47\'30"', "149°52'30\"W", "149°45'W", "61°15'N", + '61°07\'30"N']) + # Two ticks on each edge, and a cross (two lines) where each tick of latitude meets one of longitude. + assert len(pen.lines) == 4 * 2 + 4 * 2 + + +def test_clip(): + rect = (0, 0, 10, 10) + assert sheet._clip([(-5, 5), (15, 5)], rect) == [[(0, 5), (10, 5)]] + assert sheet._clip([(2, 2), (4, 4), (6, 2)], rect) == [[(2, 2), (4, 4), (6, 2)]] + # Out of the rectangle and in again is two lines. + assert sheet._clip([(2, 5), (5, 15), (8, 5)], rect) == [[(2, 5), (3.5, 10)], [(6.5, 10), (8, 5)]] + assert sheet._clip([(-5, -5), (-1, 20)], rect) == [] + assert sheet._clip([(5, 5)], rect) == [] + + +def test_sheet_pdf(): + """A map on a sheet is in the middle of a larger page, with its grid and its margin in layers of their own.""" + plain = build_layered_pdf([ExportLayer('Background', make_image(size=(WIDTH, HEIGHT)))], WIDTH, HEIGHT, DPI, + bounds=ANCHORAGE) + pdf = build() + assert layer_names(plain) == ['Background'] + assert layer_names(pdf) == ['Background', 'UTM grid', 'Sheet'] + + doc = pymupdf.open(stream=pdf, filetype='pdf') + page = doc[0] + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + assert (page.rect.width, page.rect.height) == pytest.approx((plan.width, plan.height), abs=0.01) + # The map is as large as it is without the sheet. + image = page.get_image_rects(page.get_images()[0][0])[0] + assert tuple(image) == pytest.approx(plan.map_rect, abs=0.01) + + text = page.get_text() + # The name and the scale, the corners, the ticks between them, the grid, the bars and the notes. + for expected in ('61°07\'30"N 150°00\'W', '1:24,000', "150°00'W", '149°52\'30"W', "61°15'N", '61°07\'30"N', + "55'", '57\'30"', "10'", '12\'30"', '6790', '342', 'mE', 'mN', 'SCALE 1:24,000', 'KILOMETERS', + 'METERS', '2000', 'MILES', 'FEET', '7000', 'zone 6N', '© OpenStreetMap contributors; Protomaps', + 'Made with WROLPi, 2026-11-15.'): + assert expected in text, expected + + +def test_sheet_layers_can_be_hidden(): + """The grid and the margin are drawn only in their own layers.""" + doc = pymupdf.open(stream=build(), filetype='pdf') + full = doc[0].get_pixmap(dpi=36) + for config in doc.layer_ui_configs(): + if config['text'] in ('UTM grid', 'Sheet'): + doc.set_layer_ui_config(config['number'], action=2) + hidden = doc[0].get_pixmap(dpi=36) + assert full.samples != hidden.samples + # Only the map is left: the margin is as white as paper. + assert hidden.pixel(5, 5) == (255, 255, 255) + assert hidden.color_count() == 2 + + +def test_sheet_georeference_is_of_the_map(): + """The PDF says where the map is, not where the page is.""" + pdf = build() + viewport = read_viewport(pdf) + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + x0, y0, x1, y1 = plan.map_rect + assert viewport['bbox'] == pytest.approx([x0, plan.height - y1, x1, plan.height - y0], abs=0.01) + assert viewport['lpts'] == [0, 1, 0, 0, 1, 0, 1, 1] + assert viewport['gpts'] == [61.25, -150, 61.125, -150, 61.125, -149.875, 61.25, -149.875] + + +def test_sheet_near_a_pole_has_no_grid(): + """UTM stops at 84 degrees north and 80 south.""" + pdf = build(bounds=[0, 84.5, 0.125, 84.625]) + assert layer_names(pdf) == ['Background', 'Sheet'] + assert 'Universal Transverse Mercator' not in page_text(pdf) + assert 'UTM grid' in layer_names(build(bounds=[0, -80, 0.125, -79.875])) + + +def test_sheet_of_a_narrow_map(): + """A map too narrow for a scale bar goes without it.""" + narrow = build_layered_pdf([ExportLayer('Background', make_image(size=(90, 400)))], 90, 400, DPI, + bounds=[0, 84.5, 0.125, 84.625], sheet=Sheet()) + assert 'MILES' not in page_text(narrow) + + +@pytest.mark.parametrize('bounds,name', [ + # The narrowest quadrangles there are, with the longest names. + ([-179.875, 84.875, -179.75, 85], '84°52′30″N 179°52′30″W'), + ([-179.875, -84.875, -179.75, -84.75], '84°52′30″S 179°52′30″W'), + ([10, 84.875, 10.125, 85], '84°52′30″N 10°00′E'), + (ANCHORAGE, '61°07′30″N 150°00′W'), +]) +def test_sheet_title_of_a_narrow_map(bounds, name): + """The name of a map and its scale are not written over each other, however narrow the map is.""" + # As wide as the paper of a 1:24,000 sheet is. + map_width = sheet.map_scale(1, bounds) / 24000 + width, height = round(map_width * DPI / 72), 400 + pdf = build_layered_pdf([ExportLayer('Background', make_image(size=(width, height)))], width, height, DPI, + bounds=bounds, sheet=Sheet(name=name)) + page = pymupdf.open(stream=pdf, filetype='pdf')[0] + titles = [pymupdf.Rect(span['bbox']) for block in page.get_text('dict')['blocks'] for line in block.get('lines', []) + for span in line['spans'] if span['size'] == pytest.approx(sheet.TITLE_SIZE)] + assert len(titles) == 2, titles + assert not titles[0].intersects(titles[1]), titles + # Both are on the page, above the map and what is written along its top edge. + top = sheet.plan_sheet(map_width, height * 72 / DPI, bounds, Sheet(name=name)).map_rect[1] + for title in titles: + assert title in page.rect + assert title.y1 <= top - sheet.LABEL_SIZE + + +def test_plan_norths(): + """A sheet says how far the grid's north and a compass's north are from true north, at the middle of it.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + # West of the middle of its zone the grid leans west; a compass in Alaska points well east. + assert plan.grid_north == pytest.approx(-2.571, abs=0.001) + assert plan.magnetic_north == pytest.approx(magnetic.declination(61.1875, -149.9375, 2026 + 318 / 365), abs=0.01) + assert 13 < plan.magnetic_north < 15 + notes = ' '.join(plan.notes) + assert "Grid north is 2°34' west (46 mils) of true north" in notes + assert re.search(r"Magnetic north is 13°5\d' east \(24\d mils\) of true north at the middle of the map on 2026-11-15," + r" and moves about 0°1\d' west each year \(WMM-2025\)\.", notes), notes + + +def test_plan_compass_which_passes_south(): + """Near the south magnetic pole a compass points almost due south, and within a year passes from a little + west of south to a little east of it. It has moved a little, not most of the way around.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, [140.5, -74, 140.625, -73.875], Sheet(date='2026-11-15')) + assert plan.magnetic_north == pytest.approx(-179.94, abs=0.01) + assert magnetic.load_model().declination(-73.9375, 140.5625, sheet._decimal_year('2027-11-15')) > 179 + notes = ' '.join(plan.notes) + assert "Magnetic north is 179°56' west (3199 mils) of true north" in notes + assert "moves about 0°21' west each year" in notes + + +@pytest.mark.parametrize('date', [None, 'yesterday', '', '2024-12-31', '2030-01-01', '2026-13-45']) +def test_plan_without_a_compass(date): + """Where a compass points is not said when the day is not known, or is not one the model is of.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet(date=date)) + assert plan.magnetic_north is None + assert 'Magnetic' not in ' '.join(plan.notes) + assert plan.grid_north is not None + + +def test_decimal_year(): + assert sheet._decimal_year('2026-01-01') == 2026 + assert sheet._decimal_year('2026-07-02') == pytest.approx(2026.4986, abs=0.0001) + assert sheet._decimal_year('2028-12-31T10:00:00') == pytest.approx(2028 + 365 / 366) + + +def test_norths_are_on_their_own_sides(): + """In the diagram a north is drawn on the side of true north it is on, and apart from it however near it + is.""" + assert sheet._north_tip(0, 0, 14, 50)[0] > 0 > sheet._north_tip(0, 0, -2.5, 50)[0] + # A north a tenth of a degree off, and one eighty off, are both drawn where they can be seen. + near, far = sheet._north_tip(0, 0, 0.1, 50), sheet._north_tip(0, 0, -80, 50) + assert near[0] == pytest.approx(50 * math.sin(math.radians(7))) + assert far == pytest.approx((-25, -50 * math.cos(math.radians(30)))) + + +def test_sheet_norths_diagram(): + """The diagram of the norths is beside the scale bars, with each angle written; a sheet too narrow for it, + or with nothing to put in it, goes without.""" + text = page_text(build()) + assert 'MN 13°' in text and 'E\n' in text + assert 'GN 2°34\' W 46 MILS' in text + assert re.search(r"MN 13°5\d' E 24\d MILS", text), text + + # No compass: the grid's north is still drawn. + text = page_text(build(sheet_=Sheet())) + assert 'GN 2°34\' W' in text and 'MN' not in text + + # No grid near a pole, but a compass points somewhere; the quadrangle is too narrow for the diagram. + narrow = build_layered_pdf([ExportLayer('Background', make_image(size=(90, 400)))], 90, 400, DPI, + bounds=[0, 84.5, 0.125, 84.625], sheet=Sheet(date='2026-11-15')) + text = page_text(narrow) + assert 'Magnetic north is' in text + assert 'MN ' not in text and 'GN ' not in text + + +def test_sheet_text_which_the_font_can_not_write(): + pdf = build(sheet_=Sheet(name='Карта “map” – 地図', credits=['© Somebody\n\nelse'])) + text = page_text(pdf) + assert '"map" -' in text + assert '© Somebody else' in text + + +def test_sheet_needs_bounds(): + with pytest.raises(InvalidExport, match='edges of the map are needed'): + build_layered_pdf([ExportLayer('Background', make_image())], 60, 90, 72, sheet=Sheet()) + + +@pytest.mark.asyncio +async def test_api_export_pdf_sheet(async_client): + images = [('Background', make_image(size=(WIDTH, HEIGHT)))] + size = dict(width=WIDTH, height=HEIGHT, dpi=DPI) + response, job = await api_export( + async_client, images, bounds=ANCHORAGE, **size, + sheet=dict(name='A quadrangle', credits=['Somebody', 'x' * 500] + ['More'] * 50, date='2026-11-15')) + assert response.status_code == HTTPStatus.OK, response.text + assert layer_names(response.content) == ['Background', 'UTM grid', 'Sheet'] + text = page_text(response.content) + assert 'A quadrangle' in text and 'Somebody' in text + # What is written on a sheet is limited. + assert len(re.findall('More', text)) == 38 + assert 'x' * 301 not in text.replace('\n', '') + + # An empty sheet is a sheet. + response, job = await api_export(async_client, images, bounds=ANCHORAGE, sheet={}, **size) + assert response.status_code == HTTPStatus.OK, response.text + assert 'Sheet' in layer_names(response.content) + + # A sheet which is not one, and a sheet of a map which is nowhere. + for meta in (dict(bounds=ANCHORAGE, sheet='yes'), dict(sheet={})): + response, job = await api_export(async_client, images, **size, **meta) + assert response.status_code == HTTPStatus.BAD_REQUEST, json.dumps(meta) + + +def test_mils(): + """A compass marked in mils has 6,400 of them around.""" + assert sheet.mils(360) == 6400 + assert sheet.mils(90) == 1600 + assert sheet.mils(-2.57) == 46 + assert sheet.mils(0) == 0 + + +def test_sheet_scale_bars(): + """The bars are of kilometers, meters, miles and feet, from the top down.""" + doc = pymupdf.open(stream=build(), filetype='pdf') + tops = {word[4]: word[1] for word in doc[0].get_text('words') + if word[4] in ('KILOMETERS', 'METERS', 'MILES', 'FEET')} + assert sorted(tops, key=tops.get) == ['KILOMETERS', 'METERS', 'MILES', 'FEET'] + gaps = [tops[b] - tops[a] for a, b in (('KILOMETERS', 'METERS'), ('METERS', 'MILES'), ('MILES', 'FEET'))] + assert gaps == pytest.approx([sheet.BAR_SPACING] * 3, abs=0.01) + + +@pytest.mark.parametrize('interval,unit,expected', [ + (40, 'feet', 'CONTOUR INTERVAL 40 FEET'), + (20.0, 'meters', 'CONTOUR INTERVAL 20 METERS'), + (1000, 'feet', 'CONTOUR INTERVAL 1,000 FEET'), + (2.5, 'meters', 'CONTOUR INTERVAL 2.5 METERS'), +]) +def test_sheet_contour_interval(interval, unit, expected): + """A map with contour lines says how far apart they are, under its scale bars and over its notes.""" + plain = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + with_contours = Sheet(name=SHEET.name, credits=SHEET.credits, date=SHEET.date, + contour_interval=interval, contour_unit=unit) + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, with_contours) + assert plan.contours == expected + assert plain.contours is None + # The page is longer by the room it takes. + assert plan.height == pytest.approx(plain.height + sheet.CONTOUR_LEADING) + + doc = pymupdf.open(stream=build(sheet_=with_contours), filetype='pdf') + lines = {line.strip(): y for _, y, _, _, text, *_ in doc[0].get_text('blocks') for line in text.splitlines()} + assert expected in lines + feet = min(y for line, y in lines.items() if line == 'FEET') + notes = min(y for line, y in lines.items() if line.startswith('Scale 1:24,000')) + assert feet < lines[expected] < notes + assert 'CONTOUR INTERVAL' not in page_text(build()) + + +@pytest.mark.parametrize('interval,unit', [ + (None, 'feet'), (40, None), (40, 'furlongs'), (0, 'feet'), (-40, 'feet'), (math.nan, 'feet'), (math.inf, 'meters'), + ('40', 'feet'), +]) +def test_sheet_contour_interval_which_is_not_one(interval, unit): + """What is not an interval of contour lines is not written.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet(contour_interval=interval, contour_unit=unit)) + assert plan.contours is None + + +@pytest.mark.asyncio +async def test_api_export_pdf_sheet_contours(async_client): + """The browser says how far apart the contour lines of a map are, and the sheet says it.""" + images = [('Background', make_image(size=(WIDTH, HEIGHT)))] + size = dict(width=WIDTH, height=HEIGHT, dpi=DPI, bounds=ANCHORAGE) + response, job = await api_export(async_client, images, **size, + sheet=dict(contours=dict(interval=40, unit='feet'))) + assert response.status_code == HTTPStatus.OK, response.text + assert 'CONTOUR INTERVAL 40 FEET' in page_text(response.content) + + # What is not an interval is not written, and the map is exported. + for contours in (None, 'yes', {}, dict(interval='40', unit='feet'), dict(interval=True, unit='feet'), + dict(interval=40), dict(interval=40, unit='furlongs'), dict(interval=-1, unit='meters'), + dict(interval=40, unit=['feet'])): + response, job = await api_export(async_client, images, **size, sheet=dict(contours=contours)) + assert response.status_code == HTTPStatus.OK, json.dumps(contours) + assert 'CONTOUR INTERVAL' not in page_text(response.content), json.dumps(contours) + + +BERTHOUD = [-105.125, 40.25, -105.0, 40.375] +BERTHOUD_NEIGHBORS = ['Masonville', 'Loveland', 'Windsor', 'Carter Lake Reservoir', 'Johnstown', 'Hygiene', + 'Longmont', 'Gowanda'] + + +def test_adjoining_quads(): + """The eight quadrangles around a map, from the northwest across and down.""" + assert sheet.adjoining_quads(BERTHOUD) == BERTHOUD_NEIGHBORS + # The edges of what the browser drew are a hair outside the quadrangle. + assert sheet.adjoining_quads([-105.1250004, 40.2499998, -104.9999996, 40.3750002]) == BERTHOUD_NEIGHBORS + # Where the USGS has no quadrangles there is nothing to say. + assert sheet.adjoining_quads([2.25, 48.75, 2.375, 48.875]) is None + # At the edge of the land only some of the eight have a name: those out to sea have none. + coast = sheet.adjoining_quads([-124.25, 40.75, -124.125, 40.875]) + assert coast is not None and None in coast and any(coast) + + +def test_sheet_adjoining_quads(): + """A sheet says what the quadrangles around it are called, in a diagram under its bars.""" + size = (630, 820) + pdf = build_layered_pdf([ExportLayer('Background', make_image(size=size))], *size, DPI, bounds=BERTHOUD, + sheet=SHEET) + plan = sheet.plan_sheet(size[0] * 72 / DPI, size[1] * 72 / DPI, BERTHOUD, SHEET) + assert plan.adjoining == BERTHOUD_NEIGHBORS + doc = pymupdf.open(stream=pdf, filetype='pdf') + assert doc[0].rect.height == pytest.approx(plan.height, abs=0.01) + lines = {line.strip(): y for _, y, _, _, text, *_ in doc[0].get_text('blocks') for line in text.splitlines()} + assert 'ADJOINING QUADRANGLES' in lines + for number, name in enumerate(BERTHOUD_NEIGHBORS, start=1): + assert f'{number} {name}' in lines, name + # Under the bars, and over the notes. + notes = min(y for line, y in lines.items() if line.startswith('Scale 1:')) + assert lines['FEET'] < lines['ADJOINING QUADRANGLES'] < lines['8 Gowanda'] < notes + + # The page is longer by the room the diagram takes. + nowhere = [2.25, 48.75, 2.375, 48.875] + without = sheet.plan_sheet(size[0] * 72 / DPI, size[1] * 72 / DPI, nowhere, Sheet()) + with_ = sheet.plan_sheet(size[0] * 72 / DPI, size[1] * 72 / DPI, BERTHOUD, Sheet()) + assert without.adjoining is None + assert with_.height - with_.map_rect[3] == pytest.approx( + without.height - without.map_rect[3] + sheet.ADJOINING_HEIGHT, abs=0.01) + + +def test_sheet_adjoining_quads_where_there_are_none(): + """A map of where the USGS names nothing, and a map too narrow for the diagram, go without it.""" + assert 'ADJOINING' not in page_text(build(bounds=[2.25, 48.75, 2.375, 48.875])) + narrow = build_layered_pdf([ExportLayer('Background', make_image(size=(90, 400)))], 90, 400, DPI, + bounds=BERTHOUD, sheet=Sheet()) + assert 'ADJOINING' not in page_text(narrow) + + +def test_sheet_adjoining_quads_some_unnamed(): + """A quadrangle with no name has no number in the diagram, and the others keep theirs.""" + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, BERTHOUD, Sheet()) + plan.adjoining = ['Northwest', None, None, None, 'East', None, None, 'A name ' + 'far too long ' * 20] + pen = FakePen() + sheet._draw_adjoining(pen, plan, 54, 100) + written = [text for _, _, text, _ in pen.texts] + assert '1 Northwest' in written and '5 East' in written + assert '1' in written and '5' in written and '8' in written + assert not {'2', '3', '4', '6', '7'} & set(written) + # A name too long for its column is cut to fit it. + long = next(text for text in written if text.startswith('8 A name')) + assert pymupdf.get_text_length(long, fontsize=sheet.LABEL_SIZE) <= sheet.ADJOINING_COLUMN + + +@pytest.mark.parametrize('bounds', [ + [-149.875, 61.125, -149.75, 61.25], + [-147.75, 64.75, -147.625, 64.875], + [-156.875, 71.25, -156.75, 71.375], + ANCHORAGE, +]) +def test_adjoining_quads_are_never_the_map_itself(bounds): + """Where a quadrangle is wider than the map (Alaska), the map beside this one may be more of the same + quadrangle. The quadrangle on that side is the next one.""" + own = quads.quad_name(*bounds) + around = sheet.adjoining_quads(bounds) + assert own.name not in around + + +def test_adjoining_quads_of_the_edges_the_map_gives(): + """The edges of an exported map are a hair from those of its quadrangle. What is around it is the same.""" + around = sheet.adjoining_quads([-149.875, 61.125, -149.75, 61.25]) + given = [-149.87500000000014, 61.12499878064878, -149.75000000000023, 61.25000121452442] + assert sheet.adjoining_quads(given) == around + assert around[3:5] == ['Anchorage A-8 NW', 'Anchorage A-7 NW'] + + +def test_adjoining_quads_step_past_the_map(): + """The east of a map in the west of a wide quadrangle is the quadrangle after it, not the rest of it.""" + bounds = [-149.875, 61.125, -149.75, 61.25] + assert quads.quad_name(*bounds).name == 'Anchorage A-8 NE' + # One map east is still Anchorage A-8 NE. + assert quads.quad_name(-149.75, 61.125, -149.625, 61.25).name == 'Anchorage A-8 NE' + around = sheet.adjoining_quads(bounds) + assert around[3:5] == ['Anchorage A-8 NW', 'Anchorage A-7 NW'] + + +LEGEND = dict(ground=[0.9, 0.88, 0.85], lines=[ + dict(label='Highway', color=[1, 1, 1], width=1.2, opacity=1, casing_color=[0.8, 0.8, 0.8], casing_width=2.4), + dict(label='Railway', color=[0.6, 0.7, 0.7], width=0.5, opacity=0.5, dash=[0.15, 0.4]), + dict(label='Contour', color=[0.6, 0.4, 0.2], width=0.25, opacity=0.3), +]) + + +def test_legend_lines(): + """The lines of a legend are what a browser sent of them, as far as they are lines.""" + ground, lines = sheet.legend_lines(LEGEND) + assert ground == (0.9, 0.88, 0.85) + assert [i.label for i in lines] == ['Highway', 'Railway', 'Contour'] + assert lines[0] == sheet.LegendLine(label='Highway', color=(1, 1, 1), width=1.2, opacity=1, + casing_color=(0.8, 0.8, 0.8), casing_width=2.4) + assert lines[1].dash == (0.15, 0.4) and lines[1].opacity == 0.5 and lines[1].casing_color is None + # As it is sent again (to the Job which makes the PDF), it is the same legend. + assert sheet.legend_lines(sheet.legend_as_sent(LEGEND)) == (ground, lines) + assert json.loads(json.dumps(sheet.legend_as_sent(LEGEND)))['lines'][0]['label'] == 'Highway' + + +@pytest.mark.parametrize('legend', [ + None, 'yes', [], {}, dict(lines='roads'), dict(lines=[]), dict(lines=['Highway', None, 7]), + dict(lines=[dict(label='', color=[1, 1, 1], width=1)]), + dict(lines=[dict(label='No color', width=1)]), + dict(lines=[dict(label='No width', color=[1, 1, 1])]), + dict(lines=[dict(label='Two parts', color=[1, 1], width=1)]), + dict(lines=[dict(label='Not numbers', color=['red', 0, 0], width=1)]), + dict(lines=[dict(label='Not a width', color=[1, 0, 0], width='wide')]), + dict(lines=[dict(label='Not a width', color=[1, 0, 0], width=math.nan)]), + dict(lines=[dict(label='Not a width', color=[1, 0, 0], width=True)]), + # Too thin to be seen on the map. + dict(lines=[dict(label='Path or track', color=[1, 0, 0], width=0.02, dash=[0.06, 0.02])]), + dict(lines=[dict(label='Nothing', color=[1, 0, 0], width=0)]), +]) +def test_legend_lines_which_are_not(legend): + """What is not a legend has no lines, and is on white.""" + assert sheet.legend_lines(legend) == (sheet.WHITE, []) + assert sheet.legend_as_sent(legend) is None + + +def test_legend_lines_are_limited(): + """A legend has only so many lines, each named in only so many letters and drawn only so wide.""" + line = dict(label='x' * 500, color=[2, -1, 0.5], width=900, opacity=7, dash=[0, 900] * 20, + casing_color=[0, 0, 0], casing_width=900) + ground, lines = sheet.legend_lines(dict(ground=[5, 5, 5], lines=[line] * 100)) + assert ground == (1, 1, 1) + assert len(lines) == sheet.LEGEND_LINES + assert lines[0].label == 'x' * sheet.LEGEND_LABEL + assert lines[0].color == (1, 0, 0.5) and lines[0].opacity == 1 + assert lines[0].width == sheet.LEGEND_WIDTHS[1] and lines[0].casing_width == 8 + assert lines[0].dash == (0.1, sheet.LEGEND_SAMPLE) * 3 + # A casing with no color, or no width, is no casing; dashes which are not numbers are no dashes. + ground, lines = sheet.legend_lines(dict(lines=[ + dict(label='A', color=[0, 0, 0], width=1, casing_width=2), + dict(label='B', color=[0, 0, 0], width=1, casing_color=[1, 1, 1]), + dict(label='C', color=[0, 0, 0], width=1, dash=[1, 'long']), + ])) + assert [(i.casing_color, i.casing_width, i.dash) for i in lines] == [(None, 0, ())] * 3 + + +def test_sheet_legend(): + """A sheet with a legend has what each line is called beside a piece of it, under the bars and over the + notes, and is longer for it.""" + with_legend = Sheet(name=SHEET.name, credits=SHEET.credits, date=SHEET.date, legend=LEGEND) + plain = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, SHEET) + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, with_legend) + assert [i.label for i in plan.legend] == ['Highway', 'Railway', 'Contour'] and not plain.legend + assert plan.legend_ground == (0.9, 0.88, 0.85) + # One row of lines: the map is wide enough for the three side by side. + assert plan.height == pytest.approx( + plain.height + sheet.LABEL_SIZE + 8 + sheet.LEGEND_ITEM_HEIGHT + 8) + + doc = pymupdf.open(stream=build(sheet_=with_legend), filetype='pdf') + words = {word[4]: (word[0], word[1]) for word in doc[0].get_text('words')} + assert words['LEGEND'][1] < words['Highway'][1] == words['Railway'][1] == words['Contour'][1] + assert words['FEET'][1] < words['LEGEND'][1] and words['Highway'][1] < words['Scale'][1] + assert words['Railway'][0] - words['Highway'][0] == pytest.approx(sheet.LEGEND_ITEM_WIDTH, abs=0.01) + assert 'LEGEND' not in page_text(build()) + # The lines are drawn in their own colors: the page has the brown of a contour line. + colors = {tuple(round(c, 2) for c in d['color']) for d in doc[0].get_drawings() if d.get('color')} + assert (0.6, 0.4, 0.2) in colors and (0.6, 0.7, 0.7) in colors + + +def test_sheet_legend_of_many_lines_is_in_rows(): + lines = [dict(label=f'Line {i}', color=[0, 0, 0], width=1) for i in range(9)] + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet(legend=dict(lines=lines))) + columns = int(MAP_WIDTH // sheet.LEGEND_ITEM_WIDTH) + assert 1 < columns < 9 + pen = FakePen() + sheet._draw_legend(pen, plan, 54, 100) + places = {text: (x, y) for x, y, text, _ in pen.texts} + # The line after the last of a row is under the first of it. + assert places[f'Line {columns}'][0] == places['Line 0'][0] + assert places[f'Line {columns}'][1] == pytest.approx(places['Line 0'][1] + sheet.LEGEND_ITEM_HEIGHT) + assert places[f'Line {columns - 1}'][1] == places['Line 0'][1] + + +@pytest.mark.asyncio +async def test_api_export_pdf_sheet_legend(async_client): + """The browser says how the lines of a map are drawn, and the sheet has a legend of them.""" + images = [('Background', make_image(size=(WIDTH, HEIGHT)))] + size = dict(width=WIDTH, height=HEIGHT, dpi=DPI, bounds=ANCHORAGE) + response, job = await api_export(async_client, images, **size, sheet=dict(legend=LEGEND)) + assert response.status_code == HTTPStatus.OK, response.text + text = page_text(response.content) + assert 'LEGEND' in text and 'Highway' in text and 'Railway' in text + + # What is not a legend is not drawn, and the map is exported. + for legend in (None, 'yes', [], {}, dict(lines='roads'), dict(lines=[dict(label='No color', width=1)])): + response, job = await api_export(async_client, images, **size, sheet=dict(legend=legend)) + assert response.status_code == HTTPStatus.OK, json.dumps(legend) + assert 'LEGEND' not in page_text(response.content), json.dumps(legend) + + +def test_sheet_legend_name_too_long_for_its_column(): + """What a line is called is cut to the room beside the line, so it is not under the next line.""" + lines = [dict(label='A name which is far too long for it ' + str(i), color=[0, 0, 0], width=1) for i in range(3)] + plan = sheet.plan_sheet(MAP_WIDTH, MAP_HEIGHT, ANCHORAGE, Sheet(legend=dict(lines=lines))) + pen = FakePen() + sheet._draw_legend(pen, plan, 54, 100) + written = [(x, text) for x, _, text, _ in pen.texts if text.startswith('A name')] + assert len(written) == 3 + for x, text in written: + assert len(text) < len(lines[0]['label']) + left = x - sheet.LEGEND_SAMPLE - 5 + assert x + pymupdf.get_text_length(text, fontsize=sheet.LABEL_SIZE) <= left + sheet.LEGEND_ITEM_WIDTH + + +def test_format_angle_of_no_angle(): + """An edge on the equator, or on the prime meridian, is on neither side of it; nor is one a hair from it.""" + assert sheet.format_angle(0, 'N', 'S') == "0°00'" + assert sheet.format_angle(-1e-7, 'E', 'W') == "0°00'" + assert sheet.format_angle(1e-7, 'N', 'S') == "0°00'" + assert sheet.format_angle(-0.125, 'N', 'S') == "0°07'30\"S" + + +@pytest.mark.parametrize('bounds,zone', [ + # The edges the map gives are a hair outside the quadrangle. + ([10, 83.8749999, 10.125, 84.0000001], 32), + ([10, -80.0000001, 10.125, -79.875], 32), + # Past where the zones end. + ([10, 84, 10.125, 84.125], None), + ([10, -80.125, 10.125, -80], None), +]) +def test_sheet_grid_where_the_zones_end(bounds, zone): + """A map which ends where the UTM zones do (84 north, 80 south) has a grid; one past there has none.""" + plan = sheet.plan_sheet(300, 600, bounds, sheet.Sheet()) + assert plan.zone == zone + + +@pytest.mark.parametrize('date', ['9999-12-31', '0000-01-01', 'yesterday', '', None]) +def test_sheet_of_no_such_day(date): + """A sheet whose day is not one is a sheet which does not say where magnetic north is.""" + plan = sheet.plan_sheet(300, 600, BERTHOUD, sheet.Sheet(date=date)) + assert plan.magnetic_north is None + + +def test_one_mil_is_a_mil(): + """An angle of one mil is "1 mil", in the notes and under the norths; more are "mils".""" + # Grid north at Berthoud is 0°02' west of true north: one mil. + plan = sheet.plan_sheet(300, 600, BERTHOUD, sheet.Sheet(date='2026-11-19')) + assert sheet.mils(plan.grid_north) == 1 + assert '(1 mil) of true north' in ' '.join(plan.notes) + text = page_text(build_layered_pdf( + [ExportLayer('Background', make_image(size=(WIDTH, HEIGHT)))], WIDTH, HEIGHT, DPI, bounds=BERTHOUD, + sheet=sheet.Sheet(date='2026-11-19'))) + assert '1 MIL' in text and '1 MILS' not in text + # Magnetic north there is many. + assert re.search(r'MN .* \d{2,} MILS', text) diff --git a/modules/map/wmm/WMM2025.COF b/modules/map/wmm/WMM2025.COF new file mode 100644 index 00000000..098c579d --- /dev/null +++ b/modules/map/wmm/WMM2025.COF @@ -0,0 +1,93 @@ + 2025.0 WMM-2025 11/13/2024 + 1 0 -29351.8 0.0 12.0 0.0 + 1 1 -1410.8 4545.4 9.7 -21.5 + 2 0 -2556.6 0.0 -11.6 0.0 + 2 1 2951.1 -3133.6 -5.2 -27.7 + 2 2 1649.3 -815.1 -8.0 -12.1 + 3 0 1361.0 0.0 -1.3 0.0 + 3 1 -2404.1 -56.6 -4.2 4.0 + 3 2 1243.8 237.5 0.4 -0.3 + 3 3 453.6 -549.5 -15.6 -4.1 + 4 0 895.0 0.0 -1.6 0.0 + 4 1 799.5 278.6 -2.4 -1.1 + 4 2 55.7 -133.9 -6.0 4.1 + 4 3 -281.1 212.0 5.6 1.6 + 4 4 12.1 -375.6 -7.0 -4.4 + 5 0 -233.2 0.0 0.6 0.0 + 5 1 368.9 45.4 1.4 -0.5 + 5 2 187.2 220.2 0.0 2.2 + 5 3 -138.7 -122.9 0.6 0.4 + 5 4 -142.0 43.0 2.2 1.7 + 5 5 20.9 106.1 0.9 1.9 + 6 0 64.4 0.0 -0.2 0.0 + 6 1 63.8 -18.4 -0.4 0.3 + 6 2 76.9 16.8 0.9 -1.6 + 6 3 -115.7 48.8 1.2 -0.4 + 6 4 -40.9 -59.8 -0.9 0.9 + 6 5 14.9 10.9 0.3 0.7 + 6 6 -60.7 72.7 0.9 0.9 + 7 0 79.5 0.0 -0.0 0.0 + 7 1 -77.0 -48.9 -0.1 0.6 + 7 2 -8.8 -14.4 -0.1 0.5 + 7 3 59.3 -1.0 0.5 -0.8 + 7 4 15.8 23.4 -0.1 0.0 + 7 5 2.5 -7.4 -0.8 -1.0 + 7 6 -11.1 -25.1 -0.8 0.6 + 7 7 14.2 -2.3 0.8 -0.2 + 8 0 23.2 0.0 -0.1 0.0 + 8 1 10.8 7.1 0.2 -0.2 + 8 2 -17.5 -12.6 0.0 0.5 + 8 3 2.0 11.4 0.5 -0.4 + 8 4 -21.7 -9.7 -0.1 0.4 + 8 5 16.9 12.7 0.3 -0.5 + 8 6 15.0 0.7 0.2 -0.6 + 8 7 -16.8 -5.2 -0.0 0.3 + 8 8 0.9 3.9 0.2 0.2 + 9 0 4.6 0.0 -0.0 0.0 + 9 1 7.8 -24.8 -0.1 -0.3 + 9 2 3.0 12.2 0.1 0.3 + 9 3 -0.2 8.3 0.3 -0.3 + 9 4 -2.5 -3.3 -0.3 0.3 + 9 5 -13.1 -5.2 0.0 0.2 + 9 6 2.4 7.2 0.3 -0.1 + 9 7 8.6 -0.6 -0.1 -0.2 + 9 8 -8.7 0.8 0.1 0.4 + 9 9 -12.9 10.0 -0.1 0.1 + 10 0 -1.3 0.0 0.1 0.0 + 10 1 -6.4 3.3 0.0 0.0 + 10 2 0.2 0.0 0.1 -0.0 + 10 3 2.0 2.4 0.1 -0.2 + 10 4 -1.0 5.3 -0.0 0.1 + 10 5 -0.6 -9.1 -0.3 -0.1 + 10 6 -0.9 0.4 0.0 0.1 + 10 7 1.5 -4.2 -0.1 0.0 + 10 8 0.9 -3.8 -0.1 -0.1 + 10 9 -2.7 0.9 -0.0 0.2 + 10 10 -3.9 -9.1 -0.0 -0.0 + 11 0 2.9 0.0 0.0 0.0 + 11 1 -1.5 0.0 -0.0 -0.0 + 11 2 -2.5 2.9 0.0 0.1 + 11 3 2.4 -0.6 0.0 -0.0 + 11 4 -0.6 0.2 0.0 0.1 + 11 5 -0.1 0.5 -0.1 -0.0 + 11 6 -0.6 -0.3 0.0 -0.0 + 11 7 -0.1 -1.2 -0.0 0.1 + 11 8 1.1 -1.7 -0.1 -0.0 + 11 9 -1.0 -2.9 -0.1 0.0 + 11 10 -0.2 -1.8 -0.1 0.0 + 11 11 2.6 -2.3 -0.1 0.0 + 12 0 -2.0 0.0 0.0 0.0 + 12 1 -0.2 -1.3 0.0 -0.0 + 12 2 0.3 0.7 -0.0 0.0 + 12 3 1.2 1.0 -0.0 -0.1 + 12 4 -1.3 -1.4 -0.0 0.1 + 12 5 0.6 -0.0 -0.0 -0.0 + 12 6 0.6 0.6 0.1 -0.0 + 12 7 0.5 -0.1 -0.0 -0.0 + 12 8 -0.1 0.8 0.0 0.0 + 12 9 -0.4 0.1 0.0 -0.0 + 12 10 -0.2 -1.0 -0.1 -0.0 + 12 11 -1.3 0.1 -0.0 0.0 + 12 12 -0.7 0.2 -0.1 -0.1 +999999999999999999999999999999999999999999999999 +999999999999999999999999999999999999999999999999 diff --git a/wrolpi/scripts/build_map_quad_names.py b/wrolpi/scripts/build_map_quad_names.py new file mode 100644 index 00000000..e7c0b819 --- /dev/null +++ b/wrolpi/scripts/build_map_quad_names.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Make the index of the names of the USGS 7.5 minute quadrangles which WROLPi ships +(modules/map/quads/usgs_7_5_minute.tsv.gz). + +The names are from the Map Indices of The National Map (U.S. Geological Survey; public domain): + + https://prd-tnm.s3.amazonaws.com/StagedProducts/MapIndices/National/GDB/MapIndices_National_GDB.zip + +Its cells are read with GDAL, which WROLPi does not need otherwise, so that is done first and by hand: + + ogr2ogr -f CSV cells.csv MapIndices_National_GDB.gdb -dialect SQLite -sql "SELECT CELL_NAME, PRIMARY_STATE, + ST_MinX(SHAPE) AS west, ST_MinY(SHAPE) AS south, ST_MaxX(SHAPE) AS east, ST_MaxY(SHAPE) AS north + FROM CellGrid_7_5Minute" + + python3 wrolpi/scripts/build_map_quad_names.py cells.csv + +The index is a line for each quadrangle: its south, west, east and north edges (whole seconds of arc), its name +and its state, between tabs; the lines are in order. +""" +import argparse +import csv +import gzip +import pathlib +import sys + +OUTPUT = pathlib.Path(__file__).resolve().parents[2] / 'modules/map/quads/usgs_7_5_minute.tsv.gz' + + +def seconds(degrees: str) -> int: + return round(float(degrees) * 3600) + + +def read_cells(path: pathlib.Path) -> list: + cells = [] + with path.open(newline='') as fh: + for row in csv.DictReader(fh): + name, state = row['CELL_NAME'].strip(), row['PRIMARY_STATE'].strip() + if not name or not state or state == 'None': + # A cell which is of no state is of no map. + continue + if '\t' in name or '\t' in state: + raise ValueError(f'A tab in the name of a quadrangle: {name!r} {state!r}') + south, west, east, north = (seconds(row[i]) for i in ('south', 'west', 'east', 'north')) + if not (south < north and west < east): + raise ValueError(f'The quadrangle {name!r} has no size: {row}') + cells.append((south, west, east, north, name, state)) + return sorted(cells) + + +def write_index(cells: list, path: pathlib.Path): + text = ''.join('\t'.join(str(i) for i in cell) + '\n' for cell in cells) + path.parent.mkdir(parents=True, exist_ok=True) + with path.open('wb') as fh: + # No name and no time in the file, so the same cells are the same bytes. + with gzip.GzipFile(filename='', mode='wb', fileobj=fh, mtime=0, compresslevel=9) as gz: + gz.write(text.encode()) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('cells', type=pathlib.Path, help='The CSV of the cells, as ogr2ogr writes it') + parser.add_argument('--output', type=pathlib.Path, default=OUTPUT) + args = parser.parse_args() + cells = read_cells(args.cells) + write_index(cells, args.output) + print(f'{len(cells)} quadrangles, {args.output.stat().st_size} bytes: {args.output}', file=sys.stderr) + + +if __name__ == '__main__': + main()