This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgmaps2bdmaps.sh
115 lines (101 loc) · 2.62 KB
/
gmaps2bdmaps.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
#!/bin/bash
# Baidu Maps Coordinates Utils
# https://github.com/caiguanhao/baidu-maps-coord-utils
#
# Baidu's JavaScript:
#
# var cb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
# function db(a) {
# var b = "",
# c, d, e = "",
# g, j = "",
# k = 0;
# g = /[^A-Za-z0-9\+\/\=]/g;
# if (!a || g.exec(a)) return a;
# a = a.replace(/[^A-Za-z0-9\+\/\=]/g, "");
# do {
# c = cb.indexOf(a.charAt(k++));
# d = cb.indexOf(a.charAt(k++));
# g = cb.indexOf(a.charAt(k++));
# j = cb.indexOf(a.charAt(k++));
# c = c << 2 | d >> 4, d = (d & 15) << 4 | g >> 2;
# e = (g & 3) << 6 | j, b += String.fromCharCode(c);
# 64 != g && (b += String.fromCharCode(d));
# 64 != j && (b += String.fromCharCode(e));
# } while (k < a.length);
# return b
# }
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 -G -L -s"
fi
else
DOWNLOAD="$WGET --quiet -O -"
fi
CHAR="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
ARG_1=${1//,/}
ARG_2=$2
if [[ $ARG_1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
[[ $ARG_2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
COORD_X=$ARG_1
COORD_Y=$ARG_2
else
echo "No coordinates specified."
exit
fi
append_char_to()
{
RESULT=$(printf "\x$(printf %x ${!3})") # String.fromCharCode()
eval "${1}=\"${!1}\${RESULT}\""
}
decode()
{
K=0
B=""
while [[ $K -lt ${#1} ]]; do
C=${CHAR%%${1:$K:1}*}
C=${#C}
(( K = K + 1 ))
D=${CHAR%%${1:$K:1}*}
D=${#D}
(( K = K + 1 ))
G=${CHAR%%${1:$K:1}*}
G=${#G}
(( K = K + 1 ))
J=${CHAR%%${1:$K:1}*}
J=${#J}
(( K = K + 1 ))
C=$(( $C << 2 | $D >> 4 ))
D=$(( ( $D & 15 ) << 4 | $G >> 2 ))
E=$(( ( $G & 3 ) << 6 | $J ))
append_char_to B with_char_code C
if [[ ! $G -eq 64 ]]; then
append_char_to B with_char_code D
fi
if [[ ! $J -eq 64 ]]; then
append_char_to B with_char_code E
fi
done
eval "${3}=\${B}"
}
CONVERT="http://api.map.baidu.com/ag/coord/convert"
QUERY=`$DOWNLOAD "${CONVERT}?from=2&to=4&x=${COORD_Y}&y=${COORD_X}"`
X=${QUERY%%\"x\"*}
X=$(echo ${QUERY:${#X}} | sed 's/"x":"\([^"]*\)".*/\1/')
Y=${QUERY%%\"y\"*}
Y=$(echo ${QUERY:${#Y}} | sed 's/"y":"\([^"]*\)".*/\1/')
decode $X to OUT
echo -n "$OUT, "
decode $Y to OUT
echo $OUT