/** * @name: index * @author: Administrator * @date: 2022-07-21 15:22 * @description:index * @update: 2022-07-21 15:22 */ import Measure from "../index"; class MeasureTriangle extends Measure { /** * @constructor * @param sdk * @description 三角测量 * */ constructor(sdk) { super(sdk); } cal_center(positions) { let p1 = this.cartesian3Towgs84(positions[0], this.viewer) let p2 = this.cartesian3Towgs84(positions[1], this.viewer) let center = this.computeCenter([p1, p2]); return Cesium.Cartesian3.fromDegrees(center.lng, center.lat, (p1.alt + p2.alt) / 2) } cal_distance(positions) { let p1 = this.cartesian3Towgs84(positions[0], this.viewer) let p2 = this.cartesian3Towgs84(positions[1], this.viewer) let dis = this.computeDistance2([p1, p2]) p1.alt = p1.alt.toFixed(2) p2.alt = p2.alt.toFixed(2) if (p1.alt === p2.alt) {//水平边 return dis } else if (Number(dis) === 0.00) {//竖直边 return Math.abs(p1.alt - p2.alt).toFixed(2) } else {//斜边 return Math.sqrt(dis * dis + Math.pow(Math.abs(p1.alt - p2.alt).toFixed(2), 2)).toFixed(2) } } createPolyline(id) { let obj = this.id_map.get(id) this.viewer.entities.add(new Cesium.Entity({ id, position: new Cesium.CallbackProperty(() => { if (obj.positions.length === 2) return this.cal_center(obj.positions) else return Cesium.Cartesian3() }, false), label: { text: new Cesium.CallbackProperty(() => { if (obj.positions.length === 2) return this.cal_distance(obj.positions) + "米" else return "0米" }, false), scale: 1, fillColor: Cesium.Color.RED, font: 'normal 20px MicroSoft YaHei', verticalOrigin: Cesium.VerticalOrigin.BOTTOM, style: Cesium.LabelStyle.FILL_AND_OUTLINE, pixelOffset: new Cesium.Cartesian2(0, -10), disableDepthTestDistance: Number.POSITIVE_INFINITY, }, polyline: { positions: new Cesium.CallbackProperty(() => { return obj.positions }, false), width: 2, material: Cesium.Color.YELLOW, zIndex: 99999999 } })) this.ids.push(id) } create_angle_label(positions1, positions2, id, type) { let entity = new Cesium.Entity({ id, position: new Cesium.CallbackProperty(() => { if (positions1.length === 2) return this.cal_point(positions1, positions2) else return Cesium.Cartesian3() }), label: { text: new Cesium.CallbackProperty(() => { if (positions1.length === 2) return this.cal_angle(positions1, positions2, type) + "°" else return "0°" }, false), scale: 1, fillColor: Cesium.Color.RED, font: 'normal 20px MicroSoft YaHei', verticalOrigin: Cesium.VerticalOrigin.BOTTOM, style: Cesium.LabelStyle.FILL_AND_OUTLINE, pixelOffset: new Cesium.Cartesian2(15, -10), } }) this.viewer.entities.add(entity) } cal_point(positions1, positions2) { for (let i = 0; i < positions1.length; i++) { for (let j = 0; j < positions2.length; j++) { if (positions1[i].x === positions2[j].x && positions1[i].y === positions2[j].y && positions1[i].z === positions2[j].z ) { return positions1[i] } } } } cal_angle(id1, id2, type) { if (type === 1) {//水平&竖直 return 90 } else if (type === 2 || type === 3) {//水平&斜边 竖直&斜边 let positions1 = this.id_map.get(id1).positions let positions2 = this.id_map.get(id2).positions let p1 = this.cartesian3Towgs84(positions1[0], this.viewer) let p2 = this.cartesian3Towgs84(positions1[1], this.viewer) let shuiping = this.computeDistance2([p2, p1]) let p3 = this.cartesian3Towgs84(positions2[0], this.viewer) let p4 = this.cartesian3Towgs84(positions2[1], this.viewer) let d = this.computeDistance2([p3, p4]) let h = Math.abs(p3.alt - p4.alt) let x = Math.sqrt(Math.pow(h, 2) + Math.pow(d, 2)) if (shuiping == 0.00) { shuiping = Math.abs(p2.alt - p1.alt) } return (Math.acos(shuiping / x) * 180 / Math.PI).toFixed(2) } } /** * 开始测量 */ start() { if (!YJ.Measure.GetMeasureStatus()) { super.start() this.positions = [] this.cachePositions = [] let shuiping_line_id = this.randomString();//水平线 let shuizhi_line_id = this.randomString();//竖直边 let xiebian_line_id = this.randomString();//斜边 let angle1 = this.randomString();//角度1 let angle2 = this.randomString();//角度2 let angle3 = this.randomString();//角度3 let xiebian_line_positions = [];//斜边 this.id_map = new Map() let first_point = {} this.id_map.set(xiebian_line_id, {positions: []}) this.id_map.set(shuiping_line_id, {positions: []}) this.id_map.set(shuizhi_line_id, {positions: []}) let leftEvent = (movement, car) => { xiebian_line_positions.push(car) if (this.ids.length === 0) {//创建三角形 first_point = this.cartesian3Towgs84(car, this.viewer) this.createPolyline(shuiping_line_id,) this.createPolyline(shuizhi_line_id,) this.createPolyline(xiebian_line_id,) // this.cal_angle(shuiping_line_id, shuizhi_line_id, 1) // this.cal_angle(shuiping_line_id, xiebian_line_id, 2) // this.cal_angle(shuizhi_line_id, xiebian_line_id, 3) // this.ids.push(shuiping_line_id) // this.ids.push(shuizhi_line_id) // this.ids.push(xiebian_line_id) } this.ids.push(this.create_point(car)) this.tip.setPosition(car, movement.position.x, movement.position.y) // // 隐藏斜边文字 // let xiebian = this.id_map.get(xiebian_line_id) // let xbEntity = this.viewer.entities.getById(xiebian_line_id) // if(xbEntity) { // xbEntity.label.show = false // } if (xiebian_line_positions.length) { // xiebian.positions = xiebian_line_positions.concat(car) let p = this.cartesian3Towgs84(car, this.viewer) let shuzhi = this.id_map.get(shuizhi_line_id) let shuiping = this.id_map.get(shuiping_line_id) if (p.alt < first_point.alt) { shuzhi.positions[0] = car shuzhi.positions[1] = Cesium.Cartesian3.fromDegrees(p.lng, p.lat, first_point.alt) shuiping.positions[0] = Cesium.Cartesian3.fromDegrees(p.lng, p.lat, first_point.alt) shuiping.positions[1] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, first_point.alt) } else { shuzhi.positions[0] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, p.alt) shuzhi.positions[1] = car shuiping.positions[0] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, p.alt) shuiping.positions[1] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, first_point.alt) } // shuizhi.positions = shuizhi_positions } if (xiebian_line_positions.length === 2) { this.end() } } this.event.mouse_left(leftEvent) this.event.mouse_move((movement, car) => { this.tip.setPosition(car, movement.endPosition.x, movement.endPosition.y) let xiebian = this.id_map.get(xiebian_line_id) if (xiebian_line_positions.length) { xiebian.positions = xiebian_line_positions.concat(car) let p = this.cartesian3Towgs84(car, this.viewer) let shuzhi = this.id_map.get(shuizhi_line_id) let shuiping = this.id_map.get(shuiping_line_id) if (p.alt < first_point.alt) { shuzhi.positions[0] = car shuzhi.positions[1] = Cesium.Cartesian3.fromDegrees(p.lng, p.lat, first_point.alt) shuiping.positions[0] = Cesium.Cartesian3.fromDegrees(p.lng, p.lat, first_point.alt) shuiping.positions[1] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, first_point.alt) } else { shuzhi.positions[0] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, p.alt) shuzhi.positions[1] = car shuiping.positions[0] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, p.alt) shuiping.positions[1] = Cesium.Cartesian3.fromDegrees(first_point.lng, first_point.lat, first_point.alt) } // shuizhi.positions = shuizhi_positions } }) this.event.mouse_right((movement, car) => { this.end() }) this.event.gesture_pinck_start((movement, cartesian) => { let startTime = new Date() let pos = { position: { x: (movement.position1.x + movement.position2.x) / 2, y: (movement.position1.y + movement.position2.y) / 2 } } this.event.gesture_pinck_end(() => { let endTime = new Date() if (endTime - startTime >= 500) { // 长按取消 this.end() } else { leftEvent(pos, cartesian) } }) }) } } /** * 结束测量 */ end() { super.end(); } /** * 清除测量 */ destroy() { super.destroy() } } export default MeasureTriangle