4 echo "GEO Tiles Downloader. Written by Dmitry Shalnov (c) 2018. Apache License, Version 2.0"
5 echo "Specify 2 points and zoom level diapasone of rectangular part of the map you targeted."
6 echo "Usage: $0 <lat1> <lon1> <lat2> <lon2> <zoom level 1> <zoom level 2> [other options]"
7 echo "-d path to map directory (MAP/ by default)"
8 echo "-m tiles source (https://tile.openstreetmap.org by default)"
11 echo "WARNNING: Please use reasonably. Don't abuse tile servers!"
22 MAP_DEF="https://tile.openstreetmap.org"
23 TMP_TILELIST_FILE="/tmp/tileList"
25 : > $TMP_TILELIST_FILE
29 while [ "$#" -gt 0 ]; do
31 -v) VERBOSE=1; shift 1;;
32 -o) OVERWRITE=1; shift 1;;
33 -d) MAP_DIR="$2"; shift 2;;
34 -m) MAP_SRC="$2"; shift 2;;
35 -*) echo "unknown option: $1" >&2; exit 1;;
40 # ------------------------------------------------------------------------
42 # tech details https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
47 echo "${xtile} ${zoom}" | awk '{printf("%.9f", $1 / 2.0^$2 * 360.0 - 180)}'
53 echo "${long} ${zoom}" | awk '{ xtile = ($1 + 180.0) / 360 * 2.0^$2;
54 xtile+=xtile<0?-0.5:0.5;
55 printf("%d", xtile ) }'
64 # from tms_numbering into osm_numbering
65 ytile=`echo "${ytile}" ${zoom} | awk '{printf("%d\n",((2.0^$2)-1)-$1)}'`;
67 lat=`echo "${ytile} ${zoom}" | awk -v PI=3.14159265358979323846 '{
68 num_tiles = PI - 2.0 * PI * $1 / 2.0^$2;
69 printf("%.9f", 180.0 / PI * atan2(0.5 * (exp(num_tiles) - exp(-num_tiles)),1)); }'`;
77 ytile=`echo "${lat} ${zoom}" | awk -v PI=3.14159265358979323846 '{
78 tan_x=sin($1 * PI / 180.0)/cos($1 * PI / 180.0);
79 ytile = (1 - log(tan_x + 1/cos($1 * PI/ 180))/PI)/2 * 2.0^$2;
80 ytile+=ytile<0?-0.5:0.5;
81 printf("%d", ytile ) }'`;
84 # from oms_numbering into tms_numbering
85 ytile=`echo "${ytile}" ${zoom} | awk '{printf("%d\n",((2.0^$2)-1)-$1)}'`;
91 echo "$1" "$2" | awk '{printf( "%f\n", $1 + ($2 - $1)/2 ) }'
95 echo "$1" | awk '{printf( "%d\n", $1 )}'
99 awk -v MAX="$1" 'BEGIN{ srand(); print int(rand() * MAX ); }'
103 # ------------------------------------------------------------------------
105 if [ -z "$MAP_DIR" ]; then
109 if [ -z "$MAP_SRC" ]; then
112 if [ -z "$(curl -Is "$MAP_SRC" | head -1)" ]; then
113 echo "Could not resolve $MAP_SRC. Please check URL." >&2
118 # add coordinates and zoom to the Map Directory
120 latAverage=$( average $LAT1 $LAT2 )
121 lonAverage=$( average $LONG1 $LONG2 )
122 zoomAverage=$( flat $( average $ZOOM1 $ZOOM2 ))
124 MAP_DIR="MAP/$MAP_DIR|$latAverage|$lonAverage|$zoomAverage"
126 if [ ! -d "$MAP_DIR" ]; then
127 if mkdir -p $MAP_DIR 2>/dev/null; then
128 echo "$MAP_DIR has been created"
130 echo "$MAP_DIR has not been created" >&2
135 # ------------------------------------------------------------------------
137 # seq zoom order prefixes setup
139 if [ $ZOOM1 -gt $ZOOM2 ]; then SEQ_ZOOM_STEP='-1'; fi
143 #if [ "$LONG1" == "180" ]; then LONG1="179.999"; fi
144 if [ "$LAT1" == "90" ]; then LAT1="85.0511"; fi
146 #if [ "$LONG2" == "180" ]; then LONG2="179.999"; fi
147 if [ "$LAT2" == "90" ]; then LAT2="85.0511"; fi
149 #if [ "$LONG1" == "-180" ]; then LONG1="-179.999"; fi
150 if [ "$LAT1" == "-90" ]; then LAT1="-85.0511"; fi
152 #if [ "$LONG2" == "-180" ]; then LONG2="-179.999"; fi
153 if [ "$LAT2" == "-90" ]; then LAT2="-85.0511"; fi
156 # main loop of tiles collection
158 for ZOOM in $(seq $ZOOM1 $SEQ_ZOOM_STEP $ZOOM2); do
160 mkdir -p $MAP_DIR/$ZOOM/
162 TILE1_X=$( long2xtile ${LONG1} ${ZOOM} );
163 TILE1_Y=$( lat2ytile ${LAT1} ${ZOOM} ${TMS} );
164 TILE2_X=$( long2xtile ${LONG2} ${ZOOM} );
165 TILE2_Y=$( lat2ytile ${LAT2} ${ZOOM} ${TMS} );
167 # seq order prefixes setup + wrong tile remove
169 if [ "$TILE1_X" -gt "$TILE2_X" ]; then
171 TILE1_X=$(( $TILE1_X - 1 ))
173 TILE2_X=$(( $TILE2_X - 1 ))
176 if [ "$TILE1_Y" -gt "$TILE2_Y" ]; then
178 TILE1_Y=$(( $TILE1_Y - 1 ))
180 TILE2_Y=$(( $TILE2_Y - 1 ))
183 # collecting the list of tiles
185 for tileX in $(seq $TILE1_X $SEQ_X_STEP $TILE2_X); do
187 mkdir -p $MAP_DIR/$ZOOM/$tileX/ 2>/dev/null
189 for tileY in $(seq $TILE1_Y $SEQ_Y_STEP $TILE2_Y); do
190 echo $ZOOM/$tileX/$tileY >> $TMP_TILELIST_FILE
195 sort --random-sort $TMP_TILELIST_FILE -o $TMP_TILELIST_FILE
197 lines=$(cat $TMP_TILELIST_FILE | wc -l)
199 echo Total: $lines tiles
204 sleep $r # not be too abusive to server
206 if [ ! -z "$OVERWRITE" ] || [ ! -f $MAP_DIR/$line.png ]; then
208 if [ ! -z "$VERBOSE" ]; then postFix=" $line\n"; else postFix=''; fi
210 if ! curl -sS "$MAP_SRC/$line.png" -o "$MAP_DIR/$line.png" ; then
211 echo "Something went wrong"
220 done < $TMP_TILELIST_FILE