Files
gods-eye-view/src/library/outlines.test.mjs
T
Sameh Khamis 9e57fd84c8 Draw Data Library coverage outlines from the bundled Natural Earth countries
Imagery outlines fetched Natural Earth's 1:50m countries (about 3 MB)
from the natural-earth-vector repository's master branch on GitHub at
run time: a moving, rate-limited source whose failures silently removed
the outlines. Core already ships the pinned 1:10m countries pack, so
the outlines now read that, once, the first time an outline needs it.
2026-11-12 16:02:07 -08:00

93 lines
2.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { outlineRings } from './outlines.js';
import { libraryEntry } from './catalog.js';
const square = (lon, lat, size = 1) => [
[lon, lat],
[lon + size, lat],
[lon + size, lat + size],
[lon, lat + size],
[lon, lat],
];
const country = (code, polygons, iso = code) => ({
properties: { ISO_A2_EH: code, ISO_A2: iso },
geometry: {
type: 'MultiPolygon',
coordinates: polygons.map((ring) => [ring]),
},
});
const features = [
// Netherlands: the European part and a Caribbean island.
country('NL', [square(4, 52), square(-68.3, 12.1, 0.3)]),
// Natural Earth writes France's ISO_A2 as -99; ISO_A2_EH carries FR.
country('FR', [square(2, 46), square(-53, 4)], '-99'),
country('BE', [square(4, 50.5)]),
];
test('coverage outlines keep only the parts inside the declared area', () => {
const netherlands = {
coverage: { countries: ['NL'], bbox: [3.2, 50.7, 7.3, 53.6] },
display: { outline: 'coverage' },
};
const rings = outlineRings(features, netherlands);
assert.equal(rings.length, 1);
assert.equal(rings[0][0][0], 4);
});
test('country outlines keep every part and read the ISO_A2_EH code', () => {
const everyPart = {
coverage: { countries: ['FR'] },
display: { outline: 'countries' },
};
assert.equal(
outlineRings(features, everyPart).length,
2,
'metropolitan France and French Guiana',
);
// IGN is clipped to metropolitan France, so its outline is too.
const ign = outlineRings(features, libraryEntry('ign-orthophotos-france'));
assert.equal(ign.length, 1);
assert.equal(ign[0][0][0], 2);
assert.deepEqual(
outlineRings(features, libraryEntry('nasa-black-marble')),
[],
);
});
test('a bbox outline is the coverage rectangle, with no border file needed', () => {
const rings = outlineRings([], libraryEntry('lawrence-ks'));
const [west, south, east, north] = libraryEntry('lawrence-ks').coverage.bbox;
assert.deepEqual(rings, [
[
[west, south],
[east, south],
[east, north],
[west, north],
[west, south],
],
]);
});
test('outlines come from the bundled Natural Earth countries, closed and in coverage', async () => {
const { loadCountryOutlines, resetCountryOutlines } = await import(
'./outlines.js'
);
resetCountryOutlines();
const countries = await loadCountryOutlines();
for (const id of ['ign-orthophotos-france', 'ee-maa-amet-ortofoto']) {
const entry = libraryEntry(id);
const rings = outlineRings(countries, entry);
assert.ok(rings.length > 0, id);
for (const ring of rings) assert.deepEqual(ring.at(-1), ring[0]);
}
// A pack that cannot load is retried on the next call.
resetCountryOutlines();
await assert.rejects(
loadCountryOutlines(async () => {
throw new Error('unreadable');
}),
);
assert.ok((await loadCountryOutlines()).length > 200);
});