Last active
October 1, 2021 13:47
-
-
Save arbakker/a04ae77510055acca2b1df35262abe1f to your computer and use it in GitHub Desktop.
Generate pdok-viewer json config with xmlstarlet and jq #xmlstarlet #jq #pdok-viewer
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 | |
| # author: https://github.com/arbakker | |
| # script requirements: | |
| # - curl | |
| # - xmlstarlet | |
| # - jq | |
| PROGRAM_NAME=$(basename "$0") | |
| URL=$1 | |
| function usage { | |
| echo "Generate PDOK Viewer configuration for WMS services." | |
| echo "Assumes layers are nested in a toplevel Layer element in" | |
| echo "the cap doc one level deep. " | |
| echo "" | |
| echo "usage: $PROGRAM_NAME <wms-cap-url>" | |
| echo " - <wms-cap-url>: wms capabilities url, containing" | |
| echo " both req params service=wms and request=getcapabilities" | |
| exit 1 | |
| } | |
| function gen-viewer-config-wms() { | |
| url="$1" | |
| base_url=$(cut -d"?" -f1 <<<"$url") | |
| curl --fail -s "$url" | | |
| xmlstarlet sel -T -N wms="http://www.opengis.net/wms" -t -m '//wms:Layer/wms:Layer' -v \ | |
| 'concat(wms:Name/text(), ",", wms:Title/text())' -n | | |
| jq --slurp --raw-input \ | |
| --arg v "$base_url" \ | |
| 'split("\n") | .[:-1] | map(split(",")) | | |
| map({ | |
| "technicalName": .[0], | |
| "name": .[1], | |
| "legendUrl": ($v + "?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&version=1.3.0&SLD_VERSION=1.1.0&layer=" + .[0]), | |
| })' | |
| } | |
| if [ "$#" -ne 1 ]; then | |
| usage | |
| fi | |
| set -euo pipefail | |
| # ${var,,} syntax to convert to lowercase for case insensitive matching | |
| # regex to check if url contains both service=wms and request=getcapabilities params | |
| if [[ ! "${URL,,}" =~ service=wms.*request=getcapabilities|request=getcapabilities.*service=wms ]]; then | |
| usage | |
| fi | |
| gen-viewer-config-wms "$URL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment