UNPKG

@vtx/cs-map

Version:

React components for Vortex

585 lines (558 loc) 24.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Measure = void 0; var _cesium = require("cesium"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * 测量 */ var Measure = exports.Measure = /*#__PURE__*/function () { function Measure(viewer) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, Measure); this.viewer = viewer; this.entityCollection = []; this.viewer.scene.globe.depthTestAgainstTerrain = true; this.startDraw = false; this.startTipEntity = null; this.labelEntityCollection = []; this.totalEntityCollection = []; this.clearLast = true; this.labelVisible = true; this.TextEnum = options.TextEnum; } // var this.entityCollection = []; return _createClass(Measure, [{ key: "getCollection", value: function getCollection() { return this.entityCollection; } }, { key: "clear", value: function clear() { var _this = this; this.entityCollection.forEach(function (entity) { _this.viewer.entities.remove(entity); }); this.labelEntityCollection.forEach(function (entity) { _this.viewer.entities.remove(entity); }); this.totalEntityCollection.forEach(function (entity) { _this.viewer.entities.remove(entity); }); this.entityCollection = []; this.labelEntityCollection = []; this.totalEntityCollection = []; } /** * 清除 */ }, { key: "destroy", value: function destroy(callback) { if (this.startTipEntity) { this.viewer.entities.remove(this.startTipEntity); } this.entityCollection; this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK); this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE); this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.RIGHT_CLICK); this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK); this.viewer.cancelBubble = false; this.startDraw = false; this.clear(); if (callback) { callback(); } } }, { key: "visibleLabelEntity", value: function visibleLabelEntity(visible) { this.labelEntityCollection.forEach(function (entity) { entity.show = visible; }); this.labelVisible = visible; } }, { key: "removeStartTip", value: function removeStartTip() { if (this.startTipEntity) { this.viewer.entities.remove(this.startTipEntity); } } }, { key: "startDrawTip", value: function startDrawTip(movePosition) { if (!this.startDraw) { this.removeStartTip(); // 计算距离 var lengthText = this.TextEnum.messureStartTipText; this.startTipEntity = this.addLabel(movePosition, lengthText); } } /** * 测距 */ }, { key: "measurePolyLine", value: function measurePolyLine(callback) { var _this2 = this; var positions = []; var labelEntity = null; // 标签实体 var totalLabelEntity = null; var positionsList = []; this.viewer.cancelBubble = true; this.startDrawTip(); this.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) { var movePosition = _this2.viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点 // var movePosition = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(moveEvent.endPosition), this.viewer.scene); if (!movePosition) { return false; } _this2.startDrawTip(movePosition); if (_this2.startDraw) { if (positions.length == 2) { positions.pop(); positions.push(movePosition); // 绘制label if (labelEntity) { _this2.viewer.entities.remove(labelEntity); _this2.labelEntityCollection.splice(_this2.labelEntityCollection.indexOf(labelEntity), 1); } if (totalLabelEntity) { _this2.viewer.entities.remove(totalLabelEntity); _this2.totalEntityCollection.splice(_this2.totalEntityCollection.indexOf(totalLabelEntity), 1); } // 计算中点 var centerPoint = _cesium.Cartesian3.midpoint(positions[0], positions[1], new _cesium.Cartesian3()); var currentLength = _this2.getLengthText(positions[0], positions[1]); var totalLength = Number(currentLength); positionsList.forEach(function (positions) { totalLength += Number(_this2.getLengthText(positions[0], positions[1])); }); // 计算距离 var lengthText = "".concat(_this2.TextEnum.distanceText, "\uFF1A") + currentLength + 'km'; labelEntity = _this2.addLabel(centerPoint, lengthText); labelEntity.show = _this2.labelVisible; totalLabelEntity = _this2.addLabel(movePosition, "".concat(_this2.TextEnum.totalDistanceText, "\uFF1A") + totalLength.toFixed(2) + 'km'); _this2.labelEntityCollection.push(labelEntity); _this2.totalEntityCollection.push(totalLabelEntity); } else { positions.push(movePosition); // 绘制线 _this2.addLine(positions); } } }, _cesium.ScreenSpaceEventType.MOUSE_MOVE); // 注册鼠标左击事件 this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { var cartesian = _this2.viewer.scene.pickPosition(clickEvent.position); // 坐标 // var cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(clickEvent.position), this.viewer.scene); // 存储第一个点 if (!cartesian) { return false; } if (!_this2.startDraw) { if (_this2.clearLast) { _this2.clear(); } _this2.startDraw = true; } _this2.removeStartTip(); if (positions.length == 0) { positions.push(cartesian.clone()); _this2.addPoint(cartesian); // 注册鼠标移动事件 } else { // 存储第二个点 positions.pop(); positions.push(cartesian); _this2.addPoint(cartesian); _this2.addLine(_toConsumableArray(positions)); positionsList.push(_toConsumableArray(positions)); positions = [cartesian]; _this2.viewer.cancelBubble = false; labelEntity = null; totalLabelEntity = null; if (callback) { callback(); } } }, _cesium.ScreenSpaceEventType.LEFT_CLICK); this.viewer.screenSpaceEventHandler.setInputAction(function () { _this2.startDraw = false; positions = []; positionsList = []; labelEntity = null; totalLabelEntity = null; if (callback) { callback(); } }, _cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK); } /** * 测面积 */ }, { key: "measurePolygon", value: function measurePolygon(callback) { var _this3 = this; var positions = []; var clickStatus = false; var labelEntityCollection = []; var totalLabelEntity = null; this.viewer.scene.globe.depthTestAgainstTerrain = false; this.viewer.cancelBubble = true; this.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) { // var movePosition = this.viewer.scene.pickPosition(moveEvent.endPosition); var movePosition = _this3.viewer.scene.globe.pick(_this3.viewer.camera.getPickRay(moveEvent.endPosition), _this3.viewer.scene); // console.log(movePosition); if (!movePosition) { return false; } if (!_this3.startDraw) { return _this3.startDrawTip(movePosition); } if (positions.length == 1) { positions.push(movePosition); _this3.addLine(positions); } else { if (clickStatus) { positions.push(movePosition); } else { positions.pop(); positions.push(movePosition); } } if (positions.length >= 3) { // 绘制label if (totalLabelEntity) { _this3.viewer.entities.remove(totalLabelEntity); _this3.totalEntityCollection.splice(_this3.totalEntityCollection.indexOf(totalLabelEntity), 1); } var text = "".concat(_this3.TextEnum.areaText, "\uFF1A") + _this3.getArea(positions) + 'km²'; var centerPoint = _this3.getCenterOfGravityPoint(positions); totalLabelEntity = _this3.addLabel(centerPoint, text); _this3.totalEntityCollection.push(totalLabelEntity); } if (positions.length >= 2) { var _this3$labelEntityCol; labelEntityCollection.forEach(function (entity) { _this3.viewer.entities.remove(entity); _this3.labelEntityCollection.splice(_this3.labelEntityCollection.indexOf(entity), 1); }); positions.forEach(function (position, index) { if (index === positions.length - 1) { return; } var prevPosition = positions[index + 1]; var centerPoint = _cesium.Cartesian3.midpoint(position, prevPosition, new _cesium.Cartesian3()); var currentLength = _this3.getLengthText(position, prevPosition); // 计算距离 var lengthText = "".concat(_this3.TextEnum.distanceText, "\uFF1A") + currentLength + 'km'; var labelEntity = _this3.addLabel(centerPoint, lengthText); labelEntity.show = _this3.labelVisible; labelEntityCollection.push(labelEntity); }); (_this3$labelEntityCol = _this3.labelEntityCollection).push.apply(_this3$labelEntityCol, _toConsumableArray(labelEntityCollection)); } clickStatus = false; }, _cesium.ScreenSpaceEventType.MOUSE_MOVE); this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { clickStatus = true; if (!_this3.startDraw) { _this3.startDraw = true; if (_this3.clearLast) { _this3.clear(); } } // var cartesian = this.viewer.scene.pickPosition(clickEvent.position); var cartesian = _this3.viewer.scene.globe.pick(_this3.viewer.camera.getPickRay(clickEvent.position), _this3.viewer.scene); // console.log(cartesian); if (!cartesian) { return false; } if (positions.length == 0) { positions.push(cartesian.clone()); //鼠标左击 添加第1个点 _this3.addPoint(cartesian); } else if (positions.length == 2) { if (!cartesian) { return false; } positions.pop(); positions.push(cartesian.clone()); // 鼠标左击 添加第2个点 _this3.addPoint(cartesian); _this3.addPolyGon(positions); // 右击结束 _this3.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { // var clickPosition = this.viewer.scene.pickPosition(clickEvent.position); var clickPosition = _this3.viewer.scene.globe.pick(_this3.viewer.camera.getPickRay(clickEvent.position), _this3.viewer.scene); // console.log(clickPosition); if (!clickPosition) { return false; } positions.pop(); positions.push(clickPosition); positions.push(positions[0]); // 闭合 _this3.addPoint(clickPosition); positions = []; _this3.viewer.cancelBubble = false; _this3.startDraw = false; totalLabelEntity = null; labelEntityCollection = []; if (callback) { callback(); } }, _cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK); } else if (positions.length >= 3) { if (!cartesian) { return false; } positions.pop(); positions.push(cartesian.clone()); // 鼠标左击 添加第3个点 _this3.addPoint(cartesian); } }, _cesium.ScreenSpaceEventType.LEFT_CLICK); } /** * 测高 */ }, { key: "measureHeight", value: function measureHeight(callback) { var _this4 = this; var positions = []; var labelEntity_1 = null; // 标签实体 var labelEntity_2 = null; // 标签实体 var labelEntity_3 = null; // 标签实体 // 注册鼠标左击事件 this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { var cartesian = _this4.viewer.scene.pickPosition(clickEvent.position); // 坐标 //var cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(clickEvent.position), this.viewer.scene); // 存储第一个点 if (positions.length == 0) { if (!cartesian) { return false; } positions.push(cartesian.clone()); _this4.addPoint(cartesian); // 注册鼠标移动事件 _this4.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) { var movePosition = _this4.viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点 //var movePosition = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(moveEvent.endPosition), this.viewer.scene); if (!movePosition) { return false; } if (positions.length >= 2) { positions.pop(); positions.pop(); positions.pop(); var cartographic = _cesium.Cartographic.fromCartesian(movePosition); var height = _cesium.Cartographic.fromCartesian(positions[0]).height; var verticalPoint = _cesium.Cartesian3.fromDegrees(Math.toDegrees(cartographic.longitude), Math.toDegrees(cartographic.latitude), height); positions.push(verticalPoint); positions.push(movePosition); positions.push(positions[0]); // 绘制label if (labelEntity_1) { _this4.viewer.entities.remove(labelEntity_1); _this4.entityCollection.splice(_this4.entityCollection.indexOf(labelEntity_1), 1); _this4.viewer.entities.remove(labelEntity_2); _this4.entityCollection.splice(_this4.entityCollection.indexOf(labelEntity_2), 1); _this4.viewer.entities.remove(labelEntity_3); _this4.entityCollection.splice(_this4.entityCollection.indexOf(labelEntity_3), 1); } // 计算中点 var centerPoint_1 = _cesium.Cartesian3.midpoint(positions[0], positions[1], new _cesium.Cartesian3()); // 计算距离 var lengthText_1 = "".concat(_this4.TextEnum.horizontalDistanceText, "\uFF1A") + _this4.getLengthText(positions[0], positions[1]); labelEntity_1 = _this4.addLabel(centerPoint_1, lengthText_1); _this4.entityCollection.push(labelEntity_1); // 计算中点 var centerPoint_2 = _cesium.Cartesian3.midpoint(positions[1], positions[2], new _cesium.Cartesian3()); // 计算距离 var lengthText_2 = "".concat(_this4.TextEnum.verticalDistanceText, "\uFF1A") + _this4.getLengthText(positions[1], positions[2]); labelEntity_2 = _this4.addLabel(centerPoint_2, lengthText_2); _this4.entityCollection.push(labelEntity_2); // 计算中点 var centerPoint_3 = _cesium.Cartesian3.midpoint(positions[2], positions[3], new _cesium.Cartesian3()); // 计算距离 var lengthText_3 = "".concat(_this4.TextEnum.straightLineDistanceText, "\uFF1A") + _this4.getLengthText(positions[2], positions[3]); labelEntity_3 = _this4.addLabel(centerPoint_3, lengthText_3); _this4.entityCollection.push(labelEntity_3); } else { var _verticalPoint = new _cesium.Cartesian3(movePosition.x, movePosition.y, positions[0].z); positions.push(_verticalPoint); positions.push(movePosition); positions.push(positions[0]); // 绘制线 _this4.addLine(positions); } }, _cesium.ScreenSpaceEventType.MOUSE_MOVE); } else { // 存储第二个点 positions.pop(); positions.pop(); positions.pop(); var cartographic = _cesium.Cartographic.fromCartesian(cartesian); var height = _cesium.Cartographic.fromCartesian(positions[0]).height; var verticalPoint = _cesium.Cartesian3.fromDegrees(Math.toDegrees(cartographic.longitude), Math.toDegrees(cartographic.latitude), height); positions.push(verticalPoint); positions.push(cartesian); positions.push(positions[0]); _this4.addPoint(cartesian); _this4.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK); _this4.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE); if (callback) { callback(); } } }, _cesium.ScreenSpaceEventType.LEFT_CLICK); } /** * 添加点 * @param position */ }, { key: "addPoint", value: function addPoint(position) { this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({ cancelPopup: true, position: position, point: { color: _cesium.Color.BLUE, pixelSize: 5, heightReference: _cesium.HeightReference.NONE } }))); } /** * 添加线 * @param positions */ }, { key: "addLine", value: function addLine(positions) { var dynamicPositions = new _cesium.CallbackProperty(function () { return positions; }, false); this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({ cancelPopup: true, polyline: { positions: dynamicPositions, width: 2, arcType: _cesium.ArcType.RHUMB, clampToGround: true, material: _cesium.Color.RED, //获取或设置折线的表面外观 heightReference: _cesium.HeightReference.RELATIVE_TO_GROUND } }))); } /** * 添加面 * @param positions */ }, { key: "addPolyGon", value: function addPolyGon(positions) { var dynamicPositions = new _cesium.CallbackProperty(function () { return new _cesium.PolygonHierarchy(positions); }, false); this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({ cancelPopup: true, polygon: { hierarchy: dynamicPositions, material: _cesium.Color.RED.withAlpha(0.6), classificationType: _cesium.ClassificationType.BOTH // 贴地表和贴模型,如果设置了,这不能使用挤出高度 } }))); } /** * 添加标签 * @param position * @param text */ }, { key: "addLabel", value: function addLabel(centerPoint, text) { return this.viewer.entities.add(new _cesium.Entity({ cancelPopup: true, position: centerPoint, label: { text: text, font: '14px sans-serif', style: _cesium.LabelStyle.FILL_AND_OUTLINE, //FILL FILL_AND_OUTLINE OUTLINE fillColor: _cesium.Color.YELLOW, showBackground: true, //指定标签后面背景的可见性 backgroundColor: new _cesium.Color(0.165, 0.165, 0.165, 0.8), // 背景颜色 backgroundPadding: new _cesium.Cartesian2(6, 6), //指定以像素为单位的水平和垂直背景填充padding pixelOffset: new _cesium.Cartesian2(0, -25), disableDepthTestDistance: Number.POSITIVE_INFINITY } })); } /** * 计算两点距离 * @param firstPoint * @param secondPoint */ }, { key: "getLengthText", value: function getLengthText(firstPoint, secondPoint) { // 计算距离 var length = _cesium.Cartesian3.distance(firstPoint, secondPoint); return (length / 1000).toFixed(2); } }, { key: "getArea", value: function getArea(points) { var _this5 = this; var ps = points.map(function (point) { var cartographic = _cesium.Ellipsoid.WGS84.cartesianToCartographic(point); var height = _this5.viewer.scene.globe.getHeight(cartographic); return _cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height); }); var s = 0; for (var i = 0; i < ps.length; i++) { var p1 = ps[i]; var p2 = void 0; if (i < ps.length - 1) p2 = ps[i + 1];else p2 = ps[0]; s += p1.x * p2.y - p2.x * p1.y; } return Math.abs((s / 1000000.0).toFixed(4)); } /** * 计算多边形的中心(简单的处理) * @param mPoints * @returns {*[]} */ }, { key: "getCenterOfGravityPoint", value: function getCenterOfGravityPoint(mPoints) { var centerPoint = mPoints[0]; for (var i = 1; i < mPoints.length; i++) { centerPoint = _cesium.Cartesian3.midpoint(centerPoint, mPoints[i], new _cesium.Cartesian3()); } return centerPoint; } }]); }(); //# sourceMappingURL=Measure.js.map