diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 6b0cc95cdb..724fa05523 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -966,7 +966,8 @@ the fleet set loaded at startup, so opening a card fetches no model. country and Wikidata id. `src/data/wikidataCities.js` reads it on the first lookup and answers `nearestCity` (the largest city within 12 km, else the nearest within 60 km), `citiesAround`, `findPlace` ("Paris, France", or the -nearest namesake within 150 km of the view, else the largest) and +nearest namesake within 150 km of the view, else the largest; asked to, it +counts the smaller places of the detail tiles near the view too) and `countryName`. Names match without case or accents ("lodz" finds Łódź). The app never calls Wikidata; `scripts/build-wikidata-cities.mjs` rebuilds the pack and fails if well-known cities are missing or a known grouping such as @@ -8249,7 +8250,10 @@ clears them but the browser's own site data. Setting `PHOTON_URL` empty turns the suggestions off along with it. Small places come from the bundled Wikidata packs and are never looked up online; a small place with the name of a city within 3 km in the same country is that city, and only -the city is listed. At most 64 detail tiles are kept in memory, the least +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 recently used dropped first. ### Place-search providers diff --git a/src/data/local_data/wikidata_cities/README.md b/src/data/local_data/wikidata_cities/README.md index 8c2abf3818..ff80a6dc56 100644 --- a/src/data/local_data/wikidata_cities/README.md +++ b/src/data/local_data/wikidata_cities/README.md @@ -40,3 +40,8 @@ well-known cities is missing from the result, or a known grouping gets in. **Limits:** populations are whatever Wikidata states and differ in date and definition between places (city proper, municipality, urban district). Some districts and suburbs appear as their own rows. + +**Finding a place by name:** `findPlace` takes the nearest place of the name +within 150 km of the view, else the largest city of the name. When asked, +the smaller places of `../wikidata_places` near the view count too, so a +village in view comes before a bigger namesake far away. diff --git a/src/data/local_data/wikidata_places/README.md b/src/data/local_data/wikidata_places/README.md index 2831f89a56..0694fe5c5c 100644 --- a/src/data/local_data/wikidata_places/README.md +++ b/src/data/local_data/wikidata_places/README.md @@ -1,8 +1,11 @@ # Wikidata places detail tiles Smaller towns and villages, below the cut-off of the cities pack -(`../wikidata_cities`), for "places nearby" around one picked or named spot. Read by `placesNear` in `src/data/wikidataCities.js`. The app reads -these files; it does not call Wikidata or any place service for them. +(`../wikidata_cities`), for "places nearby" around one picked or named spot. Read by `placesNear` in `src/data/wikidataCities.js`, and by `findPlace` +when asked to look for a small place by name near the view: a place of that +name in view, or close to it, comes before a bigger city of the name far +away. The app reads these files; it does not call Wikidata or any place +service for them. | File | Contents | |------|----------| diff --git a/src/data/wikidataCities.js b/src/data/wikidataCities.js index 70dcb3e67a..1cc7eae468 100644 --- a/src/data/wikidataCities.js +++ b/src/data/wikidataCities.js @@ -339,12 +339,13 @@ export async function placesNear( const TWIN_KM = 3; /** - * The city with this name, or null. A qualifier after a comma that names a - * country ("Paris, France", "Paris, US") keeps only cities there, and finds + * The place with this name, or null. A qualifier after a comma that names a + * country ("Paris, France", "Paris, US") keeps only places there, and finds * nothing when none is; one that names no country ("Paris, Texas") is - * ignored. A city of that name within `radiusKm` of `near` (the view) wins, - * the nearest first, else the largest city of that name anywhere. Omnibar - * can also ask for a smaller place nearby when the cities pack has no match. + * ignored. A place of that name within `radiusKm` of `near` (the view) wins, + * the nearest first, else the largest city of that name anywhere. With + * `includeSmallPlaces`, the smaller places of the detail tiles near the view + * count too: a village in view comes before a far bigger city of its name. */ export async function findPlace( name, @@ -360,20 +361,6 @@ export async function findPlace( const cities = (names.get(wanted) || []).filter( (row) => !country || row[4] === country, ); - if (!cities.length) { - if (!includeSmallPlaces || !near) return null; - const nearby = await placesNear( - { ...near, radiusKm, limit: Infinity }, - fetchImpl, - ); - return ( - nearby.find( - (place) => - foldName(place.name) === wanted && - (!country || place.country === country), - ) ?? null - ); - } if (Number.isFinite(near?.latitude) && Number.isFinite(near?.longitude)) { let best = null; for (const row of cities) { @@ -381,7 +368,24 @@ export async function findPlace( if (km <= radiusKm && (!best || km < best.distanceKm)) best = toCity(row, km); } + if (includeSmallPlaces) { + const nearby = await placesNear( + { + latitude: near.latitude, + longitude: near.longitude, + radiusKm, + limit: Infinity, + }, + fetchImpl, + ); + const small = nearby.find( + (place) => + foldName(place.name) === wanted && + (!country || place.country === country), + ); + if (small && (!best || small.distanceKm < best.distanceKm)) best = small; + } if (best) return best; } - return toCity(cities[0], 0); + return cities.length ? toCity(cities[0], 0) : null; } diff --git a/src/data/wikidataCities.test.mjs b/src/data/wikidataCities.test.mjs index ae96f1e462..dd3259efa1 100644 --- a/src/data/wikidataCities.test.mjs +++ b/src/data/wikidataCities.test.mjs @@ -96,6 +96,55 @@ test('findPlace takes a country qualifier and prefers a namesake in view', async ); }); +test('a small place in view comes before a far bigger city of its name', async () => { + const module = await import(`./wikidataCities.js?small-${Date.now()}`); + // A village called Paris near Austin, in the 30_-100 detail tile. + const village = ['Paris', 30.4, -97.6, 1200, 'US', 'Q-village']; + const fetchImpl = async (url) => { + const file = url.pathname.split('/').at(-1); + const body = + file === 'cities.json' + ? { meta: {}, rows: PARIS } + : file === 'index.json' + ? { tiles: { '30_-100': 1 } } + : { rows: file === '30_-100.json' ? [village] : [] }; + return { ok: true, json: async () => body }; + }; + const find = (name, near) => + module.findPlace(name, { near, includeSmallPlaces: true }, fetchImpl); + 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); + // Only when small places are asked for. + assert.equal( + (await module.findPlace('Paris', { near: austin }, fetchImpl)).wikidata, + 'Q90', + ); + // A qualifier still decides. + assert.equal((await find('Paris, France', austin)).wikidata, 'Q90'); + // Nowhere near the village: the largest city of the name. + assert.equal( + (await find('Paris', { latitude: 0, longitude: 0 })).wikidata, + 'Q90', + ); + assert.equal( + (await module.findPlace('Paris', {}, fetchImpl)).wikidata, + 'Q90', + ); + // Paris, Texas is nearer a view over it than the village is. + const texas = { latitude: 33.6, longitude: -95.6 }; + assert.equal( + ( + await module.findPlace( + 'Paris', + { near: texas, radiusKm: 600, includeSmallPlaces: true }, + fetchImpl, + ) + ).wikidata, + 'Q830149', + ); +}); + test('a circle that reaches a pole spans every longitude', () => { assert.equal(lonHalfSpan(89, 150), 180); assert.ok(lonHalfSpan(60, 150) < 3);