Skip to content

Instantly share code, notes, and snippets.

@aldoridhoni
Created July 30, 2018 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldoridhoni/bd61a5e9bd14e293d9b9c4b852a63c4b to your computer and use it in GitHub Desktop.
Save aldoridhoni/bd61a5e9bd14e293d9b9c4b852a63c4b to your computer and use it in GitHub Desktop.
Download Google Maps Images
#!/bin/bash
function show_help
{
echo "Usage: $0 (-x xcoord) (-y ycoord) (-l level)"
echo " [-r x width] [-s y width] -h for help"
}
if [ $# -le 1 ]; then
show_help
exit 1
fi
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
## Note !1e0=png !1e3=webp !1e4=bin
G_SAT='https://www.google.com/maps/vt/pb=!1m5!1m4!1i$level!2i[$x-$x1]!3i[$y-$y1]!4i128!2m2!1e1!3i803!3m9!2sid!3sid!5e1105!12m1!1e4!12m1!1e47!12m1!1e3!4e0!5m1!1e0'
G_SAT_1='https://www.google.com/maps/vt/pb=!1m5!1m4!1i$level!2i[$x-$x1]!3i[$y-$y1]!4i128!2m2!1e1!3i803!3m9!2sid!3sid!5e1105!12m1!1e4!12m1!1e47!4e0!1e0'
G_MAP='https://www.google.com/maps/vt/pb=!1m5!1m4!1i$level!2i[$x-$x1]!3i[$y-$y1]!4i128!2m2!1e0!3i429133041!3m7!2sid!3sid!5e1105!12m1!1e47!12m1!1e3!4e0!5m1!1e0'
G_CON_0='https://www.google.com/maps/vt/pb=!1m4!1m3!1i$level!2i[$x-$x1]!3i[$y-$y1]!2m2!1e5!2sshading!2m2!1e6!2scontours!2m3!1e0!2sm!3i429133076!3m7!5e1105!12m1!1e67!12m1!1e63!12m1!1e3!4e0!5m2!5f1!6b1'
G_CON_1='https://www.google.com/maps/vt/pb=!1m4!1m3!1i$level!2i[$x-$x1]!3i[$y-$y1]!2m2!1e5!2sshading!2m2!1e6!2scontours!2m3!1e0!2sm!3i429133076!3m7!5e1105!12m1!1e67!12m1!1e63!5f1!6b1'
SOURCE_URL=$G_SAT
X_WIDTH=10
Y_WIDTH=10
EXT="png"
ANNOTATE=false
while getopts ":x:y:r:s:l:h?" opt; do
case $opt in
x ) x=$OPTARG ;;
y ) y=$OPTARG ;;
l ) level=$OPTARG ;;
r ) X_WIDTH=$OPTARG ;;
s ) Y_WIDTH=$OPTARG ;;
h ) show_help; exit 0 ;;
\? ) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
: ) echo "Option -$OPTARG requires an argument." >&2; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [ -z "$x" -o -z "$y" -o -z "$level" ]; then
echo "Option -x -y -l required"
exit 1
fi
echo "x: $x, y: $y, level: $level, xrange: $X_WIDTH, yrange: $Y_WIDTH"
function calculate
{
let x1=x+$X_WIDTH
let y1=y+$Y_WIDTH
eval URL="$SOURCE_URL"
echo $URL
}
function fetch
{
echo "==> Downloading"
curl -C - --output "#1_#2.${EXT}" "$URL"
}
function merge
{
mkdir 'out'
if $ANNOTATE; then
echo "==> Annotating"
mogrify -gravity South \
-annotate +0+5 '%f' \
-pointsize 11 \
-fill white \
-bordercolor '#DFDFDF' \ #grey
-border 1 *.${EXT}
fi
echo "==> Merging"
for ((c=$x; c<=$x1; c++));
do
echo $c
if [ $x -lt 10 ]; then
for ((i=0; i<=9; i++));
do
echo -n "rename ${c}_${i} ";
a=${c}_${i}.${EXT};
j=`echo $a | cut -d _ -f 2` ;
mv $a `printf ${c}_0${j}`;
done
fi
montage -verbose \
-mode concatenate \
-tile 1x ${c}\_*.${EXT} out/${c}.${EXT}
done
cd 'out'
for ((a=0; a<=9; a++)); # $(seq 0 1 9)
do
if [ -z ${a}.${EXT} ]; then
mv ${a}.{EXT} 0${a}.${EXT};
fi
done;
let START_POS=x/10
let END_POS=x1/10
let DIFF=$END_POS-$START_POS
for ((a=START_POS; a<=END_POS; a++));
do
montage -verbose \
-mode concatenate \
-tile x1 ${a}*.${EXT} F${a}.${EXT}
done;
if [ $DIFF -gt 3 ]; then
let HALF=$START_POS+$DIFF/2
let NEXT=$HALF+1
montage -verbose \
-mode concatenate \
-tile x1 $(eval echo F{${START_POS}..${HALF}}.${EXT}) L.${EXT}
montage -verbose \
-mode concatenate \
-tile x1 $(eval echo F{${NEXT}..${END_POS}}.${EXT}) R.${EXT}
montage -verbose \
-mode concatenate \
-tile x1 L.${EXT} R.${EXT} FINAL.${EXT}
else
montage -verbose \
-mode concatenate \
-tile x1 $(eval echo F{${START_POS}..${END_POS}}.${EXT}) FINAL.${EXT}
fi
cd ".."
echo "==> Done merging"
}
function cleanup
{
echo "==> Cleanup"
mkdir "source"
mv *.${EXT} source/
mv out/FINAL.${EXT} .
convert -resize 1366x\> FINAL.${EXT} RE1000.${EXT}
}
function main
{
mkdir "${level}-${x}-${y}"
cd "${level}-${x}-${y}"
calculate
fetch
merge
cleanup
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment