MapHelper.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import proj4 from "proj4";
  2. import { Notification, MessageBox, Message, Loading } from "element-ui";
  3. import { tansParams, blobValidate } from "@/utils/ruoyi";
  4. const CTservice = axios.create({
  5. // 超时
  6. timeout: 60000,
  7. });
  8. // 响应拦截器
  9. CTservice.interceptors.response.use(
  10. (res) => {
  11. // 未设置状态码则默认成功状态
  12. const code = res.data.code || 200;
  13. // 二进制数据则直接返回
  14. if (
  15. res.request.responseType === "blob" ||
  16. res.request.responseType === "arraybuffer"
  17. ) {
  18. return res.data;
  19. }
  20. if (code !== 200) {
  21. console.log("err:模型服务获取失败,请检查服务!!" + error);
  22. // Message({
  23. // message: "模型服务获取失败,请检查服务",
  24. // type: "warning",
  25. // });
  26. return null;
  27. } else {
  28. return res;
  29. }
  30. },
  31. (error) => {
  32. console.log("err:模型服务获取失败,请检查服务!!" + error);
  33. // Message({
  34. // message: "模型服务获取失败,请检查服务",
  35. // type: "warning",
  36. // });
  37. return null;
  38. }
  39. );
  40. export const mapQuery = async (url, queryObj) => {
  41. let response = await CTservice.post(url, queryObj);
  42. if (response && response.data.featureCount > 0) {
  43. return response.data;
  44. } else {
  45. return null;
  46. }
  47. };
  48. export const mapanalyzePost = async (url, queryObj) => {
  49. let response = await CTservice.post(url, queryObj);
  50. if (response && response.data) {
  51. return response.data;
  52. } else {
  53. return null;
  54. }
  55. };
  56. export const mapanalyze = async (url, params) => {
  57. let response = await CTservice({
  58. url: url,
  59. method: "get",
  60. params: params,
  61. });
  62. if (response && response.data) {
  63. return response.data;
  64. } else {
  65. return null;
  66. }
  67. };
  68. /**
  69. * 获取面积
  70. * @param {Array} points [
  71. { x: 109.513901747847, y: 18.3138227456355 },
  72. { x: 109.513756340215, y: 18.313823538585 },
  73. { x: 109.513301924234, y: 18.3138260150956 },
  74. { x: 109.513159896168, y: 18.313962267038 },
  75. { x: 109.513158251941, y: 18.3151722832427 },
  76. { x: 109.513299913524, y: 18.3153069903435 },
  77. { x: 109.513907590019, y: 18.3153036780715 },
  78. { x: 109.513901747847, y: 18.3138227456355 },
  79. ]
  80. * @returns Number
  81. */
  82. export const mapArea = async (points) => {
  83. if (!points) {
  84. return 0;
  85. }
  86. let response = await CTservice({
  87. url:
  88. window.supermapIServerUrl +
  89. "/iserver/services/geometry/restjsr/v1/geometry/area",
  90. method: "get",
  91. params: {
  92. point2Ds: JSON.stringify(points),
  93. unit: window.unit || "METER",
  94. prjCoordSys: "{ epsgcode: " + (window.epsgcode || "4490") + " }",
  95. },
  96. });
  97. if (response && response.area) {
  98. return response.area;
  99. } else {
  100. return 0;
  101. }
  102. };
  103. /**
  104. * 墨卡托转经纬度
  105. * @author
  106. * @param {*} x
  107. * @param {*} y
  108. * @returns [x,y]
  109. */
  110. export const mercator2lonLat = (x, y) => {
  111. var obj = proj4(proj4("EPSG:3857"), proj4("EPSG:4326"), [x, y]);
  112. return obj;
  113. };
  114. /**
  115. * 经纬度转墨卡托
  116. * @author
  117. * @param {*} x
  118. * @param {*} y
  119. * @returns [x,y]
  120. */
  121. export const lonLat2Mercator = (x, y) => {
  122. var obj = proj4(proj4("EPSG:4326"), proj4("EPSG:3857"), [x, y]);
  123. return obj;
  124. };
  125. /**
  126. * 笛卡尔坐标系转WGS84坐标系(经纬度)
  127. * @author
  128. * @param {object} point 点,笛卡尔坐标{x:x,y:y,z:z}
  129. * @returns {object} -lat: lat, lng: lng, alt: alt
  130. */
  131. export const cartesian3ToWGS84 = (point) => {
  132. var cartesian3 = new Cesium.Cartesian3(point.x, point.y, point.z);
  133. var cartographic = Cesium.Cartographic.fromCartesian(cartesian3);
  134. var lat = Cesium.Math.toDegrees(cartographic.latitude);
  135. var lng = Cesium.Math.toDegrees(cartographic.longitude);
  136. var alt = cartographic.height;
  137. return {
  138. lat: lat,
  139. lng: lng,
  140. alt: alt,
  141. };
  142. };
  143. /**
  144. * 对象数组扁平化
  145. * @param {array} arr 数组
  146. * @param {string} children
  147. * @returns
  148. */
  149. export const flatten = (arr, children = "children") => {
  150. return arr.reduce((result, item) => {
  151. return result.concat(
  152. item,
  153. Array.isArray(item[children]) ? flatten(item[children], children) : []
  154. );
  155. }, []);
  156. };
  157. /**
  158. * @author
  159. * @description 设置地下模式状态
  160. * @param {*} state
  161. */
  162. export const undergroundMode = (state) => {
  163. if (state) {
  164. viewer.scene.globe.globeAlpha = window.Layeralpha;
  165. } else {
  166. viewer.scene.globe.globeAlpha = 1;
  167. }
  168. // this.scene.undergroundMode = state; //设置开启地下场景
  169. };
  170. /**
  171. * @namespace 名称:getGroundPoint
  172. * @description 作用:根据经纬度(或者墨卡托)获取地面三维坐标
  173. * @param {*} lng 经度,
  174. * @param {*} lat 纬度,
  175. * @returns {Cartesian3} 返回笛卡尔坐标
  176. */
  177. export const getGroundPoint = async (lng, lat, completed) => {
  178. if (!lng || !lat) return null;
  179. try {
  180. let positions = [Cesium.Cartographic.fromDegrees(lng, lat)];
  181. //如果有dem高程,则取dem高程,如果没有,则地图面高度为0
  182. if (viewer.terrainProvider) {
  183. var promise = Cesium.sampleTerrainMostDetailed(
  184. viewer.terrainProvider,
  185. positions
  186. );
  187. const resultP = await Promise.resolve(promise);
  188. if (resultP && resultP.length > 0) {
  189. let myP = new Cesium.Cartesian3.fromDegrees(
  190. lng,
  191. lat,
  192. resultP[0].height
  193. );
  194. return myP;
  195. }
  196. }
  197. } catch (error) {
  198. return null;
  199. }
  200. let myP = Cesium.Cartesian3.fromDegrees(lng, lat, 0);
  201. return myP;
  202. };
  203. /**
  204. * @namespace 名称:getGroundPoint
  205. * @description 作用:根据经纬度(或者墨卡托)获取地面三维坐标
  206. * @param {Array} Points 经纬度,
  207. * @returns {Cartesian3} 返回经纬度
  208. */
  209. export const getXYZPoint = async (Points) => {
  210. if (!Points && Points.length > 0) return [];
  211. try {
  212. var positions = [];
  213. for (var index = 0; index < Points.length; index++) {
  214. var position = Points[index];
  215. positions.push(Cesium.Cartographic.fromDegrees(position[0], position[1]));
  216. }
  217. //如果有dem高程,则取dem高程,如果没有,则地图面高度为0
  218. if (viewer.terrainProvider) {
  219. var promise = Cesium.sampleTerrainMostDetailed(
  220. viewer.terrainProvider,
  221. positions
  222. );
  223. var resultPs = await Promise.resolve(promise);
  224. if (resultPs && resultPs.length > 0) {
  225. var xyzs = [];
  226. for (var index = 0; index < resultPs.length; index++) {
  227. var resultP = resultPs[index];
  228. var longitude = Cesium.Math.toDegrees(resultP.longitude);
  229. var latitude = Cesium.Math.toDegrees(resultP.latitude);
  230. xyzs.push({ x: longitude, y: latitude, z: resultP.height });
  231. }
  232. return xyzs;
  233. }
  234. } else {
  235. for (var index = 0; index < Points.length; index++) {
  236. var Point = Points[index];
  237. Point = { x: Point[0], y: Point[1], z: 0 };
  238. }
  239. return Points;
  240. }
  241. } catch (error) {
  242. return [];
  243. }
  244. };