import EventEmitter from "eventemitter3"; import HPaaSL7 from "../HPaaSL7"; import CircleOrRectDraw from "./draw/CircleOrRect"; import PolygonDraw from "./draw/polygon"; import PolygonEditor from "./edit/PolygonEditor"; import PolygonDrawLayerl7 from "./PolygonDrawLayerL7"; import PolygonSelector from "./select/polygon/PolygonSelector"; import RoadSelector from "./select/road/RoadSelector"; import LineSplit from "./edit/LineSplit"; import { PolygonFeatureType } from "./data/PolygonStore"; import IHPaaS from "../IHPaaS"; import IPolygonDrawLayer from "./IPolygonDrawLayer"; import PolygonDrawLayerDeckGL from "./PolygonDrawLayerDeckGL"; import HPaaSDeckGL from "../HPaaSDeckGL"; type drawType = "circle" | "polygon" | "quickpolygon" | "rect" | "polygonbyroad"; class ToolsCenter extends EventEmitter { hpaas!: IHPaaS; options = { pinBaseUnit: false, // 是否贴路 pinFeature: false, // 是否贴其他图形 }; drawLayer: IPolygonDrawLayer; editor: { polygonEditor?: PolygonEditor; } = {}; selector: { road?: RoadSelector; polygon?: PolygonSelector; } = {}; drawer: { polygon?: PolygonDraw; circle?: CircleOrRectDraw; rect?: CircleOrRectDraw; polygonbyroad?: PolygonDraw; quickpolygon?: PolygonDraw; } = {}; splitter: { lineSplit?: LineSplit; } = {}; constructor(hpaas: IHPaaS) { super(); this.hpaas = hpaas; if (hpaas.type == "DECKGL") { this.drawLayer = new PolygonDrawLayerDeckGL(hpaas as HPaaSDeckGL); } else { this.drawLayer = new PolygonDrawLayerl7(hpaas as HPaaSL7); } this.initDrawer(); this.initSelector(); this.initEditor(); this.initSplitter(); } private initDrawer() { this.drawer.polygon = new PolygonDraw(this, { type: "line", }); this.drawer.circle = new CircleOrRectDraw(this, { type: "circle", }); this.drawer.rect = new CircleOrRectDraw(this, { type: "rect", }); this.drawer.polygonbyroad = new PolygonDraw(this, { type: "byroad", }); this.drawer.quickpolygon = new PolygonDraw(this, { type: "quickline", }); } private initSelector() { this.selector.polygon = new PolygonSelector(this); // if (this.hpaas.type != "L7") return; this.selector.road = new RoadSelector(this); } private initEditor() { if (this.hpaas.type != "L7") return; this.editor.polygonEditor = new PolygonEditor(this.hpaas as HPaaSL7); // this.selector.polygon = new PolygonSelector(this.hpaas); } private initSplitter() { if (this.hpaas.type != "L7") return; this.splitter.lineSplit = new LineSplit(this.hpaas as HPaaSL7); } actions = { selector: { beginSelectRoad: () => { this.actions.selector.clearAll(); this.selector.road?.begin("road"); }, beginSelectSegment: () => { this.actions.selector.clearAll(); this.selector.road?.begin("segment"); }, beginSelectPolygon: (drawLayer: IPolygonDrawLayer, selectMode: "multi" | "single" = "single") => { this.actions.selector.clearAll(); this.selector.polygon?.begin(drawLayer, selectMode); }, pauseSelect: () => { this.selector.road?.setStatus(false); this.selector.polygon?.setStatus(false); }, clearAll: () => { this.selector.road?.clear(); this.selector.polygon?.clear(); }, }, drawer: { begin: (type: drawType) => { this.actions.drawer.clearAll(); this.drawer[type]?.begin(); }, clear: (type: drawType) => { this.drawer[type]?.clear(); }, clearAll: () => { for (const key in this.drawer) { this.drawer[key as drawType]?.clear(); } }, getComboedFeature: () => { return this.drawLayer.store.getComboedMultiPolygon(); // let features: Feature[] = []; // this.drawer.polygon?.completePolygons && (features = features.concat(this.drawer.polygon?.completePolygons)); // this.drawer.circle?.completePolygons && (features = features.concat(this.drawer.circle?.completePolygons)); // this.drawer.rect?.completePolygons && (features = features.concat(this.drawer.rect?.completePolygons)); // this.drawer.polygonbyroad?.completePolygons && // (features = features.concat(this.drawer.polygonbyroad?.completePolygons)); // const collection = combine(featureCollection(features)); // this.actions.drawer.clearAll(); // return collection.features[0] as Feature; }, reset: () => { this.drawLayer.reset(); }, }, editor: { beginEditPolygon: () => { const features = this.selector.polygon?.selectedFeatures.features as PolygonFeatureType[]; if (!features || !features.length) { throw new Error("请先选中图形"); } this.actions.selector.clearAll(); this.editor.polygonEditor?.begin(features); const drawLayer = (this.hpaas as HPaaSL7).polygonDrawLayer; this.editor.polygonEditor?.on("FEATURE_EDIT", (features) => { features.forEach((feat) => { drawLayer?.store.updateFeature(feat); }); }); }, beginEditSinglePolygon: () => { const features = this.selector.polygon?.selectedFeatures.features as PolygonFeatureType[]; if (features.length !== 1) { throw new Error("不能选中超过一个图形"); } this.actions.editor.beginEditPolygon(); }, cancelPolygonEdit: () => { const features = this.editor.polygonEditor?.cancel(); const drawLayer = (this.hpaas as HPaaSL7).polygonDrawLayer; if (features) { features.forEach((feat) => { drawLayer?.store.updateFeature(feat); }); } this.actions.selector.clearAll(); }, getPolygonEditResult: () => { return this.editor.polygonEditor?.editingFeatures; }, }, splitter: { beginSplitByLine: () => { const features = this.selector.polygon?.selectedFeatures.features; if (!features?.length) { throw new Error("请先选择图形"); } console.log("test"); if (features?.length > 1) { throw new Error("只能选择一个图形做打断!"); } this.actions.selector.pauseSelect(); this.splitter.lineSplit?.begin(features[0] as PolygonFeatureType); }, cancelSplit: () => { this.splitter.lineSplit?.clear(); this.actions.selector.clearAll(); }, }, }; } export default ToolsCenter;