2023-08-15 07:16:12 +00:00
|
|
|
import * as CesiumTypeOnly from '@examples/cesium';
|
2023-08-14 09:47:59 +00:00
|
|
|
import { State } from './interface';
|
2023-08-09 09:55:29 +00:00
|
|
|
|
|
|
|
export default class Draw {
|
2023-08-10 10:58:38 +00:00
|
|
|
cesium: typeof CesiumTypeOnly;
|
|
|
|
viewer: CesiumTypeOnly.Viewer;
|
|
|
|
eventHandler: CesiumTypeOnly.ScreenSpaceEventHandler;
|
2023-08-16 08:17:33 +00:00
|
|
|
polygonEntity: CesiumTypeOnly.Entity;
|
2023-08-14 09:47:59 +00:00
|
|
|
geometryPoints: CesiumTypeOnly.Cartesian3[] = [];
|
|
|
|
state: State = 'drawing';
|
2023-08-17 11:35:20 +00:00
|
|
|
controlPoints: CesiumTypeOnly.EntityCollection = [];
|
2023-08-14 09:47:59 +00:00
|
|
|
controlPointsEventHandler: CesiumTypeOnly.ScreenSpaceEventHandler;
|
2023-08-16 08:17:33 +00:00
|
|
|
lineEntity: CesiumTypeOnly.Entity;
|
2023-08-21 05:47:28 +00:00
|
|
|
type!: 'polygon' | 'line';
|
2023-08-09 09:55:29 +00:00
|
|
|
|
2023-08-10 10:58:38 +00:00
|
|
|
constructor(cesium: typeof CesiumTypeOnly, viewer: CesiumTypeOnly.Viewer) {
|
2023-08-09 09:55:29 +00:00
|
|
|
this.cesium = cesium;
|
|
|
|
this.viewer = viewer;
|
2023-08-16 08:17:33 +00:00
|
|
|
|
2023-08-10 10:58:38 +00:00
|
|
|
this.cartesianToLnglat = this.cartesianToLnglat.bind(this);
|
|
|
|
this.pixelToCartesian = this.pixelToCartesian.bind(this);
|
2023-08-14 09:47:59 +00:00
|
|
|
this.onClick();
|
2023-08-09 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:47:59 +00:00
|
|
|
/**
|
|
|
|
* The base class provides a method to change the state, and different logic is implemented based on the state.
|
|
|
|
* The state is controlled by individual sub-components according to the actual situation.
|
|
|
|
* @param state
|
|
|
|
*/
|
|
|
|
setState(state: State) {
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
getState(): State {
|
|
|
|
return this.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind a global click event that responds differently based on the state. When in the drawing state,
|
|
|
|
* a click will add points for geometric shapes. During editing, selecting a drawn shape puts it in an
|
|
|
|
* editable state. Clicking on empty space sets it to a static state.
|
|
|
|
*/
|
2023-08-10 02:23:56 +00:00
|
|
|
onClick() {
|
2023-08-09 09:55:29 +00:00
|
|
|
this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
|
2023-08-16 08:17:33 +00:00
|
|
|
this.eventHandler.setInputAction((evt: any) => {
|
2023-08-14 09:47:59 +00:00
|
|
|
const pickedObject = this.viewer.scene.pick(evt.position);
|
|
|
|
const hitEntities = this.cesium.defined(pickedObject) && pickedObject.id instanceof CesiumTypeOnly.Entity;
|
2023-08-21 05:47:28 +00:00
|
|
|
let activeEntity = this.polygonEntity;
|
|
|
|
if (this.type === 'line') {
|
|
|
|
activeEntity = this.lineEntity;
|
|
|
|
}
|
|
|
|
|
2023-08-14 09:47:59 +00:00
|
|
|
if (this.state === 'drawing') {
|
|
|
|
// In the drawing state, the points clicked are key nodes of the shape, and they are saved in this.points.
|
|
|
|
const cartesian = this.pixelToCartesian(evt.position);
|
2023-08-16 08:17:33 +00:00
|
|
|
const points = this.getPoints();
|
|
|
|
// If clicked outside the sphere or if the distance between the current click position and
|
|
|
|
// the previous click position is less than 10 meters, it is considered an invalid point.
|
|
|
|
if (!cartesian || (points.length > 0 && !this.checkDistance(cartesian, points[points.length - 1]))) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-14 09:47:59 +00:00
|
|
|
this.addPoint(cartesian);
|
|
|
|
} else if (this.state === 'edit') {
|
2023-08-17 11:35:20 +00:00
|
|
|
//In edit mode, exit the editing state and delete control points when clicking outside the currently edited shape.
|
2023-08-21 05:47:28 +00:00
|
|
|
if (!hitEntities || activeEntity.id !== pickedObject.id.id) {
|
2023-08-14 09:47:59 +00:00
|
|
|
this.setState('static');
|
|
|
|
this.removeControlPoints();
|
|
|
|
}
|
|
|
|
} else if (this.state === 'static') {
|
2023-08-17 09:31:54 +00:00
|
|
|
//When drawing multiple shapes, the click events for all shapes are triggered. Only when hitting a completed shape should it enter editing mode.
|
2023-08-21 05:47:28 +00:00
|
|
|
if (hitEntities && activeEntity.id === pickedObject.id.id) {
|
2023-08-14 09:47:59 +00:00
|
|
|
const pickedEntity = pickedObject.id;
|
|
|
|
if (this.cesium.defined(pickedEntity.polygon)) {
|
|
|
|
// Hit PolygonGraphics geometry.
|
|
|
|
this.setState('edit');
|
|
|
|
this.addControlPoints();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-09 09:55:29 +00:00
|
|
|
}, this.cesium.ScreenSpaceEventType.LEFT_CLICK);
|
|
|
|
}
|
|
|
|
|
2023-08-10 02:23:56 +00:00
|
|
|
onMouseMove() {
|
2023-08-16 08:17:33 +00:00
|
|
|
this.eventHandler.setInputAction((evt: any) => {
|
|
|
|
const points = this.getPoints();
|
|
|
|
const cartesian = this.pixelToCartesian(evt.endPosition);
|
|
|
|
if (!cartesian) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.checkDistance(cartesian, points[points.length - 1])) {
|
|
|
|
// Synchronize data to subclasses.If the distance is less than 10 meters, do not proceed
|
|
|
|
this.updateMovingPoint(cartesian, points.length);
|
|
|
|
}
|
2023-08-10 02:23:56 +00:00
|
|
|
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
|
|
}
|
|
|
|
|
2023-08-16 08:17:33 +00:00
|
|
|
onDoubleClick() {
|
|
|
|
// this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
|
|
|
|
this.eventHandler.setInputAction((evt: any) => {
|
|
|
|
this.finishDrawing();
|
|
|
|
}, this.cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the distance between two points is greater than 10 meters.
|
|
|
|
*/
|
|
|
|
checkDistance(cartesian1: CesiumTypeOnly.Cartesian3, cartesian2: CesiumTypeOnly.Cartesian3) {
|
|
|
|
const distance = this.cesium.Cartesian3.distance(cartesian1, cartesian2);
|
|
|
|
return distance > 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
finishDrawing() {
|
|
|
|
this.removeMoveListener();
|
|
|
|
this.setState('static');
|
2023-08-14 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 10:58:38 +00:00
|
|
|
removeClickListener() {
|
|
|
|
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_CLICK);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeMoveListener() {
|
|
|
|
this.eventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
setGeometryPoints(geometryPoints: CesiumTypeOnly.Cartesian3[]) {
|
2023-08-10 02:23:56 +00:00
|
|
|
this.geometryPoints = geometryPoints;
|
|
|
|
}
|
|
|
|
|
2023-08-14 09:47:59 +00:00
|
|
|
getGeometryPoints(): CesiumTypeOnly.Cartesian3[] {
|
|
|
|
return this.geometryPoints;
|
|
|
|
}
|
|
|
|
|
2023-08-21 05:47:28 +00:00
|
|
|
drawPolygon() {
|
2023-08-09 09:55:29 +00:00
|
|
|
const callback = () => {
|
2023-08-10 02:23:56 +00:00
|
|
|
return new this.cesium.PolygonHierarchy(this.geometryPoints);
|
2023-08-09 09:55:29 +00:00
|
|
|
};
|
2023-08-16 08:17:33 +00:00
|
|
|
if (!this.polygonEntity) {
|
|
|
|
this.polygonEntity = this.viewer.entities.add({
|
2023-08-10 02:23:56 +00:00
|
|
|
polygon: new this.cesium.PolygonGraphics({
|
|
|
|
hierarchy: new this.cesium.CallbackProperty(callback, false),
|
|
|
|
show: true,
|
|
|
|
// fill: true,
|
|
|
|
// material: this.cesium.Color.LIGHTSKYBLUE.withAlpha(0.8),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2023-08-09 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 08:17:33 +00:00
|
|
|
drawLine() {
|
|
|
|
if (!this.lineEntity) {
|
|
|
|
this.lineEntity = this.viewer.entities.add({
|
|
|
|
polyline: {
|
|
|
|
positions: new this.cesium.CallbackProperty(() => this.geometryPoints, false),
|
2023-08-17 09:31:54 +00:00
|
|
|
width: 2,
|
2023-08-16 08:17:33 +00:00
|
|
|
// material: this.cesium.Color.RED,
|
|
|
|
clampToGround: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 10:58:38 +00:00
|
|
|
cartesianToLnglat(cartesian: CesiumTypeOnly.Cartesian3): number[] {
|
|
|
|
const lnglat = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
|
|
|
|
const lat = this.cesium.Math.toDegrees(lnglat.latitude);
|
|
|
|
const lng = this.cesium.Math.toDegrees(lnglat.longitude);
|
2023-08-09 09:55:29 +00:00
|
|
|
return [lng, lat];
|
|
|
|
}
|
2023-08-10 02:23:56 +00:00
|
|
|
|
2023-08-10 10:58:38 +00:00
|
|
|
pixelToCartesian(position: CesiumTypeOnly.Cartesian2): CesiumTypeOnly.Cartesian3 | undefined {
|
2023-08-10 02:23:56 +00:00
|
|
|
const ray = this.viewer.camera.getPickRay(position);
|
|
|
|
const cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
|
|
|
|
return cartesian;
|
|
|
|
}
|
2023-08-14 09:47:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display key points when creating a shape, allowing dragging of these points to edit and generate new shapes.
|
|
|
|
*/
|
|
|
|
addControlPoints() {
|
|
|
|
const points = this.getPoints();
|
|
|
|
this.controlPoints = points.map((position) => {
|
2023-08-17 09:31:54 +00:00
|
|
|
// return this.viewer.entities.add({
|
|
|
|
// position,
|
|
|
|
// billboard: {
|
|
|
|
// image: './src/assets/circle_red.png',
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
2023-08-14 09:47:59 +00:00
|
|
|
return this.viewer.entities.add({
|
|
|
|
position,
|
2023-08-17 09:31:54 +00:00
|
|
|
point: {
|
|
|
|
pixelSize: 10,
|
|
|
|
heightReference: this.cesium.HeightReference.CLAMP_TO_GROUND,
|
|
|
|
color: this.cesium.Color.RED,
|
2023-08-14 09:47:59 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let isDragging = false;
|
|
|
|
let draggedIcon: CesiumTypeOnly.Entity = null;
|
|
|
|
|
|
|
|
this.controlPointsEventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
|
|
|
|
// Listen for left mouse button press events
|
2023-08-16 08:17:33 +00:00
|
|
|
this.controlPointsEventHandler.setInputAction((clickEvent: any) => {
|
2023-08-14 09:47:59 +00:00
|
|
|
const pickedObject = this.viewer.scene.pick(clickEvent.position);
|
|
|
|
|
|
|
|
if (this.cesium.defined(pickedObject)) {
|
|
|
|
for (let i = 0; i < this.controlPoints.length; i++) {
|
|
|
|
if (pickedObject.id === this.controlPoints[i]) {
|
|
|
|
isDragging = true;
|
|
|
|
draggedIcon = this.controlPoints[i];
|
|
|
|
draggedIcon.index = i; //Save the index of dragged points for dynamic updates during movement
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Disable default camera interaction.
|
|
|
|
this.viewer.scene.screenSpaceCameraController.enableRotate = false;
|
|
|
|
}
|
|
|
|
}, this.cesium.ScreenSpaceEventType.LEFT_DOWN);
|
|
|
|
|
|
|
|
// Listen for mouse movement events
|
2023-08-16 08:17:33 +00:00
|
|
|
this.controlPointsEventHandler.setInputAction((moveEvent: any) => {
|
2023-08-14 09:47:59 +00:00
|
|
|
if (isDragging && draggedIcon) {
|
|
|
|
const cartesian = this.viewer.camera.pickEllipsoid(moveEvent.endPosition, this.viewer.scene.globe.ellipsoid);
|
|
|
|
if (cartesian) {
|
|
|
|
draggedIcon.position.setValue(cartesian);
|
2023-08-16 08:17:33 +00:00
|
|
|
this.updateDraggingPoint(cartesian, draggedIcon.index);
|
2023-08-14 09:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
|
|
|
|
|
|
// Listen for left mouse button release events
|
|
|
|
this.controlPointsEventHandler.setInputAction(() => {
|
|
|
|
isDragging = false;
|
|
|
|
draggedIcon = null;
|
|
|
|
this.viewer.scene.screenSpaceCameraController.enableRotate = true;
|
|
|
|
}, this.cesium.ScreenSpaceEventType.LEFT_UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeControlPoints() {
|
2023-08-17 11:35:20 +00:00
|
|
|
if (this.controlPoints.length > 0) {
|
|
|
|
this.controlPoints.forEach((entity: CesiumTypeOnly.Entity) => {
|
|
|
|
this.viewer.entities.remove(entity);
|
|
|
|
});
|
|
|
|
this.controlPointsEventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_DOWN);
|
|
|
|
this.controlPointsEventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
|
|
this.controlPointsEventHandler.removeInputAction(this.cesium.ScreenSpaceEventType.LEFT_UP);
|
|
|
|
}
|
2023-08-14 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addPoint(cartesian: CesiumTypeOnly.Cartesian3) {
|
|
|
|
//Abstract method that must be implemented by subclasses.
|
|
|
|
}
|
|
|
|
|
|
|
|
getPoints(): CesiumTypeOnly.Cartesian3[] {
|
|
|
|
//Abstract method that must be implemented by subclasses.
|
|
|
|
return this.cesium.Cartesian3();
|
|
|
|
}
|
|
|
|
|
2023-08-16 08:17:33 +00:00
|
|
|
updateMovingPoint(cartesian: CesiumTypeOnly.Cartesian3, index?: number) {
|
|
|
|
//Abstract method that must be implemented by subclasses.
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDraggingPoint(cartesian: CesiumTypeOnly.Cartesian3, index: number) {
|
2023-08-14 09:47:59 +00:00
|
|
|
//Abstract method that must be implemented by subclasses.
|
|
|
|
}
|
2023-08-09 09:55:29 +00:00
|
|
|
}
|