#!/usr/bin/env bash # Pre-cache OSM tiles for the trip's map extent through the local proxy # (which stores them in mock/.tilecache). Usage: # pre-cache-tiles.sh # all trips # pre-cache-tiles.sh colombia z16 # one trip, one zoom set -euo pipefail cd "$(dirname "$0")/.." BASE="${BASE:-http://localhost:8077}" TRIPS=("$@") [ ${#TRIPS[@]} -eq 0 ] && TRIPS=(colombia boston florence) # tile range for a lon/lat box range() { # z minlon minlat maxlon maxlat -> "x0 x1 y0 y1" python3 -c " import math z, minlo, minla, maxlo, maxla = float('$1'), float('$2'), float('$3'), float('$4'), float('$5') def tx(lon): return int((lon + 180) / 360 * 2**z) def ty(lat): r = math.radians(lat) return int((1 - math.log(math.tan(r) + 1 / math.cos(r)) / math.pi) / 2 * 2**z) print(tx(minlo), tx(maxlo), ty(maxla), ty(minla))" } fetch() { # trip z local trip="$1" z="$2" case "$trip" in colombia) # Cartagena + Santa Marta boxes for box in "-75.62 10.36 -75.42 10.48" "-74.27 11.08 -74.13 11.28"; do set -- $box read -r x0 x1 y0 y1 <<< "$(range "$z" "$1" "$2" "$3" "$4")" echo " $trip z$z box($1,$2,$3,$4): x $x0-$x1 y $y0-$y1" >&2 for ((x=x0; x<=x1; x++)); do for ((y=y0; y<=y1; y++)); do echo "$BASE/tiles/$z/$x/$y.png" done; done done ;; boston) set -- -71.16 42.29 -71.00 42.41 read -r x0 x1 y0 y1 <<< "$(range "$z" "$1" "$2" "$3" "$4")" for ((x=x0; x<=x1; x++)); do for ((y=y0; y<=y1; y++)); do echo "$BASE/tiles/$z/$x/$y.png" done; done ;; florence) set -- 11.19 43.72 11.32 43.80 read -r x0 x1 y0 y1 <<< "$(range "$z" "$1" "$2" "$3" "$4")" for ((x=x0; x<=x1; x++)); do for ((y=y0; y<=y1; y++)); do echo "$BASE/tiles/$z/$x/$y.png" done; done ;; esac } for trip in "${TRIPS[@]}"; do for z in 14 15 16; do echo "== $trip z$z" # one URL per curl: -o applies to the first URL only, otherwise the rest # print to stdout fetch "$trip" "$z" | xargs -P 8 -n 1 curl -s -o /dev/null -m 20 --retry 2 > /dev/null done done echo "done. cache size: $(du -sh .tilecache | cut -f1)"