|
|
@ -2,7 +2,7 @@
|
|
|
|
* @Author: cbwu 504-wuchengbo@htsdfp.com
|
|
|
|
* @Author: cbwu 504-wuchengbo@htsdfp.com
|
|
|
|
* @Date: 2024-03-27 08:43:44
|
|
|
|
* @Date: 2024-03-27 08:43:44
|
|
|
|
* @LastEditors: cbwu
|
|
|
|
* @LastEditors: cbwu
|
|
|
|
* @LastEditTime: 2024-04-13 10:46:58
|
|
|
|
* @LastEditTime: 2024-04-22 16:19:27
|
|
|
|
* @Description: 绘制Polyline类
|
|
|
|
* @Description: 绘制Polyline类
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
import {
|
|
|
@ -14,26 +14,22 @@ import {
|
|
|
|
Entity,
|
|
|
|
Entity,
|
|
|
|
CallbackProperty,
|
|
|
|
CallbackProperty,
|
|
|
|
PolylineDashMaterialProperty,
|
|
|
|
PolylineDashMaterialProperty,
|
|
|
|
|
|
|
|
Cartesian2,
|
|
|
|
|
|
|
|
HeightReference,
|
|
|
|
} from 'cesium'
|
|
|
|
} from 'cesium'
|
|
|
|
import { cartesian2ToCartesian3 } from '@/utils/map/coordinate'
|
|
|
|
import {
|
|
|
|
|
|
|
|
cartesian2ToCartesian3,
|
|
|
|
|
|
|
|
cartesian3ToWGS84,
|
|
|
|
|
|
|
|
} from '@/utils/map/coordinate'
|
|
|
|
// import { PointEntity, PolylineEntity } from '@/utils/map/geometry'
|
|
|
|
// import { PointEntity, PolylineEntity } from '@/utils/map/geometry'
|
|
|
|
import { PolylineEntity } from '../geometry/polylineEntity'
|
|
|
|
import { PolylineEntity } from '../geometry/polylineEntity'
|
|
|
|
import { PointEntity } from '@/utils/map/geometry/pointEntity'
|
|
|
|
import { PointEntity } from '@/utils/map/geometry/pointEntity'
|
|
|
|
import EditGeometry from '@/utils/map/draw/editGeometry'
|
|
|
|
import EditGeometry from '@/utils/map/draw/editGeometry'
|
|
|
|
import {getDistance} from "@/utils/map/geocomputation.ts";
|
|
|
|
import { getDistance } from '@/utils/map/geocomputation.ts'
|
|
|
|
import {Angle} from "@/utils/map/angle.ts";
|
|
|
|
import { Angle } from '@/utils/map/angle.ts'
|
|
|
|
import {TextLabel} from "@/utils/map/geometry/textLabel.ts";
|
|
|
|
import { TextLabel } from '@/utils/map/geometry/textLabel.ts'
|
|
|
|
type EntityOptions = {
|
|
|
|
import { EntityOptions } from '@/types/entityoptions.ts'
|
|
|
|
id?: string
|
|
|
|
|
|
|
|
name?: string
|
|
|
|
|
|
|
|
show?: boolean
|
|
|
|
|
|
|
|
pixelSize?: number
|
|
|
|
|
|
|
|
color?: Color
|
|
|
|
|
|
|
|
fillColor?: Color
|
|
|
|
|
|
|
|
fill?: boolean
|
|
|
|
|
|
|
|
width?: number
|
|
|
|
|
|
|
|
outlineWidth?: number
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class CreatePolyline {
|
|
|
|
export default class CreatePolyline {
|
|
|
|
viewer: Viewer
|
|
|
|
viewer: Viewer
|
|
|
|
handler: ScreenSpaceEventHandler
|
|
|
|
handler: ScreenSpaceEventHandler
|
|
|
@ -51,6 +47,11 @@ export default class CreatePolyline {
|
|
|
|
totalDistance: number = 0
|
|
|
|
totalDistance: number = 0
|
|
|
|
bLongClick: boolean = false
|
|
|
|
bLongClick: boolean = false
|
|
|
|
clickTimeout: any
|
|
|
|
clickTimeout: any
|
|
|
|
|
|
|
|
altitudeOffset: number = 20 //相对高度
|
|
|
|
|
|
|
|
vDashLinePosition: Cartesian3[][] = [] //垂直辅助线坐标数组
|
|
|
|
|
|
|
|
// 存储第一次点击的信息
|
|
|
|
|
|
|
|
firstClickPosition: Cartesian2 | null = null
|
|
|
|
|
|
|
|
firstClickTime: number | null = null
|
|
|
|
// layer: CustomDataSource
|
|
|
|
// layer: CustomDataSource
|
|
|
|
defaultStyle: EntityOptions = {
|
|
|
|
defaultStyle: EntityOptions = {
|
|
|
|
// id: 'Polyline' + String(PolylineEntity.id),
|
|
|
|
// id: 'Polyline' + String(PolylineEntity.id),
|
|
|
@ -59,9 +60,13 @@ export default class CreatePolyline {
|
|
|
|
width: 2,
|
|
|
|
width: 2,
|
|
|
|
color: Color.GREEN,
|
|
|
|
color: Color.GREEN,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constructor(viewer: Viewer, bMeasure: boolean=false,options?: EntityOptions) {
|
|
|
|
constructor(
|
|
|
|
|
|
|
|
viewer: Viewer,
|
|
|
|
|
|
|
|
bMeasure: boolean = false,
|
|
|
|
|
|
|
|
options?: EntityOptions,
|
|
|
|
|
|
|
|
) {
|
|
|
|
this.viewer = viewer
|
|
|
|
this.viewer = viewer
|
|
|
|
this.bMeasure = bMeasure;
|
|
|
|
this.bMeasure = bMeasure
|
|
|
|
this.handler = new ScreenSpaceEventHandler(this.viewer.scene.canvas)
|
|
|
|
this.handler = new ScreenSpaceEventHandler(this.viewer.scene.canvas)
|
|
|
|
this.polyline = null
|
|
|
|
this.polyline = null
|
|
|
|
this.trackingLine = null
|
|
|
|
this.trackingLine = null
|
|
|
@ -98,14 +103,13 @@ export default class CreatePolyline {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//左单击回调事件
|
|
|
|
//左单击回调事件
|
|
|
|
private leftClickCallBack = (event: ScreenSpaceEventHandler.PositionedEvent) => {
|
|
|
|
private leftClickCallBack = (
|
|
|
|
const pickedObject = this.viewer.scene.pick(event.position)
|
|
|
|
event: ScreenSpaceEventHandler.PositionedEvent,
|
|
|
|
if (pickedObject) {
|
|
|
|
) => {
|
|
|
|
//点击同一位置,返回
|
|
|
|
//根据屏幕坐标判断是否为同一点
|
|
|
|
if (pickedObject.id.id === this.polyline?.controlPointsID[this.positions.length - 1])
|
|
|
|
if (this.isSamplePosition(event.position.clone())) return
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
let cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
|
|
|
|
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
|
|
|
|
|
|
|
|
if (cartesian3 != undefined) {
|
|
|
|
if (cartesian3 != undefined) {
|
|
|
|
if (!this.polyline) {
|
|
|
|
if (!this.polyline) {
|
|
|
|
this.polyline = new PolylineEntity(this.positions)
|
|
|
|
this.polyline = new PolylineEntity(this.positions)
|
|
|
@ -113,20 +117,46 @@ export default class CreatePolyline {
|
|
|
|
this.viewer.entities.add(this.dashLine)
|
|
|
|
this.viewer.entities.add(this.dashLine)
|
|
|
|
this.viewer.dataSources.add(this.polyline)
|
|
|
|
this.viewer.dataSources.add(this.polyline)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldPosition = cartesian3
|
|
|
|
|
|
|
|
//计算高度偏移后的新坐标
|
|
|
|
|
|
|
|
cartesian3 = this.calculateAltitudeOffsetPosition(
|
|
|
|
|
|
|
|
cartesian3,
|
|
|
|
|
|
|
|
this.altitudeOffset,
|
|
|
|
|
|
|
|
)
|
|
|
|
this.polyline.addPoint(cartesian3)
|
|
|
|
this.polyline.addPoint(cartesian3)
|
|
|
|
this.trackingLinePositions[0] = cartesian3
|
|
|
|
this.trackingLinePositions[0] = cartesian3
|
|
|
|
|
|
|
|
//垂直辅助线
|
|
|
|
|
|
|
|
const n = this.positions.length - 1
|
|
|
|
|
|
|
|
const ptArr = [oldPosition, cartesian3]
|
|
|
|
|
|
|
|
this.vDashLinePosition[n] = ptArr
|
|
|
|
|
|
|
|
this.viewer.entities.add(
|
|
|
|
|
|
|
|
this.createTrackingLine(this.vDashLinePosition[n], Color.WHITE),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
//添加地表控制点
|
|
|
|
|
|
|
|
const groundControlPoint = new PointEntity(this.vDashLinePosition[n][0])
|
|
|
|
|
|
|
|
this.viewer.entities.add(groundControlPoint)
|
|
|
|
this.bMove = true
|
|
|
|
this.bMove = true
|
|
|
|
this.viewer.scene.requestRender() //刷新
|
|
|
|
this.viewer.scene.requestRender() //刷新
|
|
|
|
// 计算2点距离
|
|
|
|
// 计算2点距离
|
|
|
|
if (this.positions.length >= 2 && this.bMeasure) {
|
|
|
|
if (this.positions.length >= 2 && this.bMeasure) {
|
|
|
|
let distance = getDistance(this.positions[this.positions.length-1],this.positions[this.positions.length-2])
|
|
|
|
const distance = getDistance(
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 1],
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 2],
|
|
|
|
|
|
|
|
)
|
|
|
|
this.totalDistance += distance
|
|
|
|
this.totalDistance += distance
|
|
|
|
// 计算2点方位角
|
|
|
|
// 计算2点方位角
|
|
|
|
let azimuth = Angle.getAzimuth(this.positions[this.positions.length-2],this.positions[this.positions.length-1])
|
|
|
|
const azimuth = Angle.getAzimuth(
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 2],
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 1],
|
|
|
|
|
|
|
|
)
|
|
|
|
// 计算2点的中间点
|
|
|
|
// 计算2点的中间点
|
|
|
|
let midPoint = Cartesian3.midpoint(this.positions[this.positions.length-1],this.positions[this.positions.length-2],new Cartesian3())
|
|
|
|
const midPoint = Cartesian3.midpoint(
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 1],
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 2],
|
|
|
|
|
|
|
|
new Cartesian3(),
|
|
|
|
|
|
|
|
)
|
|
|
|
// 添加label
|
|
|
|
// 添加label
|
|
|
|
let labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°`
|
|
|
|
const labelText = `距离: ${distance.toFixed(2)}km, 方位角: ${azimuth}°`
|
|
|
|
new TextLabel(this.viewer, midPoint, labelText)
|
|
|
|
new TextLabel(this.viewer, midPoint, labelText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -134,8 +164,13 @@ export default class CreatePolyline {
|
|
|
|
// 移动回调事件
|
|
|
|
// 移动回调事件
|
|
|
|
private moveCallBack = (event: ScreenSpaceEventHandler.MotionEvent) => {
|
|
|
|
private moveCallBack = (event: ScreenSpaceEventHandler.MotionEvent) => {
|
|
|
|
if (this.bMove) {
|
|
|
|
if (this.bMove) {
|
|
|
|
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.endPosition)
|
|
|
|
let cartesian3 = cartesian2ToCartesian3(this.viewer, event.endPosition)
|
|
|
|
if (cartesian3 != undefined) {
|
|
|
|
if (cartesian3 != undefined) {
|
|
|
|
|
|
|
|
//计算高度偏移后的新坐标
|
|
|
|
|
|
|
|
cartesian3 = this.calculateAltitudeOffsetPosition(
|
|
|
|
|
|
|
|
cartesian3,
|
|
|
|
|
|
|
|
this.altitudeOffset,
|
|
|
|
|
|
|
|
)
|
|
|
|
//更新追踪线坐标
|
|
|
|
//更新追踪线坐标
|
|
|
|
this.trackingLinePositions[1] = cartesian3
|
|
|
|
this.trackingLinePositions[1] = cartesian3
|
|
|
|
if (!this.trackingLine) {
|
|
|
|
if (!this.trackingLine) {
|
|
|
@ -150,7 +185,9 @@ export default class CreatePolyline {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 左双击回调事件
|
|
|
|
// 左双击回调事件
|
|
|
|
private leftDoubleClickCallBack = (event: ScreenSpaceEventHandler.PositionedEvent,) => {
|
|
|
|
private leftDoubleClickCallBack = (
|
|
|
|
|
|
|
|
event: ScreenSpaceEventHandler.PositionedEvent,
|
|
|
|
|
|
|
|
) => {
|
|
|
|
if (!this.polyline) return
|
|
|
|
if (!this.polyline) return
|
|
|
|
this.bMove = false
|
|
|
|
this.bMove = false
|
|
|
|
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
|
|
|
|
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
|
|
|
@ -163,10 +200,16 @@ export default class CreatePolyline {
|
|
|
|
|
|
|
|
|
|
|
|
if (this.bMeasure) {
|
|
|
|
if (this.bMeasure) {
|
|
|
|
// 添加总距离label
|
|
|
|
// 添加总距离label
|
|
|
|
new TextLabel(this.viewer,this.positions[this.positions.length-1],
|
|
|
|
new TextLabel(
|
|
|
|
`总距离: ${this.totalDistance.toFixed(2)}km`)
|
|
|
|
this.viewer,
|
|
|
|
|
|
|
|
this.positions[this.positions.length - 1],
|
|
|
|
|
|
|
|
`总距离: ${this.totalDistance.toFixed(2)}km`,
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.clearEvent()
|
|
|
|
this.clearEvent()
|
|
|
|
|
|
|
|
console.log('end:' + this.positions.length.toString())
|
|
|
|
|
|
|
|
console.log(this.positions)
|
|
|
|
|
|
|
|
console.log(this.polyline.controlPointsID.length)
|
|
|
|
//结束绘制进入编辑模式
|
|
|
|
//结束绘制进入编辑模式
|
|
|
|
this.polyline.removeControlPoints()
|
|
|
|
this.polyline.removeControlPoints()
|
|
|
|
const editTool = new EditGeometry(this.viewer, this.polyline.geometry!)
|
|
|
|
const editTool = new EditGeometry(this.viewer, this.polyline.geometry!)
|
|
|
@ -187,8 +230,21 @@ export default class CreatePolyline {
|
|
|
|
this.handler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE)
|
|
|
|
this.handler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE)
|
|
|
|
this.handler.removeInputAction(ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
|
|
|
|
this.handler.removeInputAction(ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//计算高度偏移后的坐标
|
|
|
|
|
|
|
|
calculateAltitudeOffsetPosition(
|
|
|
|
|
|
|
|
oldPosition: Cartesian3,
|
|
|
|
|
|
|
|
altitudeOffset: number,
|
|
|
|
|
|
|
|
): Cartesian3 {
|
|
|
|
|
|
|
|
const newGeoPosition = cartesian3ToWGS84(oldPosition)
|
|
|
|
|
|
|
|
newGeoPosition[2] = newGeoPosition[2] + altitudeOffset
|
|
|
|
|
|
|
|
return Cartesian3.fromDegrees(
|
|
|
|
|
|
|
|
newGeoPosition[0],
|
|
|
|
|
|
|
|
newGeoPosition[1],
|
|
|
|
|
|
|
|
newGeoPosition[2],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
//创建追踪线
|
|
|
|
//创建追踪线
|
|
|
|
createTrackingLine(positions: Cartesian3[]) {
|
|
|
|
createTrackingLine(positions: Cartesian3[], color: Color = Color.GREEN) {
|
|
|
|
return new Entity({
|
|
|
|
return new Entity({
|
|
|
|
polyline: {
|
|
|
|
polyline: {
|
|
|
|
positions: new CallbackProperty(() => {
|
|
|
|
positions: new CallbackProperty(() => {
|
|
|
@ -196,11 +252,35 @@ export default class CreatePolyline {
|
|
|
|
}, false),
|
|
|
|
}, false),
|
|
|
|
width: 2,
|
|
|
|
width: 2,
|
|
|
|
material: new PolylineDashMaterialProperty({
|
|
|
|
material: new PolylineDashMaterialProperty({
|
|
|
|
color: Color.GREEN,
|
|
|
|
color: color,
|
|
|
|
dashLength: 15, //短划线长度
|
|
|
|
dashLength: 12, //短划线长度
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
clampToGround: true,
|
|
|
|
// clampToGround: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//判断点击是否同一位置
|
|
|
|
|
|
|
|
isSamplePosition(clickPosition: Cartesian2) {
|
|
|
|
|
|
|
|
if (this.firstClickPosition) {
|
|
|
|
|
|
|
|
const dist = Cartesian2.distance(this.firstClickPosition, clickPosition)
|
|
|
|
|
|
|
|
if (dist <= 3) {
|
|
|
|
|
|
|
|
console.log('********click the same point0')
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.firstClickPosition = clickPosition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const pickedObject = this.viewer.scene.pick(clickPosition)
|
|
|
|
|
|
|
|
// if (pickedObject) {
|
|
|
|
|
|
|
|
// //点击同一位置,返回
|
|
|
|
|
|
|
|
// if (
|
|
|
|
|
|
|
|
// pickedObject.id.id ===
|
|
|
|
|
|
|
|
// this.polyline?.controlPointsID[this.positions.length - 1]
|
|
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
|
|
// console.log('********click the same point')
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|