WSCoordinate.js 7.3 KB

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