point2coord.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. # Baidu Maps Coordinates Utils
  3. # https://github.com/caiguanhao/baidu-maps-coord-utils
  4. BC=$(which bc)
  5. if [[ ${#BC} -eq 0 ]]; then
  6. echo "Install bc first."
  7. exit 1
  8. fi
  9. ARG_1=${1//,/}
  10. ARG_2=$2
  11. if [[ $ARG_1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
  12. [[ $ARG_2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
  13. then
  14. POINT_X=$ARG_1
  15. POINT_Y=$ARG_2
  16. else
  17. echo "No point specified."
  18. exit
  19. fi
  20. MCBAND=(
  21. 12890594.86 8362377.87 5591021
  22. 3481989.83 1678043.12 0
  23. )
  24. MC2LL_0=(
  25. 1.410526172116255e-8 0.00000898305509648872 -1.9939833816331
  26. 200.9824383106796 -187.2403703815547 91.6087516669843
  27. -23.38765649603339 2.57121317296198 -0.03801003308653
  28. 17337981.2
  29. )
  30. MC2LL_1=(
  31. -7.435856389565537e-9 0.000008983055097726239 -0.78625201886289
  32. 96.32687599759846 -1.85204757529826 -59.36935905485877
  33. 47.40033549296737 -16.50741931063887 2.28786674699375
  34. 10260144.86
  35. )
  36. MC2LL_2=(
  37. -3.030883460898826e-8 0.00000898305509983578 0.30071316287616
  38. 59.74293618442277 7.357984074871 -25.38371002664745
  39. 13.45380521110908 -3.29883767235584 0.32710905363475
  40. 6856817.37
  41. )
  42. MC2LL_3=(
  43. -1.981981304930552e-8 0.000008983055099779535 0.03278182852591
  44. 40.31678527705744 0.65659298677277 -4.44255534477492
  45. 0.85341911805263 0.12923347998204 -0.04625736007561
  46. 4482777.06
  47. )
  48. MC2LL_4=(
  49. 3.09191371068437e-9 0.000008983055096812155 0.00006995724062
  50. 23.10934304144901 -0.00023663490511 -0.6321817810242
  51. -0.00663494467273 0.03430082397953 -0.00466043876332
  52. 2555164.4
  53. )
  54. MC2LL_5=(
  55. 2.890871144776878e-9 0.000008983055095805407 -3.068298e-8
  56. 7.47137025468032 -0.00000353937994 -0.02145144861037
  57. -0.00001234426596 0.00010322952773 -0.00000323890364
  58. 826088.5
  59. )
  60. calc()
  61. {
  62. FORMU=$3
  63. # replace scientific notation
  64. FORMU=${FORMU//[eE]+/*10^}
  65. FORMU=${FORMU//[eE]-/*10^-}
  66. RESULT=$(echo "scale=30; ${FORMU}" | $BC)
  67. eval "${1}=\$RESULT"
  68. }
  69. comp()
  70. {
  71. RESULT=$($BC <<< "$3")
  72. eval "${1}=\$RESULT"
  73. }
  74. round()
  75. {
  76. RESULT=$(printf %.6f ${!1})
  77. eval "${1}=\$RESULT"
  78. }
  79. for (( cL=0 ; cL<${#MCBAND[@]} ; cL++ )) ; do
  80. comp CMP = "${POINT_Y/-/} >= ${MCBAND[$cL]}"
  81. if [ $CMP -eq 1 ]; then
  82. MC2LL="MC2LL_$cL[@]"
  83. M=("${!MC2LL}")
  84. calc LNG = "${M[0]} + ${M[1]} * ${POINT_X/-/}"
  85. calc INT = "${POINT_Y/-/} / ${M[9]}"
  86. calc LAT = "${M[2]} + ${M[3]} * ${INT} ^ 1 + ${M[4]} * ${INT} ^ 2 + \
  87. ${M[5]} * ${INT} ^ 3 + ${M[6]} * ${INT} ^ 4 + \
  88. ${M[7]} * ${INT} ^ 5 + ${M[8]} * ${INT} ^ 6"
  89. comp CMP = "${POINT_X} < 0"
  90. if [ $CMP -eq 1 ]; then
  91. calc LNG = "${LNG} * -1"
  92. fi
  93. comp CMP = "${POINT_Y} < 0"
  94. if [ $CMP -eq 1 ]; then
  95. calc LAT = "${LAT} * -1"
  96. fi
  97. round LNG
  98. round LAT
  99. echo "${LNG}, ${LAT}"
  100. break
  101. fi
  102. done
  103. # Baidu's JavaScript:
  104. #
  105. # convertMC2LL: function(cB) {
  106. # var cC, cE;
  107. # cC = new b3(Math.abs(cB.lng), Math.abs(cB.lat));
  108. # for (var cD = 0; cD < this.MCBAND.length; cD++) {
  109. # if (cC.lat >= this.MCBAND[cD]) {
  110. # cE = this.MC2LL[cD];
  111. # break
  112. # }
  113. # }
  114. # var T = this.convertor(cB, cE);
  115. # var cB = new b3(T.lng.toFixed(6), T.lat.toFixed(6));
  116. # return cB
  117. # }
  118. # convertor: function(cC, cD) {
  119. # if (!cC || !cD) {
  120. # return
  121. # }
  122. # var T = cD[0] + cD[1] * Math.abs(cC.lng);
  123. # var cB = Math.abs(cC.lat) / cD[9];
  124. # var cE = cD[2] + cD[3] * cB + cD[4] * cB * cB +
  125. # cD[5] * cB * cB * cB + cD[6] * cB * cB * cB * cB +
  126. # cD[7] * cB * cB * cB * cB * cB + cD[8] * cB * cB * cB * cB * cB * cB;
  127. # T *= (cC.lng < 0 ? -1 : 1);
  128. # cE *= (cC.lat < 0 ? -1 : 1);
  129. # return new b3(T, cE)
  130. # }