import proj4 from "proj4"; import { Notification, MessageBox, Message, Loading } from "element-ui"; const CTservice = axios.create({ // 超时 timeout: 60000, }); // 响应拦截器 CTservice.interceptors.response.use( (res) => { // 未设置状态码则默认成功状态 const code = res.data.code || 200; // 二进制数据则直接返回 if ( res.request.responseType === "blob" || res.request.responseType === "arraybuffer" ) { return res.data; } if (code !== 200) { Message({ message: "模型服务获取失败,请检查服务", type: "warning", }); return null; } else { return res; } }, (error) => { console.log("err" + error); Message({ message: "模型服务获取失败,请检查服务", type: "warning", }); return null; } ); export const mapQuery = async (url, queryObj) => { let response = await CTservice.post(url, queryObj); if (response && response.data.featureCount > 0) { return response.data; } else { return null; } }; /** * 墨卡托转经纬度 * @author * @param {*} x * @param {*} y * @returns [x,y] */ export const mercator2lonLat = (x, y) => { var obj = proj4(proj4("EPSG:3857"), proj4("EPSG:4326"), [x, y]); return obj; }; /** * 经纬度转墨卡托 * @author * @param {*} x * @param {*} y * @returns [x,y] */ export const lonLat2Mercator = (x, y) => { var obj = proj4(proj4("EPSG:4326"), proj4("EPSG:3857"), [x, y]); return obj; }; /** * 笛卡尔坐标系转WGS84坐标系(经纬度) * @author * @param {object} point 点,笛卡尔坐标{x:x,y:y,z:z} * @returns {object} -lat: lat, lng: lng, alt: alt */ export const cartesian3ToWGS84 = (point) => { var cartesian3 = new Cesium.Cartesian3(point.x, point.y, point.z); var cartographic = Cesium.Cartographic.fromCartesian(cartesian3); var lat = Cesium.Math.toDegrees(cartographic.latitude); var lng = Cesium.Math.toDegrees(cartographic.longitude); var alt = cartographic.height; return { lat: lat, lng: lng, alt: alt, }; }; /** * 对象数组扁平化 * @param {array} arr 数组 * @param {string} children * @returns */ export const flatten = (arr, children = "children") => { return arr.reduce((result, item) => { return result.concat( item, Array.isArray(item[children]) ? flatten(item[children], children) : [] ); }, []); }; /** * @author * @description 设置地下模式状态 * @param {*} state */ export const undergroundMode = (state) => { if (state) { viewer.scene.globe.globeAlpha = 0; } else { viewer.scene.globe.globeAlpha = 1; } // this.scene.undergroundMode = state; //设置开启地下场景 }; /** * @namespace 名称:getGroundPoint * @description 作用:根据经纬度(或者墨卡托)获取地面三维坐标 * @param {*} lng 经度, * @param {*} lat 纬度, * @returns {Cartesian3} 返回笛卡尔坐标 */ export const getGroundPoint = async (lng, lat, completed) => { if (!lng || !lat) return null; try { debugger; let positions = [Cesium.Cartographic.fromDegrees(lng, lat)]; //如果有dem高程,则取dem高程,如果没有,则地图面高度为0 if (viewer.terrainProvider) { var promise = Cesium.sampleTerrainMostDetailed( viewer.terrainProvider, positions ); const resultP = await Promise.resolve(promise); if (resultP && resultP.length > 0) { let myP = new Cesium.Cartesian3.fromDegrees( lng, lat, resultP[0].height ); return myP; } } } catch (error) { return null; } let myP = Cesium.Cartesian3.fromDegrees(lng, lat, 0); return myP; };