附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
3.2 KiB

  1. package utils
  2. import (
  3. "math"
  4. )
  5. // WGS84坐标系:即地球坐标系,国际上通用的坐标系。
  6. // GCJ02坐标系:即火星坐标系,WGS84坐标系经加密后的坐标系。Google Maps,高德在用。
  7. // BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系。
  8. const (
  9. X_PI = math.Pi * 3000.0 / 180.0
  10. OFFSET = 0.00669342162296594323
  11. AXIS = 6378245.0
  12. )
  13. //BD09toGCJ02 百度坐标系->火星坐标系
  14. func BD09toGCJ02(lon, lat float64) (float64, float64) {
  15. x := lon - 0.0065
  16. y := lat - 0.006
  17. z := math.Sqrt(x*x+y*y) - 0.00002*math.Sin(y*X_PI)
  18. theta := math.Atan2(y, x) - 0.000003*math.Cos(x*X_PI)
  19. gLon := z * math.Cos(theta)
  20. gLat := z * math.Sin(theta)
  21. return gLon, gLat
  22. }
  23. //GCJ02toBD09 火星坐标系->百度坐标系
  24. func GCJ02toBD09(lon, lat float64) (float64, float64) {
  25. z := math.Sqrt(lon*lon+lat*lat) + 0.00002*math.Sin(lat*X_PI)
  26. theta := math.Atan2(lat, lon) + 0.000003*math.Cos(lon*X_PI)
  27. bdLon := z*math.Cos(theta) + 0.0065
  28. bdLat := z*math.Sin(theta) + 0.006
  29. return bdLon, bdLat
  30. }
  31. //WGS84toGCJ02 WGS84坐标系->火星坐标系
  32. func WGS84toGCJ02(lon, lat float64) (float64, float64) {
  33. if isOutOFChina(lon, lat) {
  34. return lon, lat
  35. }
  36. mgLon, mgLat := delta(lon, lat)
  37. return mgLon, mgLat
  38. }
  39. //GCJ02toWGS84 火星坐标系->WGS84坐标系
  40. func GCJ02toWGS84(lon, lat float64) (float64, float64) {
  41. if isOutOFChina(lon, lat) {
  42. return lon, lat
  43. }
  44. mgLon, mgLat := delta(lon, lat)
  45. return lon*2 - mgLon, lat*2 - mgLat
  46. }
  47. //BD09toWGS84 百度坐标系->WGS84坐标系
  48. func BD09toWGS84(lon, lat float64) (float64, float64) {
  49. lon, lat = BD09toGCJ02(lon, lat)
  50. return GCJ02toWGS84(lon, lat)
  51. }
  52. //WGS84toBD09 WGS84坐标系->百度坐标系
  53. func WGS84toBD09(lon, lat float64) (float64, float64) {
  54. lon, lat = WGS84toGCJ02(lon, lat)
  55. return GCJ02toBD09(lon, lat)
  56. }
  57. func delta(lon, lat float64) (float64, float64) {
  58. dlat := transformlat(lon-105.0, lat-35.0)
  59. dlon := transformlng(lon-105.0, lat-35.0)
  60. radlat := lat / 180.0 * math.Pi
  61. magic := math.Sin(radlat)
  62. magic = 1 - OFFSET*magic*magic
  63. sqrtmagic := math.Sqrt(magic)
  64. dlat = (dlat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtmagic) * math.Pi)
  65. dlon = (dlon * 180.0) / (AXIS / sqrtmagic * math.Cos(radlat) * math.Pi)
  66. mgLat := lat + dlat
  67. mgLon := lon + dlon
  68. return mgLon, mgLat
  69. }
  70. func transformlat(lon, lat float64) float64 {
  71. var ret = -100.0 + 2.0*lon + 3.0*lat + 0.2*lat*lat + 0.1*lon*lat + 0.2*math.Sqrt(math.Abs(lon))
  72. ret += (20.0*math.Sin(6.0*lon*math.Pi) + 20.0*math.Sin(2.0*lon*math.Pi)) * 2.0 / 3.0
  73. ret += (20.0*math.Sin(lat*math.Pi) + 40.0*math.Sin(lat/3.0*math.Pi)) * 2.0 / 3.0
  74. ret += (160.0*math.Sin(lat/12.0*math.Pi) + 320*math.Sin(lat*math.Pi/30.0)) * 2.0 / 3.0
  75. return ret
  76. }
  77. func transformlng(lon, lat float64) float64 {
  78. var ret = 300.0 + lon + 2.0*lat + 0.1*lon*lon + 0.1*lon*lat + 0.1*math.Sqrt(math.Abs(lon))
  79. ret += (20.0*math.Sin(6.0*lon*math.Pi) + 20.0*math.Sin(2.0*lon*math.Pi)) * 2.0 / 3.0
  80. ret += (20.0*math.Sin(lon*math.Pi) + 40.0*math.Sin(lon/3.0*math.Pi)) * 2.0 / 3.0
  81. ret += (150.0*math.Sin(lon/12.0*math.Pi) + 300.0*math.Sin(lon/30.0*math.Pi)) * 2.0 / 3.0
  82. return ret
  83. }
  84. func isOutOFChina(lon, lat float64) bool {
  85. return !(lon > 73.66 && lon < 135.05 && lat > 3.86 && lat < 53.55)
  86. }