@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,368 @@
1
+ import { Polygon } from "../basic/Polygon";
2
+ import { ChainNode, PolygonEditor } from "../utils/PolygonEditor";
3
+ import { CesiumMap } from "./CesiumMap";
4
+ import iconPoint from '../assets/point.png'
5
+ import iconAdd from '../assets/add.png'
6
+ import { dealColor } from "../utils";
7
+
8
+ function createLineMaterial(e: Polygon<unknown>) {
9
+ return e.dash
10
+ ? Cesium.Material.fromType("PolylineDash", {
11
+ color: Cesium.Color.fromCssColorString(e.stroke),
12
+ dashLength: 10,
13
+ })
14
+ : Cesium.Material.fromType("Color", {
15
+ color: Cesium.Color.fromCssColorString(e.stroke),
16
+ });
17
+ }
18
+
19
+ function createFillMaterial(e: Polygon<unknown>) {
20
+ const [color, opacity] = dealColor(e.fill);
21
+ return Cesium.Material.fromType("Color", {
22
+ color: Cesium.Color.fromCssColorString(color).withAlpha(opacity),
23
+ })
24
+ }
25
+
26
+ function cartesian3Tgeography(e: any): number[] {
27
+ const r = Cesium.Cartographic.fromCartesian(e)
28
+ return [Cesium.Math.toDegrees(r.longitude), Cesium.Math.toDegrees(r.latitude), r.height]
29
+ }
30
+
31
+ function geographyTcartesian3(e: number[]) {
32
+ return Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
33
+ }
34
+
35
+ const eyeOffset = new Cesium.Cartesian3(0, 0, -1)
36
+
37
+ export function drawPolygon(map: CesiumMap, polygon: Polygon<unknown>) {
38
+ const { event, viewer } = map
39
+ let status = 0
40
+ let skip = 0
41
+
42
+ let cache: any[]
43
+ let activeNode: ChainNode<any, any>
44
+
45
+ let primitiveFill: any
46
+ let primitiveLine: any
47
+
48
+ const collection = viewer.scene.primitives.add(new Cesium.PrimitiveCollection({
49
+ show: polygon.show,
50
+ destroyPrimitives: true,
51
+ }))
52
+
53
+ const appearanceFill = new Cesium.MaterialAppearance({
54
+ material: createFillMaterial(polygon)
55
+ });
56
+
57
+ const appearanceLine = new Cesium.PolylineMaterialAppearance({
58
+ material: createLineMaterial(polygon),
59
+ })
60
+
61
+ const markers = collection.add(
62
+ new Cesium.BillboardCollection({
63
+ show: polygon.show
64
+ })
65
+ );
66
+
67
+ const _event = {
68
+ cursor: polygon.cursor,
69
+ silent: polygon.silent,
70
+ onPointerIn() {
71
+ status == 2 && polygon.event.trigger('pointer-in')
72
+ },
73
+ onPointerOut() {
74
+ status == 2 && polygon.event.trigger('pointer-out')
75
+ },
76
+ onLClick() {
77
+ status == 2 && polygon.event.trigger('left-click')
78
+ },
79
+ onRClick() {
80
+ if (polygon.menu) {
81
+ map.menu.show(polygon.menu);
82
+ }
83
+ status == 2 && polygon.event.trigger('right-click')
84
+ },
85
+ onDbClick() {
86
+ polygon.editable && status == 2 && setTimeout(() => {
87
+ polygon.event.trigger('start-edit')
88
+ });
89
+ }
90
+ }
91
+
92
+ const initPrimitive = () => {
93
+ primitiveFill && collection.remove(primitiveFill);
94
+ primitiveLine && collection.remove(primitiveLine);
95
+
96
+ if (cache.length > 2) {
97
+ const geometry = Cesium.PolygonGeometry.createGeometry(new Cesium.PolygonGeometry({
98
+ polygonHierarchy: new Cesium.PolygonHierarchy(cache),
99
+ }));
100
+
101
+ if (geometry) {
102
+ primitiveFill = new Cesium.Primitive({
103
+ asynchronous: false,
104
+ geometryInstances: new Cesium.GeometryInstance({
105
+ geometry: geometry,
106
+ id: _event,
107
+ }),
108
+ appearance: appearanceFill
109
+ });
110
+ collection.add(primitiveFill)
111
+ } else {
112
+ primitiveFill = undefined
113
+ }
114
+ } else {
115
+ primitiveFill = undefined
116
+ }
117
+
118
+ if (cache.length > 1) {
119
+
120
+ const geometry = Cesium.PolylineGeometry.createGeometry(new Cesium.PolylineGeometry({
121
+ positions: [...cache, cache[0]],
122
+ width: polygon.lineWidth,
123
+ vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
124
+ }))
125
+ if (geometry) {
126
+ primitiveLine = new Cesium.Primitive({
127
+ show: polygon.show,
128
+ asynchronous: false,
129
+ geometryInstances: new Cesium.GeometryInstance({
130
+ geometry,
131
+
132
+ }),
133
+ appearance: appearanceLine,
134
+ });
135
+ collection.add(primitiveLine);
136
+ } else {
137
+ primitiveLine = undefined
138
+ }
139
+
140
+ } else {
141
+ primitiveLine = undefined
142
+ }
143
+
144
+ }
145
+
146
+
147
+ if (polygon.coordinates) {
148
+ status = 2
149
+ cache = polygon.coordinates.map((e) =>
150
+ Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
151
+ )
152
+ initPrimitive()
153
+ }
154
+
155
+
156
+
157
+ const editor = new PolygonEditor<any, any>()
158
+
159
+ editor.on('create-node', (n) => {
160
+ if (n.type == 'node') {
161
+ const feature = markers.add({
162
+ position: new Cesium.Cartesian3(...n.position),
163
+ width: 16,
164
+ height: 16,
165
+ image: iconPoint,
166
+ eyeOffset,
167
+ id: {
168
+ cursor: 'move',
169
+ onPointerDown() {
170
+ status = 4
171
+ activeNode = n
172
+ map.moveable = false
173
+ },
174
+ onPointerUp() {
175
+ if (status == 4) {
176
+ status = 3
177
+ map.moveable = true
178
+ activeNode = undefined
179
+ }
180
+ },
181
+ onDbClick() {
182
+ n.destroy()
183
+ cache = editor.getCoordinates().map(e => new Cesium.Cartesian3(...e))
184
+ initPrimitive()
185
+
186
+ }
187
+ }
188
+ })
189
+ n.shape = feature
190
+ } else {
191
+ const feature = markers.add({
192
+ position: new Cesium.Cartesian3(...n.position),
193
+ width: 16,
194
+ height: 16,
195
+ image: iconAdd,
196
+ eyeOffset,
197
+ id: {
198
+ cursor: 'pointer',
199
+ onPointerDown() {
200
+ status = 4
201
+ activeNode = n.insert()
202
+ map.cursor = 'move'
203
+ map.moveable = false
204
+ },
205
+ }
206
+ })
207
+ n.shape = feature
208
+ }
209
+ })
210
+
211
+ editor.on('update-node', (n) => {
212
+ n.shape.position = new Cesium.Cartesian3(...n.position)
213
+ })
214
+ editor.on('remove-node', (n) => {
215
+ markers.remove(n.shape)
216
+ })
217
+ editor.on('empty', () => {
218
+ // markers.removeAll()
219
+ collection.remove(primitiveLine);
220
+ collection.remove(primitiveFill);
221
+ status = 0
222
+ })
223
+
224
+ const onUpdate = (e: Set<string>) => {
225
+ if (skip) {
226
+ skip--
227
+ return
228
+ }
229
+ const keys = new Set()
230
+ e.forEach(key => {
231
+ switch (key) {
232
+ case 'show':
233
+ collection.show = polygon.show
234
+ break;
235
+ case 'editable':
236
+ break;
237
+ case 'coordinates':
238
+ keys.add('Primitive')
239
+ break;
240
+ case 'lineWidth':
241
+ keys.add('Primitive')
242
+ break;
243
+ case 'stroke':
244
+ appearanceLine.material.uniforms.color = Cesium.Color.fromCssColorString(polygon.stroke);
245
+ break;
246
+ case 'dash':
247
+ appearanceLine.material = createLineMaterial(polygon)
248
+ break;
249
+ default:
250
+ throw new Error(`${key} 还不支持修改`)
251
+ }
252
+ })
253
+ keys.forEach(key => {
254
+ switch (key) {
255
+ case 'Primitive':
256
+ if (polygon.coordinates) {
257
+ cache = polygon.coordinates.map(e => geographyTcartesian3(e))
258
+ } else {
259
+ cache = []
260
+ markers.removeAll()
261
+ status = 0
262
+ }
263
+
264
+ initPrimitive()
265
+
266
+ break;
267
+ }
268
+ })
269
+ }
270
+
271
+ const onStartEdit = () => {
272
+
273
+ switch (status) {
274
+ case 2:
275
+ status = 3
276
+ editor.setCoordinates(cache.map(e => [e.x, e.y, e.z]))
277
+ break
278
+ }
279
+ }
280
+ let temp: any
281
+ const onMouseMove = () => {
282
+ const e = event.geography
283
+
284
+ switch (status) {
285
+ case 1:
286
+ cache.pop()
287
+ cache.push(Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2]))
288
+ initPrimitive()
289
+ break
290
+ case 4:
291
+ temp = Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])
292
+ activeNode.update([temp.x, temp.y, temp.z])
293
+ cache = editor.getCoordinates().map(e => new Cesium.Cartesian3(...e))
294
+ initPrimitive()
295
+ break;
296
+ }
297
+ }
298
+
299
+ const onClick = () => {
300
+ const e = event.geography
301
+ switch (status) {
302
+ case 0:
303
+ status = 1
304
+ cache = [Cesium.Cartesian3.fromDegrees(e[0], e[1], e[2])]
305
+ cache.push(cache[0])
306
+ initPrimitive()
307
+ break;
308
+ case 1:
309
+ cache.push(cache[cache.length - 1])
310
+ // geometry.setCoordinates(cache)
311
+ initPrimitive()
312
+ break
313
+ }
314
+ }
315
+
316
+
317
+
318
+
319
+ const onDbClick = () => {
320
+ switch (status) {
321
+ case 1:
322
+ status = 2
323
+ skip++
324
+ cache.pop()
325
+ cache.pop()
326
+ // geometry.setCoordinates(cache)
327
+ polygon.coordinates = cache.map(e => cartesian3Tgeography(e))
328
+ polygon.event.trigger('data-loaded')
329
+ polygon.event.trigger('pointer-in')
330
+ break
331
+ case 3:
332
+ polygon.event.trigger('stop-edit')
333
+ break
334
+ }
335
+ }
336
+
337
+
338
+ const onStopEdit = () => {
339
+ if (status == 3) {
340
+ status = 2
341
+ skip++
342
+ polygon.coordinates = editor.getCoordinates().map(e => cartesian3Tgeography(e))
343
+ editor.setCoordinates([])
344
+ polygon.event.trigger('pointer-in')
345
+ }
346
+ }
347
+
348
+ const onDestroy = () => {
349
+ viewer.scene.primitives.remove(collection)
350
+ event.off('pointer-move', onMouseMove)
351
+ event.off('left-click', onClick)
352
+ event.off('double-click', onDbClick)
353
+ polygon.event.off('update', onUpdate)
354
+ polygon.event.off('start-edit', onStartEdit)
355
+ polygon.event.off('stop-edit', onStopEdit)
356
+ polygon.event.off('destroy', onDestroy)
357
+
358
+ }
359
+
360
+ event.on('pointer-move', onMouseMove)
361
+ event.on('left-click', onClick)
362
+ event.on('double-click', onDbClick)
363
+ polygon.event.on('update', onUpdate)
364
+ polygon.event.on('start-edit', onStartEdit)
365
+ polygon.event.on('stop-edit', onStopEdit)
366
+ polygon.event.on('destroy', onDestroy)
367
+
368
+ }