Created
September 27, 2021 21:29
-
-
Save arbakker/388d5e0abb8a3c1350494b43bf07aea3 to your computer and use it in GitHub Desktop.
Bash script to generate routes with openrouteservice and a GeoPackage (with Dutch postal code area's)
This file contains hidden or 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
| #!/usr/bin/env bash | |
| function gen_routes(){ | |
| # shellcheck disable=SC1091 | |
| if [ -z "$API_KEY" ]; then | |
| echo "ERROR: environmental variable API_KEY for openrouteservice is unset or empty" | |
| exit 1 | |
| fi | |
| QUERY="select load_extension('mod_spatialite');SELECT ST_AsText(ST_Centroid(GeomFromGPB(locaties.geom))) as locatie_geom, ST_AsText(ST_Centroid(GeomFromGPB(klanten.geom))) as klanten_geom, klanten.postcode as klant_postcode, locaties.postcode as locatie_postcode FROM locaties, klanten;" | |
| rm -f routes.geosjon | |
| rm -rf response/*.json | |
| mkdir -p response | |
| result="" | |
| while read -r LINE; do | |
| if [[ -z $LINE ]];then | |
| continue | |
| fi | |
| geom_from=$(cut -d"|" -f1 <<< "$LINE") | |
| geom_to=$(cut -d"|" -f2 <<< "$LINE") | |
| pc_from=$(cut -d"|" -f3 <<< "$LINE") | |
| pc_to=$(cut -d"|" -f4 <<< "$LINE") | |
| regex='^POINT\((.*)\)$' | |
| [[ $geom_from =~ $regex ]] | |
| point_from=$(echo ${BASH_REMATCH[1]} | cs2cs -f "%.6f" "+init=epsg:28992" +to " | |
| +init=epsg:4326" | awk -F' ' '{print "["$1","$2"]"}') | |
| [[ $geom_to =~ $regex ]] | |
| point_to=$(echo ${BASH_REMATCH[1]} | cs2cs -f "%.6f" "+init=epsg:28992" +to " | |
| +init=epsg:4326" | awk -F' ' '{print "["$1","$2"]"}') | |
| route_output="response/${pc_from}_${pc_to}.json" | |
| BODY='{"coordinates":['$point_to','$point_from']}' | |
| curl -s -X POST \ | |
| 'https://api.openrouteservice.org/v2/directions/driving-car/geojson' \ | |
| -H 'Content-Type: application/json; charset=utf-8' \ | |
| -H 'Accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8' \ | |
| -H "Authorization: ${API_KEY}" \ | |
| -d "$BODY" > "$route_output" | |
| distance=$(jq ".features[0].properties.summary.distance" < "$route_output") | |
| geom=$(jq ".features[0].geometry" < "$route_output") | |
| result="${result},{\"pcFrom\": \"$pc_from\", \"pcTo\": \"$pc_to\", \"file\": \"$route_output\", \"distance\": \"$distance\", \"geometry\": $geom }" | |
| done <<< "$(sqlite3 data/cbs_pc4_2020.gpkg <<<"$QUERY")" | |
| result="[${result:1}]" # ${result:1} removes extraneous comma at start of array | |
| features="" | |
| # convert result to GeoJSON featurecollection | |
| for item in $(jq -r ".[] | @base64" <<< "$result");do # base64 encoding to iterate over json array with jq | |
| _jq() { | |
| echo "${item}" | base64 --decode | jq -r "${1}" | |
| } | |
| file=$(_jq '.file') | |
| pcFrom=$(_jq '.pcFrom') | |
| pcTo=$(_jq '.pcTo') | |
| distance=$(_jq '.distance') | |
| geom=$(_jq '.geometry') | |
| ft="{ | |
| \"type\": \"Feature\", | |
| \"properties\": { | |
| \"pcFrom\": $pcFrom, | |
| \"pcTo\": $pcTo, | |
| \"distance\": $distance, | |
| \"file\": \"$file\" | |
| }, | |
| \"geometry\": $geom | |
| }" | |
| features="$features,$ft" | |
| done | |
| fc="{\"type\":\"FeatureCollection\",\"features\":"[${features:1}]"}" # ${features:1} removes extraneous comma at start of array | |
| echo "$fc" | jq -c "." > routes.geosjon | |
| echo "saved routes in routes.geojson and responses in ./response" | |
| } | |
| function dl_data(){ | |
| wget https://service.pdok.nl/cbs/pc4/atom/v1_0/downloads/cbs_pc4_2020.gpkg.zip -O data/cbs_pc4_2020.gpkg.zip | |
| unzip data/cbs_pc4_2020.gpkg.zip -d data/ | |
| } | |
| function create_views(){ | |
| sqlite3 data/cbs_pc4_2020.gpkg " | |
| drop view if exists locaties; | |
| create view locaties as SELECT * FROM cbs_pc4_2020 ORDER BY RANDOM() LIMIT 1; -- add ORDER BY RANDOM() for randomized view | |
| drop view if exists klanten; | |
| create view klanten as SELECT * FROM cbs_pc4_2020, locaties WHERE cbs_pc4_2020.postcode <> locaties.postcode ORDER BY RANDOM() LIMIT 5; -- add ORDER BY RANDOM() for randomized view" | |
| } | |
| function all(){ | |
| dl_data | |
| create_views | |
| gen_routes | |
| } | |
| function usage(){ | |
| echo "usage: ${PROGRAM_NAME} <command>" | |
| echo "command values: gen-routes,dl-data,create-views,all" | |
| exit 1 | |
| } | |
| PROGRAM_NAME=$0 | |
| x=$1 | |
| if [[ $# -eq 0 ]];then | |
| usage | |
| fi | |
| case $x in | |
| dl_data|create-views|gen-routes|all) | |
| case $x in | |
| gen-routes) | |
| gen_routes ;; | |
| dl-data) | |
| dl_data ;; | |
| create-views) | |
| create_views ;; | |
| all) | |
| all ;; | |
| esac ;; | |
| *) | |
| usage ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment