import Draw from './draw' import MouseTip from '../MouseTip' import MouseEvent from '../Event' export default class DrawElliptic extends Draw { constructor(sdk, options = {}) { super(sdk, options) } start(cb) { if (YJ.Measure.GetMeasureStatus()) { cb('上一次测量未结束') } else { super.start() let into this.entity_ids = [] YJ.Measure.SetMeasureStatus(true) this.tip = new MouseTip('左键开始,右键取消', this.sdk) this.event = new MouseEvent(this.sdk) let clickNum = 0 this.elliptic_id = this.randomString() //圆id let radius_points = [] let cache_points = [] let cache_84_position = [] let radius = 1 //默认半径 let positions = [] let center let semiMinorAxis = 0 let semiMajorAxis = 0 let endpoint = null let distanceAB = 0 let distanceAC = 0 let distanceBC = 0 let bearing = 0 this.event.mouse_left((movement, cartesian) => { if(into === '2D') { return } into = '3D' this.tip.set_text('再次左键,完成绘制;右键取消') clickNum++ this.points_ids.push(this.create_point(cartesian)) cache_points.push(cartesian) if (clickNum === 1) { cache_points = [cartesian, cartesian, cartesian] let pos84 = this.cartesian3Towgs84(cartesian, this.viewer) center = pos84 cache_84_position = [pos84, pos84, pos84] calculateElliptic() createEllipticPolygon() } if (clickNum === 2) { cache_points[1] = cartesian cache_points[2] = cartesian let pos84 = this.cartesian3Towgs84(cartesian, this.viewer) cache_84_position[1] = pos84 cache_84_position[2] = pos84 } if (clickNum >= 3) { this.end() cb(null, { center, bearing, semiMajorAxis, semiMinorAxis }) } // if (clickNum === 2) { // radius_points = cache_points.concat(cartesian) // endpoint = this.cartesian3Towgs84(cartesian, this.viewer) // radius = this.computeDistance([center, endpoint]) // positions = this.createCircle(center, radius) // this.end() // cb(null, { center, radius: Number(radius) }) // } }) this.event.mouse_right((movement, cartesian) => { if(into === '2D') { return } this.end() cb(false) }) this.event.mouse_move((movement, cartesian) => { if(into === '2D') { return } this.tip.setPosition( cartesian, movement.endPosition.x, movement.endPosition.y ) cache_points[clickNum] = cartesian cache_84_position[clickNum] = this.cartesian3Towgs84(cartesian, this.viewer) if (clickNum !== 0) { calculateElliptic() } }) if (!this._is2D && this._sdk2D) { this.event2D = new MouseEvent(this._sdk2D) this.event2D.mouse_left((movement, cartesian) => { if(into === '3D') { return } into = '2D' this.tip.set_text('再次左键,完成绘制;右键取消') clickNum++ this.points_ids.push(this.create_point(cartesian, this._sdk2D.viewer)) cache_points.push(cartesian) if (clickNum === 1) { cache_points = [cartesian, cartesian, cartesian] let pos84 = this.cartesian3Towgs84(cartesian, this.viewer) center = pos84 cache_84_position = [pos84, pos84, pos84] calculateElliptic() createEllipticPolygon(this._sdk2D.viewer) } if (clickNum === 2) { cache_points[1] = cartesian cache_points[2] = cartesian let pos84 = this.cartesian3Towgs84(cartesian, this.viewer) cache_84_position[1] = pos84 cache_84_position[2] = pos84 } if (clickNum >= 3) { this.end() cb(null, { center, bearing, semiMajorAxis, semiMinorAxis }) } // if (clickNum === 2) { // radius_points = cache_points.concat(cartesian) // endpoint = this.cartesian3Towgs84(cartesian, this.viewer) // radius = this.computeDistance([center, endpoint]) // positions = this.createCircle(center, radius) // this.end() // cb(null, { center, radius: Number(radius) }) // } }) this.event2D.mouse_right((movement, cartesian) => { if(into === '3D') { return } this.end() cb(false) }) this.event2D.mouse_move((movement, cartesian) => { if(into === '3D') { return } this.tip.setPosition( cartesian, movement.endPosition.x + this.viewer.canvas.width, movement.endPosition.y ) cache_points[clickNum] = cartesian cache_84_position[clickNum] = this.cartesian3Towgs84(cartesian, this.viewer) if (clickNum !== 0) { calculateElliptic() } }) } let that = this function calculateElliptic() { let pointA = Cesium.Cartesian3.fromDegrees(cache_84_position[0].lng, cache_84_position[0].lat); let pointB = Cesium.Cartesian3.fromDegrees(cache_84_position[1].lng, cache_84_position[1].lat); let pointC = Cesium.Cartesian3.fromDegrees(cache_84_position[2].lng, cache_84_position[2].lat); if (clickNum === 1) { distanceAB = Cesium.Cartesian3.distance(pointA, pointB); semiMajorAxis = distanceAB semiMinorAxis = semiMajorAxis / 2; let start = { x: center.lng, y: center.lat } let end = { x: cache_84_position[1].lng, y: cache_84_position[1].lat } let rad = Math.PI / 180, lat1 = start.y * rad, lat2 = end.y * rad, lon1 = start.x * rad, lon2 = end.x * rad; const a = Math.sin(lon2 - lon1) * Math.cos(lat2); const b = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); const radians = Math.atan2(a, b) const degrees = radians % (2 * Math.PI); bearing = 450 - ((degrees * 180) / Math.PI < 0 ? 360 + (degrees * 180) / Math.PI : (degrees * 180) / Math.PI); } if (clickNum === 2) { distanceAC = Cesium.Cartesian3.distance(pointA, pointC); distanceBC = Cesium.Cartesian3.distance(pointB, pointC); let point1 = turf.point([cache_84_position[0].lng, cache_84_position[0].lat]); let point2 = turf.point([cache_84_position[1].lng, cache_84_position[1].lat]); let point3 = turf.point([cache_84_position[2].lng, cache_84_position[2].lat]); const bearing1 = turf.rhumbBearing(point1, point2); const bearing2 = turf.rhumbBearing(point2, point3); const angleDiff = Math.abs(bearing1 - bearing2); let finalAngle = angleDiff > 180 ? 360 - angleDiff : angleDiff; finalAngle = 180 - finalAngle semiMinorAxis = distanceBC * Math.sin(Cesium.Math.toRadians(finalAngle)); } } function createEllipticPolygon(viewer = that.viewer) { viewer.entities.add( new Cesium.Entity({ id: that.elliptic_id, position: Cesium.Cartesian3.fromDegrees(center.lng, center.lat), ellipse: { semiMinorAxis: new Cesium.CallbackProperty((e) => { return semiMinorAxis }, false), semiMajorAxis: new Cesium.CallbackProperty((e) => { return semiMajorAxis }, false), granularity: Cesium.Math.toRadians(0.1), rotation: new Cesium.CallbackProperty((e) => { return Cesium.Math.toRadians(bearing) }, false), material: Cesium.Color.fromCssColorString(that.color), zIndex: 99999999 } }) ) } } } end() { this.remove_entity(this.elliptic_id) this.points_ids.forEach((id) => { this.remove_entity(id) }) YJ.Measure.SetMeasureStatus(false) this.tip && this.tip.destroy() this.event && this.event.destroy() this.event2D && this.event2D.destroy() } }