Last active
June 21, 2023 08:06
-
-
Save arbakker/79944b242a6947e2af684712fc8661fb to your computer and use it in GitHub Desktop.
Bash script voor het ophalen van percelen uit de Kadastrale Kaart WFS aan de hand van een adres, met behulp van de PDOK Locatieserver #jq #pdok #WFS #bash #
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 | |
# Bash script voor het ophalen van percelen uit de Kadastrale Kaart WFS aan de hand van een adres, met behulp van de PDOK Locatieserver | |
# Author: [email protected] | |
# Dependencies: | |
# - python3 | |
# - jq | |
# - xmllint (libxml2-utils) | |
# - curl | |
set -euo pipefail | |
empty_featurecollection=$(cat << EOF | |
{ | |
"type": "FeatureCollection", | |
"name": "perceel", | |
"crs": { | |
"type": "name", | |
"properties": { | |
"name": "urn:ogc:def:crs:EPSG::28992" | |
} | |
}, | |
"features":[] | |
} | |
EOF | |
) | |
function url_encode(){ | |
python3 -c "import urllib.parse; print(urllib.parse.quote('''$1'''))" # url encode with python | |
} | |
function get_percelen_for_address(){ | |
adres_query="$1" | |
IFS=';' read -r query_straat query_nummer_letter query_woonplaats<<< "$adres_query" | |
url="https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=*&fl=id%20weergavenaam%20type%20score%20adrestype&fq=type:adres&start=0&rows=10&sort=score%20desc%2Csortering%20asc%2Cweergavenaam%20asc&wt=json&fq=huis_nlt:${query_nummer_letter}&fq=woonplaatsnaam:${query_woonplaats}&fq=straatnaam:${query_straat}&fl=gekoppeld_perceel,weergavenaam" | |
perceel_ids=$(curl -s "$url" | jq -r ".response.docs[0].gekoppeld_perceel") | |
result="$empty_featurecollection" | |
for id in $(jq -rc '.[]' <<< "$perceel_ids");do # jq -c get array items each on newline | |
IFS='-' read -r akr_kad_gem_code_waarde sectie perceel_nummer<<< "$id" | |
filter=$( | |
cat << EOF | |
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | |
<ogc:And> | |
<ogc:PropertyIsEqualTo><ogc:PropertyName>perceelnummer</ogc:PropertyName><ogc:Literal>${perceel_nummer}</ogc:Literal></ogc:PropertyIsEqualTo> | |
<ogc:PropertyIsEqualTo><ogc:PropertyName>sectie</ogc:PropertyName><ogc:Literal>${sectie}</ogc:Literal></ogc:PropertyIsEqualTo> | |
<ogc:PropertyIsEqualTo><ogc:PropertyName>AKRKadastraleGemeenteCodeWaarde</ogc:PropertyName><ogc:Literal>${akr_kad_gem_code_waarde}</ogc:Literal></ogc:PropertyIsEqualTo> | |
</ogc:And> | |
</ogc:Filter> | |
EOF | |
) | |
filter=$(xmllint --format --compress -exc-c14n - <<< "$filter") # minify xml filter | |
filter=$(url_encode "$filter") # urlencode xml filter | |
dkk_url="https://geodata.nationaalgeoregister.nl/kadastralekaart/wfs/v4_0?request=GetFeature&service=WFS&typenames=kadastralekaartv4:perceel&version=2.0.0&&outputFormat=application/json&filter=${filter}" | |
feature_collection=$(curl -s "$dkk_url") | |
feature_count=$(jq <<< "$feature_collection" '.features | length') | |
if [[ $feature_count -eq 0 ]];then | |
>&2 echo "ERROR: geen features gevonden voor adres_query: ${adres_query}" | |
continue | |
fi | |
feature_collection=$(jq --arg adres_query "$adres_query" <<< "$feature_collection" '.features[].properties.adres_query=$adres_query') | |
result=$(jq --argjson features "$(jq -c '.features' <<< "$feature_collection")" <<< "$result" '.features += $features') # append features from featurecollection to result | |
done | |
echo "$result" | |
} | |
# format input addresses as `$straatnaam;$nummer_letter;$woonplaatsnaam` | |
adressen_queries=$(cat << EOF | |
Steenstraat;10;Arnhem | |
Kalverstraat;30;Amsterdam | |
Blaak;10;Rotterdam | |
EOF | |
) | |
result="$empty_featurecollection" | |
for adres_query in $adressen_queries;do | |
percelen=$(get_percelen_for_address "$adres_query") | |
result=$(jq --argjson features "$(jq -c '.features' <<< "$percelen")" <<< "$result" '.features += $features') # append features from percelen to result | |
done | |
jq "." <<< "$result" # print result as formatted JSON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment