// Which imagery each tile of the open satellite basemap is made of, from saved tiles of the // tileset of the countries. import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { describe, it } from 'node:test'; import * as maplibregl from 'maplibre-gl'; import { attributionsOf, fallbackNeeded, planTile, trackOpenSatelliteAttribution, type CountryTiles, } from './open-satellite'; const fixtures = new URL('../../../test/fixtures/satellite-countries/', import.meta.url); /** The saved tiles, none where there is no file, and the ones that were asked for. */ function savedTiles() { const asked: string[] = []; const tiles: CountryTiles = async (z, x, y) => { asked.push(`${z}/${x}/${y}`); try { return new Uint8Array(readFileSync(new URL(`${z}-${x}-${y}.mvt`, fixtures))).buffer; } catch { return null; } }; return { asked, tiles }; } function tile(lon: number, lat: number, z: number): [number, number, number] { const n = 2 ** z; const sin = Math.sin((lat * Math.PI) / 180); return [ z, Math.floor(((lon + 180) / 360) * n), Math.floor((0.5 - Math.log((1 + sin) / (1 - sin)) / (4 * Math.PI)) * n), ]; } async function plan([z, x, y]: [number, number, number], tiles: CountryTiles) { const { parts, fallback } = await planTile(z, x, y, tiles); return { parts: parts.map((p) => `${p.source.id}:${p.relation}`).sort(), fallback }; } describe('planTile', () => { it('takes every tile up to zoom 12 from the imagery of the whole world', async () => { const { asked, tiles } = savedTiles(); assert.deepEqual(await plan(tile(2.35, 48.85, 12), tiles), { parts: [], fallback: true }); assert.deepEqual(asked, []); }); it('takes a tile far from every country from the fallback, without asking for countries', async () => { const { asked, tiles } = savedTiles(); // London, and the Atlantic assert.deepEqual(await plan(tile(-0.12, 51.5, 14), tiles), { parts: [], fallback: true }); assert.deepEqual(await plan(tile(-30, 40, 13), tiles), { parts: [], fallback: true }); assert.deepEqual(asked, []); }); it('takes a tile inside a country from its agency alone', async () => { const { tiles } = savedTiles(); assert.deepEqual(await plan(tile(2.35, 48.85, 15), tiles), { parts: ['FR:full'], fallback: false, }); }); it('puts the countries of a border tile together, over the fallback where none is', async () => { const { tiles } = savedTiles(); // Basel: France, Switzerland and Baden-Württemberg, which is not in the patchwork assert.deepEqual(await plan([13, 4268, 2861], tiles), { parts: ['CH:partial', 'FR:partial'], fallback: true, }); // only Baden-Württemberg assert.deepEqual(await plan([13, 4269, 2860], tiles), { parts: [], fallback: true }); }); it('needs no fallback where the countries cover the tile between them', async () => { const { tiles } = savedTiles(); // Wattrelos: France and Belgium assert.deepEqual(await plan([13, 4166, 2750], tiles), { parts: ['BE:partial', 'FR:partial'], fallback: false, }); // Baarle, with its enclaves of Belgium and the Netherlands assert.deepEqual(await plan([13, 4208, 2726], tiles), { parts: ['BE:partial', 'NL:partial'], fallback: false, }); // Elvas and Badajoz, on the border of Portugal and Spain assert.deepEqual(await plan([13, 3935, 3135], tiles), { parts: ['ES:partial', 'PT:partial'], fallback: false, }); }); it('takes a country from its agency next to one without open imagery', async () => { const { tiles } = savedTiles(); // Ulm, in Baden-Württemberg, and Neu-Ulm, in Bavaria assert.deepEqual(await plan(tile(9.99, 48.39, 13), tiles), { parts: ['DE-BY:partial'], fallback: true, }); }); it('takes the states of Germany from their own agencies', async () => { const { tiles } = savedTiles(); // Neu-Ulm assert.deepEqual(await plan(tile(10.01, 48.38, 16), tiles), { parts: ['DE-BY:full'], fallback: false, }); // Padborg, with Schleswig-Holstein to the south assert.deepEqual(await plan([13, 4309, 2597], tiles), { parts: ['DE-SH:partial', 'DK:partial'], fallback: false, }); }); it('takes Slovenia, Slovakia and Estonia from their agencies', async () => { const { tiles } = savedTiles(); // Ljubljana assert.deepEqual(await plan(tile(14.506, 46.051, 16), tiles), { parts: ['SI:full'], fallback: false, }); // Bad Radkersburg and Gornja Radgona, on the border of Austria and Slovenia assert.deepEqual(await plan(tile(15.99, 46.685, 13), tiles), { parts: ['AT:partial', 'SI:partial'], fallback: false, }); // Bratislava, on the border of Austria assert.deepEqual(await plan(tile(17.05, 48.14, 13), tiles), { parts: ['AT:partial', 'SK:partial'], fallback: false, }); // Valga, with Latvia, which is not in the patchwork, to the south assert.deepEqual(await plan(tile(26.03, 57.777, 13), tiles), { parts: ['EE:partial'], fallback: true, }); }); it('takes Liechtenstein from the agency of Switzerland, with it as one', async () => { const { tiles } = savedTiles(); // Vaduz assert.deepEqual(await plan(tile(9.5215, 47.141, 16), tiles), { parts: ['LI:full'], fallback: false, }); // the Rhine, on the border of Switzerland const { parts, fallback } = await planTile(...tile(9.51, 47.141, 14), tiles); assert.deepEqual( parts.map((part) => part.relation), ['full'] ); assert.equal(fallback, false); // near Schaanwald, on the border of Austria const border = await plan([14, 8628, 5748], tiles); assert.deepEqual(border, { parts: ['AT:partial', 'LI:partial'], fallback: false }); }); it('takes Monaco from the agency of France, with it as one', async () => { const { tiles } = savedTiles(); // the Casino assert.deepEqual(await plan(tile(7.4281, 43.7392, 18), tiles), { parts: ['MC:full'], fallback: false, }); // La Condamine, on the border of France const { parts, fallback } = await planTile(...tile(7.4165, 43.7365, 17), tiles); assert.deepEqual( parts.map((part) => part.relation), ['full'] ); assert.equal(fallback, false); }); it('takes Madeira from its own agency, and leaves the Azores to the fallback', async () => { const { tiles } = savedTiles(); // Funchal assert.deepEqual(await plan(tile(-16.91, 32.65, 16), tiles), { parts: ['PT-30:full'], fallback: false, }); const { asked, tiles: none } = savedTiles(); // Ponta Delgada assert.deepEqual(await plan(tile(-25.67, 37.74, 15), none), { parts: [], fallback: true }); assert.deepEqual(asked, []); }); it('takes the regions of Italy with imagery of the same year as one', async () => { const { tiles } = savedTiles(); const years = async (...at: [number, number, number]) => { const [z, x, y] = tile(...at); const { parts, fallback } = await planTile(z, x, y, tiles); return { parts: parts .map((p) => `${p.source.url(z, x, y).match(/AgEA_(\d+)/)?.[1]}:${p.relation}`) .sort(), fallback, }; }; // Orte, in Lazio, and the Umbrian side of the Tiber assert.deepEqual(await years(12.386, 42.46, 13), { parts: ['2023:full'], fallback: false }); // the Vatican and Rome assert.deepEqual(await years(12.4534, 41.9022, 14), { parts: ['2023:full'], fallback: false, }); // Piacenza, in Emilia-Romagna, and Lombardy, north of the Po assert.deepEqual(await years(9.69, 45.06, 13), { parts: ['2023:partial', '2024:partial'], fallback: false, }); }); it('takes Basilicata from its agency above the last zoom level of its imagery', async () => { const { tiles } = savedTiles(); // Potenza assert.deepEqual(await plan(tile(15.805, 40.64, 18), tiles), { parts: ['IT-77:full'], fallback: false, }); // on the border of Campania, whose imagery is closer: as one only up to there const border = [15.53345, 40.57318] as const; const { parts } = await planTile(...tile(...border, 16), tiles); assert.deepEqual( parts.map((part) => part.relation), ['full'] ); assert.deepEqual(await plan(tile(...border, 17), tiles), { parts: ['IT-72:partial', 'IT-77:partial'], fallback: false, }); }); it('takes New Zealand from its agency, on both sides of the 180th meridian', async () => { const { tiles } = savedTiles(); // New Plymouth, inland and on the coast, with the fallback at sea assert.deepEqual(await plan(tile(174.07, -39.08, 15), tiles), { parts: ['NZ:full'], fallback: false, }); assert.deepEqual(await plan(tile(174.07, -39.055, 15), tiles), { parts: ['NZ:partial'], fallback: true, }); // the Chatham Islands assert.deepEqual(await plan(tile(-176.5, -43.95, 13), tiles), { parts: ['NZ:full'], fallback: false, }); }); it('takes Japan from its agency', async () => { const { tiles } = savedTiles(); // Matsumoto assert.deepEqual(await plan(tile(137.97, 36.235, 13), tiles), { parts: ['JP:full'], fallback: false, }); // the Shiretoko peninsula, with the fallback at sea assert.deepEqual(await plan(tile(145.19, 44.02, 14), tiles), { parts: ['JP:partial'], fallback: true, }); }); it('takes Taiwan from its agency, without the islands that have no imagery', async () => { const { tiles } = savedTiles(); // Yushan assert.deepEqual(await plan(tile(120.957, 23.47, 14), tiles), { parts: ['TW:full'], fallback: false, }); // Kinmen, with the fallback at sea assert.deepEqual(await plan(tile(118.29, 24.43, 13), tiles), { parts: ['TW:partial'], fallback: true, }); // Matsu assert.deepEqual(await plan(tile(119.93, 26.155, 14), tiles), { parts: [], fallback: true, }); }); it('takes the states of the United States with the same imagery as one', async () => { const { tiles } = savedTiles(); // Kansas City, in Kansas and Missouri, both from the NAIP const { parts, fallback } = await planTile(...tile(-94.6075, 39.11, 13), tiles); assert.deepEqual( parts.map((part) => part.relation), ['full'] ); assert.equal(fallback, false); }); it('takes other states from their own agencies', async () => { const { tiles } = savedTiles(); // Washington DC assert.deepEqual(await plan(tile(-77.036, 38.897, 16), tiles), { parts: ['US-DC:full'], fallback: false, }); // Portland, and the Columbia, with Washington to the north assert.deepEqual(await plan(tile(-122.68, 45.59, 16), tiles), { parts: ['US-OR:full'], fallback: false, }); assert.deepEqual(await plan([13, 1305, 2927], tiles), { parts: ['US-OR:partial', 'US-WA:partial'], fallback: false, }); }); it('takes the states missing from the server of the USDA from that of GeoPlatform', async () => { const { tiles } = savedTiles(); // Honolulu, with the fallback at sea const honolulu = tile(-157.858, 21.307, 16); assert.deepEqual(await plan(honolulu, tiles), { parts: ['US-HI:full'], fallback: false }); const [part] = (await planTile(...honolulu, tiles)).parts; assert.match(part.source.url(...honolulu), /^https:\/\/imagery\.geoplatform\.gov\//); assert.deepEqual(await plan(tile(-157.87, 21.29, 13), tiles), { parts: ['US-HI:partial'], fallback: true, }); // Four Corners, with Arizona and Colorado from there, and New Mexico and Utah from the USDA const { parts, fallback } = await planTile(...tile(-109.0452, 36.999, 14), tiles); assert.deepEqual( parts.map((p) => p.relation), ['partial', 'partial'] ); assert.equal(fallback, false); }); it('asks for a tile of the countries once for all the tiles of imagery inside it', async () => { const { asked, tiles } = savedTiles(); // around Lausanne, which no other test asks for const [, x, y] = tile(6.63, 46.52, 12); for (let dx = 0; dx < 4; dx++) { for (let dy = 0; dy < 4; dy++) { await planTile(14, x * 4 + dx, y * 4 + dy, tiles); } } assert.deepEqual(asked, [`12/${x}/${y}`]); }); }); describe('fallbackNeeded', () => { it('is not needed up to zoom 12, where the imagery of the whole world is', async () => { const { asked, tiles } = savedTiles(); // a tile of 512 pixels at zoom 11 covers four tiles of the open imagery at zoom 12 assert.equal(await fallbackNeeded(...tile(-0.12, 51.5, 11), tiles), false); assert.deepEqual(asked, []); }); it('is needed far from every country, without asking for countries', async () => { const { asked, tiles } = savedTiles(); assert.equal(await fallbackNeeded(...tile(-0.12, 51.5, 13), tiles), true); assert.deepEqual(asked, []); }); it('is not needed where the countries cover its four tiles', async () => { const { tiles } = savedTiles(); // Paris, and Wattrelos between France and Belgium assert.equal(await fallbackNeeded(...tile(2.35, 48.85, 14), tiles), false); assert.equal(await fallbackNeeded(12, 2083, 1375, tiles), false); }); it('is needed where one of its four tiles is outside the countries', async () => { const { tiles } = savedTiles(); // Basel, with a tile entirely in Germany assert.equal(await fallbackNeeded(12, 2134, 1430, tiles), true); }); }); describe('attributionsOf', () => { const credits = async (tilesInView: [number, number, number][]) => { const { open, fallback } = await attributionsOf( tilesInView.map(([z, x, y]) => ({ z, x, y })), savedTiles().tiles ); return { open: open.replace(/<[^>]*>/g, ''), fallback: fallback.replace(/<[^>]*>/g, '') }; }; it('credits only the imagery of the whole world up to zoom 12', async () => { assert.deepEqual(await credits([tile(6.63, 46.52, 12)]), { open: 'IGN-F/Géoplateforme', fallback: '', }); }); it('credits the countries in view, each once', async () => { // Paris, twice, and Basel assert.deepEqual( await credits([tile(2.35, 48.85, 15), tile(2.35, 48.85, 14), [13, 4268, 2861]]), { open: 'swisstopo, IGN-F/Géoplateforme', fallback: '© MapTiler' } ); assert.equal((await credits([tile(-94.6075, 39.11, 13)])).open, 'USDA'); }); it('credits only the fallback far from every country', async () => { assert.deepEqual(await credits([tile(-0.12, 51.5, 14)]), { open: '', fallback: '© MapTiler', }); }); it('credits the imagery of the whole world for the far tiles of a tilted map', async () => { assert.deepEqual(await credits([tile(-0.12, 51.5, 14), tile(-0.12, 51.5, 11)]), { open: 'IGN-F/Géoplateforme', fallback: '© MapTiler', }); }); }); describe('trackOpenSatelliteAttribution', () => { it('changes the credits of the sources with the view, and has the control read them', async () => { const map = new (class extends maplibregl.Evented {})() as unknown as maplibregl.Map; const fired: string[] = []; const sources: Record = {}; const add = (id: string, protocol: string) => { const spec = { type: 'raster', tiles: [`${protocol}://{z}/{x}/{y}`] }; const tell = () => map.fire( new maplibregl.MapSourceDataEvent('sourcedata', { sourceDataType: 'metadata', sourceId: id, source: spec as maplibregl.RasterSourceSpecification, }) ); sources[id] = Object.assign({}, spec, { fire: (event: maplibregl.MapSourceDataEvent) => { assert.equal(event.sourceDataType, 'metadata'); fired.push(id); tell(); }, }); tell(); }; let view = tile(-0.12, 51.5, 10); Object.assign(map, { getSource: (id: string) => sources[id], coveringTiles: () => [{ canonical: { z: view[0], x: view[1], y: view[2] } }], }); const settled = () => new Promise((resolve) => setTimeout(resolve, 50)); trackOpenSatelliteAttribution(map); add('satellite', 'opensat'); add('fallback', 'opensat-fallback'); await settled(); assert.match(sources.satellite.attribution ?? '', /^]*>IGN-F\/Géoplateforme<\/a>$/); assert.equal(sources.fallback.attribution, undefined); assert.deepEqual(fired, ['satellite']); // London, only on the fallback view = tile(-0.12, 51.5, 14); map.fire('moveend'); await settled(); assert.equal(sources.satellite.attribution, ''); assert.match(sources.fallback.attribution ?? '', /MapTiler/); // nothing to change fired.length = 0; map.fire('moveend'); await settled(); assert.deepEqual(fired, []); }); });