You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB
JavaScript

const fetch = require('node-fetch')
const praxen = require('./testpraxen_nordrhein.json')
// const praxen = require('./testpraxen_bonn.json')
const tokens = [ // get a token from https://openrouteservice.org/dev
'58d904a497c67e00015b4...',
'58d904a497c67e00015b4...',
]
async function geocode (token, addr) {
const baseurl = 'https://api.openrouteservice.org/geocode'
const url = `${baseurl}/search?api_key=${token}&text=${encodeURIComponent(addr)}&size=1&layers=address`
const res = await fetch(url)
const { features, error } = await res.json()
if (error || !features || !features.length)
throw new Error(error || 'couldnt geocode')
return features[0]
}
async function work () {
const feats = { type: 'FeatureCollection', features: [] }
let i = 0
for (const p of praxen) {
const addr = `${p.Straße} ${p.PLZ} ${p.Ort}`
.replace(/\n/g, ' ')
.replace(/str(\.) ?/gi, 'straße ')
const token = tokens[i % tokens.length]
try {
// g is a geojson feature
const g = await geocode(token, addr)
g.properties = p
feats.features.push(g)
console.warn(`${i++}/${praxen.length}`, JSON.stringify(g))
} catch (err) {
console.error(`failed to geocode ${p.Nachname} (${addr}) (${token}):\n ${err}`)
}
await new Promise(res => setTimeout(res, 600 / tokens.length)) // 100 reqs per minute per token
}
return feats
}
work()
.then(feats => console.log(JSON.stringify(feats, null, 2)))
.catch(console.error)