Probeer het uit op flems.io.
Last active
October 8, 2024 16:14
-
-
Save erikvullings/f417c3469b62d93d3d241723a4ee6cad to your computer and use it in GitHub Desktop.
PDOK Locatieservice - gebruik de gratis API service om een adres of postcode te converteren in een WGS84 or RD coördinate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Example of using the PDOK API service, source https://github.com/PDOK/locatieserver/wiki/API-Locatieserver */ | |
export interface IPdokSearchResult { | |
response: { | |
numFound: number; | |
start: number; | |
maxScore: number; | |
docs: Array<{ | |
bron: 'BAG' | 'NWB' | string; | |
woonplaatscode: string; | |
type: 'adres' | 'postcode' | 'weg'; | |
woonplaatsnaam: string; | |
wijkcode: string; | |
huis_nlt: string; | |
openbareruimtetype: string; | |
buurtnaam: string; | |
gemeentecode: string; | |
rdf_seealso: string; | |
weergavenaam: string; | |
straatnaam_verkort: string; | |
id: string; | |
gekoppeld_perceel: string; | |
gemeentenaam: string; | |
buurtcode: string; | |
wijknaam: string; | |
identificatie: string; | |
openbareruimte_id: string; | |
waterschapsnaam: string; | |
provinciecode: string; | |
postcode: string; | |
provincienaam: string; | |
centroide_ll: string; | |
nummeraanduiding_id: string; | |
waterschapscode: string; | |
adresseerbaarobject_id: string; | |
huisnummer: string; | |
provincieafkorting: string; | |
centroide_rd: string; | |
straatnaam: string; | |
score: string; | |
}>; | |
}; | |
} | |
/** Extracts two numbers from a geopoint */ | |
const pointRegex = /POINT\(([\d.]+) ([\d.]+)\)/; | |
export const pdokLocationSvc = async (pc: string, hn: string, toev: string = '') => { | |
const pdokUrl = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=${pc.replace(/ /g, '')} ${hn} ${toev}`; | |
// await sleep(10); | |
console.log(`PDOK resolving ${pc}, ${hn}${toev ? `, ${toev}` : ''}`); | |
const response = await fetch(pdokUrl).catch((_) => { | |
console.error(`Error resolving ${pc}, ${hn}${toev ? `, ${toev}` : ''} !`); | |
console.error(''); | |
return undefined; | |
}); | |
if (!response.ok) { | |
console.error(`Error resolving ${pc}, ${hn}${toev ? `, ${toev}` : ''}!`); | |
return undefined; | |
} | |
const searchResult = await response.json() as IPdokSearchResult; | |
// console.log(searchResult) | |
if (searchResult) { | |
const { response: { docs = [] } } = searchResult; | |
const found = docs.filter((doc) => doc.bron === 'BAG' && doc.type === 'adres'); | |
if (found.length > 0) { | |
const best = found[0]; | |
console.log(best); | |
const { centroide_ll, centroide_rd } = best; | |
const ll = pointRegex.exec(centroide_ll); | |
const rd = pointRegex.exec(centroide_rd); | |
if (ll && rd) { | |
console.log({ | |
lat: +ll[2], | |
lon: +ll[1], | |
x: +rd[1], | |
y: +rd[2], | |
}); | |
return { | |
lat: +ll[2], | |
lon: +ll[1], | |
x: +rd[1], | |
y: +rd[2], | |
}; | |
} | |
} | |
} | |
console.error(`Error resolving ${pc}, ${hn}${toev ? `, ${toev}` : ''}!`); | |
return undefined; | |
}; | |
pdokLocationSvc('2597AK', '63'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment