import { Cesium } from "@/global";
|
import dayjs from "dayjs";
|
import * as R from "ramda";
|
|
Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI1NjM5MjMxOS1lMWVkLTQyNDQtYTM4Yi0wZjA4ZDMxYTlmNDMiLCJpZCI6MTQ4MiwiaWF0IjoxNTI4Njc3NDQyfQ.vVoSexHMqQhKK5loNCv6gCA5d5_z3wE2M0l_rWnIP_w";
|
|
/**
|
* 获取CESIUM开始时钟
|
* @param {String | Null} time 时间
|
* @param {String | Null} date 日期
|
*/
|
function getDayTimeClock(time, date) {
|
const dateString =
|
dayjs(date || new Date())
|
.format("YYYY-MM-DD")
|
.toString() +
|
"T" +
|
(time || "09:30:00");
|
const startTime = Cesium.JulianDate.fromDate(new Date(dateString));
|
return new Cesium.Clock({ startTime: startTime });
|
}
|
|
/**
|
* 获取CESIUM时钟ViewModel
|
* @param {String} time 时间
|
* @return {Cesium.ClockViewModel}
|
*/
|
export function getClockViewModel(time) {
|
const startTimeClock = getDayTimeClock(time);
|
return new Cesium.ClockViewModel(startTimeClock);
|
}
|
|
/**
|
* 设置对象属性
|
* @param {*} entity
|
* @param {*} properties
|
* @returns
|
*/
|
export function setEntityAttributes(entity, properties) {
|
if(!entity || !properties) return;
|
if(!entity.attribute.attr) {
|
entity.attribute.attr = {};
|
}
|
for(const key in properties) {
|
entity.attribute.attr[key] = properties[key];
|
}
|
}
|
|
/**
|
* Cartesian3转WGS84
|
* @param {Cartesian3} point
|
* @return {Object}
|
*/
|
export function Cartesian3_to_WGS84(point) {
|
if(!point) return null;
|
var cartesian33 = new Cesium.Cartesian3(point.x, point.y, point.z || 0.0);
|
var cartographic = Cesium.Cartographic.fromCartesian(cartesian33);
|
var lat = Cesium.Math.toDegrees(cartographic.latitude);
|
var lng = Cesium.Math.toDegrees(cartographic.longitude);
|
var alt = cartographic.height;
|
return {
|
x: lng,
|
y: lat,
|
z: alt
|
};
|
}
|
|
let entity = null;
|
export function viewerflyToLonLat(viewer, lon, lat, alt, heading, pitch) {
|
if (!viewer) return;
|
if (entity) viewer.entities.remove(entity);
|
entity = new Cesium.Entity({
|
id: "flyTmp",
|
position: Cesium.Cartesian3.fromDegrees(lon, lat),
|
point: {
|
pixelSize: 0,
|
color: Cesium.Color.WHITE.withAlpha(0),
|
},
|
});
|
viewer.entities.add(entity);
|
viewer.flyTo(entity, {
|
offset: {
|
heading: Cesium.Math.toRadians(heading || 0.0),
|
pitch: Cesium.Math.toRadians(pitch || -90),
|
range: alt,
|
},
|
});
|
}
|
|
|
export function updateImageLayerStyle(viewer, layerId, option) {
|
if (!viewer) return;
|
const layer = viewer.imageryLayers.get(layerId);
|
if (!layer) return;
|
for (const key in option) {
|
layer[key] = option[key];
|
}
|
}
|
|
/**
|
* 根据中心点创建多边形动画
|
* @param {*} center
|
* @param {*} option
|
* @param {*} entities
|
*/
|
export function createAnimateShapeByCenter(center, option, entities) {
|
if (!center || center.x === undefined) return dataSource;
|
const _option = R.merge(
|
{
|
minHeight: 1,
|
maxHeight: 90,
|
repeat: [2, 1],
|
color: "#8BDBFA",
|
duration: 2000,
|
opacity: 0.6,
|
radius: 100,
|
},
|
option
|
);
|
const point = turf.point([center.x, center.y]);
|
const buffered = turf.buffer(point, _option.radius, { units: "meters" });
|
const points = buffered.geometry.coordinates[0];
|
const _points = [];
|
const minHeights = [];
|
const maxHeights = [];
|
for (let i = 0; i < points.length; i++) {
|
_points.push(points[i][0], points[i][1]);
|
if (points[i][2] !== undefined) {
|
minHeights.push(points[i][2]);
|
} else {
|
minHeights.push(_option.minHeight);
|
}
|
maxHeights.push(_option.maxHeight);
|
}
|
|
return entities.add({
|
wall: {
|
positions: Cesium.Cartesian3.fromDegreesArray(_points),
|
maximumHeights: maxHeights,
|
minimumHeights: minHeights,
|
material: new wutu3d.LineFlowMaterial({
|
//动画线材质
|
color: new Cesium.Color.fromCssColorString(_option.color).withAlpha(
|
_option.opacity
|
),
|
duration: _option.duration,
|
url: `./images/fence.png`,
|
repeat: new Cesium.Cartesian2(_option.repeat[0], _option.repeat[1]),
|
axisY: true,
|
}),
|
},
|
});
|
}
|
|
/**
|
* 检查坐标值
|
* @param {Number} x 经度
|
* @param {Number} y 纬度
|
*/
|
export function checkWgs84Point(x, y) {
|
x = Number(x);
|
y = Number(y);
|
return (
|
x !== null &&
|
y !== null &&
|
x !== "" &&
|
y !== "" &&
|
x !== undefined &&
|
y !== undefined &&
|
!isNaN(x) &&
|
!isNaN(y) &&
|
x >= -180 &&
|
x <= 180 &&
|
y >= -90 &&
|
y <= 90
|
);
|
}
|