// 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 { fallbackNeeded, planTile, 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 Germany, which is not in the patchwork assert.deepEqual(await plan([13, 4268, 2861], tiles), { parts: ['CH:partial', 'FR:partial'], fallback: true, }); // only Germany 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(); // Padborg, with Germany to the south assert.deepEqual(await plan([13, 4309, 2597], tiles), { parts: ['DK:partial'], fallback: true, }); }); it('leaves the islands of Portugal to the fallback', async () => { const { asked, tiles } = savedTiles(); // Funchal, on Madeira assert.deepEqual(await plan(tile(-16.91, 32.65, 15), tiles), { parts: [], fallback: true }); assert.deepEqual(asked, []); }); 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); }); });