重构进度填报
This commit is contained in:
@ -7,39 +7,23 @@ import { Style, Stroke, Fill } from 'ol/style';
|
||||
import GeoJSON from 'ol/format/GeoJSON';
|
||||
import { polygon as turfPolygon, booleanIntersects } from '@turf/turf';
|
||||
import { toLonLat } from 'ol/proj';
|
||||
import DragPan from 'ol/interaction/DragPan';
|
||||
import MouseWheelZoom from 'ol/interaction/MouseWheelZoom';
|
||||
|
||||
export class LassoSelector {
|
||||
private map: OLMap;
|
||||
private drawLayer: VectorLayer<VectorSource>;
|
||||
private drawSource: VectorSource;
|
||||
private overlaySource: VectorSource;
|
||||
private overlaySource: VectorSource; // 新增用于闭合多边形
|
||||
private overlayLayer: VectorLayer<VectorSource>;
|
||||
private drawing = false;
|
||||
private coordinates: [number, number][] = [];
|
||||
private targetSource: VectorSource;
|
||||
private isShiftKeyDown = false;
|
||||
private onSelectCallback: (selected: Feature[], isInvert?: boolean) => void;
|
||||
private dragPanInteraction: DragPan | null = null;
|
||||
private mouseWheelZoomInteraction: MouseWheelZoom | null = null;
|
||||
private onSelectCallback: (selected: Feature[]) => void;
|
||||
|
||||
constructor(map: OLMap, targetSource: VectorSource, onSelect: (selected: Feature[], isInvert?: boolean) => void) {
|
||||
constructor(map: OLMap, targetSource: VectorSource, onSelect: (selected: Feature[]) => void) {
|
||||
this.map = map;
|
||||
this.targetSource = targetSource;
|
||||
this.onSelectCallback = onSelect;
|
||||
|
||||
// 找出拖动和滚轮缩放交互
|
||||
this.dragPanInteraction = this.map
|
||||
.getInteractions()
|
||||
.getArray()
|
||||
.find((interaction) => interaction instanceof DragPan) as DragPan;
|
||||
|
||||
this.mouseWheelZoomInteraction = this.map
|
||||
.getInteractions()
|
||||
.getArray()
|
||||
.find((interaction) => interaction instanceof MouseWheelZoom) as MouseWheelZoom;
|
||||
|
||||
this.drawSource = new VectorSource();
|
||||
this.drawLayer = new VectorLayer({
|
||||
source: this.drawSource,
|
||||
@ -52,6 +36,7 @@ export class LassoSelector {
|
||||
});
|
||||
this.map.addLayer(this.drawLayer);
|
||||
|
||||
// 新增:闭合多边形图层(半透明填充)
|
||||
this.overlaySource = new VectorSource();
|
||||
this.overlayLayer = new VectorLayer({
|
||||
source: this.overlaySource,
|
||||
@ -74,25 +59,17 @@ export class LassoSelector {
|
||||
// 禁用默认右键菜单
|
||||
this.map.getViewport().addEventListener('contextmenu', (e) => e.preventDefault());
|
||||
|
||||
// pointerdown 捕获左键按下
|
||||
this.map.getViewport().addEventListener('pointerdown', (e) => {
|
||||
if (e.button === 0 && !this.drawing) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
this.isShiftKeyDown = e.shiftKey;
|
||||
// 右键按下开始绘制
|
||||
this.map.getViewport().addEventListener('mousedown', (e) => {
|
||||
if (e.button === 2 && !this.drawing) {
|
||||
this.drawing = true;
|
||||
this.coordinates = [];
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
|
||||
// 禁用拖动和缩放
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(false);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(false);
|
||||
}
|
||||
});
|
||||
|
||||
// pointermove 画线
|
||||
// 鼠标移动实时绘制线和闭合多边形
|
||||
this.map.on('pointermove', (evt) => {
|
||||
if (!this.drawing) return;
|
||||
const coord = evt.coordinate as [number, number];
|
||||
@ -101,30 +78,11 @@ export class LassoSelector {
|
||||
this.renderPolygon();
|
||||
});
|
||||
|
||||
// pointerup 捕获左键抬起
|
||||
this.map.getViewport().addEventListener('pointerup', (e) => {
|
||||
if (e.button === 0 && this.drawing) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// 右键抬起结束绘制并选中
|
||||
this.map.getViewport().addEventListener('mouseup', (e) => {
|
||||
if (e.button === 2 && this.drawing) {
|
||||
this.drawing = false;
|
||||
this.handleDrawEnd();
|
||||
|
||||
// 恢复拖动和缩放
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(true);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(true);
|
||||
}
|
||||
});
|
||||
|
||||
// 防止拖动导致意外事件
|
||||
this.map.getViewport().addEventListener('pointercancel', (e) => {
|
||||
if (this.drawing) {
|
||||
this.drawing = false;
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(true);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -142,6 +100,7 @@ export class LassoSelector {
|
||||
this.overlaySource.clear();
|
||||
if (this.coordinates.length < 3) return;
|
||||
|
||||
// 闭合多边形坐标(首尾闭合)
|
||||
const polygonCoords = [...this.coordinates, this.coordinates[0]];
|
||||
const polygon = new Polygon([polygonCoords]);
|
||||
const feature = new Feature({ geometry: polygon });
|
||||
@ -181,10 +140,7 @@ export class LassoSelector {
|
||||
}
|
||||
});
|
||||
|
||||
if (selected.length) {
|
||||
this.onSelectCallback(selected, this.isShiftKeyDown);
|
||||
}
|
||||
|
||||
this.onSelectCallback(selected);
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
}
|
||||
@ -192,5 +148,6 @@ export class LassoSelector {
|
||||
destroy() {
|
||||
this.map.removeLayer(this.drawLayer);
|
||||
this.map.removeLayer(this.overlayLayer);
|
||||
// 如有需要,解绑事件
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ export const globalHeaders = () => {
|
||||
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
|
||||
axios.defaults.headers['clientid'] = import.meta.env.VITE_APP_CLIENT_ID;
|
||||
axios.defaults.headers['projectId'] = cache.local.getJSON('selectedProject').id;
|
||||
axios.defaults.headers['projectId'] = cache.local.getJSON('selectedProject')?.id || '';
|
||||
|
||||
// 创建 axios 实例
|
||||
const service = axios.create({
|
||||
|
Reference in New Issue
Block a user