overpassql/fixes/fix_elevation.js

68 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-12-28 16:58:41 +01:00
/**
* Usage:
*
* node fix_elevation.js <OSM_RELATION_ID> [<ELEVATION_THRESHOLD>]
*
* Examples:
* node fix_elevation.js 349001
* node fix_elevation.js 349014 150
*/
const [, , area, THRESHOLD = 50] = process.argv
const DEFAULT_DECIMALS = 6
const ELEVATION_API = 'https://api.open-elevation.com/api/v1/lookup'
2024-03-30 14:41:49 +01:00
// const ELEVATION_API = 'http://0.0.0.0/api/v1/lookup'
2023-12-28 16:58:41 +01:00
const OVERPASS_API = 'https://overpass-api.de/api/interpreter'
2024-03-28 12:24:53 +01:00
const OVERPASS_QUERY = `[out:json][timeout:25];area(id:${36e8 + +area})->.searchArea;node["ele"](area.searchArea);out;`
2023-12-28 16:58:41 +01:00
function round (num, decimalPlaces = DEFAULT_DECIMALS) {
const p = Math.pow(10, decimalPlaces)
const n = (num * p) * (1 + Number.EPSILON)
return Math.round(n) / p
}
async function main () {
const { elements } = await fetch(OVERPASS_API, {
method: 'post',
body: 'data=' + encodeURIComponent(OVERPASS_QUERY),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
2024-03-30 14:41:49 +01:00
}).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) }))
2023-12-28 16:58:41 +01:00
const { results } = await fetch(ELEVATION_API, {
method: 'post',
2024-03-30 14:41:49 +01:00
body: JSON.stringify({ locations }),
2023-12-28 16:58:41 +01:00
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
2024-03-30 14:41:49 +01:00
}).then(response => response.ok && response.json())
if (!results) return console.error(`No results found from ${ELEVATION_API}`)
2023-12-28 16:58:41 +01:00
const errors = elements.reduce((acc, x) => {
const t = results.find(y => y.latitude === round(+x.lat) && y.longitude === round(+x.lon))
2024-03-30 14:41:49 +01:00
if (!t) return acc
2023-12-28 16:58:41 +01:00
if (+x.tags.ele + +THRESHOLD >= +t.elevation && +x.tags.ele - +THRESHOLD <= +t.elevation) return acc
2024-02-22 12:31:35 +01:00
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 }]
// return [...acc, { osm: +x.tags.ele, strm: t.elevation, url: `https://www.openstreetmap.org/${x.type}/${x.id}`, type: x.type, id: x.id, 'ref:ine': x.tags['ref:ine'] }]
2023-12-28 16:58:41 +01:00
}, [])
2024-02-22 12:31:35 +01:00
console.table(errors, ['osm', 'strm', 'error', 'url'])
2023-12-28 16:58:41 +01:00
console.log('\nEnlace JOSM:\nhttp://localhost:8111/load_object?new_layer=true&objects=' + errors.map(x => x.type[0] + x.id).join(','))
console.log('\nJSON de actualización:\n' + JSON.stringify(Object.fromEntries(errors.map(x => [x.id, x.strm]))))
2023-12-28 16:58:41 +01:00
return errors
}
main()