fix(atlas): show Ireland's 26 counties instead of its four provinces

geoBoundaries has no county layer for Ireland: its ADM1 is the four
provinces and its ADM2 the 166 local electoral areas, so the Atlas showed
Ireland as four provinces next to a UK of counties.

The Atlas builder can now take a country's regions from another source.
Ireland's come from Tailte Éireann's 26 counties (CC BY 4.0), with Dublin,
Cork, Galway and Limerick each one county, under their ISO 3166-2:IE
codes. Those outlines come at full detail, so the builder simplifies them
itself and keeps the borders two counties share identical on both sides,
so no point between them falls into neither. ATLAS_ONLY rebuilds just the
named countries without downloading the whole world.

A migration clears Ireland's cached place regions so they are worked out
again as counties, moves visited rows that name a county onto its code,
and hides each county of a province a user had hidden. Provinces a user
marked as visited are kept as they are.

Fixes #2922
This commit is contained in:
Maurice
2026-11-11 21:26:31 +00:00
parent bac968c0bf
commit 948c9ef57b
12 changed files with 796 additions and 18 deletions
@@ -0,0 +1,147 @@
/**
* `Migration20200101046380_ireland_gained_its_counties` (#2922): the Atlas ships
* Ireland's 26 counties instead of its four provinces. Cached place regions are
* cleared, hand-marked provinces stay, county names under stale codes take the county
* code, and a hidden province hides its counties.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import type { MikroORM } from '@mikro-orm/sqlite';
import { afterEach, describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101046380_ireland_gained_its_counties';
const LEINSTER = [
'IE-CW',
'IE-D',
'IE-KE',
'IE-KK',
'IE-LD',
'IE-LH',
'IE-LS',
'IE-MH',
'IE-OY',
'IE-WH',
'IE-WW',
'IE-WX',
];
let orm: MikroORM;
async function ormBeforeTarget(): Promise<MikroORM> {
const o = await createMigrationOrm();
const names = await pendingNames(o);
const index = names.indexOf(TARGET);
expect(index).toBeGreaterThan(0);
await migrateTo(o, names[index - 1]);
return o;
}
async function seedUsers(): Promise<void> {
await rawExec(
orm,
`INSERT INTO users (id, username, email, password_hash) VALUES (1, 'one', 'one@test', 'x'), (2, 'two', 'two@test', 'x')`,
);
}
const visited = (userId: number) =>
rawQuery<{ region_code: string; region_name: string; country_code: string }>(
orm,
'SELECT region_code, region_name, country_code FROM visited_regions WHERE user_id = ? ORDER BY region_code',
[userId],
);
const hidden = async (userId: number) =>
(
await rawQuery<{ region_code: string }>(
orm,
'SELECT region_code FROM hidden_regions WHERE user_id = ? ORDER BY region_code',
[userId],
)
).map((r) => r.region_code);
afterEach(async () => {
await orm?.close(true);
});
describe('Ireland gained its counties', () => {
it('IE-COUNTIES-001: clears the cached Irish place regions and leaves other countries cached', async () => {
orm = await ormBeforeTarget();
await seedUsers();
await rawExec(orm, `INSERT INTO trips (id, user_id, title) VALUES (1, 1, 'Isles')`);
await rawExec(
orm,
`INSERT INTO places (id, trip_id, name, lat, lng) VALUES (1, 1, 'Dublin', 53.34, -6.25), (2, 1, 'Belfast', 54.6, -5.93)`,
);
await rawExec(
orm,
`INSERT INTO place_regions (place_id, country_code, region_code, region_name) VALUES
(1, 'IE', 'IE-L', 'Leinster'), (2, 'GB', 'GB-BELFAST', 'Belfast')`,
);
await migrateTo(orm, TARGET);
expect(await rawQuery(orm, 'SELECT place_id, region_code FROM place_regions ORDER BY place_id')).toEqual([
{ place_id: 2, region_code: 'GB-BELFAST' },
]);
});
it('IE-COUNTIES-002: keeps a marked province and a marked county, and moves a county name onto its code', async () => {
orm = await ormBeforeTarget();
await seedUsers();
await rawExec(
orm,
`INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES
(1, 'IE-L', 'Leinster', 'IE'),
(1, 'IE-KY', 'Kerry', 'IE'),
(1, 'IE-COR', 'County Cork', 'IE'),
(1, 'IE-GALWAY', 'Co. Galway', 'ie'),
(1, 'FR-IDF', 'Ile-de-France', 'FR')`,
);
await migrateTo(orm, TARGET);
expect(await visited(1)).toEqual([
{ region_code: 'FR-IDF', region_name: 'Ile-de-France', country_code: 'FR' },
{ region_code: 'IE-CO', region_name: 'Cork', country_code: 'IE' },
{ region_code: 'IE-G', region_name: 'Galway', country_code: 'ie' },
{ region_code: 'IE-KY', region_name: 'Kerry', country_code: 'IE' },
{ region_code: 'IE-L', region_name: 'Leinster', country_code: 'IE' },
]);
});
it('IE-COUNTIES-003: drops a stale row whose county the user already holds, never touching another user', async () => {
orm = await ormBeforeTarget();
await seedUsers();
await rawExec(
orm,
`INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES
(1, 'IE-D', 'Dublin', 'IE'),
(1, 'IE-DUB', 'County Dublin', 'IE'),
(1, 'IE-DN', 'dublin', 'IE'),
(2, 'IE-DUB', 'County Dublin', 'IE')`,
);
await migrateTo(orm, TARGET);
expect(await visited(1)).toEqual([{ region_code: 'IE-D', region_name: 'Dublin', country_code: 'IE' }]);
expect(await visited(2)).toEqual([{ region_code: 'IE-D', region_name: 'Dublin', country_code: 'IE' }]);
});
it('IE-COUNTIES-004: a hidden province hides each of its counties, once, and stays hidden itself', async () => {
orm = await ormBeforeTarget();
await seedUsers();
await rawExec(
orm,
`INSERT INTO hidden_regions (user_id, region_code, country_code) VALUES
(1, 'IE-L', 'IE'),
(1, 'IE-D', 'IE'),
(2, 'IE-U', 'IE'),
(2, 'FR-IDF', 'FR')`,
);
await migrateTo(orm, TARGET);
expect(await hidden(1)).toEqual([...LEINSTER, 'IE-L'].sort());
expect(await hidden(2)).toEqual(['FR-IDF', 'IE-CN', 'IE-DL', 'IE-MN', 'IE-U']);
});
});
@@ -105,6 +105,7 @@ const UNNUMBERED = [
'Migration20200101046350_a_place_lies_on_the_real_world',
'Migration20200101046360_a_new_install_starts_with_more_addons_on',
'Migration20200101046370_a_deleted_chat_message_keeps_no_text',
'Migration20200101046380_ireland_gained_its_counties',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -1,4 +1,4 @@
import { getRegionGeoGz, getRegionGeoJson } from '../../../src/nest/atlas/atlas-geo';
import { getRegionFromCoords, getRegionGeoGz, getRegionGeoJson } from '../../../src/nest/atlas/atlas-geo';
import { describe, it, expect } from 'vitest';
import zlib from 'zlib';
@@ -57,3 +57,33 @@ describe('getRegionGeoGz', () => {
expect(await Promise.all(codes.map((code) => getRegionGeoGz(code)))).toEqual(codes.map(() => null));
});
});
describe('Ireland resolves to its counties (#2922)', () => {
// Inland, coastal and near-border places, one county each. The counties come from
// Tailte Éireann and are simplified by the builder itself, so a point that lands in no
// county or in the wrong one would mean the simplification opened a gap or moved a border.
const places: [string, number, number, string][] = [
['Dublin, Trinity College', 53.3438, -6.2546, 'IE-D'],
['Howth village', 53.3866, -6.07, 'IE-D'],
['Cork, St. Patrick Street', 51.8985, -8.4731, 'IE-CO'],
['Galway, Eyre Square', 53.2743, -9.0491, 'IE-G'],
['Killarney', 52.0599, -9.5044, 'IE-KY'],
['Cliffs of Moher visitor centre', 52.9719, -9.4265, 'IE-CE'],
['Kilkenny Castle', 52.6503, -7.2492, 'IE-KK'],
['Letterkenny', 54.9503, -7.7334, 'IE-DL'],
['Westport', 53.8008, -9.5223, 'IE-MO'],
['Cashel, Rock of Cashel', 52.5201, -7.8906, 'IE-TA'],
];
it.each(places)('ATLAS-SVC-051: %s lies in its county', async (_name, lat, lng, code) => {
expect((await getRegionFromCoords('IE', lat, lng))?.region_code).toBe(code);
});
it('ATLAS-SVC-052: a county comes back under its English name', async () => {
expect(await getRegionFromCoords('ie', 53.3438, -6.2546)).toEqual({
country_code: 'IE',
region_code: 'IE-D',
region_name: 'Dublin',
});
});
});
@@ -152,6 +152,26 @@ describe('Atlas admin1 region bundle (#1217)', () => {
}
});
/*
* geoBoundaries has no county layer for Ireland: its ADM1 is the four provinces and
* its ADM2 the 166 local electoral areas, so Ireland showed four provinces next to
* a UK of counties (#2922). Ireland's regions come from Tailte Éireann's 26 counties
* instead, under their ISO 3166-2:IE codes.
*/
it('ATLAS-BUNDLE-011 — Ireland ships its 26 counties, not the four provinces (#2922)', () => {
const ie = regions('IE') as unknown as { properties: { iso_3166_2: string; name: string; admin: string } }[];
const codes = ie.map((f) => f.properties.iso_3166_2).sort();
// ISO 3166-2:IE, Dublin, Cork, Galway and Limerick each one county (city and county together).
const iso =
'IE-CE IE-CN IE-CO IE-CW IE-D IE-DL IE-G IE-KE IE-KK IE-KY IE-LD IE-LH IE-LK ' +
'IE-LM IE-LS IE-MH IE-MN IE-MO IE-OY IE-RN IE-SO IE-TA IE-WD IE-WH IE-WW IE-WX';
expect(codes).toEqual(iso.split(' '));
const byCode = new Map(ie.map((f) => [f.properties.iso_3166_2, f.properties]));
expect(byCode.get('IE-D')?.name).toBe('Dublin');
expect(byCode.get('IE-TA')?.name).toBe('Tipperary');
expect(new Set(ie.map((f) => f.properties.admin))).toEqual(new Set(['Ireland']));
});
it('ATLAS-BUNDLE-006 — China ships Guangdong Province, not Guangzhou Province', () => {
const cn = (
features as unknown as { properties: { iso_a2: string | null; name?: string; iso_3166_2: string } }[]
@@ -0,0 +1,161 @@
/**
* scripts/lib/shared-border-simplify.mjs simplifies a country's region outlines for the
* Atlas builder when the source has no simplified version of its own (Tailte Éireann's
* counties, #2922). What matters is that two regions sharing a border keep the same
* points on it, so no point between them falls into a sliver that belongs to neither.
*/
import { douglasPeucker, simplifySharedBorders } from '../../../scripts/lib/shared-border-simplify.mjs';
import { describe, expect, it } from 'vitest';
type Ring = number[][];
type Geometry = { type: 'Polygon'; coordinates: Ring[] } | { type: 'MultiPolygon'; coordinates: Ring[][] };
type Feature = { type: 'Feature'; properties: Record<string, unknown>; geometry: Geometry | null };
const polygon = (name: string, ...rings: Ring[]): Feature => ({
type: 'Feature',
properties: { name },
geometry: { type: 'Polygon', coordinates: rings },
});
// A border from about (1,0) up to about (1,1) that swings out and back, quantized like the builder's input.
const border: Ring = Array.from({ length: 41 }, (_, i) => [
Math.round((1 + 0.05 * Math.sin(i * 0.7)) * 1000) / 1000,
i / 40,
]);
/**
* Two neighbours: west runs up the shared border, east runs down it from a different
* start. Simplified each on its own, these two keep different points on the border
* (SIMPLIFY-003 checks that the fixture really does that).
*/
function neighbours(): Feature[] {
const west = [[0, 0], ...border, [0, 1], [0, 0]];
const east = [[2, 0], [2, 1], ...border.slice().reverse(), [2, 0]];
return [polygon('west', west), polygon('east', east)];
}
const outer = (f: Feature) => (f.geometry as { coordinates: Ring[] }).coordinates[0];
// The points of a ring on the shared border, its repeated closing point left out.
const onBorder = (ring: Ring) =>
ring
.slice(0, -1)
.filter(([x]) => x > 0.5 && x < 1.5)
.map((p) => p.join(','))
.sort();
describe('douglasPeucker', () => {
it('SIMPLIFY-001: keeps both ends and drops points within the tolerance', () => {
expect(
douglasPeucker(
[
[0, 0],
[1, 0.001],
[2, 0],
[3, 1],
],
0.01,
),
).toEqual([
[0, 0],
[2, 0],
[3, 1],
]);
});
it('SIMPLIFY-002: leaves a line of two points or fewer as it is', () => {
expect(
douglasPeucker(
[
[0, 0],
[1, 1],
],
5,
),
).toEqual([
[0, 0],
[1, 1],
]);
});
});
describe('simplifySharedBorders', () => {
it('SIMPLIFY-003: two neighbours keep exactly the same points on the border they share', () => {
const [west, east] = neighbours();
// Each polygon simplified on its own drifts apart on the border: the bug this avoids.
expect(onBorder(douglasPeucker(outer(west), 0.01))).not.toEqual(onBorder(douglasPeucker(outer(east), 0.01)));
const [westOut, eastOut] = simplifySharedBorders([west, east], 0.01) as Feature[];
expect(onBorder(outer(westOut))).toEqual(onBorder(outer(eastOut)));
expect(onBorder(outer(westOut)).length).toBeLessThan(border.length);
// Its two ends are where three outlines meet, and stay put.
expect(onBorder(outer(westOut))).toEqual(expect.arrayContaining([border[0].join(','), border[40].join(',')]));
});
it('SIMPLIFY-004: the result does not depend on which neighbour comes first', () => {
const [east, west] = simplifySharedBorders(neighbours().reverse(), 0.01) as Feature[];
const [west2, east2] = simplifySharedBorders(neighbours(), 0.01) as Feature[];
expect(outer(west)).toEqual(outer(west2));
expect(outer(east)).toEqual(outer(east2));
});
it('SIMPLIFY-005: an enclave shared whole with the hole around it simplifies the same on both sides', () => {
const circle = (reverse: boolean): Ring => {
const pts = Array.from({ length: 40 }, (_, i) => {
const a = (i / 40) * 2 * Math.PI;
return [Math.round((5 + Math.cos(a)) * 1000) / 1000, Math.round((5 + Math.sin(a)) * 1000) / 1000];
});
const ring = reverse ? pts.reverse() : pts;
return [...ring, ring[0]];
};
const square: Ring = [
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0],
];
const [around, enclave] = simplifySharedBorders(
[polygon('around', square, circle(true)), polygon('enclave', circle(false))],
0.05,
) as Feature[];
const hole = (around.geometry as { coordinates: Ring[] }).coordinates[1];
const key = (ring: Ring) => ring.map((p) => p.join(',')).sort();
expect(key(hole)).toEqual(key(outer(enclave)));
expect(outer(enclave).length).toBeLessThan(41);
});
it('SIMPLIFY-006: a polygon too small to keep is dropped, and a feature with nothing left gets no geometry', () => {
const speck: Ring = [
[0, 0],
[0.001, 0],
[0.001, 0.001],
[0, 0],
];
const island = polygon('island', speck);
const [out] = simplifySharedBorders([island], 0.01) as Feature[];
expect(out.geometry).toBeNull();
expect(out.properties).toEqual({ name: 'island' });
});
it('SIMPLIFY-007: a MultiPolygon keeps the parts that survive and becomes a Polygon when one is left', () => {
const big: Ring = [
[0, 0],
[4, 0],
[4, 4],
[0, 4],
[0, 0],
];
const speck: Ring = [
[9, 9],
[9.001, 9],
[9.001, 9.001],
[9, 9],
];
const [out] = simplifySharedBorders(
[{ type: 'Feature', properties: {}, geometry: { type: 'MultiPolygon', coordinates: [[big], [speck]] } }],
0.01,
) as Feature[];
expect(out.geometry).toEqual({ type: 'Polygon', coordinates: [big] });
});
});