import { EventEmitter } from "eventemitter3"; import { Feature, featureCollection, FeatureCollection, LineString, nearestPoint, point, Point, Position, } from "@turf/turf"; import { hpaasTheme } from "./theme"; import IBaseUnit from "./IBaseUnit"; import { HpaasEventKey } from "../types"; import { ILayer, LineLayer, PointLayer } from "@antv/l7"; import HPaaSL7 from "../HPaaSL7"; class BaseUnitL7 extends EventEmitter implements IBaseUnit { private hpaas: HPaaSL7; public segmentsIsShow = false; private lineAreaLayer!: ILayer; private pointsLayer!: ILayer; // 当前视图里所有的路段控制点 public allPoints: Position[] = []; public allPointsCollection: FeatureCollection = featureCollection([]); constructor(hpaas: HPaaSL7) { super(); this.hpaas = hpaas; this.initLayer(); this.bindEvent(); } initLayer(data?: FeatureCollection) { const theme = hpaasTheme(this.hpaas.options.mode); // 路段底层 this.lineAreaLayer = new LineLayer({ blend: "normal", zIndex: 1, enableHighlight: true, }) .source(featureCollection([])) .shape("line") .size("roadType", (roadType) => { if (roadType === 101 || roadType === 102) { return 2; } else { return 1.4; } }) .style({ opacity: 1, cursor: "pointer", }) .active({ color: "#666", }) .color("roadType", (roadType) => { if (roadType === 101 || roadType === 102) { return theme.roadBgColorMain; } else { return theme.roadBgColorNormal; } }); this.hpaas.l7Scene.addLayer(this.lineAreaLayer); this.pointsLayer = new PointLayer({ zIndex: 2, blend: "normal", pickingBuffer: 2, }) .source(featureCollection([])) .shape("circle") .size(2) .color("#ccc") .style({ opacity: 1, stroke: "#fff", strokeWidth: 1, }); this.hpaas.l7Scene.addLayer(this.pointsLayer); // // 路段名称 } private bindEvent() { this.hpaas.l7Scene.on("zoomend", (e) => { const minZoom = this.hpaas.options.showBaseUnitMinZoom as number; if (this.hpaas.l7Scene.getZoom() >= minZoom) { this.segmentsIsShow = true; this.show(); this.loadSegmentsInViewport(); } else { this.segmentsIsShow = false; this.hide(); } }); this.hpaas.l7Scene.on("moveend", (e) => { // const { viewState } = e; this.loadSegmentsInViewport(); }); } /** * 渲染区域内的路段 * @param areaUid 区域uid * @param areaData 区域信息 * @param forceRefresh 默认fasle优先取indexDB中的数据,设置true则强制请求http接口获取数据 */ async loadSegmentsInViewport() { if (!this.segmentsIsShow) return; const bounds = this.hpaas.l7Scene.getBounds().flat(); const segments = await this.hpaas.wfsApi.querySegmentFeaturesAtBounds(bounds as [number, number, number, number]); this.lineAreaLayer?.setData(segments); this.allPoints = []; segments.features.forEach((f: Feature) => { f.geometry.coordinates.forEach((c) => { this.allPoints.push(c); }); }); const features: Feature[] = []; this.allPoints.forEach((p) => { features.push(point(p)); }); this.allPointsCollection = featureCollection(features); // this.pointLayer?.updateState({ // props: { data: this.allPointsCollection }, // changeFlags: { dataChanged: true }, // }); this.pointsLayer.setData(this.allPointsCollection); this.hpaas.events.emit(HpaasEventKey.SEGMENT_DATA_LOADED, segments); } show() {} hide() { this.lineAreaLayer?.setData(featureCollection([])); } /** * 获取离此点最近的点 */ getNearestPoint(sourcePoint: Position) { return nearestPoint(sourcePoint, this.allPointsCollection); } } export default BaseUnitL7;