gmaps2bdmaps.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # Baidu Maps Coordinates Utils
  3. # https://github.com/caiguanhao/baidu-maps-coord-utils
  4. #
  5. # Baidu's JavaScript:
  6. #
  7. # var cb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  8. # function db(a) {
  9. # var b = "",
  10. # c, d, e = "",
  11. # g, j = "",
  12. # k = 0;
  13. # g = /[^A-Za-z0-9\+\/\=]/g;
  14. # if (!a || g.exec(a)) return a;
  15. # a = a.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  16. # do {
  17. # c = cb.indexOf(a.charAt(k++));
  18. # d = cb.indexOf(a.charAt(k++));
  19. # g = cb.indexOf(a.charAt(k++));
  20. # j = cb.indexOf(a.charAt(k++));
  21. # c = c << 2 | d >> 4, d = (d & 15) << 4 | g >> 2;
  22. # e = (g & 3) << 6 | j, b += String.fromCharCode(c);
  23. # 64 != g && (b += String.fromCharCode(d));
  24. # 64 != j && (b += String.fromCharCode(e));
  25. # } while (k < a.length);
  26. # return b
  27. # }
  28. BC=$(which bc)
  29. if [[ ${#BC} -eq 0 ]]; then
  30. echo "Install bc first."
  31. exit 1
  32. fi
  33. WGET=$(which wget)
  34. CURL=$(which curl)
  35. if [[ ${#WGET} -eq 0 ]]; then
  36. if [[ ${#CURL} -eq 0 ]]; then
  37. echo "Install wget or curl first."
  38. exit 1
  39. else
  40. DOWNLOAD="$CURL -G -L -s"
  41. fi
  42. else
  43. DOWNLOAD="$WGET --quiet -O -"
  44. fi
  45. CHAR="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  46. ARG_1=${1//,/}
  47. ARG_2=$2
  48. if [[ $ARG_1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
  49. [[ $ARG_2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
  50. then
  51. COORD_X=$ARG_1
  52. COORD_Y=$ARG_2
  53. else
  54. echo "No coordinates specified."
  55. exit
  56. fi
  57. append_char_to()
  58. {
  59. RESULT=$(printf "\x$(printf %x ${!3})") # String.fromCharCode()
  60. eval "${1}=\"${!1}\${RESULT}\""
  61. }
  62. decode()
  63. {
  64. K=0
  65. B=""
  66. while [[ $K -lt ${#1} ]]; do
  67. C=${CHAR%%${1:$K:1}*}
  68. C=${#C}
  69. (( K = K + 1 ))
  70. D=${CHAR%%${1:$K:1}*}
  71. D=${#D}
  72. (( K = K + 1 ))
  73. G=${CHAR%%${1:$K:1}*}
  74. G=${#G}
  75. (( K = K + 1 ))
  76. J=${CHAR%%${1:$K:1}*}
  77. J=${#J}
  78. (( K = K + 1 ))
  79. C=$(( $C << 2 | $D >> 4 ))
  80. D=$(( ( $D & 15 ) << 4 | $G >> 2 ))
  81. E=$(( ( $G & 3 ) << 6 | $J ))
  82. append_char_to B with_char_code C
  83. if [[ ! $G -eq 64 ]]; then
  84. append_char_to B with_char_code D
  85. fi
  86. if [[ ! $J -eq 64 ]]; then
  87. append_char_to B with_char_code E
  88. fi
  89. done
  90. eval "${3}=\${B}"
  91. }
  92. CONVERT="http://api.map.baidu.com/ag/coord/convert"
  93. QUERY=`$DOWNLOAD "${CONVERT}?from=2&to=4&x=${COORD_Y}&y=${COORD_X}"`
  94. X=${QUERY%%\"x\"*}
  95. X=$(echo ${QUERY:${#X}} | sed 's/"x":"\([^"]*\)".*/\1/')
  96. Y=${QUERY%%\"y\"*}
  97. Y=$(echo ${QUERY:${#Y}} | sed 's/"y":"\([^"]*\)".*/\1/')
  98. decode $X to OUT
  99. echo -n "$OUT, "
  100. decode $Y to OUT
  101. echo $OUT