Created
September 12, 2017 18:44
-
-
Save openfirmware/1381d92d03262bccde9b7b26d0d5e588 to your computer and use it in GitHub Desktop.
Script to automate render_list from mod_tile/renderd. It will generate tiles for all or some renderd styles, rendering one zoom level at a time.
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
#!/bin/bash | |
set -e | |
RENDER_BIN="/usr/local/bin/render_list" | |
# Default Options | |
ALL_STYLES=1 | |
CATCH_SIGPIPE=1 | |
CONFIG="/etc/renderd.conf" | |
RENDER_OPTS=() | |
SOCKET="/var/run/renderd/renderd.sock" | |
STYLES="" | |
TILE_DIR="/work/tiles" | |
VERBOSE=0 | |
# print local output in a different colour to differentiate | |
# output from render_list's output | |
function cecho { | |
echo -e "\e[32m$1\e[39m" | |
} | |
# Parse the options. Supports cases where user sets an option | |
# multiple times on the command line: only last invocation is | |
# used. | |
for opt in "$@" | |
do | |
case $opt in | |
--all-styles) | |
ALL_STYLES=1 | |
shift | |
;; | |
--catch-sigpipe) | |
CATCH_SIGPIPE=1 | |
shift | |
;; | |
-c=*|--config=*) | |
CONFIG="${opt#*=}" | |
shift | |
;; | |
-Z=*|--max-zoom=*) | |
MAXZOOM="${opt#*=}" | |
shift | |
;; | |
-z=*|--min-zoom=*) | |
MINZOOM="${opt#*=}" | |
shift | |
;; | |
--no-catch-sigpipe) | |
CATCH_SIGPIPE=0 | |
shift | |
;; | |
-s=*|--socket=*) | |
SOCKET="${opt#*=}" | |
shift | |
;; | |
--styles=*) | |
ALL_STYLES=0 | |
STYLES="${opt#*=}" | |
shift | |
;; | |
-t=*|--tile-dir=*) | |
TILE_DIR="${opt#*=}" | |
shift | |
;; | |
-v|--verbose) | |
VERBOSE=1 | |
shift | |
;; | |
*) | |
# All unknown options will be passed to render_list | |
RENDER_OPTS+=($opt) | |
;; | |
esac | |
done | |
# Do some basic checks before even invoking render_list | |
if [[ ! $MINZOOM =~ ^([0-9]|[1-9][0-9])$ ]]; then | |
cecho "MINZOOM must be greater than or equal to 0" | |
exit 1 | |
fi | |
if [[ ! $MAXZOOM =~ ^([0-9]|[1-9][0-9])$ ]]; then | |
cecho "MAXZOOM must be greater than or equal to 0" | |
exit 1 | |
fi | |
if [[ $MINZOOM -gt $MAXZOOM ]]; then | |
cecho "MINZOOM ($MINZOOM) cannot be greater than MAXZOOM ($MAXZOOM)" | |
exit 1 | |
fi | |
# Get all styles from config file | |
if [[ $ALL_STYLES -eq 1 ]]; then | |
directives=$(awk 'match($0, /\[(.+)\]/) { print substr($0, RSTART + 1, RLENGTH - 2) }' $CONFIG) | |
for directive in $directives; do | |
if [[ ! ($directive == "renderd" || $directive == "mapnik") ]]; then | |
cecho "found style '$directive'" | |
STYLES+="$directive," | |
fi | |
done | |
if [ "${STYLES: -1}" == "," ]; then | |
STYLES="${STYLES:0:-1}" | |
fi | |
else | |
if [[ $STYLES == "" ]]; then | |
cecho "No styles have been specified. Specify a comma-separated list of styles, or" | |
cecho "specify all styles with --all-styles." | |
exit 1 | |
fi | |
fi | |
# Split styles to iterable string | |
STYLES=$(echo $STYLES | sed 's/,/ /g') | |
# Re-add options for render_list | |
RENDER_OPTS+=("--tile-dir=$TILE_DIR --socket=$SOCKET") | |
if [[ $VERBOSE -eq 1 ]]; then | |
RENDER_OPTS+=("-v") | |
cecho "Verbose output? ${VERBOSE}" | |
cecho "Render all styles? ${ALL_STYLES}" | |
cecho "Styles to render: ${STYLES}" | |
cecho "Config file: ${CONFIG}" | |
cecho "Catch SIGPIPE errors? ${CATCH_SIGPIPE}" | |
cecho "Tile directory: ${TILE_DIR}" | |
cecho "Min zoom: ${MINZOOM}" | |
cecho "Max zoom: ${MAXZOOM}" | |
cecho "Opts for render_list: ${RENDER_OPTS[@]}" | |
fi | |
function render { | |
if [[ $VERBOSE -eq 1 ]]; then | |
cecho "Running: $RENDER_BIN --all --map=$1 --min-zoom=$2 --max-zoom=$2 ${RENDER_OPTS[@]}" | |
fi | |
"$RENDER_BIN" --all --map=$1 --min-zoom=$2 --max-zoom=$2 ${RENDER_OPTS[@]} | |
trap "{ if [[ $CATCH_SIGPIPE -eq 1 ]]; then render $1 $2; fi; }" SIGPIPE | |
} | |
for z in $(seq $MINZOOM $MAXZOOM); do | |
if [[ $VERBOSE -eq 1 ]]; then | |
cecho "==========" | |
cecho "ZOOM LEVEL $z" | |
cecho "==========" | |
fi | |
for style in $STYLES; do | |
render $style $z | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment