2026-11-29 15:06:35 +01:00
|
|
|
// Which imagery each tile of the open satellite basemap is made of, from saved tiles of the
|
|
|
|
|
// tileset of the countries.
|
2026-11-29 12:52:41 +01:00
|
|
|
import assert from 'node:assert/strict';
|
2026-11-29 15:06:35 +01:00
|
|
|
import { readFileSync } from 'node:fs';
|
2026-11-29 12:52:41 +01:00
|
|
|
import { describe, it } from 'node:test';
|
2026-11-29 15:06:35 +01:00
|
|
|
import { 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 };
|
|
|
|
|
}
|
2026-11-29 12:52:41 +01:00
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-29 15:06:35 +01:00
|
|
|
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 };
|
2026-11-29 12:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('planTile', () => {
|
2026-11-29 13:09:26 +01:00
|
|
|
it('takes every tile up to zoom 12 from the imagery of the whole world', async () => {
|
2026-11-29 15:06:35 +01:00
|
|
|
const { asked, tiles } = savedTiles();
|
|
|
|
|
assert.deepEqual(await plan(tile(2.35, 48.85, 12), tiles), { parts: [], fallback: true });
|
|
|
|
|
assert.deepEqual(asked, []);
|
2026-11-29 13:09:26 +01:00
|
|
|
});
|
|
|
|
|
|
2026-11-29 15:06:35 +01:00
|
|
|
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, []);
|
2026-11-29 12:52:41 +01:00
|
|
|
});
|
|
|
|
|
|
2026-11-29 15:06:35 +01:00
|
|
|
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'],
|
2026-11-29 12:52:41 +01:00
|
|
|
fallback: false,
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-11-29 15:06:35 +01:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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}`]);
|
|
|
|
|
});
|
2026-11-29 12:52:41 +01:00
|
|
|
});
|