← back to Costa Rica
scripts/ingest/osm-fetch.js
51 lines
'use strict';
// Fetch all NAMED Costa Rica POIs that carry a website tag (and grab phone/geo
// while we're there) from OpenStreetMap via Overpass. $0, ODbL-attribution.
// Writes Overpass raw JSON (has .elements) to scripts/data/cache/osm-cr-websites.json,
// which cr-osm-match.js consumes to fill website/phone/lat/lng on matching places.
const fs = require('fs');
const path = require('path');
const OUT_DIR = path.join(__dirname, 'data', 'cache');
const OUT = path.join(OUT_DIR, 'osm-cr-websites.json');
const ENDPOINTS = [
'https://overpass-api.de/api/interpreter',
'https://overpass.kumi.systems/api/interpreter',
'https://maps.mail.ru/osm/tools/overpass/api/interpreter',
];
// Named CR POIs with any website/phone/contact info — the useful enrichment set.
const QUERY = `[out:json][timeout:300];
area["ISO3166-1"="CR"][admin_level=2]->.cr;
(
nwr(area.cr)["name"]["website"];
nwr(area.cr)["name"]["contact:website"];
nwr(area.cr)["name"]["phone"]["tourism"];
nwr(area.cr)["name"]["phone"]["amenity"~"restaurant|cafe|bar|hotel"];
);
out center tags;`;
(async () => {
fs.mkdirSync(OUT_DIR, { recursive: true });
for (const ep of ENDPOINTS) {
try {
console.log(`[osm] trying ${ep}`);
const res = await fetch(ep, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'CR-Directory/1.0 (info@agentabrams.com)' },
body: 'data=' + encodeURIComponent(QUERY),
});
if (!res.ok) { console.log(`[osm] HTTP ${res.status}, next`); continue; }
const j = await res.json();
const n = (j.elements || []).length;
if (!n) { console.log('[osm] 0 elements, next endpoint'); continue; }
fs.writeFileSync(OUT, JSON.stringify(j));
console.log(`[osm] wrote ${n} elements → ${OUT}`);
process.exit(0);
} catch (e) {
console.log(`[osm] err ${String(e.message||e).slice(0,120)}, next`);
}
}
console.error('[osm] all endpoints failed'); process.exit(1);
})();