MapHelper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import proj4 from "proj4";
  2. import { Notification, MessageBox, Message, Loading } from "element-ui";
  3. const CTservice = axios.create({
  4. // 超时
  5. timeout: 60000,
  6. });
  7. // 响应拦截器
  8. CTservice.interceptors.response.use(
  9. (res) => {
  10. // 未设置状态码则默认成功状态
  11. const code = res.data.code || 200;
  12. // 二进制数据则直接返回
  13. if (
  14. res.request.responseType === "blob" ||
  15. res.request.responseType === "arraybuffer"
  16. ) {
  17. return res.data;
  18. }
  19. if (code !== 200) {
  20. Message({
  21. message: "模型服务获取失败,请检查服务",
  22. type: "warning",
  23. });
  24. return null;
  25. } else {
  26. return res;
  27. }
  28. },
  29. (error) => {
  30. console.log("err" + error);
  31. Message({
  32. message: "模型服务获取失败,请检查服务",
  33. type: "warning",
  34. });
  35. return null;
  36. }
  37. );
  38. export const mapQuery = async (url, queryObj) => {
  39. let response = await CTservice.post(url, queryObj);
  40. if (response && response.data.featureCount > 0) {
  41. return response.data;
  42. } else {
  43. return null;
  44. }
  45. };
  46. export const mapanalyzePost = async (url, queryObj) => {
  47. let response = await CTservice.post(url, queryObj);
  48. debugger;
  49. if (response && response.data) {
  50. return response.data;
  51. } else {
  52. return null;
  53. }
  54. };
  55. export const mapanalyze = async (url, params) => {
  56. let response = await CTservice({
  57. url: url,
  58. method: "get",
  59. params: params,
  60. });
  61. if (response && response.data) {
  62. return response.data;
  63. } else {
  64. return null;
  65. }
  66. };
  67. /**
  68. * 墨卡托转经纬度
  69. * @author
  70. * @param {*} x
  71. * @param {*} y
  72. * @returns [x,y]
  73. */
  74. export const mercator2lonLat = (x, y) => {
  75. var obj = proj4(proj4("EPSG:3857"), proj4("EPSG:4326"), [x, y]);
  76. return obj;
  77. };
  78. /**
  79. * 经纬度转墨卡托
  80. * @author
  81. * @param {*} x
  82. * @param {*} y
  83. * @returns [x,y]
  84. */
  85. export const lonLat2Mercator = (x, y) => {
  86. var obj = proj4(proj4("EPSG:4326"), proj4("EPSG:3857"), [x, y]);
  87. return obj;
  88. };
  89. /**
  90. * 笛卡尔坐标系转WGS84坐标系(经纬度)
  91. * @author
  92. * @param {object} point 点,笛卡尔坐标{x:x,y:y,z:z}
  93. * @returns {object} -lat: lat, lng: lng, alt: alt
  94. */
  95. export const cartesian3ToWGS84 = (point) => {
  96. var cartesian3 = new Cesium.Cartesian3(point.x, point.y, point.z);
  97. var cartographic = Cesium.Cartographic.fromCartesian(cartesian3);
  98. var lat = Cesium.Math.toDegrees(cartographic.latitude);
  99. var lng = Cesium.Math.toDegrees(cartographic.longitude);
  100. var alt = cartographic.height;
  101. return {
  102. lat: lat,
  103. lng: lng,
  104. alt: alt,
  105. };
  106. };
  107. /**
  108. * 对象数组扁平化
  109. * @param {array} arr 数组
  110. * @param {string} children
  111. * @returns
  112. */
  113. export const flatten = (arr, children = "children") => {
  114. return arr.reduce((result, item) => {
  115. return result.concat(
  116. item,
  117. Array.isArray(item[children]) ? flatten(item[children], children) : []
  118. );
  119. }, []);
  120. };
  121. /**
  122. * @author
  123. * @description 设置地下模式状态
  124. * @param {*} state
  125. */
  126. export const undergroundMode = (state) => {
  127. if (state) {
  128. viewer.scene.globe.globeAlpha = 0.3;
  129. } else {
  130. viewer.scene.globe.globeAlpha = 1;
  131. }
  132. // this.scene.undergroundMode = state; //设置开启地下场景
  133. };
  134. /**
  135. * @namespace 名称:getGroundPoint
  136. * @description 作用:根据经纬度(或者墨卡托)获取地面三维坐标
  137. * @param {*} lng 经度,
  138. * @param {*} lat 纬度,
  139. * @returns {Cartesian3} 返回笛卡尔坐标
  140. */
  141. export const getGroundPoint = async (lng, lat, completed) => {
  142. if (!lng || !lat) return null;
  143. try {
  144. debugger;
  145. let positions = [Cesium.Cartographic.fromDegrees(lng, lat)];
  146. //如果有dem高程,则取dem高程,如果没有,则地图面高度为0
  147. if (viewer.terrainProvider) {
  148. var promise = Cesium.sampleTerrainMostDetailed(
  149. viewer.terrainProvider,
  150. positions
  151. );
  152. const resultP = await Promise.resolve(promise);
  153. if (resultP && resultP.length > 0) {
  154. let myP = new Cesium.Cartesian3.fromDegrees(
  155. lng,
  156. lat,
  157. resultP[0].height
  158. );
  159. return myP;
  160. }
  161. }
  162. } catch (error) {
  163. return null;
  164. }
  165. let myP = Cesium.Cartesian3.fromDegrees(lng, lat, 0);
  166. return myP;
  167. };