fix(places): look for small places within 500 km, so a whole-globe view reads only the tiles near its centre

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-18 22:34:59 -08:00
1 parent f414df97eb
commit 30ec362c13
4 files changed
+68 -7

No files matched your search

@@ -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
+17 -4
View File
@@ -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),
+39
View File
@@ -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,
);
});