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,110 @@
import { Migration } from '@mikro-orm/migrations';
interface RegionRow {
id: number;
user_id: number;
region_code: string;
region_name: string;
}
/** The 26 counties the Atlas now ships for Ireland: ISO 3166-2:IE code, name, province. */
const COUNTIES: ReadonlyArray<readonly [code: string, name: string, province: string]> = [
['IE-CW', 'Carlow', 'IE-L'],
['IE-D', 'Dublin', 'IE-L'],
['IE-KE', 'Kildare', 'IE-L'],
['IE-KK', 'Kilkenny', 'IE-L'],
['IE-LS', 'Laois', 'IE-L'],
['IE-LD', 'Longford', 'IE-L'],
['IE-LH', 'Louth', 'IE-L'],
['IE-MH', 'Meath', 'IE-L'],
['IE-OY', 'Offaly', 'IE-L'],
['IE-WH', 'Westmeath', 'IE-L'],
['IE-WX', 'Wexford', 'IE-L'],
['IE-WW', 'Wicklow', 'IE-L'],
['IE-CE', 'Clare', 'IE-M'],
['IE-CO', 'Cork', 'IE-M'],
['IE-KY', 'Kerry', 'IE-M'],
['IE-LK', 'Limerick', 'IE-M'],
['IE-TA', 'Tipperary', 'IE-M'],
['IE-WD', 'Waterford', 'IE-M'],
['IE-G', 'Galway', 'IE-C'],
['IE-LM', 'Leitrim', 'IE-C'],
['IE-MO', 'Mayo', 'IE-C'],
['IE-RN', 'Roscommon', 'IE-C'],
['IE-SO', 'Sligo', 'IE-C'],
['IE-CN', 'Cavan', 'IE-U'],
['IE-DL', 'Donegal', 'IE-U'],
['IE-MN', 'Monaghan', 'IE-U'],
];
/** "County Dublin", "Co. Dublin" and "dublin" all name Dublin. */
const countyKey = (name: string): string =>
name
.trim()
.toLowerCase()
.replace(/^(county|co\.?)\s+/, '');
/**
* Ireland gained its counties (#2922).
*
* geoBoundaries has no county layer for Ireland, so the Atlas showed the four
* provinces (IE-C, IE-L, IE-M, IE-U) next to a UK of counties. The bundle now ships
* Tailte Éireann's 26 counties under their ISO 3166-2:IE codes. What exists already:
*
* `place_regions` is a re-derivable cache full of province rows that nothing would
* ever re-derive, so Ireland's rows are cleared and come back as counties on the next
* Atlas load, as Great Britain's did when it gained its counties.
*
* `visited_regions` holds what a user marked by hand. A marked province stays as it
* is: which of its counties the user saw is not ours to guess. A row whose name is a
* county under another code (a Natural Earth or geocoder code from older versions)
* takes the county's code, and is dropped where the user already holds that county.
*
* `hidden_regions` remembers a region the user switched off. A hidden province hid
* every place in it, so each of its counties is hidden in its place; the province row
* stays and keeps hiding a marked province.
*
* `bucket_list` is a wish, not a polygon, and is left alone.
*/
export class Migration20200101046380_ireland_gained_its_counties extends Migration {
override name = 'Migration20200101046380_ireland_gained_its_counties';
override async up(): Promise<void> {
await this.execute(`DELETE FROM place_regions WHERE UPPER(COALESCE(country_code, '')) = 'IE'`);
for (const [code, , province] of COUNTIES) {
await this.execute(
`INSERT INTO hidden_regions (user_id, region_code, country_code)
SELECT h.user_id, ?, h.country_code FROM hidden_regions h
WHERE h.region_code = ? AND UPPER(h.country_code) = 'IE'
AND NOT EXISTS (SELECT 1 FROM hidden_regions x WHERE x.user_id = h.user_id AND x.region_code = ?)`,
[code, province, code],
);
}
const rows = (await this.execute(
`SELECT id, user_id, region_code, region_name FROM visited_regions
WHERE UPPER(COALESCE(country_code, '')) = 'IE' ORDER BY id`,
)) as RegionRow[];
const counties = new Map(COUNTIES.map(([code, name]) => [countyKey(name), { code, name }]));
const countyCodes = new Set(COUNTIES.map(([code]) => code));
const held = new Set(rows.map((r) => `${r.user_id}|${r.region_code}`));
for (const row of rows) {
if (countyCodes.has(row.region_code)) continue;
const county = counties.get(countyKey(row.region_name || ''));
if (!county) continue;
const target = `${row.user_id}|${county.code}`;
if (held.has(target)) {
await this.execute(`DELETE FROM visited_regions WHERE id = ?`, [row.id]);
} else {
await this.execute(`UPDATE visited_regions SET region_code = ?, region_name = ? WHERE id = ?`, [
county.code,
county.name,
row.id,
]);
held.add(target);
}
}
}
}