106 lines
4.5 KiB
JavaScript
106 lines
4.5 KiB
JavaScript
#!/usr/bin/env node
|
|
// Builds lib/maptool/data/geofabrik_regions.geojson — the offline map's
|
|
// region catalog (PmtilesBuilder) — from Geofabrik's own index
|
|
// (https://download.geofabrik.de/index-v1.json): every downloadable country
|
|
// grouped by continent, plus one level of subregions (US states, German
|
|
// Länder, ...), each with its Geofabrik path and boundary polygon (the
|
|
// polygon is what terrain extraction clips to). Rerun to pick up Geofabrik
|
|
// changes: `node bin/geofabrik-regions-sync.mjs`.
|
|
//
|
|
// Codes: a country's ISO 3166-1 alpha-2 code (the same codes the old
|
|
// Europe-only list used, so existing caches/selections stay valid); a
|
|
// subregion's ISO 3166-2 code, or <country>-<geofabrik id>; a region with no
|
|
// ISO code of its own (Canary Islands, Azores, ...) keeps its Geofabrik id.
|
|
import { writeFileSync } from "node:fs";
|
|
|
|
const OUT = new URL("../lib/maptool/data/geofabrik_regions.geojson", import.meta.url).pathname;
|
|
|
|
// Geofabrik's top-level index entries, as the picker's sections.
|
|
const CONTINENTS = {
|
|
europe: "Europe",
|
|
"north-america": "North America",
|
|
"central-america": "Central America",
|
|
"south-america": "South America",
|
|
asia: "Asia",
|
|
russia: "Russia",
|
|
africa: "Africa",
|
|
"australia-oceania": "Australia and Oceania",
|
|
antarctica: "Antarctica",
|
|
};
|
|
|
|
// Aggregates that only repeat countries listed on their own.
|
|
const SKIP = new Set([
|
|
"alps", "britain-and-ireland", "dach", "great-britain", "american-oceania", "sea",
|
|
"us-midwest", "us-northeast", "us-pacific", "us-south", "us-west",
|
|
]);
|
|
|
|
// Countries Geofabrik lists without an ISO code, or with a borrowed one
|
|
// (Clipperton is tagged PF, French Polynesia's).
|
|
const CODE_OVERRIDES = { kosovo: "XK", "ile-de-clipperton": "CP" };
|
|
|
|
const round = (geometry) => {
|
|
const r = (c) => (typeof c[0] === "number" ? [Math.round(c[0] * 1e4) / 1e4, Math.round(c[1] * 1e4) / 1e4] : c.map(r));
|
|
return { type: geometry.type, coordinates: r(geometry.coordinates) };
|
|
};
|
|
|
|
const index = await (await fetch("https://download.geofabrik.de/index-v1.json")).json();
|
|
const byId = new Map(index.features.map((f) => [f.properties.id, f]));
|
|
const slug = (f) => new URL(f.properties.urls.pbf).pathname.replace(/^\//, "").replace(/-latest\.osm\.pbf$/, "");
|
|
|
|
const regions = [];
|
|
const countryCode = new Map(); // geofabrik id -> country code
|
|
|
|
function country(f, continent) {
|
|
const p = f.properties;
|
|
const iso = p["iso3166-1:alpha2"];
|
|
const code = CODE_OVERRIDES[p.id] || (iso?.length === 1 ? iso[0] : p.id);
|
|
countryCode.set(p.id, code);
|
|
regions.push({ f, code, continent, parent: null });
|
|
}
|
|
|
|
for (const f of index.features) {
|
|
const p = f.properties;
|
|
if (SKIP.has(p.id)) continue;
|
|
if (p.id === "russia") country(f, "russia"); // a top-level entry that is itself a country
|
|
else if (CONTINENTS[p.parent] && p.parent !== "russia" && !p.id.startsWith("us/")) country(f, p.parent);
|
|
}
|
|
|
|
// One level of subregions: a country's direct children (Geofabrik lists the
|
|
// US states, and Russia's federal districts, under the continent instead).
|
|
for (const f of index.features) {
|
|
const p = f.properties;
|
|
if (SKIP.has(p.id) || countryCode.has(p.id)) continue;
|
|
const parentId = p.id.startsWith("us/") ? "us" : p.parent;
|
|
const parent = countryCode.get(parentId);
|
|
if (!parent) continue;
|
|
const code = p["iso3166-2"]?.[0] || `${parent}-${p.id.split("/").pop()}`;
|
|
regions.push({ f, code, continent: regions.find((r) => r.code === parent).continent, parent });
|
|
}
|
|
|
|
const codes = regions.map((r) => r.code);
|
|
const duplicates = codes.filter((c, i) => codes.indexOf(c) !== i);
|
|
if (duplicates.length) throw new Error(`duplicate codes: ${duplicates.join(", ")}`);
|
|
|
|
// Geofabrik's names: some carry HTML ("Nord-Norge<br />(Northern Norway)"),
|
|
// and the US states are named by their path ("us/new-york").
|
|
const displayName = ({ id, name }) =>
|
|
name === id
|
|
? id.split("/").pop().replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())
|
|
: name.replace(/<br\s*\/?>/gi, " ").replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
const features = regions.map(({ f, code, continent, parent }) => ({
|
|
type: "Feature",
|
|
properties: { code, name: displayName(f.properties), slug: slug(f), continent, ...(parent ? { parent } : {}) },
|
|
geometry: round(f.geometry),
|
|
}));
|
|
writeFileSync(
|
|
OUT,
|
|
JSON.stringify({
|
|
type: "FeatureCollection",
|
|
// The picker's section order and names.
|
|
continents: Object.entries(CONTINENTS).map(([id, name]) => ({ id, name })),
|
|
features,
|
|
}),
|
|
);
|
|
console.log(`${features.length} regions (${features.filter((f) => !f.properties.parent).length} countries) -> ${OUT}`);
|