diff --git a/build/vite.js b/build/vite.js index 29918290e3..883c0db62f 100644 --- a/build/vite.js +++ b/build/vite.js @@ -258,6 +258,12 @@ export function createBrowserViteConfig({ mapillaryToken ?? '', ), }, - build: { chunkSizeWarningLimit: 1500 }, + build: { + chunkSizeWarningLimit: 1500, + // The places pack's tiles stay files, read only when a lookup touches + // them; inlined, the small ones would all load with the first lookup. + assetsInlineLimit: (file) => + file.includes('/wikidata_places/') ? false : undefined, + }, }; } diff --git a/scripts/build-wikidata-cities.mjs b/scripts/build-wikidata-cities.mjs index 044fc158f1..a276062831 100644 --- a/scripts/build-wikidata-cities.mjs +++ b/scripts/build-wikidata-cities.mjs @@ -53,12 +53,11 @@ const LABEL_BATCH = 400; const args = process.argv.slice(2); const flag = (name) => - args.includes(name) ? Number(args[args.indexOf(name) + 1]) : NaN; + args.includes(name) ? Number(args[args.indexOf(name) + 1]) : undefined; const tilesMode = args.includes('--tiles'); -const minPopulation = flag('--min-population') || (tilesMode ? 1000 : 15000); -const maxPopulation = tilesMode - ? flag('--max-population') || 15000 - : flag('--max-population') || Infinity; +const minPopulation = flag('--min-population') ?? (tilesMode ? 1000 : 15000); +const maxPopulation = + flag('--max-population') ?? (tilesMode ? 15000 : Infinity); if (!(maxPopulation > minPopulation)) throw new Error('--max-population must be above --min-population'); const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); diff --git a/scripts/wikidataPlaceTiles.mjs b/scripts/wikidataPlaceTiles.mjs index 8621621450..b874734472 100644 --- a/scripts/wikidataPlaceTiles.mjs +++ b/scripts/wikidataPlaceTiles.mjs @@ -33,7 +33,10 @@ export function withoutCities(rows, cityIds) { /** The tile a point falls in, named by its south-west corner. */ export function tileKey(lat, lon) { const corner = (value) => Math.floor(value / TILE_DEGREES) * TILE_DEGREES; - return `${corner(lat)}_${corner(lon)}`; + // As the lookups read them: latitude 90 is in the top row of tiles, and + // longitude 180 is longitude -180. + const top = 90 - TILE_DEGREES; + return `${Math.min(top, corner(lat))}_${corner((((lon % 360) + 540) % 360) - 180)}`; } /** diff --git a/src/data/wikidataCities.js b/src/data/wikidataCities.js index 7066492839..70dcb3e67a 100644 --- a/src/data/wikidataCities.js +++ b/src/data/wikidataCities.js @@ -251,7 +251,9 @@ async function readPlaceTiles(latitude, longitude, radiusKm, fetchImpl) { const keys = tileKeysAround(latitude, longitude, radiusKm).filter( (key) => index.tiles[key], ); - return Promise.all( + // 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( keys.map((key) => { let load = tiles.get(key); if (load) tiles.delete(key); @@ -267,6 +269,9 @@ async function readPlaceTiles(latitude, longitude, radiusKm, fetchImpl) { return load(); }), ); + return settled + .filter((result) => result.status === 'fulfilled') + .map((result) => result.value); } async function readPlacePack(url, fetchImpl) { @@ -292,7 +297,8 @@ export async function placesNear( const radius = Math.min(radiusKm, Math.PI * EARTH_KM); const [{ cells }, detail] = await Promise.all([ loadCities(fetchImpl), - readPlaceTiles(latitude, longitude, radius, fetchImpl), + // Without the pack's index there are still the cities. + readPlaceTiles(latitude, longitude, radius, fetchImpl).catch(() => []), ]); // A smaller place that has the name of a city within TWIN_KM in the same // country is the same place listed twice (a town and its municipality): diff --git a/src/data/wikidataCities.test.mjs b/src/data/wikidataCities.test.mjs index b7c9b904a0..ae96f1e462 100644 --- a/src/data/wikidataCities.test.mjs +++ b/src/data/wikidataCities.test.mjs @@ -217,3 +217,18 @@ test('the detail tile cache keeps the 64 most recently used tiles', async () => await placesNear(around(keys[0]), fetchPack); assert.equal(tileReads().length, 66); }); + +test('a tile that cannot be read leaves the other places and the cities', async () => { + const { placesNear } = await import('./wikidataCities.js?failing'); + const fetchPack = async (url) => + /_-100\.json$/.test(url.pathname) + ? new Response('', { status: 503 }) + : new Response(await readFile(url)); + // Around Austin, on the corner of the 30_-100 and 30_-95 tiles. + const near = await placesNear( + { latitude: 30.27, longitude: -97.74, radiusKm: 300, limit: Infinity }, + fetchPack, + ); + assert.ok(near.some((place) => place.name === 'Austin')); + assert.ok(near.some((place) => place.longitude > -95)); +}); diff --git a/src/tooling/wikidataPlaceTiles.test.mjs b/src/tooling/wikidataPlaceTiles.test.mjs index 0150631987..007c15ed15 100644 --- a/src/tooling/wikidataPlaceTiles.test.mjs +++ b/src/tooling/wikidataPlaceTiles.test.mjs @@ -24,6 +24,8 @@ test('a tile is named by its south-west corner', () => { assert.equal(tileKey(30.343, -97.557), '30_-100'); assert.equal(tileKey(-0.5, 0), '-5_0'); assert.equal(tileKey(10.682, -71.036), '10_-75'); + // In the tiles the lookups read: the top row, and -180 for 180. + assert.equal(tileKey(90, 180), '85_-180'); }); test('re-tiles the shipped rows deterministically', async () => {