Skip to content

Instantly share code, notes, and snippets.

@arbakker
Last active September 24, 2021 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbakker/207a12b59018fb155b98eeb7dbe84078 to your computer and use it in GitHub Desktop.
Save arbakker/207a12b59018fb155b98eeb7dbe84078 to your computer and use it in GitHub Desktop.
Calculate distance between two groups of geometries with GeoPackage/SQLite

README

First download and unzip CBS Postcode4 GeoPackage and then open with sqlite3:

wget https://service.pdok.nl/cbs/pc4/atom/v1_0/downloads/cbs_pc4_2020.gpkg.zip
unzip cbs_pc4_2020.gpkg.zip
sqlite3 cbs_pc4_2020.gpkg

Then execute the following queries:

select load_extension('mod_spatialite')
create view locaties as SELECT * FROM cbs_pc4_2020 ORDER BY RANDOM() LIMIT 5; -- selecteer 5 willekeurige postcodes als locaties
create view klanten as SELECT * FROM cbs_pc4_2020, locaties WHERE cbs_pc4_2020.postcode <> locaties.postcode ORDER BY RANDOM() LIMIT 50; -- selecteer 50 willekeurige postcodes (die niet al in locaties view zitten) als klanten
SELECT locaties.postcode as locatie_postcode, klanten.postcode as klant_postcode, ST_Distance(ST_Centroid(GeomFromGPB(locaties.geom)), ST_Centroid(GeomFromGPB(klanten.geom))) as distance  FROM locaties, klanten GROUP BY klant_postcode HAVING MIN(distance) ORDER BY distance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment