Get lat/lon coordinates of Dutch postcodes completely client-side without API requests.
The script src/pack.py reads a GeoPackage (GPKG) file from CBS and packs the location data into a binary file. It creates an intermediate postcodes.csv file for faster runs.
CBS provides postcode data in a GeoPackage (GPKG) file which includes polygons of each postcode. This script reads this data, calculates polygon centroids and outputs an intermediate postcodes.csv file.
If you want to bring your own postcode data: place it in postcodes.csv in the following format and this script will use that instead of CBS data:
postcode6,lat,lon
1034XZ,52.40376675629908,4.907777651195642
1058EH,52.36149008188852,4.844611782197562
1082MD,52.33777631760533,4.87075088206934
...Otherwise:
- Download the most recent
volledige postcode (PC6)from CBS available on this page: https://www.cbs.nl/nl-nl/dossier/nederland-regionaal/geografische-data/gegevens-per-postcode. - Unzip and place
cbs_pc6_2024_v1.gpkg(or a more recent year) in the same folder aspack.py. - Set
input_fileinsrc/pack.pyto match the filename.
- Set up virtual env. Install packages with
pip install -r requirements.txt. - Run
python pack.py. The binary data will be stored as a.packfile and compressed as a.pack.gzfile.
- Include postcode-locaties.js:
<script src="postcode-locaties.js"></script>- Fetch binaries and initialize:
let pcloc;
window.onload = async function() {
pcloc = await PostcodeLocaties({
packUrl: 'https://example.com/postcodes.pack',
packGzUrl: 'https://example.com/postcodes.pack.gz',
});
};console.log(pcloc.lookup('1398PX'));{
geohash: "u179gk",
lat: 52.36358642578125,
lon: 5.0701904296875
}