more verbosity & error handling

This commit is contained in:
Hugoren Martinako 2024-03-30 14:41:49 +01:00
parent 22103fb243
commit 7159b7ced5
1 changed files with 12 additions and 3 deletions

View File

@ -11,6 +11,7 @@ const [, , area, THRESHOLD = 50] = process.argv
const DEFAULT_DECIMALS = 6
const ELEVATION_API = 'https://api.open-elevation.com/api/v1/lookup'
// const ELEVATION_API = 'http://0.0.0.0/api/v1/lookup'
const OVERPASS_API = 'https://overpass-api.de/api/interpreter'
const OVERPASS_QUERY = `[out:json][timeout:25];area(id:${36e8 + +area})->.searchArea;node["ele"](area.searchArea);out;`
@ -28,20 +29,28 @@ async function main () {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}).then(response => response.json())
}).then(response => response.ok && response.json())
if (!elements) return console.error(`No elements found from ${OVERPASS_API}`)
// prepare POST data request: https://github.com/Jorl17/open-elevation/blob/master/docs/api.md#post-apiv1lookup
const locations = elements.map(x => ({ latitude: round(x.lat), longitude: round(x.lon) }))
const { results } = await fetch(ELEVATION_API, {
method: 'post',
body: JSON.stringify({ locations: elements.map(x => ({ latitude: round(x.lat), longitude: round(x.lon) })) }),
body: JSON.stringify({ locations }),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}).then(response => response.json())
}).then(response => response.ok && response.json())
if (!results) return console.error(`No results found from ${ELEVATION_API}`)
const errors = elements.reduce((acc, x) => {
const t = results.find(y => y.latitude === round(+x.lat) && y.longitude === round(+x.lon))
if (!t) return acc
if (+x.tags.ele + +THRESHOLD >= +t.elevation && +x.tags.ele - +THRESHOLD <= +t.elevation) return acc
return [...acc, { osm: +x.tags.ele, strm: t.elevation, error: (Math.abs(t.elevation - +x.tags.ele) / t.elevation).toLocaleString(undefined, { style: 'percent' }), url: `https://www.openstreetmap.org/${x.type}/${x.id}`, type: x.type, id: x.id }]