@hzab/map-combine 0.0.1

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 (40) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +67 -0
  3. package/package.json +56 -0
  4. package/src/assets/Thumbs.db +0 -0
  5. package/src/assets/add.png +0 -0
  6. package/src/assets/point.png +0 -0
  7. package/src/basic/ContextMenu.tsx +49 -0
  8. package/src/basic/MapCombine.ts +67 -0
  9. package/src/basic/MapElement.ts +32 -0
  10. package/src/basic/MapEvent.ts +18 -0
  11. package/src/basic/Point.ts +176 -0
  12. package/src/basic/PointAggregation.tsx +189 -0
  13. package/src/basic/Polygon.ts +180 -0
  14. package/src/basic/Polyline.ts +194 -0
  15. package/src/basic/ReactPoint.tsx +357 -0
  16. package/src/basic/ReactPopup.tsx +34 -0
  17. package/src/basic/Tile3D.ts +99 -0
  18. package/src/cesium/CesiumEvent.ts +86 -0
  19. package/src/cesium/CesiumMap.ts +167 -0
  20. package/src/cesium/CesiumPoint.ts +137 -0
  21. package/src/cesium/CesiumPolygon.ts +368 -0
  22. package/src/cesium/CesiumPolyline.ts +382 -0
  23. package/src/cesium/CesiumTile3D.ts +100 -0
  24. package/src/mine/MineEvent.ts +29 -0
  25. package/src/mine/MineMap.ts +90 -0
  26. package/src/mine/MinePoint.ts +139 -0
  27. package/src/mine/MinePolygon.ts +388 -0
  28. package/src/mine/MinePolyline.ts +359 -0
  29. package/src/mine/MineTile3D.ts +32 -0
  30. package/src/openlayer/OpenlayerEvent.ts +75 -0
  31. package/src/openlayer/OpenlayerMap.ts +149 -0
  32. package/src/openlayer/OpenlayerPoint.ts +177 -0
  33. package/src/openlayer/OpenlayerPolygon.ts +300 -0
  34. package/src/openlayer/OpenlayerPolyline.ts +289 -0
  35. package/src/utils/Image.ts +46 -0
  36. package/src/utils/PolygonEditor.ts +159 -0
  37. package/src/utils/PolylineEditor.ts +149 -0
  38. package/src/utils/Projection.ts +46 -0
  39. package/src/utils/index.ts +131 -0
  40. package/src/utils/static.ts +7 -0
