@hzab/map-combine 0.4.2-alpha.1 → 0.4.2-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +5 -5
  3. package/src/amap/AMap.ts +318 -306
  4. package/src/amap/AMapEvent.ts +110 -54
  5. package/src/amap/AMapLayer.ts +241 -0
  6. package/src/amap/AMapPoint.ts +140 -143
  7. package/src/amap/AMapPolygon.ts +214 -195
  8. package/src/amap/AMapPolyline.ts +199 -185
  9. package/src/basic/BasicBusiness.ts +5 -5
  10. package/src/basic/BasicModel.ts +49 -0
  11. package/src/basic/Layer.ts +154 -0
  12. package/src/basic/MapCombine.ts +105 -98
  13. package/src/basic/MapElement.ts +2 -1
  14. package/src/basic/MapEvent.ts +5 -0
  15. package/src/basic/Point.ts +1 -0
  16. package/src/basic/PointAggregation.tsx +22 -11
  17. package/src/basic/ReactPopup.tsx +3 -0
  18. package/src/basic/Tile3D.ts +0 -1
  19. package/src/cesium/CesiumEvent.ts +97 -84
  20. package/src/cesium/CesiumLayer.ts +365 -0
  21. package/src/cesium/CesiumMap.ts +363 -347
  22. package/src/cesium/CesiumModel.ts +64 -0
  23. package/src/cesium/CesiumPoint.ts +3 -0
  24. package/src/cesium/CesiumPolygon.ts +2 -0
  25. package/src/cesium/CesiumPolyline.ts +185 -47
  26. package/src/cesium/CesiumTile3D.ts +222 -225
  27. package/src/mine/MineEvent.ts +70 -46
  28. package/src/mine/MineMap.ts +217 -125
  29. package/src/mine/MinePoint.ts +7 -1
  30. package/src/mine/MinePolygon.ts +6 -1
  31. package/src/mine/MinePolyline.ts +6 -1
  32. package/src/openlayer/OpenlayerEvent.ts +109 -87
  33. package/src/openlayer/OpenlayerMap.ts +33 -6
  34. package/src/openlayer/OpenlayerPoint.ts +6 -1
  35. package/src/openlayer/OpenlayerPolygon.ts +6 -1
  36. package/src/openlayer/OpenlayerPolyline.ts +8 -1
  37. package/src/utils/color.ts +49 -0
  38. package/src/utils/index.ts +3 -0
  39. package/src/utils/points-viewer.ts +27 -1
  40. package/src/utils/turfjs-utils.ts +52 -0
  41. package/src/utils/types.d.ts +12 -0
@@ -1,98 +1,105 @@
1
- import { ContextMenu } from "./ContextMenu";
2
- import { MapElement } from "./MapElement";
3
- import { MapEvent } from "./MapEvent";
4
- import { Point, PointOption } from "./Point";
5
- import { PointAggregation, PointAggregationOption } from "./PointAggregation";
6
- import { Polygon, PolygonOption } from "./Polygon";
7
- import { Polyline, PolylineOption } from "./Polyline";
8
- import { ReactPoint, ReactPointOption, drawReactPoint } from "./ReactPoint";
9
- import { Tile3D, Tile3DOption } from "./Tile3D";
10
- import { type BasicBusiness, type BasicBusinessOption } from "./BasicBusiness";
11
-
12
- import { getBounds } from "../utils/points-viewer";
13
-
14
- export abstract class MapCombine {
15
- /* 用于挂载html元素的根节点 */
16
- readonly htmllayer = document.createElement("div");
17
- readonly elements = new Map<string, MapElement>();
18
- readonly event = new MapEvent();
19
- readonly menu: ContextMenu;
20
- abstract get cursor(): string;
21
- abstract set cursor(val: string);
22
-
23
- abstract get moveable(): boolean;
24
- abstract set moveable(val: boolean);
25
-
26
- abstract get center(): number[];
27
- abstract set center(val);
28
-
29
- abstract get zoom(): number;
30
- abstract set zoom(val);
31
-
32
- loadPromise: Promise<this> = Promise.resolve(this);
33
-
34
- bussinesses = new Map<string, BasicBusiness<BasicBusinessOption>>();
35
-
36
- /** 地图是否存在 */
37
- isAlive = true;
38
- constructor() {
39
- const { htmllayer } = this;
40
- htmllayer.style.zIndex = "1000";
41
- htmllayer.style.position = "absolute";
42
- htmllayer.style.width = "0";
43
- htmllayer.style.height = "0";
44
- htmllayer.style.left = "0";
45
- htmllayer.style.top = "0";
46
- this.menu = new ContextMenu(this);
47
- (window as any).map = this;
48
- setTimeout(() => {
49
- this.bussinesses.forEach((e) => e.show && e.onMapLoaded?.());
50
- }, 100);
51
- }
52
-
53
- createReactPoint<K>(option?: Partial<ReactPointOption<K>>): ReactPoint<K> {
54
- const e = new ReactPoint(this, option);
55
- drawReactPoint(this, e);
56
- return e;
57
- }
58
-
59
- /**
60
- * 设置地图视图
61
- * @param option 视图参数
62
- * @param time 设置时间会动画形式调整视图
63
- */
64
- abstract createPoint<K>(option?: Partial<PointOption<K>>): Point<K>;
65
-
66
- /** 聚合点 */
67
- abstract createPointAggregation(option?: PointAggregationOption): PointAggregation;
68
-
69
- abstract createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K>;
70
-
71
- abstract createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K>;
72
-
73
- abstract createTile3D(option?: Partial<Tile3DOption>): Tile3D;
74
-
75
- abstract canvasTgeography(p: number[]): number[];
76
-
77
- abstract geographyTcanvas(p: number[]): number[];
78
-
79
- /**
80
- * 所有点位移动至视口,调整位置及层级
81
- * @param points
82
- */
83
- abstract flyToViewer(points: number[][], opt?: Object): void;
84
-
85
- /**
86
- * 移动点位到中心点
87
- * @param points
88
- */
89
- abstract flyTo(point: number[], opt?: Object): void;
90
-
91
- getBounds = getBounds;
92
-
93
- destroy() {
94
- this.isAlive = false;
95
- this.htmllayer.remove();
96
- this.event.trigger("destroy");
97
- }
98
- }
1
+ import { ContextMenu } from "./ContextMenu";
2
+ import { MapElement } from "./MapElement";
3
+ import { MapEvent } from "./MapEvent";
4
+ import { Point, PointOption } from "./Point";
5
+ import { PointAggregation, PointAggregationOption } from "./PointAggregation";
6
+ import { Polygon, PolygonOption } from "./Polygon";
7
+ import { Polyline, PolylineOption } from "./Polyline";
8
+ import { ReactPoint, ReactPointOption, drawReactPoint } from "./ReactPoint";
9
+ import { Tile3D, Tile3DOption } from "./Tile3D";
10
+ import { BasicModel, BasicModelOption } from "./BasicModel";
11
+ import { type BasicBusiness, type BasicBusinessOption } from "./BasicBusiness";
12
+
13
+ import { getBounds } from "../utils/points-viewer";
14
+ import { Layer, LayerOption } from "./Layer";
15
+
16
+ export abstract class MapCombine {
17
+ /* 用于挂载html元素的根节点 */
18
+ readonly htmllayer = document.createElement("div");
19
+ readonly elements = new Map<string, MapElement>();
20
+ readonly event = new MapEvent();
21
+ readonly menu: ContextMenu;
22
+ abstract get cursor(): string;
23
+ abstract set cursor(val: string);
24
+
25
+ abstract get moveable(): boolean;
26
+ abstract set moveable(val: boolean);
27
+
28
+ abstract get center(): number[];
29
+ abstract set center(val);
30
+
31
+ abstract get zoom(): number;
32
+ abstract set zoom(val);
33
+
34
+ loadPromise: Promise<this> = Promise.resolve(this);
35
+
36
+ bussinesses = new Map<string, BasicBusiness<BasicBusinessOption>>();
37
+
38
+ /** 地图是否存在 */
39
+ isAlive = true;
40
+ constructor() {
41
+ const { htmllayer } = this;
42
+ htmllayer.style.zIndex = "1000";
43
+ htmllayer.style.position = "absolute";
44
+ htmllayer.style.width = "0";
45
+ htmllayer.style.height = "0";
46
+ htmllayer.style.left = "0";
47
+ htmllayer.style.top = "0";
48
+ this.menu = new ContextMenu(this);
49
+ (window as any).map = this;
50
+ setTimeout(() => {
51
+ this.bussinesses.forEach((e) => e.show && e.onMapLoaded?.());
52
+ }, 100);
53
+ }
54
+
55
+ createReactPoint<K>(option?: Partial<ReactPointOption<K>>): ReactPoint<K> {
56
+ const e = new ReactPoint(this, option);
57
+ drawReactPoint(this, e);
58
+ return e;
59
+ }
60
+
61
+ /**
62
+ * 设置地图视图
63
+ * @param option 视图参数
64
+ * @param time 设置时间会动画形式调整视图
65
+ */
66
+ abstract createPoint<K>(option?: Partial<PointOption<K>>): Point<K>;
67
+
68
+ /** 聚合点 */
69
+ abstract createPointAggregation(option?: PointAggregationOption): PointAggregation;
70
+
71
+ abstract createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K>;
72
+
73
+ abstract createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K>;
74
+
75
+ abstract createTile3D(option?: Partial<Tile3DOption>): Tile3D;
76
+
77
+ abstract createLayer(option?: Partial<LayerOption>): Layer;
78
+
79
+ /** 创建模型 */
80
+ createModel?(option?: Partial<BasicModelOption>): BasicModel;
81
+
82
+ abstract canvasTgeography(p: number[]): number[];
83
+
84
+ abstract geographyTcanvas(p: number[]): number[];
85
+
86
+ /**
87
+ * 所有点位移动至视口,调整位置及层级
88
+ * @param points
89
+ */
90
+ abstract flyToViewer(points: number[][], opt?: Object): void;
91
+
92
+ /**
93
+ * 移动点位到中心点
94
+ * @param points
95
+ */
96
+ abstract flyTo(point: number[], opt?: Object): void;
97
+
98
+ getBounds = getBounds;
99
+
100
+ destroy() {
101
+ this.isAlive = false;
102
+ this.htmllayer.remove();
103
+ this.event.trigger("destroy");
104
+ }
105
+ }
@@ -16,7 +16,8 @@ export abstract class MapElement<T extends MapElementOption = MapElementOption>
16
16
  this.map = map;
17
17
  this._option = option;
18
18
  if (map.elements.has(option.name)) {
19
- throw new Error(`已存在同名元素 ${option.name}`);
19
+ console.error(new Error(`已存在同名元素 ${option.name}`));
20
+ return;
20
21
  }
21
22
  map.elements.set(option.name, this);
22
23
  }
@@ -5,6 +5,11 @@ export class MapEvent extends Eventful<{
5
5
  "pointer-move": () => void;
6
6
  "pointer-up": () => void;
7
7
  "left-click": () => void;
8
+ /**
9
+ * 获取当前点击位置的所有元素
10
+ * @returns
11
+ */
12
+ "drill-pick": (elements) => void;
8
13
  "right-click": () => void;
9
14
  "double-click": () => void;
10
15
  "view-change": () => void;
@@ -10,6 +10,7 @@ export interface PointOption<K> extends MapElementOption {
10
10
  src: string;
11
11
  center: number[];
12
12
  size: number[];
13
+ /** 角度制 */
13
14
  rotation: number;
14
15
  editable: boolean;
15
16
  userdata: K;
@@ -1,24 +1,27 @@
1
1
  import { ReactElement } from "react";
2
- import { throttle } from "lodash";
3
2
 
4
3
  import { LATTOONE, LONTOONE } from "../utils/static";
5
4
  import { getUUID, setValue } from "../utils";
6
5
  import { MapCombine } from "../basic/MapCombine";
7
6
  import { ReactPoint } from "./ReactPoint";
8
7
 
8
+ export interface PointOption {
9
+ coordinates: number[];
10
+ props?: any;
11
+ }
9
12
  export interface PointAggregationOption {
10
13
  /** 组件名,唯一标识 */
11
14
  name: string;
12
15
  /** 组件是否显示 */
13
16
  show: boolean;
14
17
  /** 数据点 */
15
- datas: any[];
18
+ datas: PointOption[];
16
19
  /** 小于该值的等级下会聚合 */
17
20
  maxZoomLevel?: number;
18
21
  /** 值越大聚合点的间距越小 */
19
22
  density?: number;
20
23
  /** 点的展示元素 */
21
- element(datas: any[], p: number[]): ReactElement;
24
+ element(datas: PointOption[], p: number[]): ReactElement;
22
25
  /** 坐标处理回调 */
23
26
  location(lonlat: number[]): number[];
24
27
  }
@@ -28,10 +31,12 @@ class TileNode {
28
31
  private _element: any;
29
32
  marker: ReactPoint | undefined;
30
33
  private _map: MapCombine;
31
- constructor(map: MapCombine, location: number[], element: ReactElement) {
34
+ option;
35
+ constructor(map: MapCombine, location: number[], element: ReactElement, option = {}) {
32
36
  this._map = map;
33
37
  this._location = location;
34
38
  this._element = element;
39
+ this.option = option;
35
40
  }
36
41
  test(east: number, south: number, west: number, north: number) {
37
42
  if (
@@ -42,6 +47,7 @@ class TileNode {
42
47
  ) {
43
48
  if (!this.marker) {
44
49
  const option = {
50
+ ...this.option,
45
51
  show: true,
46
52
  name: getUUID(),
47
53
  element: () => this._element,
@@ -94,6 +100,8 @@ export class PointAggregation {
94
100
  this._onAfterViewChange = this.onAfterViewChange.bind(this);
95
101
  this.onAfterViewChange();
96
102
  event.on("view-moveEnd", this._onAfterViewChange);
103
+ // 初始化没有渲染,移动之后才渲染点位
104
+ this._update();
97
105
  }
98
106
 
99
107
  private _turnon(zoom: number) {
@@ -106,10 +114,12 @@ export class PointAggregation {
106
114
  const grid = new Map();
107
115
  const max = Math.pow(2, zoom) * (_option.density ?? 1);
108
116
  _option.datas.forEach((data) => {
109
- const location = typeof _option.location === "function" ? _option.location(data) : data;
117
+ const location =
118
+ typeof _option.location === "function" ? _option.location(data.coordinates) : data.coordinates;
110
119
  const x = Math.floor(LONTOONE.forward(location[0]) * max * 2);
111
120
  const y = Math.floor(LATTOONE.forward(location[1]) * max);
112
121
  const k = `${x}_${y}`;
122
+ // 对数据进行分组
113
123
  const group = grid.get(k);
114
124
  if (group) {
115
125
  group.push(data);
@@ -118,10 +128,11 @@ export class PointAggregation {
118
128
  }
119
129
  });
120
130
  // 处理聚合点坐标
121
- grid.forEach((e) => {
122
- const { lon, lat } = e.reduce(
131
+ grid.forEach((group) => {
132
+ const { lon, lat } = group?.reduce(
123
133
  (a, b) => {
124
- const loaction = typeof _option.location === "function" ? _option.location(b) : b;
134
+ const { coordinates } = b;
135
+ const loaction = typeof _option.location === "function" ? _option.location(coordinates) : coordinates;
125
136
  a.lon += loaction[0];
126
137
  a.lat += loaction[1];
127
138
  return a;
@@ -131,8 +142,8 @@ export class PointAggregation {
131
142
  cache.push(
132
143
  new TileNode(
133
144
  this._map,
134
- [lon / e.length, lat / e.length],
135
- _option.element(e, [lon / e.length, lat / e.length]),
145
+ [lon / group.length, lat / group.length],
146
+ _option.element(group, [lon / group.length, lat / group.length]),
136
147
  ),
137
148
  );
138
149
  });
@@ -140,7 +151,7 @@ export class PointAggregation {
140
151
  } else {
141
152
  const cache: TileNode[] = [];
142
153
  _option.datas.forEach((e) => {
143
- const location = _option.location(e);
154
+ const location = _option.location(e.coordinates);
144
155
  cache.push(new TileNode(this._map, location, _option.element([e], location)));
145
156
  });
146
157
  _cache.set(zoom, cache);
@@ -1,3 +1,6 @@
1
+ /**
2
+ * WARM: 后续将废弃该功能,使用 ReactPoint 替换!!!
3
+ */
1
4
  import { ReactElement } from "react";
2
5
  import ReactDOM from "react-dom";
3
6
  import { MapCombine } from "./MapCombine";
@@ -105,7 +105,6 @@ export class Tile3D extends MapElement<Tile3DOption> {
105
105
  console.info("TileD translate");
106
106
  }
107
107
  /** 基于本地的 ENU 坐标系的旋转,也就是垂直于地表向上为 Z,东为 X,北为 Y
108
- * https://blog.csdn.net/weixin_70945905/article/details/142419968
109
108
  * @param rx 绕X轴旋转的角度。单位:度
110
109
  * @param ry 绕Y轴旋转的角度。单位:度
111
110
  * @param rz 绕Z轴旋转的角度。单位:度
@@ -1,84 +1,97 @@
1
- import { CesiumMap } from "./CesiumMap";
2
-
3
- export function bindEvent(map: CesiumMap) {
4
- const { event, viewer, container } = map;
5
- viewer.camera.percentageChanged = 0.0001;
6
-
7
- viewer.scene.preRender.addEventListener(() => {
8
- event.trigger("view-preRender");
9
- });
10
-
11
- viewer.camera.changed.addEventListener(() => {
12
- event.trigger("view-change");
13
- });
14
- viewer.camera.moveEnd.addEventListener(() => {
15
- event.trigger("view-moveEnd");
16
- });
17
- const handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
18
- let target: any;
19
- handler.setInputAction((e: any) => {
20
- event.canvas = [e.endPosition.x, e.endPosition.y];
21
- event.geography = map.canvasTgeography(event.canvas);
22
- let _target: any = undefined;
23
- const res = viewer.scene.pick(e.endPosition);
24
- const a = res ? (res.id ? res.id : res.primitive ? res.primitive : undefined) : undefined;
25
-
26
- if (a && !a.silent) {
27
- _target = a;
28
- }
29
- if (_target !== target) {
30
- target?.onPointerOut?.();
31
- _target?.onPointerIn?.();
32
- target = _target;
33
- }
34
-
35
- if (target) {
36
- map.cursor = target.cursor;
37
- } else {
38
- map.cursor = "";
39
- }
40
- event.trigger("pointer-move");
41
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
42
-
43
- handler.setInputAction(() => {
44
- if (target) {
45
- target.onPointerDown?.();
46
- }
47
- event.trigger("pointer-down");
48
- }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
49
-
50
- // 解决 ReactPoint 点击事件无法触发的问题
51
- container.addEventListener("click", () => {
52
- event.trigger("left-click");
53
- });
54
- container.addEventListener("pointerup", () => {
55
- if (target) {
56
- target.onPointerUp?.();
57
- }
58
- event.trigger("pointer-up");
59
- });
60
-
61
- handler.setInputAction((e) => {
62
- if (target) {
63
- target.onLClick?.();
64
- }
65
- event.trigger("left-click");
66
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
67
- handler.setInputAction(() => {
68
- if (target) {
69
- target.onRClick?.();
70
- }
71
- event.trigger("right-click");
72
- }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
73
-
74
- handler.setInputAction(() => {
75
- if (target) {
76
- target.onDbClick?.();
77
- }
78
- event.trigger("double-click");
79
- }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
80
-
81
- event.on("destroy", () => {
82
- viewer.destroy();
83
- });
84
- }
1
+ import { CesiumMap } from "./CesiumMap";
2
+
3
+ export function bindEvent(map: CesiumMap) {
4
+ const { event, viewer, container } = map;
5
+ viewer.camera.percentageChanged = 0.0001;
6
+
7
+ viewer.scene.preRender.addEventListener(() => {
8
+ event.trigger("view-preRender");
9
+ map.bussinesses.forEach((e) => e.show && e.onFrame?.());
10
+ });
11
+
12
+ viewer.camera.changed.addEventListener(() => {
13
+ event.trigger("view-change");
14
+ });
15
+ viewer.camera.moveEnd.addEventListener(() => {
16
+ event.trigger("view-moveEnd");
17
+ });
18
+ const handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
19
+ let target: any;
20
+ handler.setInputAction((e: any) => {
21
+ event.canvas = [e.endPosition.x, e.endPosition.y];
22
+ event.geography = map.canvasTgeography(event.canvas);
23
+ let _target: any = undefined;
24
+ const res = viewer.scene.pick(e.endPosition);
25
+
26
+ const a = res ? (res.id ? res.id : res.primitive ? res.primitive : undefined) : undefined;
27
+
28
+ if (a && !a.silent) {
29
+ _target = a;
30
+ }
31
+ if (_target !== target) {
32
+ target?.onPointerOut?.();
33
+ _target?.onPointerIn?.();
34
+ target = _target;
35
+ }
36
+
37
+ if (target) {
38
+ map.cursor = target.cursor;
39
+ } else {
40
+ map.cursor = "";
41
+ }
42
+ event.trigger("pointer-move");
43
+ }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
44
+
45
+ handler.setInputAction(() => {
46
+ if (target) {
47
+ target.onPointerDown?.();
48
+ }
49
+ event.trigger("pointer-down");
50
+ }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
51
+
52
+ // 解决 ReactPoint 点击事件无法触发的问题
53
+ container.addEventListener("click", () => {
54
+ event.trigger("left-click");
55
+ });
56
+ container.addEventListener("pointerup", () => {
57
+ if (target) {
58
+ target.onPointerUp?.();
59
+ }
60
+ event.trigger("pointer-up");
61
+ });
62
+
63
+ handler.setInputAction((e) => {
64
+ if (target) {
65
+ target.onLClick?.();
66
+ }
67
+ event.trigger("left-click");
68
+
69
+ // 穿透拾取,获取点击位置的所有对象
70
+ const pickedObjects = viewer.scene.drillPick(e.position);
71
+ if (pickedObjects && pickedObjects.length > 0) {
72
+ event.trigger("drill-pick", {
73
+ event: e,
74
+ pickedTargets: pickedObjects.map((it) => it.id?.target),
75
+ sourceObjects: pickedObjects,
76
+ position: e.position,
77
+ });
78
+ }
79
+ }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
80
+ handler.setInputAction(() => {
81
+ if (target) {
82
+ target.onRClick?.();
83
+ }
84
+ event.trigger("right-click");
85
+ }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
86
+
87
+ handler.setInputAction(() => {
88
+ if (target) {
89
+ target.onDbClick?.();
90
+ }
91
+ event.trigger("double-click");
92
+ }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
93
+
94
+ event.on("destroy", () => {
95
+ viewer.destroy();
96
+ });
97
+ }