import * as Cesium from 'cesium' import { MapTooltip } from '@ztzf/utils' import { ToolBase } from '../ToolBase' const POINT_ENTITY_NAME = 'edit-polygon-point' const DEFAULT_STYLE = { fill: Cesium.Color.fromBytes(45, 140, 240, 99), outline: Cesium.Color.fromBytes(45, 140, 240, 255), } const resolveStyle = style => ({ fill: style?.fill || DEFAULT_STYLE.fill, outline: style?.outline || DEFAULT_STYLE.outline, }) const normalizePoints = points => (points || []).map(point => ({ lng: point.lng ?? point.longitude, lat: point.lat ?? point.latitude, height: point.height ?? 0, })) export class EditPolygonTool extends ToolBase { constructor(viewer, options = {}) { super(viewer) this.tooltip = new MapTooltip(viewer) this.dataSource = null this.polygonEntity = null this.polylineEntity = null this.positions = [] this.isDragging = false this.draggedIndex = -1 this.style = resolveStyle(options?.style) } start(points = []) { const normalized = normalizePoints(points) this.positions = normalized .filter(point => point?.lng !== undefined && point?.lat !== undefined) .map(point => Cesium.Cartesian3.fromDegrees(Number(point.lng), Number(point.lat), Number(point.height || 0))) this.dataSource = new Cesium.CustomDataSource('edit-polygon') this.viewer.dataSources.add(this.dataSource) this.createEntities() this.rebuildPointEntities() this.initHandler() } initHandler() { if (!this.viewer || this.handler) return this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas) this.handler.setInputAction(movement => this.handleMouseMove(movement), Cesium.ScreenSpaceEventType.MOUSE_MOVE) this.handler.setInputAction(click => this.handleLeftDown(click), Cesium.ScreenSpaceEventType.LEFT_DOWN) this.handler.setInputAction(() => this.handleLeftUp(), Cesium.ScreenSpaceEventType.LEFT_UP) this.handler.setInputAction(click => this.handleRightClick(click), Cesium.ScreenSpaceEventType.RIGHT_CLICK) } handleLeftDown(click) { const pickedEntity = this.viewer.scene.pick(click.position)?.id if (!pickedEntity || pickedEntity.name !== POINT_ENTITY_NAME) return const index = pickedEntity.customData?.index if (typeof index !== 'number') return this.isDragging = true this.draggedIndex = index this.disableMapControl() } handleLeftUp() { if (!this.isDragging) return this.isDragging = false this.draggedIndex = -1 this.enableMapControl() this.notify('getPolygonPositions', this.positions) } handleMouseMove(movement) { if (!this.tooltip?.isVisible) { this.tooltip.show('????????????', movement.endPosition) } else { this.tooltip.show('????????????') this.tooltip.move(movement.endPosition) } if (!this.isDragging || this.draggedIndex < 0) return const position = this.getPositionFromScreen(movement.endPosition) if (!position) return this.positions[this.draggedIndex] = position const pointEntity = this.dataSource.entities.values.find( entity => entity?.name === POINT_ENTITY_NAME && entity.customData?.index === this.draggedIndex ) if (pointEntity) { pointEntity.position = position } } handleRightClick(click) { const pickedEntity = this.viewer.scene.pick(click.position)?.id if (!pickedEntity || pickedEntity.name !== POINT_ENTITY_NAME) return if (this.positions.length <= 3) return const index = pickedEntity.customData?.index if (typeof index !== 'number') return this.positions.splice(index, 1) this.rebuildPointEntities() this.notify('getPolygonPositions', this.positions) } createEntities() { this.polygonEntity = this.dataSource.entities.add({ polygon: { hierarchy: new Cesium.CallbackProperty(() => new Cesium.PolygonHierarchy(this.positions), false), material: this.style.fill, outline: false, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, show: new Cesium.CallbackProperty(() => this.positions.length >= 3, false), }, }) this.polylineEntity = this.dataSource.entities.add({ polyline: { positions: new Cesium.CallbackProperty(() => { if (this.positions.length < 2) return this.positions return [...this.positions, this.positions[0]] }, false), clampToGround: true, width: 2, material: this.style.outline, show: new Cesium.CallbackProperty(() => this.positions.length >= 2, false), }, }) } rebuildPointEntities() { this.dataSource.entities.values .slice() .filter(entity => entity?.name === POINT_ENTITY_NAME) .forEach(entity => this.dataSource.entities.remove(entity)) this.positions.forEach((position, index) => { this.dataSource.entities.add({ name: POINT_ENTITY_NAME, position: position.clone ? position.clone() : position, point: { pixelSize: 12, color: Cesium.Color.WHITE, outlineColor: this.style.outline, outlineWidth: 2, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, disableDepthTestDistance: Number.POSITIVE_INFINITY, }, customData: { index, }, }) }) } getPositions() { return this.positions.slice() } getPositionFromScreen(screenPosition) { const scene = this.viewer.scene const cartesian = scene.pickPosition(screenPosition) if (cartesian) return cartesian return scene.camera.pickEllipsoid(screenPosition, scene.globe.ellipsoid) } setStyle(style) { this.style = resolveStyle(style) if (this.polygonEntity?.polygon) { this.polygonEntity.polygon.material = this.style.fill } if (this.polylineEntity?.polyline) { this.polylineEntity.polyline.material = this.style.outline } if (this.dataSource) { this.dataSource.entities.values .filter(entity => entity?.name === POINT_ENTITY_NAME) .forEach(entity => { if (entity?.point) { entity.point.outlineColor = this.style.outline } }) } } disableMapControl() { const controller = this.viewer.scene.screenSpaceCameraController controller.enableRotate = false controller.enableTranslate = false controller.enableZoom = false } enableMapControl() { const controller = this.viewer.scene.screenSpaceCameraController controller.enableRotate = true controller.enableTranslate = true controller.enableZoom = true } destroy() { if (this.dataSource) { this.dataSource.entities.removeAll() this.viewer.dataSources.remove(this.dataSource) this.dataSource = null } if (this.handler) { this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE) this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN) this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP) this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK) this.handler.destroy() this.handler = null } this.tooltip?.destroy() this.tooltip = null this.positions = [] this.isDragging = false this.draggedIndex = -1 } }