Files
gods-eye-view/src/data/wikidataCities.test.mjs
T
Manjunath PaparajuandSameh Khamis 1dc5a67518 Bundle Wikidata cities for offline place lookups
A pack of the world's settlements of 15,000 people or more (31,827 places,
1.6 MB, CC0) built from the Wikidata Query Service by
scripts/build-wikidata-cities.mjs. src/data/wikidataCities.js reads it on the
first lookup and answers the nearest city to a point, the cities around it,
a city by name ("Paris, France", or the nearest namesake in view) and a
country's name in the page language. Names match without accents or case.

The build leaves out groupings of settlements (metropolitan areas,
agglomerations) and places that no longer stand, keeps a city that is also a
narrower grouping kind (Istanbul) or a unit dissolved where nothing else
names the spot (the Vietnamese provincial cities of 2025), fills a country
from the area a place lies in, splits a population band the service cannot
answer in time, and fails if well-known cities are missing or a known
grouping gets in. The app never calls Wikidata itself.

Co-authored-by: Sameh Khamis <[email protected]>
2026-11-14 19:27:46 -08:00

122 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// src/data/wikidataCities.test.mjs — lookups over the bundled Wikidata
// places packs, with the packs served from memory.
import test from 'node:test';
import assert from 'node:assert/strict';
import {
citiesWithin,
foldName,
indexCities,
lonHalfSpan,
} from './wikidataCities.js';
const ROWS = [
['São Paulo', -23.55, -46.633, 11451999, 'BR', 'Q174'],
['Austin', 30.267, -97.743, 961855, 'US', 'Q16559'],
['Round Rock', 30.508, -97.679, 119468, 'US', 'Q128269'],
['Zürich', 47.374, 8.541, 421878, 'CH', 'Q72'],
];
test('names compare without case or accents', () => {
assert.equal(foldName('São Paulo'), 'sao paulo');
assert.equal(foldName(' ZÜRICH '), 'zurich');
assert.equal(foldName(null), '');
});
test('a lookup reads the cells around a point and measures each place', () => {
const cells = indexCities(ROWS);
const near = citiesWithin(cells, 30.27, -97.74, 40).sort(
(a, b) => a.distanceKm - b.distanceKm,
);
assert.deepEqual(
near.map((city) => city.name),
['Austin', 'Round Rock'],
);
assert.ok(near[0].distanceKm < 1);
assert.equal(near[1].country, 'US');
assert.deepEqual(citiesWithin(cells, 0, 0, 50), []);
});
test('findPlace finds a city by its name without accents', async () => {
// A fresh module, so its pack cache starts empty.
const { findPlace } = await import(`./wikidataCities.js?case=${Date.now()}`);
const fetchImpl = async (url) => ({
ok: true,
json: async () =>
String(url).includes('cities.json') ? { meta: {}, rows: ROWS } : null,
});
const found = await findPlace('sao paulo, Brazil', {}, fetchImpl);
assert.equal(found?.wikidata, 'Q174');
assert.equal(await findPlace('Atlantis', {}, fetchImpl), null);
});
test('names fold letters that carry no separate accent', () => {
assert.equal(foldName('Łódź'), 'lodz');
assert.equal(foldName('Tromsø'), 'tromso');
assert.equal(foldName('Đà Nẵng'), 'da nang');
assert.equal(foldName('Diyarbakır'), 'diyarbakir');
assert.equal(foldName('Straße'), 'strasse');
});
const PARIS = [
['Paris', 48.857, 2.352, 2145906, 'FR', 'Q90'],
['Paris', 33.661, -95.556, 24476, 'US', 'Q830149'],
];
async function freshPack(rows) {
const module = await import(
`./wikidataCities.js?case=${Date.now()}-${Math.random()}`
);
const fetchImpl = async () => ({
ok: true,
json: async () => ({ meta: {}, rows }),
});
return { ...module, fetchImpl };
}
test('findPlace takes a country qualifier and prefers a namesake in view', async () => {
const { findPlace, fetchImpl } = await freshPack(PARIS);
const texas = { latitude: 33.2, longitude: -96.0 };
assert.equal((await findPlace('Paris', {}, fetchImpl)).wikidata, 'Q90');
assert.equal(
(await findPlace('Paris', { near: texas }, fetchImpl)).wikidata,
'Q830149',
);
assert.equal(
(await findPlace('Paris, United States', {}, fetchImpl)).wikidata,
'Q830149',
);
assert.equal(
(await findPlace('paris, us', {}, fetchImpl)).wikidata,
'Q830149',
);
// A qualifier the packs cannot place is ignored.
assert.equal(
(await findPlace('Paris, Texas', { near: texas }, 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);
const cells = indexCities([['Pole camp', 89.9, 179, 1, '', 'Q1']]);
assert.equal(citiesWithin(cells, 89, 0, 150).length, 1);
});
test('a country qualifier that matches no namesake finds nothing', async () => {
const { findPlace, fetchImpl } = await freshPack([
...PARIS,
['Berlin', 52.52, 13.405, 3755251, 'DE', 'Q64'],
]);
assert.equal(await findPlace('Paris, Germany', {}, fetchImpl), null);
assert.equal(
(await findPlace('Paris, France', {}, fetchImpl)).wikidata,
'Q90',
);
});
test('a lookup wraps at the antimeridian', () => {
const cells = indexCities([['Taveuni', -16.8, -179.9, 15000, 'FJ', 'Q2']]);
assert.equal(citiesWithin(cells, -16.8, 179.9, 40).length, 1);
});