@@ -0,0 +1,382 @@
1
+ import { Polyline } from "../basic/Polyline";
2
+ import { ChainNode, PolylineEditor } from "../utils/PolylineEditor";
3
+ import { CesiumMap } from "./CesiumMap";
4
+ import iconPoint from '../assets/point.png'
5
+ import iconAdd from '../assets/add.png'
6
+
7
+
8
+
9
+ const source = `
10
+ czm_material czm_getMaterial(czm_materialInput materialInput){
11
+ czm_material material = czm_getDefaultMaterial(materialInput);
12
+ vec2 st =vec2(fract(materialInput.st.x*k), materialInput.st.y);
13
+ vec4 colorImage = texture2D(image, vec2(fract((st.s - speed*czm_frameNumber*0.005)), st.t));
14
+ material.alpha = colorImage.a;
15
+ material.diffuse = colorImage.xyz;
16
+ return material;
17
+ }`
18
+
19
+
20
+ function createLineMaterial(e: Polyline<unknown>) {
21
+
22
+ if (e.icon || e.flow) {
23
+ const material = new Cesium.Material({
24
+ fabric: {
25
+ type: 'PolylineFlowIcon',
26
+ uniforms: {
27
+ image: e.icon || 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAO1JREFUWEftl8EJwkAQRWd7iAFTgZZgJD0YtJOQu/eQToyxh2AsIVbgQdKDsoEBT0L+H9hL9j7smzd/l12XNPFZAi63AAQ3sL6uPgEjIA4FSKO99OOdZocAim0pxaaUU5fTEBTAY+zl2B0oCxCA13/J2mlj1gIE4DduspvsolTqZyX1UMEWYAC1wI4BBvi1wIyBAtDTwFigACzCSAFYhJEGYMNIA2gOvI2kjWcfRzMA9D6gAV75e+oa6d7XUQCqH+2eBtDugwCw4dO0wiOw0E+NgA0fZcCqe9iA1VsABvCFQR+ls+/bPwXL12wx8AUhFKmRjCCoogAAAABJRU5ErkJggg==',
28
+ speed: e.flow ? 10 : 0,
29
+ k: 20
30
+ },
31
+ source,
32
+ },
33
+ translucent() {
34
+ return true
35
+ }
36
+ })
37
+ return material
38
+ } else {
39
+ return e.dash
40
+ ? Cesium.Material.fromType("PolylineDash", {
41
+ color: Cesium.Color.fromCssColorString(e.stroke),
42
+ dashLength: 10,
43
+ })
44
+ : Cesium.Material.fromType("Color", {
45
+ color: Cesium.Color.fromCssColorString(e.stroke),
46
+ });
47
+ }
48
+
49
+
50
+
51
+ }
52
+
53
+ function cartesian3Tgeography(e: any): number[] {
54
+ const r = Cesium.Cartographic.fromCartesian(e)
55
+ return [Cesium.Math.toDegrees(r.longitude), Cesium.Math.toDegrees(r.latitude), r.height]
56
+ }
57
+
58
+ function geographyTcartesian3(e: number[]) {
59
+ return Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
60
+ }
61
+
62
+ const eyeOffset = new Cesium.Cartesian3(0, 0, -1)
63
+
64
+ export function drawPolyline(map: CesiumMap, polyline: Polyline<unknown>) {
65
+ const { event, viewer } = map
66
+ let status = 0
67
+ let skip = 0
68
+
69
+
70
+ let material = createLineMaterial(polyline)
71
+ const appearance = new Cesium.PolylineMaterialAppearance({
72
+ material,
73
+ })
74
+ const markers = viewer.scene.primitives.add(
75
+ new Cesium.BillboardCollection({
76
+ show: polyline.show
77
+ })
78
+ );
79
+
80
+ const _event = {
81
+ cursor: polyline.cursor,
82
+ silent: polyline.silent,
83
+ onPointerIn() {
84
+ status == 2 && polyline.event.trigger('pointer-in')
85
+ },
86
+ onPointerOut() {
87
+ status == 2 && polyline.event.trigger('pointer-out')
88
+ },
89
+ onLClick() {
90
+ status == 2 && polyline.event.trigger('left-click')
91
+ },
92
+ onRClick() {
93
+ if (polyline.menu) {
94
+ map.menu.show(polyline.menu);
95
+ }
96
+ status == 2 && polyline.event.trigger('right-click')
97
+ },
98
+ onDbClick() {
99
+ polyline.editable && status == 2 && setTimeout(() => {
100
+ polyline.event.trigger('start-edit')
101
+ });
102
+ }
103
+ }
104
+
105
+ let primitive: any
106
+ let cache: any[]
107
+ let activeNode: ChainNode<any, any>
108
+ if (polyline.coordinates) {
109
+ status = 2
110
+ cache = polyline.coordinates.map((e) =>
111
+ Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
112
+ )
113
+ primitive = new Cesium.Primitive({
114
+ show: polyline.show,
115
+ geometryInstances: new Cesium.GeometryInstance({
116
+ geometry: new Cesium.PolylineGeometry({
117
+ positions: cache,
118
+ width: polyline.lineWidth,
119
+ vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
120
+ }),
121
+ id: _event,
122
+ }),
123
+ appearance,
124
+
125
+ });
126
+ viewer.scene.primitives.add(primitive);
127
+ }
128
+
129
+
130
+
131
+ const editor = new PolylineEditor<any, any>()
132
+
133
+ editor.on('create-node', (n) => {
134
+ if (n.type == 'node') {
135
+ const feature = markers.add({
136
+ position: new Cesium.Cartesian3(...n.position),
137
+ width: 16,
138
+ height: 16,
139
+ image: iconPoint,
140
+ eyeOffset,
141
+ id: {
142
+ cursor: 'move',
143
+ onPointerDown() {
144
+ status = 4
145
+ activeNode = n
146
+ map.moveable = false
147
+ },
148
+ onPointerUp() {
149
+ if (status == 4) {
150
+ status = 3
151
+ map.moveable = true
152
+ activeNode = undefined
153
+ }
154
+ },
155
+ onDbClick() {
156
+ n.destroy()
157
+ cache = editor.getCoordinates().map(e => new Cesium.Cartesian3(...e))
158
+ initPrimitive()
159
+
160
+ }
161
+ }
162
+ })
163
+ n.shape = feature
164
+ } else {
165
+ const feature = markers.add({
166
+ position: new Cesium.Cartesian3(...n.position),
167
+ width: 16,
168
+ height: 16,
169
+ image: iconAdd,
170
+ eyeOffset,
171
+ id: {
172
+ cursor: 'pointer',
173
+ onPointerDown() {
174
+ status = 4
175
+ activeNode = n.insert()
176
+ map.cursor = 'move'
177
+ map.moveable = false
178
+ },
179
+ }
180
+ })
181
+ n.shape = feature
182
+ }
183
+ })
184
+
185
+ editor.on('update-node', (n) => {
186
+ n.shape.position = new Cesium.Cartesian3(...n.position)
187
+ })
188
+ editor.on('remove-node', (n) => {
189
+ markers.remove(n.shape)
190
+ })
191
+ editor.on('empty', () => {
192
+ // markers.removeAll()
193
+ viewer.scene.primitives.remove(primitive);
194
+ status = 0
195
+ })
196
+
197
+ const onUpdate = (e: Set<string>) => {
198
+ if (skip) {
199
+ skip--
200
+ return
201
+ }
202
+ const keys = new Set()
203
+ e.forEach(key => {
204
+ switch (key) {
205
+ case 'show':
206
+ if (primitive) {
207
+ primitive.show = polyline.show
208
+ markers.show = polyline.show
209
+ }
210
+ break;
211
+ case 'editable':
212
+ break;
213
+ case 'coordinates':
214
+ keys.add('Primitive')
215
+ break;
216
+ case 'lineWidth':
217
+ keys.add('Primitive')
218
+ break;
219
+ case 'icon':
220
+ keys.add('Material')
221
+ break;
222
+ case 'flow':
223
+ keys.add('Material')
224
+ break;
225
+ case 'stroke':
226
+ material.uniforms.color = Cesium.Color.fromCssColorString(polyline.stroke);
227
+ break;
228
+ case 'dash':
229
+ keys.add('Material')
230
+
231
+ break;
232
+ default:
233
+ throw new Error(`${key} 还不支持修改`)
234
+ }
235
+ })
236
+ keys.forEach(key => {
237
+ switch (key) {
238
+ case 'Primitive':
239
+ if (polyline.coordinates) {
240
+ cache = polyline.coordinates.map(e => geographyTcartesian3(e))
241
+ } else {
242
+ cache = []
243
+ markers.removeAll()
244
+ status = 0
245
+ }
246
+
247
+ initPrimitive()
248
+
249
+ break;
250
+ case 'Material':
251
+ material = createLineMaterial(polyline)
252
+ appearance.material = material
253
+ break
254
+ }
255
+ })
256
+ }
257
+
258
+ const onStartEdit = () => {
259
+
260
+ switch (status) {
261
+ case 2:
262
+ status = 3
263
+ editor.setCoordinates(cache.map(e => [e.x, e.y, e.z]))
264
+ break
265
+ }
266
+ }
267
+ let temp: any
268
+ const onMouseMove = () => {
269
+ const e = event.geography
270
+
271
+ switch (status) {
272
+ case 1:
273
+ cache.pop()
274
+ cache.push(Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2]))
275
+ initPrimitive()
276
+ break
277
+ case 4:
278
+ temp = Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
279
+ activeNode.update([temp.x, temp.y, temp.z])
280
+ cache = editor.getCoordinates().map(e => new Cesium.Cartesian3(...e))
281
+ initPrimitive()
282
+ break;
283
+ }
284
+ }
285
+
286
+ const onClick = () => {
287
+ const e = event.geography
288
+ switch (status) {
289
+ case 0:
290
+ status = 1
291
+ cache = [Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])]
292
+ cache.push(cache[0])
293
+ initPrimitive()
294
+ break;
295
+ case 1:
296
+ cache.push(cache[cache.length - 1])
297
+ // geometry.setCoordinates(cache)
298
+ initPrimitive()
299
+ break
300
+ }
301
+ }
302
+
303
+
304
+ const initPrimitive = () => {
305
+ primitive && viewer.scene.primitives.remove(primitive);
306
+ if (cache.length > 1) {
307
+ const geometry = Cesium.PolylineGeometry.createGeometry(new Cesium.PolylineGeometry({
308
+ positions: cache,
309
+ width: polyline.lineWidth,
310
+ vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
311
+ }))
312
+ if (geometry) {
313
+ primitive = new Cesium.Primitive({
314
+ show: polyline.show,
315
+ asynchronous: false,
316
+ geometryInstances: new Cesium.GeometryInstance({
317
+ geometry,
318
+ id: _event,
319
+ }),
320
+ appearance,
321
+ });
322
+ viewer.scene.primitives.add(primitive);
323
+ } else {
324
+ primitive = undefined
325
+ }
326
+
327
+ } else {
328
+ primitive = undefined
329
+ }
330
+
331
+ }
332
+
333
+ const onDbClick = () => {
334
+ switch (status) {
335
+ case 1:
336
+ status = 2
337
+ skip++
338
+ cache.pop()
339
+ cache.pop()
340
+ // geometry.setCoordinates(cache)
341
+ polyline.coordinates = cache.map(e => cartesian3Tgeography(e))
342
+ polyline.event.trigger('data-loaded')
343
+ polyline.event.trigger('pointer-in')
344
+ break
345
+ case 3:
346
+ polyline.stopEdit()
347
+ break
348
+ }
349
+ }
350
+
351
+ const onStopEdit = () => {
352
+ if (status == 3) {
353
+ status = 2
354
+ skip++
355
+ polyline.coordinates = editor.getCoordinates().map(e => cartesian3Tgeography(e))
356
+ editor.setCoordinates([])
357
+ polyline.event.trigger('pointer-in')
358
+ }
359
+ }
360
+
361
+ const onDestroy = () => {
362
+ primitive && viewer.scene.primitives.remove(primitive);
363
+ viewer.scene.primitives.remove(markers)
364
+ event.off('pointer-move', onMouseMove)
365
+ event.off('left-click', onClick)
366
+ event.off('double-click', onDbClick)
367
+ polyline.event.off('update', onUpdate)
368
+ polyline.event.off('start-edit', onStartEdit)
369
+ polyline.event.off('stop-edit', onStopEdit)
370
+ polyline.event.off('destroy', onDestroy)
371
+
372
+ }
373
+
374
+ event.on('pointer-move', onMouseMove)
375
+ event.on('left-click', onClick)
376
+ event.on('double-click', onDbClick)
377
+ polyline.event.on('update', onUpdate)
378
+ polyline.event.on('start-edit', onStartEdit)
379
+ polyline.event.on('stop-edit', onStopEdit)
380
+ polyline.event.on('destroy', onDestroy)
381
+
382
+ }
@@ -0,0 +1,100 @@
1
+ import { Tile3D } from "../basic/Tile3D";
2
+ import { CesiumMap } from "./CesiumMap";
3
+
4
+ function create3DTile(tile3d: Tile3D) {
5
+ const tileset = new Cesium.Cesium3DTileset({
6
+ show: tile3d.show,
7
+ url: tile3d.src,
8
+ });
9
+ tileset.maximumScreenSpaceError = 18 * (1 - tile3d.quality)
10
+ tileset.readyPromise
11
+ return tileset
12
+ }
13
+
14
+ function updateMatrix(tileset: any, tile3d: Tile3D, origin: any) {
15
+
16
+ if (tile3d.coordinates) {
17
+ // const center = Cesium.Matrix4.multiplyByPoint(tileset.modelMatrix, tileset.boundingSphere.center, new Cesium.Cartesian3())
18
+ // const cartographic = Cesium.Cartographic.fromCartesian(center)
19
+ // console.log(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude), cartographic.height);
20
+ // cartographic.height = 0
21
+ // const transform = Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude));
22
+ // const res = Cesium.Matrix4.inverse(transform, new Cesium.Matrix4());
23
+ // console.log(inverse);
24
+
25
+ const target = Cesium.Cartesian3.fromDegrees(...tile3d.coordinates)
26
+ tileset.modelMatrix = Cesium.Matrix4.fromTranslation(Cesium.Cartesian3.subtract(target, origin, target));
27
+ } else {
28
+ tileset.modelMatrix = Cesium.Matrix4.IDENTITY
29
+ }
30
+
31
+
32
+ }
33
+
34
+ export function drawTile3D(map: CesiumMap, tile3d: Tile3D) {
35
+ const { viewer } = map
36
+ let tileset = create3DTile(tile3d)
37
+ let origin
38
+ viewer.scene.primitives.add(tileset).readyPromise.then(() => {
39
+ const cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center)
40
+ origin = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude)
41
+ updateMatrix(tileset, tile3d, origin)
42
+ tile3d.event.trigger('ready');
43
+
44
+ // (window as any).getCenter = () => {
45
+ // const center = tileset.boundingSphere.center
46
+ // const cartographic = Cesium.Cartographic.fromCartesian(center)
47
+ // console.log(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude), cartographic.height);
48
+ // }
49
+
50
+ });
51
+
52
+ const onUpdate = (e: Set<string>) => {
53
+
54
+ e.forEach(key => {
55
+ switch (key) {
56
+ case 'show':
57
+ tileset.show = tile3d.show
58
+ break;
59
+ case 'src':
60
+ viewer.scene.primitives.remove(tileset)
61
+ tileset = create3DTile(tile3d)
62
+ viewer.scene.primitives.add(tileset).readyPromise.then(() => {
63
+ const cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center)
64
+ origin = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude)
65
+ updateMatrix(tileset, tile3d, origin)
66
+ tile3d.event.trigger('ready');
67
+
68
+ });
69
+ break;
70
+ case 'coordinates':
71
+ if (!e.has('src')) {
72
+ updateMatrix(tileset, tile3d, origin)
73
+ }
74
+ break;
75
+
76
+ case 'quality':
77
+ tileset.maximumScreenSpaceError = 18 * (1 - tile3d.quality)
78
+ break;
79
+ default:
80
+ throw new Error(`${key} 还不支持修改`)
81
+ }
82
+ })
83
+
84
+
85
+ }
86
+ const onFocus = () => {
87
+ viewer.flyTo(tileset);
88
+ }
89
+
90
+ const onDestroy = () => {
91
+ viewer.scene.primitives.remove(tileset)
92
+ tile3d.event.off('update', onUpdate)
93
+ tile3d.event.off('focus', onFocus)
94
+ tile3d.event.off('destroy', onDestroy)
95
+ }
96
+ tile3d.event.on('update', onUpdate)
97
+ tile3d.event.on('focus', onFocus)
98
+ tile3d.event.on('destroy', onDestroy)
99
+
100
+ }
@@ -0,0 +1,29 @@
1
+ import { MineMap } from "./MineMap";
2
+
3
+
4
+ export function bindEvent(map: MineMap) {
5
+ const { event, viewer, container } = map
6
+ viewer.on('mousemove', (e) => {
7
+ event.canvas = [e.point.x, e.point.y]
8
+ event.geography = [e.lngLat.lng, e.lngLat.lat, 0]
9
+ })
10
+ container.addEventListener('pointermove', (e) => {
11
+ event.trigger('pointer-move')
12
+ })
13
+ viewer.on('move', () => {
14
+ event.trigger('view-change')
15
+ })
16
+ viewer.on('mouseup', () => {
17
+ event.trigger('pointer-up')
18
+ })
19
+ viewer.on('mousedown', () => {
20
+ event.trigger('pointer-down')
21
+ })
22
+ viewer.on('click', () => {
23
+ event.trigger('left-click')
24
+ })
25
+
26
+ event.on('destroy', () => {
27
+ this.viewer.remove()
28
+ })
29
+ }
@@ -0,0 +1,90 @@
1
+
2
+ import { MapCombine } from "../basic/MapCombine";
3
+ import { PointOption, Point } from "../basic/Point";
4
+ import { PolygonOption, Polygon } from "../basic/Polygon";
5
+ import { PolylineOption, Polyline } from "../basic/Polyline";
6
+ import { Tile3DOption, Tile3D } from "../basic/Tile3D";
7
+ import { bindEvent } from "./MineEvent";
8
+ import { drawPoint } from "./MinePoint";
9
+ import { drawPolygon } from "./MinePolygon";
10
+ import { drawPolyline } from "./MinePolyline";
11
+ import { drawTile3D } from "./MineTile3D";
12
+
13
+ export class MineMap extends MapCombine {
14
+
15
+ viewer: any;
16
+ container: HTMLDivElement;
17
+
18
+ get cursor(): string {
19
+ return this.viewer.getCursor()
20
+ }
21
+ set cursor(val: string) {
22
+ this.viewer.setCursor(val)
23
+ }
24
+
25
+ get moveable(): boolean {
26
+ return this.viewer.dragPan.isEnabled();
27
+ }
28
+ set moveable(val: boolean) {
29
+ val ? this.viewer.dragPan.enable() : this.viewer.dragPan.disable();
30
+ }
31
+ get center(): number[] {
32
+ const p = this.viewer.getCenter()
33
+ return [p.lng, p.lat]
34
+ }
35
+ set center(val: number[]) {
36
+ this.viewer.setCenter(val)
37
+ }
38
+
39
+ get zoom(): number {
40
+ return this.viewer.getZoom()
41
+ }
42
+ set zoom(val: number) {
43
+ this.viewer.setZoom(val)
44
+ }
45
+
46
+ constructor(viewer: any) {
47
+ super()
48
+ this.viewer = viewer
49
+ this.container = viewer.getContainer()
50
+ this.container.children.item(1).appendChild(this.htmllayer)
51
+ this.cursor = 'default'
52
+ bindEvent(this)
53
+ }
54
+
55
+
56
+
57
+ canvasTgeography(p: number[]): number[] {
58
+ const res = this.viewer.getTDSpaceCoord({ point: p, disableLayer: true });
59
+ return [res.lngLat.lng, res.lngLat.lat, 0];
60
+ }
61
+ geographyTcanvas(p: number[]): number[] {
62
+ const res = this.viewer.project([p[0], p[1]], p[2] ?? 0);
63
+ return [res.x, res.y];
64
+ }
65
+
66
+ createPoint<K>(option?: Partial<PointOption<K>>): Point<K> {
67
+ const e = new Point(this, option)
68
+ drawPoint(this, e)
69
+ return e
70
+ }
71
+
72
+ createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K> {
73
+ const e = new Polyline(this, option)
74
+ drawPolyline(this, e)
75
+ return e
76
+ }
77
+ createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K> {
78
+ const e = new Polygon(this, option)
79
+ drawPolygon(this, e)
80
+ return e
81
+ }
82
+
83
+ createTile3D(option?: Partial<Tile3DOption>): Tile3D {
84
+ const e = new Tile3D(this, option)
85
+ drawTile3D(this, e)
86
+ return e
87
+ }
88
+
89
+ }
90
+