diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 724fa05523..f51d96efcb 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -8253,7 +8253,9 @@ the name of a city within 3 km in the same country is that city, and only the city is listed. A place named in "villages near …" is the nearest of that name within 150 km of the view, small places included, so a village in view comes before a bigger namesake far away; with none near, the largest -city of the name. At most 64 detail tiles are kept in memory, the least +city of the name. Small places are looked for within 500 km at most, so a +view zoomed out to the whole globe reads only the detail tiles around its +centre. At most 64 detail tiles are kept in memory, the least recently used dropped first. ### Place-search providers diff --git a/src/data/local_data/wikidata_places/README.md b/src/data/local_data/wikidata_places/README.md index 0694fe5c5c..144f907209 100644 --- a/src/data/local_data/wikidata_places/README.md +++ b/src/data/local_data/wikidata_places/README.md @@ -20,7 +20,11 @@ entry in the index. **Loading:** nothing here loads with the app. A lookup reads `files.js` and the index once, then only the tiles its search box touches (one for a short radius well inside a tile, more near tile edges and for wider searches), -once each; at most 64 tiles are kept in memory, the least recently used +once each. The search box reaches 500 km at most (`PLACE_TILE_KM`), so a +view of the whole globe reads only the tiles around its centre: at most one +tile past the centre's own on each side, a few more columns toward the +poles, and never all 732. Cities still count to the full radius. At most +64 tiles are kept in memory, the least recently used dropped first. Rows whose name has no letter (one row, `380` in Nicaragua) are dropped when a tile is read. @@ -42,7 +46,10 @@ names from Wikidata (CC0)" as a courtesy once a name from the pack is shown. `node scripts/build-wikidata-cities.mjs --tiles --min-population 1000 --max-population 15000`. It writes only to this folder (tiles, `index.json`, `files.js`, through `scripts/wikidataPlaceTiles.mjs`) and leaves `../wikidata_cities` alone. It -runs one query at a time with pauses and takes a long while. +runs one query at a time with pauses and takes a long while. The new files +are all written before tiles that the new build has no rows for are +removed, and `meta.built` keeps its earlier date when nothing else in +`index.json` changed. **Reproducibility:** the step from rows to files is checked offline: `src/tooling/wikidataPlaceTiles.test.mjs` sorts the shipped rows as the diff --git a/src/data/wikidataCities.js b/src/data/wikidataCities.js index 1cc7eae468..2b62ac62b0 100644 --- a/src/data/wikidataCities.js +++ b/src/data/wikidataCities.js @@ -217,6 +217,14 @@ const TILE_DEG = 5; * far more than one lookup touches, and bounds what a long session keeps. */ const MAX_TILES = 64; +/** + * How far out small places are looked for, in km. 500 km is under the 556 km + * that 5 degrees of latitude span, so the search box reaches at most one tile + * past the point's own on each side: 3 rows, and 3 columns at the equator + * (more toward the poles, where tiles narrow). A wider view, out to the whole + * globe, still reads only those tiles; cities count to its full radius. + */ +export const PLACE_TILE_KM = 500; const NAMED = /\p{L}/u; const tiles = new Map(); let loadTileIndex; @@ -248,9 +256,11 @@ async function readPlaceTiles(latitude, longitude, radiusKm, fetchImpl) { readPlacePack(INDEX_URL(), fetchImpl), ); const index = await loadTileIndex(); - const keys = tileKeysAround(latitude, longitude, radiusKm).filter( - (key) => index.tiles[key], - ); + const keys = tileKeysAround( + latitude, + longitude, + Math.min(radiusKm, PLACE_TILE_KM), + ).filter((key) => index.tiles[key]); // A tile that cannot be read is left out: the places of the others, // and the cities, are still the answer. const settled = await Promise.allSettled( @@ -281,7 +291,10 @@ async function readPlacePack(url, fetchImpl) { return response.json(); } -/** Named cities and smaller places around one point, nearest first. */ +/** + * Named cities and smaller places around one point, nearest first. Smaller + * places are looked for within PLACE_TILE_KM only. + */ export async function placesNear( { latitude, longitude, radiusKm = 10, limit = 6 }, fetchImpl = (...args) => globalThis.fetch(...args), diff --git a/src/data/wikidataCities.test.mjs b/src/data/wikidataCities.test.mjs index dd3259efa1..10cec8c834 100644 --- a/src/data/wikidataCities.test.mjs +++ b/src/data/wikidataCities.test.mjs @@ -281,3 +281,42 @@ test('a tile that cannot be read leaves the other places and the cities', async assert.ok(near.some((place) => place.name === 'Austin')); assert.ok(near.some((place) => place.longitude > -95)); }); + +test('a view of the whole globe reads only the tiles near its centre', async () => { + const { placesNear, PLACE_TILE_KM } = + await import('./wikidataCities.js?globe'); + const fetched = []; + const fetchPack = async (url) => { + fetched.push(url.pathname.split('/').at(-1)); + return new Response(await readFile(url)); + }; + const near = await placesNear( + { latitude: 30.27, longitude: -97.74, radiusKm: 20000, limit: 50 }, + fetchPack, + ); + const tileReads = fetched + .filter((name) => /^-?\d+_-?\d+\.json$/.test(name)) + .sort(); + // 500 km around Austin: latitude 25.8 to 34.8, longitude -103 to -92.5. + assert.equal(PLACE_TILE_KM, 500); + assert.deepEqual(tileReads, [ + '25_-100.json', + '25_-105.json', + '25_-95.json', + '30_-100.json', + '30_-105.json', + '30_-95.json', + ]); + assert.equal(near.length, 50); + assert.ok(near.every((place) => place.distanceKm <= PLACE_TILE_KM)); + // Cities still count to the full radius. + const far = await placesNear( + { latitude: 30.27, longitude: -97.74, radiusKm: 20000, limit: Infinity }, + fetchPack, + ); + assert.ok(far.some((place) => place.wikidata === 'Q90')); + assert.equal( + fetched.filter((name) => /^-?\d+_-?\d+\.json$/.test(name)).length, + tileReads.length, + ); +});