WSCoordinate.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // 计算两点之间直线距离
  2. export const algorithm = (point1, point2)=> {
  3. let [x1, y1] = point1;
  4. let [x2, y2] = point2;
  5. let Lat1 = rad(x1); // 纬度
  6. let Lat2 = rad(x2);
  7. let a = Lat1 - Lat2;// 两点纬度之差
  8. let b = rad(y1) - rad(y2); // 经度之差
  9. let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
  10. + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));
  11. // 计算两点距离的公式
  12. s = s * 6378137.0;// 弧长等于弧度乘地球半径(半径为米)
  13. s = Math.round(s * 10000) / 10000;// 精确距离的数值
  14. return s;
  15. }
  16. // 角度转换成弧度
  17. const rad =(d) =>{
  18. return d * Math.PI / 180.00;
  19. };
  20. /**
  21. * 判断经纬度是否超出中国境内
  22. */
  23. export function isLocationOutOfChina(latitude, longitude) {
  24. if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)
  25. return true;
  26. return false;
  27. }
  28. /**
  29. * 将WGS-84(国际标准)转为GCJ-02(火星坐标):
  30. */
  31. export function transformFromWGSToGCJ(latitude, longitude) {
  32. let lat = "";
  33. let lon = "";
  34. let ee = 0.00669342162296594323;
  35. let a = 6378245.0;
  36. let pi = 3.14159265358979324;
  37. if (isLocationOutOfChina(latitude, longitude)) {
  38. lat = latitude;
  39. lon = longitude;
  40. }
  41. else {
  42. let adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);
  43. let adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);
  44. let radLat = latitude / 180.0 * pi;
  45. let magic = Math.sin(radLat);
  46. magic = 1 - ee * magic * magic;
  47. let sqrtMagic = Math.sqrt(magic);
  48. adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  49. adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  50. latitude = latitude + adjustLat;
  51. longitude = longitude + adjustLon;
  52. }
  53. return { latitude: latitude, longitude: longitude };
  54. }
  55. /**
  56. * 将GCJ-02(火星坐标)转为百度坐标:
  57. */
  58. export function transformFromGCJToBaidu(latitude, longitude) {
  59. let pi = 3.14159265358979324 * 3000.0 / 180.0;
  60. let z = Math.sqrt(longitude * longitude + latitude * latitude) + 0.00002 * Math.sin(latitude * pi);
  61. let theta = Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * pi);
  62. let a_latitude = (z * Math.sin(theta) + 0.006);
  63. let a_longitude = (z * Math.cos(theta) + 0.0065);
  64. return { latitude: a_latitude, longitude: a_longitude };
  65. }
  66. /**
  67. * 将百度坐标转为GCJ-02(火星坐标):
  68. */
  69. export function transformFromBaiduToGCJ(latitude, longitude) {
  70. let xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0;
  71. let x = longitude - 0.0065;
  72. let y = latitude - 0.006;
  73. let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
  74. let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
  75. let a_latitude = z * Math.sin(theta);
  76. let a_longitude = z * Math.cos(theta);
  77. return { latitude: a_latitude, longitude: a_longitude };
  78. }
  79. /**
  80. * 将GCJ-02(火星坐标)转为WGS-84:
  81. */
  82. export function transformFromGCJToWGS({latitude, longitude}) {
  83. let threshold = 0.00001;
  84. // The boundary
  85. let minLat = latitude - 0.5;
  86. let maxLat = latitude + 0.5;
  87. let minLng = longitude - 0.5;
  88. let maxLng = longitude + 0.5;
  89. let delta = 1;
  90. let maxIteration = 30;
  91. let success = false
  92. while (success == false) {
  93. let leftBottom = transformFromWGSToGCJ(minLat, minLng);
  94. let rightBottom = transformFromWGSToGCJ(minLat, maxLng);
  95. let leftUp = transformFromWGSToGCJ(maxLat, minLng);
  96. let midPoint = transformFromWGSToGCJ((minLat + maxLat) / 2, (minLng + maxLng) / 2);
  97. delta = Math.abs(midPoint.latitude - latitude) + Math.abs(midPoint.longitude - longitude);
  98. if (maxIteration-- <= 0 || delta <= threshold) {
  99. return { latitude: (minLat + maxLat) / 2, longitude: (minLng + maxLng) / 2 };
  100. }
  101. if (isContains({ latitude: latitude, longitude: longitude }, leftBottom, midPoint)) {
  102. maxLat = (minLat + maxLat) / 2;
  103. maxLng = (minLng + maxLng) / 2;
  104. }
  105. else if (isContains({ latitude: latitude, longitude: longitude }, rightBottom, midPoint)) {
  106. maxLat = (minLat + maxLat) / 2;
  107. minLng = (minLng + maxLng) / 2;
  108. }
  109. else if (isContains({ latitude: latitude, longitude: longitude }, leftUp, midPoint)) {
  110. minLat = (minLat + maxLat) / 2;
  111. maxLng = (minLng + maxLng) / 2;
  112. }
  113. else {
  114. minLat = (minLat + maxLat) / 2;
  115. minLng = (minLng + maxLng) / 2;
  116. }
  117. }
  118. }
  119. export function toPointWkt(location){
  120. let {longitude,latitude} = location
  121. return "POINT(" + longitude + " " + latitude + ")"
  122. }
  123. export function pointWktToLocation(wkt){
  124. let start = wkt.indexOf("(")
  125. let end = wkt.indexOf(")")
  126. let arr = wkt.substring(start + 1,end).trim().split(" ")
  127. return {longitude:parseFloat(arr[0]),latitude:parseFloat(arr[1])}
  128. }
  129. export function isContains(point, p1, p2) {
  130. return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude));
  131. }
  132. export function transformLatWithXY(x, y) {
  133. let pi = 3.14159265358979324;
  134. let lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  135. lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  136. lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  137. lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  138. return lat;
  139. }
  140. export function transformLonWithXY(x, y) {
  141. let pi = 3.14159265358979324;
  142. let lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  143. lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  144. lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  145. lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  146. return lon;
  147. }
  148. //检查视野范围是否超过了bbox
  149. export function checkRegion(bbox,region){
  150. if(bbox.northeast.latitude < region.northeast.latitude ){
  151. return true;
  152. }
  153. if(bbox.northeast.longitude < region.northeast.longitude ){
  154. return true;
  155. }
  156. if( bbox.southwest.latitude > region.southwest.latitude ){
  157. return true;
  158. }
  159. if(bbox.southwest.longitude > region.southwest.longitude ){
  160. return true;
  161. }
  162. return false
  163. }