-
Notifications
You must be signed in to change notification settings - Fork 3
/
points2pieces.sh
183 lines (164 loc) · 4.7 KB
/
points2pieces.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
BC=$(which bc)
if [[ ${#BC} -eq 0 ]]; then
echo "Install bc first."
exit 1
fi
WGET=$(which wget)
CURL=$(which curl)
if [[ ${#WGET} -eq 0 ]]; then
if [[ ${#CURL} -eq 0 ]]; then
echo "Install wget or curl first."
exit 1
else
DOWNLOAD="$CURL -L -s -o"
fi
else
DOWNLOAD="$WGET --quiet -O"
fi
DRY_RUN=""
WITH_TRAFFIC=0
for arg in "$@"
do
case "$arg" in
--dry-run)
shift
DRY_RUN="echo"
DOWNLOAD="$DRY_RUN $DOWNLOAD"
echo "#!/bin/bash"
;;
--with-traffic)
shift
WITH_TRAFFIC=1
;;
esac
done
ARG_1=${1//,/}
ARG_2=$2
ARG_3=${3//,/}
ARG_4=$4
if [[ $ARG_1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
[[ $ARG_2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
[[ $ARG_3 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
[[ $ARG_4 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
START_X=$ARG_1
START_Y=$ARG_2
END_X=$ARG_3
END_Y=$ARG_4
else
echo "Please provide two points."
exit 1
fi
if [[ $5 =~ ^[0-9]+$ ]] && [[ $5 -le 19 ]] && [[ $5 -ge 1 ]]; then
LEVEL=$5
else
LEVEL=12
fi
VER_DEFAULT="015"
VER=$VER_DEFAULT
case $6 in
sate)
TYPE=sate
MODE=46
VER="009"
;;
web-alt)
TYPE=web
if [[ $LEVEL -gt 9 ]] && [[ $LEVEL -lt 19 ]]; then
MODE=41
else
MODE=44
fi
if [[ $WITH_TRAFFIC -eq 1 ]]; then
MODE=44
fi
STYLES=ph
;;
*)
TYPE=web
MODE=44
STYLES=pl
;;
esac
MAPS="`pwd`/maps"
if [[ ! -d ${MAPS} ]]; then
$DRY_RUN mkdir "${MAPS}"
else
$DRY_RUN rm -f "${MAPS}"/*
fi
ceil()
{
if [[ $(echo "${!1} == 0${!1/.*}" | $BC ) -eq 1 ]]; then
eval "${1}=\${!1/.*}"
else
eval "${1}=\$((\${!1/.*}+1))"
fi
}
set_block_num_of()
{
local BN=$((${!3}-18))
BN=${BN/-/}
BN=$(echo "scale=10; ${!1} / 2 ^ ${BN} / 256" | $BC )
ceil BN
eval "${5}=\${BN}"
}
TRAFFIC="http://its.map.baidu.com:8002/traffic/TrafficTileService?time=0&label=web2D"
download()
{
SERVER=$((RANDOM%9+1))
if [[ ! -f "${MAPS}/${1},${2}.png" ]]; then
if [[ ${#DRY_RUN} -eq 0 ]]; then
if [[ $TYPE == "sate" ]]; then
SERVER="http://shangetu${SERVER}.map.bdimg.com/it/"
$DOWNLOAD "${MAPS}/${1},${2}.png"\
"${SERVER}u=x=${1/-/M};y=${2/-/M};z=${3};v=${5};type=${4}&fm=${6}" &
else
SERVER="http://online${SERVER}.map.bdimg.com/tile/?qt=tile&"
$DOWNLOAD "${MAPS}/${1},${2}.png"\
"${SERVER}x=${1/-/M}&y=${2/-/M}&z=${3}&styles=${7}" &
fi
if [[ $WITH_TRAFFIC -eq 1 ]]; then
$DOWNLOAD "${MAPS}/${1},${2}.png.traffic"\
"${TRAFFIC}&v=${5}&level=${3}&x=${1/-/M}&y=${2/-/M}" &
fi
else
if [[ $TYPE == "sate" ]]; then
SERVER="http://shangetu${SERVER}.map.bdimg.com/it/"
$DOWNLOAD \""${MAPS}/${1},${2}.png\""\
\""${SERVER}u=x=${1/-/M};y=${2/-/M};z=${3};v=${5};type=${4}&fm=${6}\"" \&
else
SERVER="http://online${SERVER}.map.bdimg.com/tile/?qt=tile&"
$DOWNLOAD \""${MAPS}/${1},${2}.png\""\
\""${SERVER}x=${1/-/M}&y=${2/-/M}&z=${3}&styles=${7}\"" \&
fi
if [[ $WITH_TRAFFIC -eq 1 ]]; then
$DOWNLOAD \""${MAPS}/${1},${2}.png.traffic\""\
\""${TRAFFIC}&v=${5}&level=${3}&x=${1/-/M}&y=${2/-/M}\"" \&
fi
fi
fi
}
set_block_num_of START_X at_level LEVEL to START_BLOCK_X
set_block_num_of START_Y at_level LEVEL to START_BLOCK_Y
set_block_num_of END_X at_level LEVEL to END_BLOCK_X
set_block_num_of END_Y at_level LEVEL to END_BLOCK_Y
if [[ $START_BLOCK_X -eq $END_BLOCK_X ]] && [[ $START_BLOCK_Y -eq $END_BLOCK_Y ]]; then
download $START_BLOCK_X $END_BLOCK_X $LEVEL $TYPE $VER $MODE $STYLES
elif [[ $START_BLOCK_X -eq $END_BLOCK_X ]] && [[ $START_BLOCK_Y -gt $END_BLOCK_Y ]]; then
for (( J=$END_BLOCK_Y ; J<$START_BLOCK_Y ; J++ )) ; do
download $START_BLOCK_X $J $LEVEL $TYPE $VER $MODE $STYLES
done
elif [[ $START_BLOCK_X -lt $END_BLOCK_X ]] && [[ $START_BLOCK_Y -eq $END_BLOCK_Y ]]; then
for (( J=$START_BLOCK_X; J<$END_BLOCK_X; J++ )) ; do
download $J $START_BLOCK_Y $LEVEL $TYPE $VER $MODE $STYLES
done
elif [[ $START_BLOCK_X -lt $END_BLOCK_X ]] && [[ $START_BLOCK_Y -gt $END_BLOCK_Y ]]; then
for (( J=$START_BLOCK_Y; J>$END_BLOCK_Y; J-- )) ; do
for (( K=$START_BLOCK_X; K<$END_BLOCK_X; K++ )) ; do
download $K $J $LEVEL $TYPE $VER $MODE $STYLES
done
$DRY_RUN wait
done
fi
$DRY_RUN wait