diff --git a/server/assets/atlas/admin1.geojson.gz b/server/assets/atlas/admin1.geojson.gz index d0a2bfca..0481e99d 100644 Binary files a/server/assets/atlas/admin1.geojson.gz and b/server/assets/atlas/admin1.geojson.gz differ diff --git a/server/scripts/build-atlas-geo.mjs b/server/scripts/build-atlas-geo.mjs index 7d98e446..dd4e06c8 100644 --- a/server/scripts/build-atlas-geo.mjs +++ b/server/scripts/build-atlas-geo.mjs @@ -151,18 +151,37 @@ function normalizeAdm0Feature(f) { function normalizeAdm1(geo, a3, countryName) { if (!geo?.features) return [] + const a2 = A3_TO_A2[a3] || null + // Ensure every region in a country ends up with a distinct iso_3166_2 — the Atlas + // marks/unmarks regions by this code, so duplicates make one mark light up the whole + // country. + const used = new Set() + const uniq = (base) => { + let code = base, n = 2 + while (used.has(code)) code = `${base}-${n++}` + used.add(code) + return code + } return geo.features.map(f => { const name = f.properties?.shapeName || '' const geometry = quantizeGeometry(f.geometry, ADM1_DECIMALS) if (!geometry) return null - const a2 = A3_TO_A2[a3] || null - // shapeISO is a real ISO 3166-2 code for ~90% of features; geoBoundaries leaves the - // rest blank or uses an `XX_YYY` placeholder. Keep real/placeholder codes as-is - // (stable per polygon → manual mark/unmark works, real ones match Nominatim). For - // blank codes, synthesize a stable id mirroring the server's geocode fallback so - // every region is still markable. - let code = f.properties?.shapeISO || '' - if (!code && a2) code = `${a2}-${name.replace(/[^A-Za-z0-9]/g, '').substring(0, 3).toUpperCase()}` + // shapeISO is a real ISO 3166-2 code for most features, but geoBoundaries sometimes + // fills it with the bare country code instead of a subdivision code — e.g. every + // Spanish region gets "ESP", every Chinese "CHN" (also CL/OM). Keep it only when it + // is a real `XX-…` subdivision code and not already taken; otherwise synthesize a + // stable, unique-per-country id from the region name so each region is independently + // markable. + const raw = f.properties?.shapeISO || '' + let code + if (/^[A-Za-z]{2}-[A-Za-z0-9]+$/.test(raw) && !used.has(raw)) { + code = raw + used.add(code) + } else if (a2) { + code = uniq(`${a2}-${name.replace(/[^A-Za-z0-9]/g, '').toUpperCase() || 'RGN'}`) + } else { + code = raw + } return { type: 'Feature', // Property names the Atlas region layer + server getRegionGeo already read. diff --git a/server/tests/unit/services/atlasBundle.test.ts b/server/tests/unit/services/atlasBundle.test.ts new file mode 100644 index 00000000..ef3a469b --- /dev/null +++ b/server/tests/unit/services/atlasBundle.test.ts @@ -0,0 +1,36 @@ +import { describe, it, expect } from 'vitest'; +import fs from 'fs'; +import path from 'path'; +import zlib from 'zlib'; + +// Data-integrity guard for the shipped Atlas region bundle. geoBoundaries fills +// shapeISO with the bare country code for some countries (every Spanish region got +// "ESP", every Chinese "CHN", also CL/OM), which made marking one region light up the +// whole country (#1217). build-atlas-geo.mjs now synthesizes a unique per-region code +// for those; this asserts the shipped bundle actually carries distinct codes. +describe('Atlas admin1 region bundle (#1217)', () => { + const bundlePath = path.join(__dirname, '..', '..', '..', 'assets', 'atlas', 'admin1.geojson.gz'); + const features = JSON.parse(zlib.gunzipSync(fs.readFileSync(bundlePath)).toString()).features as { + properties: { iso_a2: string | null; iso_3166_2: string }; + }[]; + + const regions = (a2: string) => features.filter(f => f.properties.iso_a2 === a2); + + it('ATLAS-BUNDLE-001 — previously-broken countries now have distinct region codes', () => { + for (const a2 of ['ES', 'CN', 'CL', 'OM']) { + const f = regions(a2); + expect(f.length, `${a2} should ship regions`).toBeGreaterThan(1); + expect(new Set(f.map(r => r.properties.iso_3166_2)).size, `${a2} region codes must be unique`).toBe(f.length); + } + }); + + it('ATLAS-BUNDLE-002 — countries with real ISO codes keep them and stay unique', () => { + for (const a2 of ['DE', 'FR', 'US']) { + const f = regions(a2); + expect(f.length).toBeGreaterThan(1); + // real ISO 3166-2 form, e.g. DE-BW + expect(f.some(r => /^[A-Z]{2}-[A-Z0-9]+$/.test(r.properties.iso_3166_2))).toBe(true); + expect(new Set(f.map(r => r.properties.iso_3166_2)).size).toBe(f.length); + } + }); +});