gadm-ts/scripts/boundaries.ts
2026-03-21 10:52:03 +01:00

73 lines
2.4 KiB
TypeScript

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { getBoundaryFromGpkg } from '../src/gpkg-reader.js';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const continentData = JSON.parse(
readFileSync(join(__dirname, '../data/gadm_continent.json'), 'utf-8')
);
async function processCountry(country: string, level: number | undefined) {
console.log(`Processing ${country} at level ${level ?? 'outer'}...`);
try {
const start = Date.now();
const res = await getBoundaryFromGpkg(country, level);
const duration = Date.now() - start;
if (res) {
console.log(`${country}: Successfully cached in ${duration}ms (${res.features.length} features).`);
} else {
console.log(`${country}: No boundary found.`);
}
} catch (e: any) {
console.error(`${country}: Failed - ${e.message}`);
}
}
async function main() {
const argv = await yargs(hideBin(process.argv))
.option('country', {
type: 'string',
description: 'ISO 3 code of the country (e.g. DEU, ESP), or "all" for all countries',
demandOption: true,
alias: 'c'
})
.option('level', {
type: 'number',
description: 'The administrative level to calculate. Omit for the outer boundary.',
alias: 'l'
})
.help()
.argv;
const country = argv.country as string;
const level = argv.level as number | undefined;
if (country.toLowerCase() === 'all') {
// Collect all countries
const allCountries = new Set<string>();
for (const codes of Object.values(continentData)) {
for (const code of codes as string[]) {
allCountries.add(code);
}
}
console.log(`Starting precalculation for ${allCountries.size} countries at level ${level ?? 'outer'}...`);
let current = 1;
for (const code of allCountries) {
console.log(`\n[${current}/${allCountries.size}]`);
await processCountry(code, level);
current++;
}
console.log('\n🎉 Finished precalculating all countries.');
} else {
await processCountry(country.toUpperCase(), level);
}
}
main().catch(console.error);