refactor(geography): separate cable sources and bundled lookup modules

This commit is contained in:
Sameh Khamis committed 2026-09-13 18:36:33 -07:00
1 parent 2213c42200
commit 3ce5d2e454
21 files changed
+2423 -1437

No files matched your search

+56 -29
View File
@@ -29,7 +29,8 @@ function ringAreaKm2(ring) {
for (let i = 0; i < n; i++) {
const [lon1, lat1] = ring[i];
const [lon2, lat2] = ring[(i + 1) % n];
sum += toRad(lon2 - lon1) * (2 + Math.sin(toRad(lat1)) + Math.sin(toRad(lat2)));
sum +=
toRad(lon2 - lon1) * (2 + Math.sin(toRad(lat1)) + Math.sin(toRad(lat2)));
}
return Math.abs((sum * EARTH_RADIUS_KM * EARTH_RADIUS_KM) / 2);
}
@@ -37,8 +38,9 @@ function ringAreaKm2(ring) {
function haversineKm(lon1, lat1, lon2, lat2) {
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const h = Math.sin(dLat / 2) ** 2
+ Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
const h =
Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
return 2 * EARTH_RADIUS_KM * Math.asin(Math.min(1, Math.sqrt(h)));
}
@@ -62,34 +64,34 @@ function normalizeName(s) {
* Keys and values are both in normalizeName() form.
*/
const ALIASES = {
'rockies': 'rocky mountains',
'himalaya': 'himalayas',
rockies: 'rocky mountains',
himalaya: 'himalayas',
'the himalaya': 'himalayas',
'alps mountains': 'alps',
'sahara desert': 'sahara',
'gobi': 'gobi desert',
'kalahari': 'kalahari desert',
'atacama': 'desierto de atacama',
gobi: 'gobi desert',
kalahari: 'kalahari desert',
atacama: 'desierto de atacama',
'atacama desert': 'desierto de atacama',
'tibetan plateau': 'plateau of tibet',
'tibet plateau': 'plateau of tibet',
'appalachians': 'appalachian mts',
appalachians: 'appalachian mts',
'appalachian mountains': 'appalachian mts',
'caucasus': 'caucasus mts',
caucasus: 'caucasus mts',
'caucasus mountains': 'caucasus mts',
'balkans': 'balkan pen',
balkans: 'balkan pen',
'balkan peninsula': 'balkan pen',
'andes mountains': 'andes',
'urals': 'ural mountains',
urals: 'ural mountains',
'pyrenees mountains': 'pyrenees',
'arabian gulf': 'persian gulf',
'gulf of arabia': 'persian gulf',
'mediterranean': 'mediterranean sea',
'caribbean': 'caribbean sea',
'baja': 'baja california',
'yucatan': 'pen de yucatan',
mediterranean: 'mediterranean sea',
caribbean: 'caribbean sea',
baja: 'baja california',
yucatan: 'pen de yucatan',
'yucatan peninsula': 'pen de yucatan',
'kamchatka': 'kamchatka peninsula',
kamchatka: 'kamchatka peninsula',
'sierra nevada mountains': 'sierra nevada',
};
@@ -97,8 +99,13 @@ const ALIASES = {
function suffixVariants(norm) {
const v = [];
// "x mountains" ↔ "x mts" (pack uses "Mts."; normalization strips the dot)
if (norm.endsWith(' mountains')) v.push(norm.replace(/ mountains$/, ' mts'), norm.replace(/ mountains$/, ''));
if (norm.endsWith(' mts')) v.push(norm.replace(/ mts$/, ' mountains'), norm.replace(/ mts$/, ''));
if (norm.endsWith(' mountains'))
v.push(
norm.replace(/ mountains$/, ' mts'),
norm.replace(/ mountains$/, ''),
);
if (norm.endsWith(' mts'))
v.push(norm.replace(/ mts$/, ' mountains'), norm.replace(/ mts$/, ''));
// "x desert" ↔ "x"
if (norm.endsWith(' desert')) v.push(norm.replace(/ desert$/, ''));
else v.push(norm + ' desert');
@@ -117,9 +124,14 @@ async function loadPackFile(base) {
// Vite bundles these JSON files as modules; the import attribute is what Node
// needs to load the same files under node:test (same pattern as
// neighborhoodPolygons.js). One path, so no node: import reaches the browser.
const mod = base === 'regions'
? await import('./local_data/natural_earth/regions.json', { with: { type: 'json' } })
: await import('./local_data/natural_earth/marine.json', { with: { type: 'json' } });
const mod =
base === 'regions'
? await import('./local_data/natural_earth/regions.json', {
with: { type: 'json' },
})
: await import('./local_data/natural_earth/marine.json', {
with: { type: 'json' },
});
return mod.default || mod;
}
@@ -129,7 +141,10 @@ function buildEntries(pack, kind) {
const polygons = ft.polygons || [];
if (!polygons.length) continue;
let areaKm2 = 0;
let minLon = Infinity, minLat = Infinity, maxLon = -Infinity, maxLat = -Infinity;
let minLon = Infinity,
minLat = Infinity,
maxLon = -Infinity,
maxLat = -Infinity;
for (const ring of polygons) {
areaKm2 += ringAreaKm2(ring);
for (const [lon, lat] of ring) {
@@ -169,10 +184,14 @@ const loadIndex = createRetryableLoader(async () => {
];
const index = new Map();
for (const entry of _entries) {
for (const key of new Set([normalizeName(entry.name), normalizeName(entry.namealt)])) {
for (const key of new Set([
normalizeName(entry.name),
normalizeName(entry.namealt),
])) {
if (!key) continue;
const list = index.get(key);
if (list) list.push(entry); else index.set(key, [entry]);
if (list) list.push(entry);
else index.set(key, [entry]);
}
}
// duplicate names exist in Natural Earth (e.g. two "Cordillera Oriental",
@@ -207,7 +226,11 @@ export async function findNaturalRegion(query) {
const norm = normalizeName(query);
if (!norm) return null;
const index = await loadIndex();
const candidates = [norm, ALIASES[norm], ...suffixVariants(ALIASES[norm] || norm)];
const candidates = [
norm,
ALIASES[norm],
...suffixVariants(ALIASES[norm] || norm),
];
for (const key of candidates) {
if (!key) continue;
const list = index.get(key);
@@ -248,8 +271,8 @@ export function pointInRing(ring, lat, lon) {
for (let i = 0, j = ring.length - 1; i < ring.length; j = i, i += 1) {
const [xi, yi] = ring[i];
const [xj, yj] = ring[j];
const intersects = (yi > lat) !== (yj > lat)
&& lon < ((xj - xi) * (lat - yi)) / (yj - yi) + xi;
const intersects =
yi > lat !== yj > lat && lon < ((xj - xi) * (lat - yi)) / (yj - yi) + xi;
if (intersects) inside = !inside;
}
return inside;
@@ -274,7 +297,11 @@ export async function lookupNaturalRegionOutline(query, lat, lon) {
const norm = normalizeName(query);
if (!norm || !Number.isFinite(lat) || !Number.isFinite(lon)) return null;
const index = await loadIndex();
const candidates = [norm, ALIASES[norm], ...suffixVariants(ALIASES[norm] || norm)];
const candidates = [
norm,
ALIASES[norm],
...suffixVariants(ALIASES[norm] || norm),
];
const seen = new Set();
for (const key of candidates) {
if (!key || seen.has(key)) continue;