feat: 新增Polygon绘制类。

pull/6/head
cbwu 11 months ago
parent 1f5c7a3244
commit 4f2823f073

@ -5,36 +5,40 @@
-->
<script setup lang="ts">
import { Angle } from '@/utils/map/angle.ts'
import {ScreenSpaceEventHandler, Math, ScreenSpaceEventType} from 'cesium'
import {onMounted, ref} from "vue";
import {cartesian2ToCartesian3} from "@/utils/map/coordinate.ts";
import { ScreenSpaceEventHandler, Math, ScreenSpaceEventType } from 'cesium'
import { onMounted, ref } from 'vue'
import { cartesian2ToCartesian3 } from '@/utils/map/coordinate.ts'
let nowLatStr: string, nowLonStr: string
let lonlatStr = ref('')
let isDecimal = ref(true)
onMounted(()=>{
onMounted(() => {
let _viewer = window.viewer
let canvas = _viewer.scene.canvas
let canvas = _viewer.scene.canvas
let handler = new ScreenSpaceEventHandler(canvas)
handler.setInputAction((e:any)=> {
handler.setInputAction((e: any) => {
//
let position = cartesian2ToCartesian3(_viewer, e.endPosition)
if (position) {
//
let cartographic = _viewer.scene.globe.ellipsoid.cartesianToCartographic(position);
try{
let cartographic =
_viewer.scene.globe.ellipsoid.cartesianToCartographic(position)
try {
//
nowLatStr = Math.toDegrees(cartographic.latitude).toFixed(7) //
nowLonStr = Math.toDegrees(cartographic.longitude).toFixed(7) //
let camera_alt = (_viewer.camera.positionCartographic.height / 1000) //
let camera_alt = _viewer.camera.positionCartographic.height / 1000 //
// 250-80
let needElevation: boolean = camera_alt < 250 && (_viewer.camera.pitch < -(Math.PI/180)*80)
let elevStr = needElevation? _viewer.scene.globe.getHeight(cartographic)?.toFixed(2)+'m'?? '' : '高度过高'
let needElevation: boolean =
camera_alt < 250 && _viewer.camera.pitch < -(Math.PI / 180) * 80
let elevStr = needElevation
? _viewer.scene.globe.getHeight(cartographic)?.toFixed(2) + 'm' ?? ''
: '高度过高'
if(isDecimal.value) { //
if (isDecimal.value) {
//
lonlatStr.value = `经度: ${nowLonStr}°, 纬度: ${nowLatStr}°, 高度: ${elevStr}`
}
else {
} else {
lonlatStr.value = `经度: ${Angle.DecimalDegree2DMS(nowLonStr)}, 纬度: ${Angle.DecimalDegree2DMS(nowLatStr)},
高度: ${elevStr}`
}
@ -46,10 +50,11 @@ onMounted(()=>{
function lonlatClick() {
let elevStr = lonlatStr.value.split('高度')[1]
console.log(elevStr)
if(isDecimal.value){
lonlatStr.value = `经度: ${Angle.DecimalDegree2DMS(nowLonStr)}, 纬度: ${Angle.DecimalDegree2DMS(nowLatStr)}, 高度` + elevStr
}
else {
if (isDecimal.value) {
lonlatStr.value =
`经度: ${Angle.DecimalDegree2DMS(nowLonStr)}, 纬度: ${Angle.DecimalDegree2DMS(nowLatStr)}, 高度` +
elevStr
} else {
lonlatStr.value = `经度: ${nowLonStr}°, 纬度: ${nowLatStr}°, 高度` + elevStr
}
isDecimal.value = !isDecimal.value
@ -81,13 +86,13 @@ function lonlatClick() {
align-items: center;
justify-content: center;
}
#lonlatText{
#lonlatText {
background: none;
border: none;
color: #ffffff;
font-size: 0.8rem;
}
#lonlatText:hover{
#lonlatText:hover {
cursor: pointer;
}
</style>

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-07 14:15:35
* @LastEditors: cbwu
* @LastEditTime: 2024-04-11 09:11:25
* @LastEditTime: 2024-04-12 09:22:20
* @Description:
-->
<template>
@ -23,6 +23,7 @@ import { initViewer, perfViewer, showNavigator } from '@/utils/map/sceneViewer'
import { flyToChina } from '@/utils/map/camera'
import CreatePolyline from '@/utils/map/draw/drawPolyline'
// import DrawPoint from '@/utils/map/draw/drawPoint'
import { DrawPolygon } from '@/utils/map/draw/drawPolygon'
const viewerDivRef = ref<HTMLDivElement>()
let viewer: Viewer
window.CESIUM_BASE_URL = 'node_modules/cesium/Build/Cesium/'
@ -39,7 +40,7 @@ Ion.defaultAccessToken =
// 60.0, //
// )
onMounted( () => {
onMounted(() => {
//
viewer = initViewer(viewerDivRef.value as HTMLElement)
//
@ -69,6 +70,8 @@ onMounted( () => {
//
// const drawPolyline = new CreatePolyline(viewer)
// drawPolyline.start()
const drawPolygon = new DrawPolygon(viewer)
drawPolygon.start()
})
</script>

@ -1,5 +1,5 @@
/*
* @Author: zhaipx
* @Date: {2024/4/11}
* @Description:
* @Description:
*/

@ -1,57 +1,67 @@
import {getAzimuth} from "@/utils/map/geocomputation.ts";
import {Cartesian3} from "cesium";
import { getAzimuth } from '@/utils/map/geocomputation.ts'
import { Cartesian3 } from 'cesium'
class Angle {
constructor() {}
constructor() {}
/**
*
* @param decimal_var
* @constructor
*/
static DecimalDegree2DMS(decimal_var: number|string){
if(!decimal_var.toString().includes('.'))
return decimal_var.toString() + '°0\'0\'\''
let decimalStr = decimal_var.toString().split('.')
let degreeStr = decimalStr[0]
if (decimalStr[1]){
let minutes = Number(decimalStr[1]) / Math.pow(10,decimalStr[1].length) * 60
if(!minutes.toString().includes('.'))
return degreeStr + '°'+ minutes.toString() +'\'0\'\''
let minuteSecondsStr = minutes.toString().split('.')
if (minuteSecondsStr[1]){
let secondStr = Number(minuteSecondsStr[1]) / Math.pow(10,minuteSecondsStr[1].length) * 60
return degreeStr + '°'+ minuteSecondsStr[0] +'\'' + secondStr.toFixed(3) + '\'\''
}
}
return ''
/**
*
* @param decimal_var
* @constructor
*/
static DecimalDegree2DMS(decimal_var: number | string) {
if (!decimal_var.toString().includes('.'))
return decimal_var.toString() + "°0'0''"
let decimalStr = decimal_var.toString().split('.')
let degreeStr = decimalStr[0]
if (decimalStr[1]) {
let minutes =
(Number(decimalStr[1]) / Math.pow(10, decimalStr[1].length)) * 60
if (!minutes.toString().includes('.'))
return degreeStr + '°' + minutes.toString() + "'0''"
let minuteSecondsStr = minutes.toString().split('.')
if (minuteSecondsStr[1]) {
let secondStr =
(Number(minuteSecondsStr[1]) /
Math.pow(10, minuteSecondsStr[1].length)) *
60
return (
degreeStr +
'°' +
minuteSecondsStr[0] +
"'" +
secondStr.toFixed(3) +
"''"
)
}
}
return ''
}
/**
*
* @param p1
* @param p2
* @param digits 1
*/
static getAzimuth(p1: Cartesian3, p2: Cartesian3, digits=1){
return getAzimuth(p1, p2, digits)
}
/**
*
* @param degree
*/
static degree2rad(degree: number): number {
return Math.PI/180 * degree
}
/**
*
* @param rad
*/
static rad2degree(rad: number): number {
return 180/Math.PI * rad
}
/**
*
* @param p1
* @param p2
* @param digits 1
*/
static getAzimuth(p1: Cartesian3, p2: Cartesian3, digits = 1) {
return getAzimuth(p1, p2, digits)
}
/**
*
* @param degree
*/
static degree2rad(degree: number): number {
return (Math.PI / 180) * degree
}
/**
*
* @param rad
*/
static rad2degree(rad: number): number {
return (180 / Math.PI) * rad
}
}
export { Angle }

@ -0,0 +1,160 @@
/*
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-04-11 13:53:40
* @LastEditors: cbwu
* @LastEditTime: 2024-04-12 15:18:40
* @Description:
*/
import {
Viewer,
ScreenSpaceEventHandler,
Cartesian3,
Color,
ScreenSpaceEventType,
Entity,
CallbackProperty,
PolylineDashMaterialProperty,
} from 'cesium'
import { cartesian2ToCartesian3 } from '@/utils/map/coordinate'
import { PolygonEntity } from '../geometry/polygonEntity'
import { PointEntity } from '@/utils/map/geometry/pointEntity'
import EditGeometry from '@/utils/map/draw/editGeometry'
export class DrawPolygon {
viewer: Viewer
handler: ScreenSpaceEventHandler
polygon: PolygonEntity | null
positions: Cartesian3[] = []
trackingLine: Entity | null
trackingLinePositions: Cartesian3[] = []
bMove: boolean = false
bLongClick: boolean = false
constructor(viewer: Viewer) {
this.viewer = viewer
this.handler = new ScreenSpaceEventHandler(viewer.scene.canvas)
this.polygon = null
this.trackingLine = null
}
public start() {
// 左单击加点
this.handler.setInputAction(
this.leftClickCallBack,
ScreenSpaceEventType.LEFT_CLICK,
)
// 移动动态绘制
this.handler.setInputAction(
this.moveCallBack,
ScreenSpaceEventType.MOUSE_MOVE,
)
// 左双击结束
this.handler.setInputAction(
this.leftDoubleClickCallBack,
ScreenSpaceEventType.LEFT_DOUBLE_CLICK,
)
}
//左单击回调事件
private leftClickCallBack = (
event: ScreenSpaceEventHandler.PositionedEvent,
) => {
const pickedObject = this.viewer.scene.pick(event.position)
// console.log(pickedObject)
if (pickedObject) {
//点击同一位置,返回
if (
pickedObject.id.id ===
this.polygon?.controlPointsID[this.positions.length - 1]
) {
console.log('********click the same point')
return
}
}
console.log('****************leftClick!')
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.position)
if (cartesian3 != undefined) {
if (this.positions.length <= 2) {
this.positions.push(cartesian3)
this.trackingLinePositions.push(cartesian3)
} else {
this.polygon!.modifyPoint(cartesian3, -1)
this.polygon!.showControlPoint(-1, true)
//多创建一个点,用于鼠标移动实时变化
this.polygon!.addPoint(cartesian3, -1)
this.polygon!.showControlPoint(-1, false)
this.trackingLinePositions[2] = cartesian3
}
this.bMove = true
this.viewer.scene.requestRender() //刷新
}
}
//移动回调事件
private moveCallBack = (event: ScreenSpaceEventHandler.MotionEvent) => {
if (this.bMove) {
const cartesian3 = cartesian2ToCartesian3(this.viewer, event.endPosition)
if (cartesian3 != undefined) {
if (this.positions.length === 1) {
//更新追踪线坐标
this.trackingLinePositions[1] = cartesian3
// this.positions[1] = cartesian3
if (!this.trackingLine) {
//创建追踪线对象
this.trackingLine = this.createTrackingLine(
this.trackingLinePositions,
)
this.viewer.entities.add(this.trackingLine)
}
}
if (this.positions.length === 2) {
//移除追踪直线
if (this.trackingLine) {
this.viewer.entities.remove(this.trackingLine)
}
this.positions.push(cartesian3)
console.log(
'*********PositionsLength:' + this.positions.length.toString(),
)
//创建多边形
if (!this.polygon) {
this.polygon = new PolygonEntity(this.positions)
this.viewer.dataSources.add(this.polygon)
// 实时移动不显示控制点
this.polygon.showControlPoint(-1, false)
}
}
//实时改变多边形
if (this.positions.length >= 3) {
this.polygon!.modifyPoint(cartesian3, -1)
}
this.viewer.scene.requestRender() //刷新
}
}
}
//左双击回调事件
private leftDoubleClickCallBack = (
event: ScreenSpaceEventHandler.PositionedEvent,
) => {
if (!this.polygon) return
console.log('**************************** double click')
this.bMove = false
//移除多余的点
this.polygon.removePoint(-1)
this.polygon.removePoint(-1)
}
//创建追踪线
createTrackingLine(positions: Cartesian3[]) {
return new Entity({
polyline: {
positions: new CallbackProperty(() => {
return positions
}, false),
width: 2,
material: new PolylineDashMaterialProperty({
color: Color.GREEN,
dashLength: 15, //短划线长度
}),
clampToGround: true,
},
})
}
}

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-27 08:43:44
* @LastEditors: cbwu
* @LastEditTime: 2024-04-02 13:33:25
* @LastEditTime: 2024-04-12 09:06:27
* @Description: Polyline
*/
import {
@ -178,7 +178,7 @@ export default class CreatePolyline {
this.handler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE)
this.handler.removeInputAction(ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
}
//创建追踪线
createTrackingLine(positions: Cartesian3[]) {
return new Entity({
polyline: {

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-15 08:43:26
* @LastEditors: cbwu
* @LastEditTime: 2024-03-26 14:20:32
* @LastEditTime: 2024-04-12 10:30:36
* @FilePath: \GCSMap\src\utils\drawer.ts
* @Description:
*/

@ -5,7 +5,14 @@
* @LastEditTime: 2024-04-01 14:05:43
* @Description:
*/
import {Cartesian3, Cartographic, EllipsoidGeodesic, Math as Cesium_Math, Matrix4, Transforms} from 'cesium'
import {
Cartesian3,
Cartographic,
EllipsoidGeodesic,
Math as Cesium_Math,
Matrix4,
Transforms,
} from 'cesium'
/**
* 线
* @param lineStart 线[longitude1, latitude1, height1]
@ -84,20 +91,18 @@ function isOnLineSegment(
*/
}
/**
*
* @param p1
* @param p2
*/
function getDistance(p1:Cartesian3, p2: Cartesian3): number
{
let point1cartographic = Cartographic.fromCartesian(p1);
let point2cartographic = Cartographic.fromCartesian(p2);
function getDistance(p1: Cartesian3, p2: Cartesian3): number {
let point1cartographic = Cartographic.fromCartesian(p1)
let point2cartographic = Cartographic.fromCartesian(p2)
/**根据经纬度计算出距离**/
let geodesic = new EllipsoidGeodesic()
geodesic.setEndPoints(point1cartographic, point2cartographic)
return geodesic.surfaceDistance/1000
return geodesic.surfaceDistance / 1000
}
/**
@ -106,20 +111,30 @@ function getDistance(p1:Cartesian3, p2: Cartesian3): number
* @param p2
* @param digits 1
*/
function getAzimuth(p1:Cartesian3, p2: Cartesian3, digits=1): string
{
function getAzimuth(p1: Cartesian3, p2: Cartesian3, digits = 1): string {
// 建立局部坐标系北为y东为xp1为原点
const localMatrix = Transforms.eastNorthUpToFixedFrame(p1)
//求世界坐标到局部坐标的变换矩阵
const worldToLocalMatrix = Matrix4.inverse(localMatrix, new Matrix4())
//p1在局部坐标系的位置即局部坐标原点
const localPosition1 = Matrix4.multiplyByPoint(worldToLocalMatrix, p1, new Cartesian3())
const localPosition1 = Matrix4.multiplyByPoint(
worldToLocalMatrix,
p1,
new Cartesian3(),
)
//p2在局部坐标系的位置
const localPosition2 = Matrix4.multiplyByPoint(worldToLocalMatrix, p2, new Cartesian3())
const localPosition2 = Matrix4.multiplyByPoint(
worldToLocalMatrix,
p2,
new Cartesian3(),
)
//弧度
const angle = Math.atan2(localPosition2.x - localPosition1.x, localPosition2.y - localPosition1.y)
const angle = Math.atan2(
localPosition2.x - localPosition1.x,
localPosition2.y - localPosition1.y,
)
//转为角度
let theta = angle * (180 / Math.PI);
let theta = angle * (180 / Math.PI)
theta = theta < 0 ? theta + 360 : theta
return theta.toFixed(digits)
}
@ -139,4 +154,10 @@ function getPolygonArea(vertexs: Cartesian3[]) {
return Math.abs(area)
}
export { getClosestPoint, isOnLineSegment, getDistance, getAzimuth, getPolygonArea }
export {
getClosestPoint,
isOnLineSegment,
getDistance,
getAzimuth,
getPolygonArea,
}

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-28 16:22:58
* @LastEditors: cbwu
* @LastEditTime: 2024-04-02 14:09:15
* @LastEditTime: 2024-04-12 14:57:49
* @Description:
*/
import {
@ -37,16 +37,21 @@ export abstract class BaseGeometry extends CustomDataSource {
* ,
* @param pos
* @param index 0
* @bAddControlPoint
*/
public addPoint(pos: Cartesian3, index: number = -1) {
public addPoint(
pos: Cartesian3,
index: number = -1,
bAddControlPoint: boolean = true,
) {
if (index === -1) {
//插入尾部
this.positions.push(pos)
// console.log(this.positions.length)
this.createPoint(pos, this.positions.length - 1)
if (bAddControlPoint)
this.createControlPoint(pos, this.positions.length - 1)
} else if (index >= 0 && index < this.positions.length) {
this.positions.splice(index, 0, pos)
this.createPoint(pos, index)
if (bAddControlPoint) this.createControlPoint(pos, index)
} else {
return
}
@ -62,11 +67,11 @@ export abstract class BaseGeometry extends CustomDataSource {
this.positions.pop()
} else if (index >= 0 && index < this.positions.length) {
this.positions.splice(index, 1)
this.entities.removeById(this.controlPointsID[index])
this.controlPointsID.splice(index, 1)
} else {
return
}
//移除控制点
this.removeControlPoint(index)
}
/**
*
@ -74,12 +79,17 @@ export abstract class BaseGeometry extends CustomDataSource {
* @param index
*/
public modifyPoint(pos: Cartesian3, index: number) {
if (index === -1) {
index = this.positions.length - 1
}
if (index >= 0 && index < this.positions.length) {
this.positions.splice(index, 1, pos)
//修改控制点坐标
const point = this.entities.getById(this.controlPointsID[index])
if (point) {
point.position = new ConstantPositionProperty(pos)
if (this.controlPointsID[index]) {
//修改控制点坐标
const point = this.entities.getById(this.controlPointsID[index])
if (point) {
point.position = new ConstantPositionProperty(pos)
}
}
}
}
@ -88,7 +98,7 @@ export abstract class BaseGeometry extends CustomDataSource {
* @param pos
* @param index ,0
*/
createPoint(pos: Cartesian3, index: number) {
public createControlPoint(pos: Cartesian3, index: number) {
// if (this.geometry) {
const point = new PointEntity(pos)
point.parent = this.geometry!
@ -97,6 +107,17 @@ export abstract class BaseGeometry extends CustomDataSource {
this.controlPointsID.splice(index, 0, point.id)
// }
}
/**
*
* @param index
*/
public removeControlPoint(index: number) {
if (index === -1) index = this.controlPointsID.length - 1
if (this.controlPointsID[index]) {
this.entities.removeById(this.controlPointsID[index])
this.controlPointsID.splice(index, 1)
}
}
/**
*
*/
@ -106,4 +127,16 @@ export abstract class BaseGeometry extends CustomDataSource {
})
this.controlPointsID = []
}
/**
*
* @param index
* @param bShow
*/
showControlPoint(index: number, bShow: boolean) {
if (index === -1) index = this.controlPointsID.length - 1
if (this.controlPointsID[index]) {
const point = this.entities.getById(this.controlPointsID[index])
if (point) point.show = bShow
}
}
}

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-28 16:35:33
* @LastEditors: cbwu
* @LastEditTime: 2024-03-30 22:06:23
* @LastEditTime: 2024-04-12 15:43:49
* @Description:
*/
import {
@ -39,8 +39,9 @@ class PointEntity extends Entity {
id: 'Point' + String(PointEntity.ID),
name: 'Point' + String(PointEntity.ID + 1),
show: true,
pixelSize: 10,
color: Color.GREEN,
pixelSize: 8,
color: Color.WHITE,
outlineColor: Color.GREEN,
outlineWidth: 0,
}
constructor(position: Cartesian3, options?: EntityOptions) {

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-04-11 09:26:56
* @LastEditors: cbwu
* @LastEditTime: 2024-04-11 13:26:25
* @LastEditTime: 2024-04-12 15:34:45
* @Description: Polygon
*/
import {
@ -10,8 +10,7 @@ import {
Cartesian3,
Color,
CallbackProperty,
Property,
PolygonGraphics,
PolygonHierarchy,
} from 'cesium'
import { BaseGeometry } from './baseGeometry'
import { type EntityOptions } from './pointEntity'
@ -28,20 +27,30 @@ export class PolygonEntity extends BaseGeometry {
fillColor: Color.RED.withAlpha(0.5),
fill: true,
}
constructor(ptArr: Cartesian3[] | Property, options?: EntityOptions) {
constructor(ptArr: Cartesian3[], options?: EntityOptions) {
super()
this.positions = ptArr
this.options = { ...this.options, ...options }
this.geometry = new Entity({
show: this.options.show,
name: this.options.name,
polygon: {
hierarchy: ptArr,
hierarchy: new CallbackProperty(() => {
return new PolygonHierarchy(this.positions)
}, false),
material: this.options.fillColor, //填充颜色
fill: this.options.fill, //是否填充
outlineWidth: this.options.width, //线宽
outline: true,
// outlineWidth: this.options.width, //线宽
outlineColor: this.options.color, //线颜色
},
})
this.entities.add(this.geometry)
// 添加控制点
ptArr.forEach((pt, index) => {
this.createControlPoint(pt, index)
})
PolygonEntity.ID++
}
}

@ -2,7 +2,7 @@
* @Author: cbwu 504-wuchengbo@htsdfp.com
* @Date: 2024-03-28 16:49:02
* @LastEditors: cbwu
* @LastEditTime: 2024-04-01 17:04:55
* @LastEditTime: 2024-04-12 14:27:12
* @Description: Polyline
*/
import { Entity, Cartesian3, Color, CallbackProperty } from 'cesium'
@ -40,7 +40,7 @@ export class PolylineEntity extends BaseGeometry {
this.entities.add(this.geometry)
// 添加控制点
ptArr.forEach((pt, index) => {
this.createPoint(pt, index)
this.createControlPoint(pt, index)
})
PolylineEntity.ID++

Loading…
Cancel
Save