diff --git a/scripts/build-wikidata-cities.mjs b/scripts/build-wikidata-cities.mjs index 1c66eea87b..d2016fa3d7 100644 --- a/scripts/build-wikidata-cities.mjs +++ b/scripts/build-wikidata-cities.mjs @@ -40,6 +40,7 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { keepBuiltIfUnchanged, + keepPackBuiltIfUnchanged, placeTileFiles, withoutCities, withoutNearTwins, @@ -505,25 +506,11 @@ if (tilesMode) { // The new files are all written before tiles from an earlier build that // this one has no rows for go, so a build that stops part way never leaves // an index naming a removed tile. - // The index holds only row counts, so its build date is kept only when - // every tile is unchanged too and no earlier tile is left over. - const earlierTiles = (await readdir(outDir)).filter((name) => - /^-?\d+_-?\d+\.json$/.test(name), - ); - let tilesUnchanged = earlierTiles.every((name) => files.has(name)); - for (const [name, text] of files) { - if (!tilesUnchanged) break; - if (name === 'index.json') continue; - tilesUnchanged = (await readIfThere(path.join(outDir, name))) === text; - } - if (tilesUnchanged) - files.set( - 'index.json', - keepBuiltIfUnchanged( - files.get('index.json'), - await readIfThere(path.join(outDir, 'index.json')), - ), - ); + const earlier = new Map(); + for (const name of await readdir(outDir)) + if (/^-?\d+_-?\d+\.json$|^index\.json$|^files\.js$/.test(name)) + earlier.set(name, await readFile(path.join(outDir, name), 'utf8')); + keepPackBuiltIfUnchanged(files, earlier); for (const [name, text] of files) await writeFile(path.join(outDir, name), text); for (const name of await readdir(outDir)) diff --git a/scripts/wikidataPlaceTiles.mjs b/scripts/wikidataPlaceTiles.mjs index 98c2b10651..d3396937f6 100644 --- a/scripts/wikidataPlaceTiles.mjs +++ b/scripts/wikidataPlaceTiles.mjs @@ -114,6 +114,26 @@ export function keepBuiltIfUnchanged(text, earlier) { return `${JSON.stringify({ ...after, meta: { ...after.meta, built: before.meta.built } })}\n`; } +/** + * Keeps the earlier build's `meta.built` in the places pack's index when the + * rebuild changed nothing: every tile and `files.js` the same text, no tile + * of the earlier build left out, and nothing else in the index changed. The + * index holds only row counts, so it alone cannot tell. + * @param {Map} files The new pack, as placeTileFiles gives it; its index is replaced in place. + * @param {Map} earlier The pack's files on disk now, by name. + */ +export function keepPackBuiltIfUnchanged(files, earlier) { + for (const name of earlier.keys()) + if (name !== 'index.json' && !files.has(name)) return files; + for (const [name, text] of files) + if (name !== 'index.json' && earlier.get(name) !== text) return files; + files.set( + 'index.json', + keepBuiltIfUnchanged(files.get('index.json'), earlier.get('index.json')), + ); + return files; +} + const kmBetween = (a, b) => greatCircleKm({ lat: a[1], lon: a[2] }, { lat: b[1], lon: b[2] }); diff --git a/src/data/wikidataCities.test.mjs b/src/data/wikidataCities.test.mjs index 84c5a8c112..4c62b1b65b 100644 --- a/src/data/wikidataCities.test.mjs +++ b/src/data/wikidataCities.test.mjs @@ -115,6 +115,17 @@ test('a small place in view comes before a far bigger city of its name', async ( const austin = { latitude: 30.27, longitude: -97.74 }; assert.equal((await find('Paris', austin)).wikidata, 'Q-village'); assert.ok((await find('Paris', austin)).distanceKm < 20); + // A bigger namesake inside a wider search: the nearer village still wins. + assert.equal( + ( + await module.findPlace( + 'Paris', + { near: austin, radiusKm: 600, includeSmallPlaces: true }, + fetchImpl, + ) + ).wikidata, + 'Q-village', + ); // Only when small places are asked for. assert.equal( (await module.findPlace('Paris', { near: austin }, fetchImpl)).wikidata, diff --git a/src/tooling/wikidataPlaceTiles.test.mjs b/src/tooling/wikidataPlaceTiles.test.mjs index 10e77b8dd3..9f807c20e5 100644 --- a/src/tooling/wikidataPlaceTiles.test.mjs +++ b/src/tooling/wikidataPlaceTiles.test.mjs @@ -9,6 +9,7 @@ import { readdir, readFile } from 'node:fs/promises'; import test from 'node:test'; import { keepBuiltIfUnchanged, + keepPackBuiltIfUnchanged, placeTileFiles, tileKey, withoutCities, @@ -114,3 +115,31 @@ test('a rebuild of unchanged rows keeps the earlier build stamp', () => { assert.equal(keepBuiltIfUnchanged(same, undefined), same); assert.equal(keepBuiltIfUnchanged(same, 'not json'), same); }); + +test('the places index keeps its build stamp only when no tile changed', () => { + const rows = [ + ['Aa', 31, -98, 2000, 'US', 'Q1'], + ['Bb', 41, -88, 1500, 'US', 'Q2'], + ]; + const meta = (built) => ({ + source: 's', + license: 'l', + built, + minPopulation: 1000, + maxPopulation: 15000, + columns: [], + }); + const earlier = placeTileFiles(rows, meta('2026-01-01T00:00:00.000Z')); + const built = (files) => JSON.parse(files.get('index.json')).meta.built; + const rebuild = (newRows) => + keepPackBuiltIfUnchanged( + placeTileFiles(newRows, meta('2026-11-18T00:00:00.000Z')), + earlier, + ); + assert.equal(built(rebuild(rows)), '2026-01-01T00:00:00.000Z'); + // Same row count in every tile, new population: a new stamp. + const changed = [['Aa', 31, -98, 2100, 'US', 'Q1'], rows[1]]; + assert.equal(built(rebuild(changed)), '2026-11-18T00:00:00.000Z'); + // A tile of the earlier build left out: a new stamp. + assert.equal(built(rebuild([rows[0]])), '2026-11-18T00:00:00.000Z'); +});