UNPKG

hf-cesium

Version:

css和js入口合并成一个

1,668 lines 160 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import * as Cesium from "cesium"; import { ScreenSpaceEventHandler, Cartographic, Math as Math$1, ScreenSpaceEventType, Entity, Cartesian3, HeadingPitchRoll, Cartesian2, Color, CallbackProperty, DistanceDisplayCondition, VerticalOrigin, HorizontalOrigin, LabelStyle, ClassificationType, PolygonHierarchy, PolylineOutlineMaterialProperty, PolylineDashMaterialProperty, UrlTemplateImageryProvider, WebMercatorTilingScheme, ArcGisMapServerImageryProvider, WebMercatorProjection, Matrix4, Viewer, JulianDate, Rectangle, BoundingSphere, DirectionalLight, SceneTransforms, createOsmBuildings, Matrix3, Transforms, CesiumTerrainProvider, EllipsoidTerrainProvider, HeadingPitchRange } from "cesium"; var Easing = { Linear: { None: function(amount) { return amount; } }, Quadratic: { In: function(amount) { return amount * amount; }, Out: function(amount) { return amount * (2 - amount); }, InOut: function(amount) { if ((amount *= 2) < 1) { return 0.5 * amount * amount; } return -0.5 * (--amount * (amount - 2) - 1); } }, Cubic: { In: function(amount) { return amount * amount * amount; }, Out: function(amount) { return --amount * amount * amount + 1; }, InOut: function(amount) { if ((amount *= 2) < 1) { return 0.5 * amount * amount * amount; } return 0.5 * ((amount -= 2) * amount * amount + 2); } }, Quartic: { In: function(amount) { return amount * amount * amount * amount; }, Out: function(amount) { return 1 - --amount * amount * amount * amount; }, InOut: function(amount) { if ((amount *= 2) < 1) { return 0.5 * amount * amount * amount * amount; } return -0.5 * ((amount -= 2) * amount * amount * amount - 2); } }, Quintic: { In: function(amount) { return amount * amount * amount * amount * amount; }, Out: function(amount) { return --amount * amount * amount * amount * amount + 1; }, InOut: function(amount) { if ((amount *= 2) < 1) { return 0.5 * amount * amount * amount * amount * amount; } return 0.5 * ((amount -= 2) * amount * amount * amount * amount + 2); } }, Sinusoidal: { In: function(amount) { return 1 - Math.cos(amount * Math.PI / 2); }, Out: function(amount) { return Math.sin(amount * Math.PI / 2); }, InOut: function(amount) { return 0.5 * (1 - Math.cos(Math.PI * amount)); } }, Exponential: { In: function(amount) { return amount === 0 ? 0 : Math.pow(1024, amount - 1); }, Out: function(amount) { return amount === 1 ? 1 : 1 - Math.pow(2, -10 * amount); }, InOut: function(amount) { if (amount === 0) { return 0; } if (amount === 1) { return 1; } if ((amount *= 2) < 1) { return 0.5 * Math.pow(1024, amount - 1); } return 0.5 * (-Math.pow(2, -10 * (amount - 1)) + 2); } }, Circular: { In: function(amount) { return 1 - Math.sqrt(1 - amount * amount); }, Out: function(amount) { return Math.sqrt(1 - --amount * amount); }, InOut: function(amount) { if ((amount *= 2) < 1) { return -0.5 * (Math.sqrt(1 - amount * amount) - 1); } return 0.5 * (Math.sqrt(1 - (amount -= 2) * amount) + 1); } }, Elastic: { In: function(amount) { if (amount === 0) { return 0; } if (amount === 1) { return 1; } return -Math.pow(2, 10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI); }, Out: function(amount) { if (amount === 0) { return 0; } if (amount === 1) { return 1; } return Math.pow(2, -10 * amount) * Math.sin((amount - 0.1) * 5 * Math.PI) + 1; }, InOut: function(amount) { if (amount === 0) { return 0; } if (amount === 1) { return 1; } amount *= 2; if (amount < 1) { return -0.5 * Math.pow(2, 10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI); } return 0.5 * Math.pow(2, -10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI) + 1; } }, Back: { In: function(amount) { var s = 1.70158; return amount * amount * ((s + 1) * amount - s); }, Out: function(amount) { var s = 1.70158; return --amount * amount * ((s + 1) * amount + s) + 1; }, InOut: function(amount) { var s = 1.70158 * 1.525; if ((amount *= 2) < 1) { return 0.5 * (amount * amount * ((s + 1) * amount - s)); } return 0.5 * ((amount -= 2) * amount * ((s + 1) * amount + s) + 2); } }, Bounce: { In: function(amount) { return 1 - Easing.Bounce.Out(1 - amount); }, Out: function(amount) { if (amount < 1 / 2.75) { return 7.5625 * amount * amount; } else if (amount < 2 / 2.75) { return 7.5625 * (amount -= 1.5 / 2.75) * amount + 0.75; } else if (amount < 2.5 / 2.75) { return 7.5625 * (amount -= 2.25 / 2.75) * amount + 0.9375; } else { return 7.5625 * (amount -= 2.625 / 2.75) * amount + 0.984375; } }, InOut: function(amount) { if (amount < 0.5) { return Easing.Bounce.In(amount * 2) * 0.5; } return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5; } } }; var now; if (typeof self === "undefined" && typeof process !== "undefined" && process.hrtime) { now = function() { var time = process.hrtime(); return time[0] * 1e3 + time[1] / 1e6; }; } else if (typeof self !== "undefined" && self.performance !== void 0 && self.performance.now !== void 0) { now = self.performance.now.bind(self.performance); } else if (Date.now !== void 0) { now = Date.now; } else { now = function() { return (/* @__PURE__ */ new Date()).getTime(); }; } var now$1 = now; var Group = ( /** @class */ function() { function Group2() { this._tweens = {}; this._tweensAddedDuringUpdate = {}; } Group2.prototype.getAll = function() { var _this = this; return Object.keys(this._tweens).map(function(tweenId) { return _this._tweens[tweenId]; }); }; Group2.prototype.removeAll = function() { this._tweens = {}; }; Group2.prototype.add = function(tween) { this._tweens[tween.getId()] = tween; this._tweensAddedDuringUpdate[tween.getId()] = tween; }; Group2.prototype.remove = function(tween) { delete this._tweens[tween.getId()]; delete this._tweensAddedDuringUpdate[tween.getId()]; }; Group2.prototype.update = function(time, preserve) { if (time === void 0) { time = now$1(); } if (preserve === void 0) { preserve = false; } var tweenIds = Object.keys(this._tweens); if (tweenIds.length === 0) { return false; } while (tweenIds.length > 0) { this._tweensAddedDuringUpdate = {}; for (var i = 0; i < tweenIds.length; i++) { var tween = this._tweens[tweenIds[i]]; var autoStart = !preserve; if (tween && tween.update(time, autoStart) === false && !preserve) { delete this._tweens[tweenIds[i]]; } } tweenIds = Object.keys(this._tweensAddedDuringUpdate); } return true; }; return Group2; }() ); var mainGroup = new Group(); var TWEEN = mainGroup; TWEEN.getAll.bind(TWEEN); TWEEN.removeAll.bind(TWEEN); TWEEN.add.bind(TWEEN); TWEEN.remove.bind(TWEEN); TWEEN.update.bind(TWEEN); const EasingFunction = { /** * Linear easing. * * @type {EasingFunction.Callback} * @constant */ LINEAR_NONE: Easing.Linear.None, /** * Quadratic in. * * @type {EasingFunction.Callback} * @constant */ QUADRATIC_IN: Easing.Quadratic.In, /** * Quadratic out. * * @type {EasingFunction.Callback} * @constant */ QUADRATIC_OUT: Easing.Quadratic.Out, /** * Quadratic in then out. * * @type {EasingFunction.Callback} * @constant */ QUADRATIC_IN_OUT: Easing.Quadratic.InOut, /** * Cubic in. * * @type {EasingFunction.Callback} * @constant */ CUBIC_IN: Easing.Cubic.In, /** * Cubic out. * * @type {EasingFunction.Callback} * @constant */ CUBIC_OUT: Easing.Cubic.Out, /** * Cubic in then out. * * @type {EasingFunction.Callback} * @constant */ CUBIC_IN_OUT: Easing.Cubic.InOut, /** * Quartic in. * * @type {EasingFunction.Callback} * @constant */ QUARTIC_IN: Easing.Quartic.In, /** * Quartic out. * * @type {EasingFunction.Callback} * @constant */ QUARTIC_OUT: Easing.Quartic.Out, /** * Quartic in then out. * * @type {EasingFunction.Callback} * @constant */ QUARTIC_IN_OUT: Easing.Quartic.InOut, /** * Quintic in. * * @type {EasingFunction.Callback} * @constant */ QUINTIC_IN: Easing.Quintic.In, /** * Quintic out. * * @type {EasingFunction.Callback} * @constant */ QUINTIC_OUT: Easing.Quintic.Out, /** * Quintic in then out. * * @type {EasingFunction.Callback} * @constant */ QUINTIC_IN_OUT: Easing.Quintic.InOut, /** * Sinusoidal in. * * @type {EasingFunction.Callback} * @constant */ SINUSOIDAL_IN: Easing.Sinusoidal.In, /** * Sinusoidal out. * * @type {EasingFunction.Callback} * @constant */ SINUSOIDAL_OUT: Easing.Sinusoidal.Out, /** * Sinusoidal in then out. * * @type {EasingFunction.Callback} * @constant */ SINUSOIDAL_IN_OUT: Easing.Sinusoidal.InOut, /** * Exponential in. * * @type {EasingFunction.Callback} * @constant */ EXPONENTIAL_IN: Easing.Exponential.In, /** * Exponential out. * * @type {EasingFunction.Callback} * @constant */ EXPONENTIAL_OUT: Easing.Exponential.Out, /** * Exponential in then out. * * @type {EasingFunction.Callback} * @constant */ EXPONENTIAL_IN_OUT: Easing.Exponential.InOut, /** * Circular in. * * @type {EasingFunction.Callback} * @constant */ CIRCULAR_IN: Easing.Circular.In, /** * Circular out. * * @type {EasingFunction.Callback} * @constant */ CIRCULAR_OUT: Easing.Circular.Out, /** * Circular in then out. * * @type {EasingFunction.Callback} * @constant */ CIRCULAR_IN_OUT: Easing.Circular.InOut, /** * Elastic in. * * @type {EasingFunction.Callback} * @constant */ ELASTIC_IN: Easing.Elastic.In, /** * Elastic out. * * @type {EasingFunction.Callback} * @constant */ ELASTIC_OUT: Easing.Elastic.Out, /** * Elastic in then out. * * @type {EasingFunction.Callback} * @constant */ ELASTIC_IN_OUT: Easing.Elastic.InOut, /** * Back in. * * @type {EasingFunction.Callback} * @constant */ BACK_IN: Easing.Back.In, /** * Back out. * * @type {EasingFunction.Callback} * @constant */ BACK_OUT: Easing.Back.Out, /** * Back in then out. * * @type {EasingFunction.Callback} * @constant */ BACK_IN_OUT: Easing.Back.InOut, /** * Bounce in. * * @type {EasingFunction.Callback} * @constant */ BOUNCE_IN: Easing.Bounce.In, /** * Bounce out. * * @type {EasingFunction.Callback} * @constant */ BOUNCE_OUT: Easing.Bounce.Out, /** * Bounce in then out. * * @type {EasingFunction.Callback} * @constant */ BOUNCE_IN_OUT: Easing.Bounce.InOut }; const EasingFunction$1 = Object.freeze(EasingFunction); class EventManager { constructor(operator) { __publicField(this, "operator"); __publicField(this, "eventHandler"); __publicField(this, "viewer"); __publicField(this, "duration", 0.85); //交互的动画时间 __publicField(this, "easingFunction", EasingFunction$1.CUBIC_IN_OUT); __publicField(this, "callFun"); __publicField(this, "registEventHandlers", []); __publicField(this, "animationFrame", 0); this.operator = operator; this.viewer = operator.viewer; this.eventHandler = new ScreenSpaceEventHandler(this.viewer.scene.canvas); this.init(); } init() { this.initLeftDownEventHandler(); this.animate(); } animate() { var _a, _b; if ((_a = this.registEventHandlers) == null ? void 0 : _a.length) { (_b = this.registEventHandlers) == null ? void 0 : _b.forEach((call) => { call(); }); } this.animationFrame = requestAnimationFrame(this.animate.bind(this)); } initLeftDownEventHandler() { this.eventHandler.setInputAction((e) => { let pos = this.viewer.scene.pickPosition(e.position); let cartographic = Cartographic.fromCartesian(pos); let lat = Math$1.toDegrees(cartographic.latitude); let lng = Math$1.toDegrees(cartographic.longitude); let alt = cartographic.height; console.log(lng, lat, alt, e.position); this.callFun && this.callFun({ lon: lng, lat, alt, position: e.position }); this.flytoEvent(e); }, ScreenSpaceEventType.LEFT_CLICK); } /** * 根据事件 做飞行动画 * @param e 事件 */ flytoEvent(e) { const pickedFeature = this.viewer.scene.pick(e.position); console.log("pickedFeature", pickedFeature); if (!pickedFeature) return; let primitive; let userData; if (pickedFeature.id instanceof Entity && pickedFeature.id.userData) { userData = pickedFeature.id.userData; console.log("pickedFeature.id", userData); if (userData.viewConfig) { const { position, hpr } = userData.viewConfig; this.viewer.camera.flyTo({ destination: Cartesian3.fromDegrees(position[0], position[1], position[2]), orientation: HeadingPitchRoll.fromDegrees(hpr[0], hpr[1], hpr[2]), duration: this.duration, easingFunction: this.easingFunction }); } } else if (pickedFeature.primitive && pickedFeature.primitive.userData) { userData = pickedFeature.primitive.userData; primitive = pickedFeature.primitive; } if (!primitive) return; if (primitive.userData && (primitive.userData.flyto || primitive.userData.viewConfig)) { console.log("primitive.userData---", primitive.userData); let { position, orientation, duration } = primitive.userData.flyto; this.viewer.camera.flyTo({ destination: Cartesian3.fromDegrees(position[0], position[1], position[2]), orientation: HeadingPitchRoll.fromDegrees(orientation[0], orientation[1], orientation[2]), duration: duration || this.duration, easingFunction: this.easingFunction }); } else { const boundingSphere = primitive.boundingSphere; if (boundingSphere) { this.viewer.camera.flyToBoundingSphere(boundingSphere, { duration: this.duration, easingFunction: this.easingFunction }); } } } /** * * @param {*} call 点击的回调函数 */ setClickCallBack(call) { call && (this.callFun = call); } clearClickCallBack() { this.callFun && (this.callFun = void 0); } //注册一个事件回调 registEventHandlerCallback(call) { call && this.registEventHandlers.push(call); } clearRegistEventHandler() { if (this.registEventHandlers && this.registEventHandlers.length) { this.registEventHandlers = []; } } dispose() { this.eventHandler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK); this.animationFrame && cancelAnimationFrame(this.animationFrame); } } var earthRadius = 63710088e-1; var factors = { centimeters: earthRadius * 100, centimetres: earthRadius * 100, degrees: earthRadius / 111325, feet: earthRadius * 3.28084, inches: earthRadius * 39.37, kilometers: earthRadius / 1e3, kilometres: earthRadius / 1e3, meters: earthRadius, metres: earthRadius, miles: earthRadius / 1609.344, millimeters: earthRadius * 1e3, millimetres: earthRadius * 1e3, nauticalmiles: earthRadius / 1852, radians: 1, yards: earthRadius * 1.0936 }; function feature(geom, properties, options) { if (options === void 0) { options = {}; } var feat = { type: "Feature" }; if (options.id === 0 || options.id) { feat.id = options.id; } if (options.bbox) { feat.bbox = options.bbox; } feat.properties = properties || {}; feat.geometry = geom; return feat; } function point(coordinates, properties, options) { if (options === void 0) { options = {}; } if (!coordinates) { throw new Error("coordinates is required"); } if (!Array.isArray(coordinates)) { throw new Error("coordinates must be an Array"); } if (coordinates.length < 2) { throw new Error("coordinates must be at least 2 numbers long"); } if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) { throw new Error("coordinates must contain numbers"); } var geom = { type: "Point", coordinates }; return feature(geom, properties, options); } function polygon(coordinates, properties, options) { if (options === void 0) { options = {}; } for (var _i = 0, coordinates_1 = coordinates; _i < coordinates_1.length; _i++) { var ring = coordinates_1[_i]; if (ring.length < 4) { throw new Error("Each LinearRing of a Polygon must have 4 or more Positions."); } for (var j = 0; j < ring[ring.length - 1].length; j++) { if (ring[ring.length - 1][j] !== ring[0][j]) { throw new Error("First and last Position are not equivalent."); } } } var geom = { type: "Polygon", coordinates }; return feature(geom, properties, options); } function lineString(coordinates, properties, options) { if (options === void 0) { options = {}; } if (coordinates.length < 2) { throw new Error("coordinates must be an array of two or more positions"); } var geom = { type: "LineString", coordinates }; return feature(geom, properties, options); } function radiansToLength(radians, units) { if (units === void 0) { units = "kilometers"; } var factor = factors[units]; if (!factor) { throw new Error(units + " units is invalid"); } return radians * factor; } function lengthToRadians(distance2, units) { if (units === void 0) { units = "kilometers"; } var factor = factors[units]; if (!factor) { throw new Error(units + " units is invalid"); } return distance2 / factor; } function radiansToDegrees(radians) { var degrees = radians % (2 * Math.PI); return degrees * 180 / Math.PI; } function degreesToRadians(degrees) { var radians = degrees % 360; return radians * Math.PI / 180; } function isNumber(num) { return !isNaN(num) && num !== null && !Array.isArray(num); } function coordEach(geojson, callback, excludeWrapCoord) { if (geojson === null) return; var j, k, l, geometry, stopG, coords, geometryMaybeCollection, wrapShrink = 0, coordIndex = 0, isGeometryCollection, type = geojson.type, isFeatureCollection = type === "FeatureCollection", isFeature = type === "Feature", stop = isFeatureCollection ? geojson.features.length : 1; for (var featureIndex = 0; featureIndex < stop; featureIndex++) { geometryMaybeCollection = isFeatureCollection ? geojson.features[featureIndex].geometry : isFeature ? geojson.geometry : geojson; isGeometryCollection = geometryMaybeCollection ? geometryMaybeCollection.type === "GeometryCollection" : false; stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1; for (var geomIndex = 0; geomIndex < stopG; geomIndex++) { var multiFeatureIndex = 0; var geometryIndex = 0; geometry = isGeometryCollection ? geometryMaybeCollection.geometries[geomIndex] : geometryMaybeCollection; if (geometry === null) continue; coords = geometry.coordinates; var geomType = geometry.type; wrapShrink = 0; switch (geomType) { case null: break; case "Point": if (callback( coords, coordIndex, featureIndex, multiFeatureIndex, geometryIndex ) === false) return false; coordIndex++; multiFeatureIndex++; break; case "LineString": case "MultiPoint": for (j = 0; j < coords.length; j++) { if (callback( coords[j], coordIndex, featureIndex, multiFeatureIndex, geometryIndex ) === false) return false; coordIndex++; if (geomType === "MultiPoint") multiFeatureIndex++; } if (geomType === "LineString") multiFeatureIndex++; break; case "Polygon": case "MultiLineString": for (j = 0; j < coords.length; j++) { for (k = 0; k < coords[j].length - wrapShrink; k++) { if (callback( coords[j][k], coordIndex, featureIndex, multiFeatureIndex, geometryIndex ) === false) return false; coordIndex++; } if (geomType === "MultiLineString") multiFeatureIndex++; if (geomType === "Polygon") geometryIndex++; } if (geomType === "Polygon") multiFeatureIndex++; break; case "MultiPolygon": for (j = 0; j < coords.length; j++) { geometryIndex = 0; for (k = 0; k < coords[j].length; k++) { for (l = 0; l < coords[j][k].length - wrapShrink; l++) { if (callback( coords[j][k][l], coordIndex, featureIndex, multiFeatureIndex, geometryIndex ) === false) return false; coordIndex++; } geometryIndex++; } multiFeatureIndex++; } break; case "GeometryCollection": for (j = 0; j < geometry.geometries.length; j++) if (coordEach(geometry.geometries[j], callback) === false) return false; break; default: throw new Error("Unknown Geometry Type"); } } } } function geomEach(geojson, callback) { var i, j, g, geometry, stopG, geometryMaybeCollection, isGeometryCollection, featureProperties, featureBBox, featureId, featureIndex = 0, isFeatureCollection = geojson.type === "FeatureCollection", isFeature = geojson.type === "Feature", stop = isFeatureCollection ? geojson.features.length : 1; for (i = 0; i < stop; i++) { geometryMaybeCollection = isFeatureCollection ? geojson.features[i].geometry : isFeature ? geojson.geometry : geojson; featureProperties = isFeatureCollection ? geojson.features[i].properties : isFeature ? geojson.properties : {}; featureBBox = isFeatureCollection ? geojson.features[i].bbox : isFeature ? geojson.bbox : void 0; featureId = isFeatureCollection ? geojson.features[i].id : isFeature ? geojson.id : void 0; isGeometryCollection = geometryMaybeCollection ? geometryMaybeCollection.type === "GeometryCollection" : false; stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1; for (g = 0; g < stopG; g++) { geometry = isGeometryCollection ? geometryMaybeCollection.geometries[g] : geometryMaybeCollection; if (geometry === null) { if (callback( null, featureIndex, featureProperties, featureBBox, featureId ) === false) return false; continue; } switch (geometry.type) { case "Point": case "LineString": case "MultiPoint": case "Polygon": case "MultiLineString": case "MultiPolygon": { if (callback( geometry, featureIndex, featureProperties, featureBBox, featureId ) === false) return false; break; } case "GeometryCollection": { for (j = 0; j < geometry.geometries.length; j++) { if (callback( geometry.geometries[j], featureIndex, featureProperties, featureBBox, featureId ) === false) return false; } break; } default: throw new Error("Unknown Geometry Type"); } } featureIndex++; } } function geomReduce(geojson, callback, initialValue) { var previousValue = initialValue; geomEach( geojson, function(currentGeometry, featureIndex, featureProperties, featureBBox, featureId) { if (featureIndex === 0 && initialValue === void 0) previousValue = currentGeometry; else previousValue = callback( previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId ); } ); return previousValue; } function bbox(geojson) { var result = [Infinity, Infinity, -Infinity, -Infinity]; coordEach(geojson, function(coord) { if (result[0] > coord[0]) { result[0] = coord[0]; } if (result[1] > coord[1]) { result[1] = coord[1]; } if (result[2] < coord[0]) { result[2] = coord[0]; } if (result[3] < coord[1]) { result[3] = coord[1]; } }); return result; } bbox["default"] = bbox; function getCoord(coord) { if (!coord) { throw new Error("coord is required"); } if (!Array.isArray(coord)) { if (coord.type === "Feature" && coord.geometry !== null && coord.geometry.type === "Point") { return coord.geometry.coordinates; } if (coord.type === "Point") { return coord.coordinates; } } if (Array.isArray(coord) && coord.length >= 2 && !Array.isArray(coord[0]) && !Array.isArray(coord[1])) { return coord; } throw new Error("coord must be GeoJSON Point or an Array of numbers"); } function distance$1(from, to, options) { if (options === void 0) { options = {}; } var coordinates1 = getCoord(from); var coordinates2 = getCoord(to); var dLat = degreesToRadians(coordinates2[1] - coordinates1[1]); var dLon = degreesToRadians(coordinates2[0] - coordinates1[0]); var lat1 = degreesToRadians(coordinates1[1]); var lat2 = degreesToRadians(coordinates2[1]); var a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); return radiansToLength(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), options.units); } function bboxPolygon(bbox2, options) { if (options === void 0) { options = {}; } var west = Number(bbox2[0]); var south = Number(bbox2[1]); var east = Number(bbox2[2]); var north = Number(bbox2[3]); if (bbox2.length === 6) { throw new Error("@turf/bbox-polygon does not support BBox with 6 positions"); } var lowLeft = [west, south]; var topLeft = [west, north]; var topRight = [east, north]; var lowRight = [east, south]; return polygon([[lowLeft, lowRight, topRight, topLeft, lowLeft]], options.properties, { bbox: bbox2, id: options.id }); } function destination(origin, distance2, bearing2, options) { if (options === void 0) { options = {}; } var coordinates1 = getCoord(origin); var longitude1 = degreesToRadians(coordinates1[0]); var latitude1 = degreesToRadians(coordinates1[1]); var bearingRad = degreesToRadians(bearing2); var radians = lengthToRadians(distance2, options.units); var latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) + Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad)); var longitude2 = longitude1 + Math.atan2(Math.sin(bearingRad) * Math.sin(radians) * Math.cos(latitude1), Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2)); var lng = radiansToDegrees(longitude2); var lat = radiansToDegrees(latitude2); return point([lng, lat], options.properties); } function bearing(start, end, options) { if (options === void 0) { options = {}; } if (options.final === true) { return calculateFinalBearing(start, end); } var coordinates1 = getCoord(start); var coordinates2 = getCoord(end); var lon1 = degreesToRadians(coordinates1[0]); var lon2 = degreesToRadians(coordinates2[0]); var lat1 = degreesToRadians(coordinates1[1]); var lat2 = degreesToRadians(coordinates2[1]); var a = Math.sin(lon2 - lon1) * Math.cos(lat2); var b = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); return radiansToDegrees(Math.atan2(a, b)); } function calculateFinalBearing(start, end) { var bear = bearing(end, start); bear = (bear + 180) % 360; return bear; } function midpoint(point1, point2) { var dist = distance$1(point1, point2); var heading = bearing(point1, point2); var midpoint2 = destination(point1, dist / 2, heading); return midpoint2; } function center(geojson, options) { if (options === void 0) { options = {}; } var ext = bbox(geojson); var x = (ext[0] + ext[2]) / 2; var y = (ext[1] + ext[3]) / 2; return point([x, y], options.properties, options); } var RADIUS$1 = 6378137; function area(geojson) { return geomReduce(geojson, function(value, geom) { return value + calculateArea(geom); }, 0); } function calculateArea(geom) { var total = 0; var i; switch (geom.type) { case "Polygon": return polygonArea(geom.coordinates); case "MultiPolygon": for (i = 0; i < geom.coordinates.length; i++) { total += polygonArea(geom.coordinates[i]); } return total; case "Point": case "MultiPoint": case "LineString": case "MultiLineString": return 0; } return 0; } function polygonArea(coords) { var total = 0; if (coords && coords.length > 0) { total += Math.abs(ringArea(coords[0])); for (var i = 1; i < coords.length; i++) { total -= Math.abs(ringArea(coords[i])); } } return total; } function ringArea(coords) { var p1; var p2; var p3; var lowerIndex; var middleIndex; var upperIndex; var i; var total = 0; var coordsLength = coords.length; if (coordsLength > 2) { for (i = 0; i < coordsLength; i++) { if (i === coordsLength - 2) { lowerIndex = coordsLength - 2; middleIndex = coordsLength - 1; upperIndex = 0; } else if (i === coordsLength - 1) { lowerIndex = coordsLength - 1; middleIndex = 0; upperIndex = 1; } else { lowerIndex = i; middleIndex = i + 1; upperIndex = i + 2; } p1 = coords[lowerIndex]; p2 = coords[middleIndex]; p3 = coords[upperIndex]; total += (rad(p3[0]) - rad(p1[0])) * Math.sin(rad(p2[1])); } total = total * RADIUS$1 * RADIUS$1 / 2; } return total; } function rad(num) { return num * Math.PI / 180; } function cartesian3ToLonLat(position) { const cartographic = Cesium.Cartographic.fromCartesian(position); const lon = Cesium.Math.toDegrees(cartographic.longitude); const lat = Cesium.Math.toDegrees(cartographic.latitude); return [lon, lat]; } function getDistanceH(p1, p2) { if (!p1 || !p2) { return 0; } let lngLat1 = cartesian3ToLonLat(p1); let lngLat2 = cartesian3ToLonLat(p2); return distance(lngLat1, lngLat2); } function distance(lngLat1, lngLat2) { let radLat1 = lngLat1[1] * Math.PI / 180; let radLat2 = lngLat2[1] * Math.PI / 180; let a = radLat1 - radLat2; let b = lngLat1[0] * Math.PI / 180 - lngLat2[0] * Math.PI / 180; let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * 6378.137; s = Math.round(s * 1e4) / 10; return s; } function positionHeight(position) { return Cesium.Cartographic.fromCartesian(position).height; } function computeArea(positions) { if (positions.length < 3) return 0; let lngLats = []; positions.forEach((item) => { lngLats.push(cartesian3ToLonLat(item)); }); lngLats.push(lngLats[0]); let polygon$1 = polygon([lngLats]); return area(polygon$1).toFixed(3); } class MeasureArea { constructor(viewer) { this.viewer = viewer; this.positions = []; this.vertexEntities = []; this.tempPositions = []; this.initEvents(); } setBaseHeight(height) { this.height = height; } initEvents() { this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this.MeasureStartEvent = new Cesium.Event(); this.MeasureEndEvent = new Cesium.Event(); } active() { this.registerEvents(); this.isMeasure = true; } deactive() { if (!this.isMeasure) { return; } this.unRegisterEvents(); } unRegisterEvents() { this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); } registerEvents() { this.leftClickEvent(); this.mouseMoveEvent(); this.rightClickEvent(); } leftClickEvent() { this.handler.setInputAction((event) => { this.viewer._element.style.cursor = "default"; let position = this.viewer.scene.pickPosition(event.position); if (!position) { let ellipsoid = this.viewer.scene.globe.ellipsoid; position = this.viewer.scene.camera.pickEllipsoid(position, ellipsoid); } if (!Cesium.defined(position)) { return; } this.positions.push(position); this.height = this.unifiedHeight(this.positions, this.height); if (this.positions.length == 1) { this.createPolygonEntity(); } this.createVertex(); console.log("position", position); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } mouseMoveEvent() { this.handler.setInputAction((e) => { if (!this.isMeasure) { return; } this.viewer._element.style.cursor = "default"; let position = this.viewer.scene.pickPosition(e.endPosition); if (!position) { position = this.viewer.scene.camera.pickEllipsoid(e.startPosition, this.viewer.scene.globe.ellipsoid); } if (!position) { return; } this.handleMoveEvent(position); }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); } handleMoveEvent(position) { if (this.positions.length < 1) { return; } this.height = this.unifiedHeight(this.positions, this.height); this.tempPositions = this.positions.concat(position); if (this.tempPositions.length > 3 && !this.mesureResultEntity) { this.createResultLabel(); } } createResultLabel() { this.mesureResultEntity = this.viewer.entities.add({ position: new Cesium.CallbackProperty((e) => { return this.getCenterPosition(); }, false), type: "MeasureAreaResult", label: { text: new Cesium.CallbackProperty((e) => { return "面积" + computeArea(this.tempPositions) + "平方米"; }, false), scale: 0.5, font: "normal 28px MicroSoft YaHei", distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5e4), scaleByDistance: new Cesium.NearFarScalar(1e3, 1, 3e3, 0.4), verticalOrigin: Cesium.VerticalOrigin.BOTTOM, style: Cesium.LabelStyle.FILL_AND_OUTLINE, pixelOffset: new Cesium.Cartesian2(0, -30), outlineWidth: 9, outlineColor: Cesium.Color.YELLOW, eyeOffset: new Cesium.Cartesian3(0, 0, -10) } }); } getCenterPosition() { let points = []; if (this.tempPositions.length < 3) { return this.tempPositions[0]; } this.tempPositions.forEach((position) => { const point3D = this.cartesian3ToPoint3D(position); points.push([point3D.x, point3D.y]); }); let geo = lineString(points); let bbox$1 = bbox(geo); let bboxPolygon$1 = bboxPolygon(bbox$1); let pointOnFeature = center(bboxPolygon$1); let lonLat = pointOnFeature.geometry.coordinates; return Cesium.Cartesian3.fromDegrees(lonLat[0], lonLat[1], this.height + 0.3); } unifiedHeight(positions, height) { if (!height) { height = this.getPositionHeight(positions[0]); } let point3d; for (let i = 0; i < positions.length; i++) { let element = positions[i]; point3d = this.cartesian3ToPoint3D(element); positions[i] = Cesium.Cartesian3.fromDegrees(point3d.x, point3d.y, height); } return height; } createPolygonEntity() { this.polygonEntity = this.viewer.entities.add({ polygon: { hierarchy: new Cesium.CallbackProperty((e) => { return new Cesium.PolygonHierarchy(this.tempPositions); }, false), material: Cesium.Color.RED.withAlpha(0.4), // perPositionHeight: true, classificationType: Cesium.ClassificationType.CESIUM_3D_TILE }, polyline: { perPositionHeight: true, positions: new Cesium.CallbackProperty((e) => { return this.tempPositions.concat(this.tempPositions[0]); }, false), width: 1, material: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }), depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) } }); } createVertex() { let entity = this.viewer.entities.add({ position: this.positions[this.positions.length - 1], type: "MeasureAreaVertex", point: { depthTest: false, color: Cesium.Color.FUCHSIA, pixelSize: 8, disableDepthTestDistance: 500 } }); entity.depthTest = false; this.vertexEntities.push(entity); } //获取某个点的高度 getPositionHeight(position) { const cartographic = Cesium.Cartographic.fromCartesian(position); return cartographic.height; } cartesian3ToPoint3D(position) { const cartographic = Cesium.Cartographic.fromCartesian(position); const lon = Cesium.Math.toDegrees(cartographic.longitude); const lat = Cesium.Math.toDegrees(cartographic.latitude); return { x: lon, y: lat, z: cartographic.height }; } rightClickEvent() { this.handler.setInputAction((e) => { if (!this.isMeasure || this.positions.length < 3) { this.deactive(); this.clear(); } else { this.tempPositions = [...this.positions]; this.polygonEntity.polyline = { positions: this.positions.concat(this.positions[0]), width: 2, material: Cesium.Color.YELLOW, depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) }; this.polygonEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(this.tempPositions); this.mesureResultEntity.position = this.getCenterPosition(); this.mesureResultEntity.label.text = "总面积" + computeArea(this.positions) + "平方米"; this.measureEnd(); } }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); } measureEnd() { this.deactivate(); this.MeasureEndEvent.raiseEvent(this.measureArea); } deactivate() { if (!this.isMeasure) return; this.unRegisterEvents(); this.viewer._element.style.cursor = "pointer"; this.viewer.enableCursorStyle = true; this.isMeasure = false; this.tempPositions = []; this.positions = []; this.height = void 0; } clear() { this.viewer.entities.remove(this.polygonEntity); this.polygonEntity = void 0; this.vertexEntities.forEach((child) => { this.viewer.entities.remove(child); }); this.vertexEntities = []; this.viewer.entities.remove(this.vertexEntities); this.vertexEntities = []; this.height = void 0; } } class MeasureDistance { constructor(viewer) { this.viewer = viewer; this.positions = []; this.vertexEntities = []; this.tempPositions = []; this.initEvents(); } setBaseHeight(height) { this.height = height; } initEvents() { this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this.MeasureStartEvent = new Cesium.Event(); this.MeasureEndEvent = new Cesium.Event(); } active() { this.registerEvents(); this.isMeasure = true; } deactive() { if (!this.isMeasure) { return; } this.unRegisterEvents(); } unRegisterEvents() { this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); } registerEvents() { this.leftClickEvent(); this.mouseMoveEvent(); this.rightClickEvent(); } leftClickEvent() { this.handler.setInputAction((event) => { this.viewer._element.style.cursor = "default"; if (this.positions.length == 2) { this.deactivate(); return; } let position = this.viewer.scene.pickPosition(event.position); if (!position) { let ellipsoid = this.viewer.scene.globe.ellipsoid; position = this.viewer.scene.camera.pickEllipsoid(position, ellipsoid); } if (!Cesium.defined(position)) { return; } this.positions.push(position); this.height = this.unifiedHeight(this.positions, this.height); if (this.positions.length == 1) { this.createPolygonEntity(); } this.createVertex(); console.log("position", position); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } createPolygonEntity() { this.polygonEntity = this.viewer.entities.add({ polyline: { positions: new Cesium.CallbackProperty((e) => { return this.tempPositions; }, false), width: 1, material: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }), depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) } }); this.polygonEntity.depthTest = false; } mouseMoveEvent() { this.handler.setInputAction((e) => { if (!this.isMeasure) { return; } if (this.positions.length == 2) { this.tempPositions = [...this.positions]; this.polygonEntity.polyline = { positions: this.positions, width: 2, material: Cesium.Color.YELLOW, depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) }; this.mesureResultEntity.position = this.getCenterPosition(); this.mesureResultEntity.label.text = "距离" + getDistanceH(this.positions[0], this.positions[1]) + "米"; this.measureEnd(); return; } this.viewer._element.style.cursor = "default"; let position = this.viewer.scene.pickPosition(e.endPosition); if (!position) { position = this.viewer.scene.camera.pickEllipsoid(e.startPosition, this.viewer.scene.globe.ellipsoid); } if (!position) { return; } this.handleMoveEvent(position); }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); } handleMoveEvent(position) { if (this.positions.length < 1) { return; } this.height = this.unifiedHeight(this.positions, this.height); this.tempPositions = this.positions.concat(position); if (this.tempPositions.length >= 2 && !this.mesureResultEntity) { this.createResultLabel(); } } createResultLabel() { this.mesureResultEntity = this.viewer.entities.add({ position: new Cesium.CallbackProperty((e) => { return this.getCenterPosition(); }, false), type: "MeasureAreaResult", label: { text: new Cesium.CallbackProperty((e) => { return "距离" + getDistanceH(this.tempPositions[0], this.tempPositions[1]) + "米"; }, false), scale: 0.5, font: "normal 28px MicroSoft YaHei", distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5e4), scaleByDistance: new Cesium.NearFarScalar(1e3, 1, 3e3, 0.4), verticalOrigin: Cesium.VerticalOrigin.BOTTOM, style: Cesium.LabelStyle.FILL_AND_OUTLINE, pixelOffset: new Cesium.Cartesian2(0, -30), outlineWidth: 9, outlineColor: Cesium.Color.YELLOW, eyeOffset: new Cesium.Cartesian3(0, 0, -10) } }); } getCenterPosition() { let points = []; if (this.tempPositions.length < 2) { return this.tempPositions[0]; } this.tempPositions.forEach((position) => { const point3D = this.cartesian3ToPoint3D(position); points.push([point3D.x, point3D.y]); }); let point1 = point(points[0]); let point2 = point(points[1]); let midpoint$1 = midpoint(point1, point2); return Cesium.Cartesian3.fromDegrees(midpoint$1.geometry.coordinates[0], midpoint$1.geometry.coordinates[1], this.height + 0.3); } unifiedHeight(positions, height) { if (!height) { height = this.getPositionHeight(positions[0]); } let point3d; for (let i = 0; i < positions.length; i++) { let element = positions[i]; point3d = this.cartesian3ToPoint3D(element); positions[i] = Cesium.Cartesian3.fromDegrees(point3d.x, point3d.y, height); } return height; } createVertex() { let entity = this.viewer.entities.add({ position: this.positions[this.positions.length - 1], type: "MeasureAreaVertex", point: { color: Cesium.Color.FUCHSIA, pixelSize: 8, disableDepthTestDistance: 500 } }); entity.depthTest = false; this.vertexEntities.push(entity); } //获取某个点的高度 getPositionHeight(position) { const cartographic = Cesium.Cartographic.fromCartesian(position); return cartographic.height; } cartesian3ToPoint3D(position) { const cartographic = Cesium.Cartographic.fromCartesian(position); const lon = Cesium.Math.toDegrees(cartographic.longitude); const lat = Cesium.Math.toDegrees(cartographic.latitude); return { x: lon, y: lat, z: cartographic.height }; } rightClickEvent() { this.handler.setInputAction((e) => { if (!this.isMeasure || this.positions.length < 2) { this.deactive(); this.clear(); } else { this.tempPositions = [...this.positions]; this.polygonEntity.polyline = { positions: this.positions, width: 2, material: Cesium.Color.YELLOW, depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) }; this.mesureResultEntity.position = this.getCenterPosition(); this.mesureResultEntity.label.text = "距离" + getDistanceH(this.positions[0], this.positions[1]) + "米"; this.measureEnd(); } }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); } measureEnd() { this.deactivate(); this.MeasureEndEvent.raiseEvent(this.measureArea); } deactivate() { if (!this.isMeasure) return; this.unRegisterEvents(); this.viewer._element.style.cursor = "pointer"; this.viewer.enableCursorStyle = true; this.isMeasure = false; this.tempPositions = []; this.positions = []; this.height = void 0; } clear() { this.viewer.entities.remove(this.mesureResultEntity); this.mesureResultEntity = void 0; this.viewer.entities.remove(this.polygonEntity); this.polygonEntity = void 0; this.vertexEntities.forEach((child) => { this.viewer.entities.remove(child); }); this.vertexEntities = []; this.height = void 0; } } class MeasureHeight { constructor(viewer) { this.viewer = viewer; this.positions = []; this.vertexEntities = []; this.tempPositions = []; this.initEvents(); } initEvents() { this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas); this.MeasureStartEvent = new Cesium.Event(); this.MeasureEndEvent = new Cesium.Event(); } active() { this.registerEvents(); this.isMeasure = true; } deactive() { if (!this.isMeasure) { return; } this.unRegisterEvents(); } unRegisterEvents() { this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); } registerEvents() { this.leftClickEvent(); this.mouseMoveEvent(); this.rightClickEvent(); } leftClickEvent() { this.handler.setInputAction((event) => { this.viewer._element.style.cursor = "default"; if (this.positions.length == 2) { this.deactivate(); return; } let position = this.viewer.scene.pickPosition(event.position); if (!position) { let ellipsoid = this.viewer.scene.globe.ellipsoid; position = this.viewer.scene.camera.pickEllipsoid(position, ellipsoid); } if (!Cesium.defined(position)) { return; } this.positions.push(position); if (this.positions.length == 1) { this.createPolygonEntity(); } this.createVertex(); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); } createPolygonEntity() { this.polygonEntity = this.viewer.entities.add({ polyline: { perPositionHeight: true, depthTest: false, positions: new Cesium.CallbackProperty((e) => { return this.tempPositions; }, false), width: 1, material: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }), depthFailMaterial: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.YELLOW }) } }); this.polygo