// api_v1/scanner/elevation.js // --------------------------------------------------------- // Provider 1: OpenElevation // --------------------------------------------------------- async function tryOpenElevation(lat, lon) { try { const url = `https://api.open-elevation.com/api/v1/lookup?locations=${lat},${lon}`; const res = await fetch(url, { timeout: 5000 }); if (!res.ok) { console.log("OpenElevation status:", res.status); return null; } const text = await res.text(); // Se non è JSON → errore HTML → fallback if (!text.startsWith('{')) { console.log("OpenElevation non-JSON:", text.slice(0, 60)); return null; } const data = JSON.parse(text); return data?.results?.[0]?.elevation ?? null; } catch (err) { console.log("Errore OpenElevation:", err.message); return null; } } // --------------------------------------------------------- // Provider 2: OpenTopoData (fallback) // --------------------------------------------------------- async function tryOpenTopoData(lat, lon) { try { const url = `https://api.opentopodata.org/v1/eudem25m?locations=${lat},${lon}`; const res = await fetch(url, { timeout: 5000 }); if (!res.ok) { console.log("OpenTopoData status:", res.status); return null; } const text = await res.text(); if (!text.startsWith('{')) { console.log("OpenTopoData non-JSON:", text.slice(0, 60)); return null; } const data = JSON.parse(text); return data?.results?.[0]?.elevation ?? null; } catch (err) { console.log("Errore OpenTopoData:", err.message); return null; } } // --------------------------------------------------------- // Funzione principale con fallback // --------------------------------------------------------- async function getElevation(lat, lon) { // 1️⃣ Prova OpenElevation const elev1 = await tryOpenElevation(lat, lon); if (elev1 !== null) return elev1; console.log("⚠️ OpenElevation fallito → uso OpenTopoData"); // 2️⃣ Prova OpenTopoData const elev2 = await tryOpenTopoData(lat, lon); if (elev2 !== null) return elev2; console.log("❌ Nessun provider di elevazione disponibile"); return null; } module.exports = getElevation;