easy-three-utils 0.0.348 → 0.0.349

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.
@@ -1,755 +0,0 @@
1
- import * as Cesium from 'cesium'
2
- import * as turf from "@turf/turf";
3
-
4
- class MeasureDistance {
5
- constructor(viewer) {
6
- this.viewer = viewer;
7
- this.initEvents();
8
- this.positions = [];
9
- this.tempPositions = [];
10
- this.vertexEntities = [];
11
- this.labelEntity = undefined;
12
- this.measureDistance = 0;
13
- }
14
-
15
- initEvents() {
16
- this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
17
- this.MeasureStartEvent = new Cesium.Event();
18
- this.MeasureEndEvent = new Cesium.Event();
19
- }
20
-
21
- activate() {
22
- this.deactivate();
23
- this.registerEvents();
24
- this.viewer.enableCursorStyle = false;
25
- this.viewer._element.style.cursor = 'default';
26
- this.isMeasure = true;
27
- this.measureDistance = 0;
28
- }
29
-
30
- deactivate() {
31
- if (!this.isMeasure) return;
32
- this.unRegisterEvents();
33
- this.viewer._element.style.cursor = 'pointer';
34
- this.viewer.enableCursorStyle = true;
35
- this.isMeasure = false;
36
- this.tempPositions = [];
37
- this.positions = [];
38
- }
39
-
40
- clear() {
41
- this.viewer.entities.remove(this.lineEntity);
42
- this.lineEntity = undefined;
43
-
44
- this.vertexEntities.forEach(item => {
45
- this.viewer.entities.remove(item);
46
- });
47
- this.vertexEntities = [];
48
- }
49
-
50
- createLineEntity() {
51
- this.lineEntity = this.viewer.entities.add({
52
- polyline: {
53
- positions: new Cesium.CallbackProperty(e => {
54
- return this.tempPositions;
55
- }, false),
56
- width: 2,
57
- material: Cesium.Color.YELLOW,
58
- depthFailMaterial: Cesium.Color.YELLOW
59
- }
60
- })
61
- }
62
-
63
- createVertex() {
64
- let vertexEntity = this.viewer.entities.add({
65
- position: this.positions[this.positions.length - 1],
66
- id: "MeasureDistanceVertex" + this.positions[this.positions.length - 1],
67
- type: "MeasureDistanceVertex",
68
- label: {
69
- text: spaceDistance(this.positions) + "米",
70
- scale: 0.5,
71
- font: 'normal 28px MicroSoft YaHei',
72
- style: Cesium.LabelStyle.FILL_AND_OUTLINE,
73
- pixelOffset: new Cesium.Cartesian2(0, -30),
74
- outlineWidth: 9,
75
- outlineColor: Cesium.Color.WHITE,
76
- heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
77
- disableDepthTestDistance: Number.POSITIVE_INFINITY
78
- },
79
- point: {
80
- color: Cesium.Color.FUCHSIA,
81
- pixelSize: 8,
82
- disableDepthTestDistance: 500,
83
- },
84
- });
85
- this.vertexEntities.push(vertexEntity);
86
- }
87
-
88
- createStartEntity() {
89
- let vertexEntity = this.viewer.entities.add({
90
- position: this.positions[0],
91
- type: "MeasureDistanceVertex",
92
- billboard: {
93
- scaleByDistance: new Cesium.NearFarScalar(300, 1, 1200, 0.4),
94
- distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000),
95
- verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
96
- heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
97
- disableDepthTestDistance: Number.POSITIVE_INFINITY
98
- },
99
- point: {
100
- color: Cesium.Color.FUCHSIA,
101
- pixelSize: 6,
102
- },
103
- });
104
- this.vertexEntities.push(vertexEntity);
105
- }
106
-
107
- createEndEntity() {
108
- let lastLabel = this.viewer.entities.getById("MeasureDistanceVertex" + this.positions[this.positions.length - 1]);
109
- this.viewer.entities.remove(lastLabel);
110
- this.viewer.entities.remove(this.moveVertexEntity);
111
-
112
- let vertexEntity = this.viewer.entities.add({
113
- position: this.positions[this.positions.length - 1],
114
- type: "MeasureDistanceVertex",
115
- label: {
116
- text: "总距离:" + spaceDistance(this.positions) + "米",
117
- scale: 0.5,
118
- font: 'normal 26px MicroSoft YaHei',
119
- distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5000),
120
- scaleByDistance: new Cesium.NearFarScalar(1000, 1, 3000, 0.4),
121
- verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
122
- style: Cesium.LabelStyle.FILL_AND_OUTLINE,
123
- pixelOffset: new Cesium.Cartesian2(0, -50),
124
- outlineWidth: 9,
125
- outlineColor: Cesium.Color.WHITE,
126
- eyeOffset: new Cesium.Cartesian3(0, 0, -10)
127
- },
128
- billboard: {
129
- image: "../../static/images/end.png",
130
- scaleByDistance: new Cesium.NearFarScalar(300, 1, 1200, 0.4),
131
- distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000),
132
- verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
133
- heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
134
- disableDepthTestDistance: Number.POSITIVE_INFINITY
135
- },
136
- point: {
137
- color: Cesium.Color.FUCHSIA,
138
- pixelSize: 6,
139
- },
140
- });
141
- this.vertexEntities.push(vertexEntity);
142
- }
143
-
144
- registerEvents() {
145
- this.leftClickEvent();
146
- this.rightClickEvent();
147
- this.mouseMoveEvent();
148
- }
149
-
150
- leftClickEvent() {
151
- this.handler.setInputAction(e => {
152
- this.viewer._element.style.cursor = 'default';
153
- let position = this.viewer.scene.pickPosition(e.position);
154
- if (!position) {
155
- const ellipsoid = this.viewer.scene.globe.ellipsoid;
156
- position = this.viewer.scene.camera.pickEllipsoid(e.position, ellipsoid);
157
- }
158
- if (!position) return;
159
- this.positions.push(position);
160
- if (this.positions.length == 1) {
161
- this.createLineEntity();
162
- this.createStartEntity();
163
- return;
164
- }
165
- this.createVertex();
166
-
167
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
168
- }
169
-
170
- mouseMoveEvent() {
171
- this.handler.setInputAction(e => {
172
- if (!this.isMeasure) return;
173
- this.viewer._element.style.cursor = 'default';
174
- let position = this.viewer.scene.pickPosition(e.endPosition);
175
- if (!position) {
176
- position = this.viewer.scene.camera.pickEllipsoid(e.startPosition, this.viewer.scene.globe.ellipsoid);
177
- }
178
- if (!position) return;
179
- this.handleMoveEvent(position);
180
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
181
- }
182
-
183
- handleMoveEvent(position) {
184
- if (this.positions.length < 1) return;
185
- this.tempPositions = this.positions.concat(position);
186
- }
187
-
188
- rightClickEvent() {
189
- this.handler.setInputAction(e => {
190
- if (!this.isMeasure || this.positions.length < 1) {
191
- this.deactivate();
192
- this.clear();
193
- } else {
194
- this.createEndEntity();
195
- this.lineEntity.polyline = {
196
- positions: this.positions,
197
- width: 2,
198
- material: Cesium.Color.YELLOW,
199
- depthFailMaterial: Cesium.Color.YELLOW
200
- };
201
- this.measureEnd();
202
- }
203
-
204
- }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
205
- }
206
-
207
- measureEnd() {
208
- this.deactivate();
209
- this.MeasureEndEvent.raiseEvent(this.measureDistance);
210
- }
211
-
212
- unRegisterEvents() {
213
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
214
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
215
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
216
- }
217
- }
218
-
219
- class MeasureHeight {
220
- constructor(viewer) {
221
- this.viewer = viewer;
222
- this.initEvents();
223
- this.positions = [];
224
- this.vertexEntities = [];
225
- this.labelEntity = undefined;
226
- this.measureHeight = 0;
227
- }
228
-
229
- initEvents() {
230
- this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
231
- this.MeasureStartEvent = new Cesium.Event();
232
- this.MeasureEndEvent = new Cesium.Event();
233
- }
234
-
235
- activate() {
236
- this.deactivate();
237
- this.registerEvents();
238
- this.viewer.enableCursorStyle = false;
239
- this.viewer._element.style.cursor = 'default';
240
- this.isMeasure = true;
241
- this.circleRadius = 0.1;
242
- this.measureHeight = 0;
243
- this.positions = [];
244
- }
245
-
246
- deactivate() {
247
- if (!this.isMeasure) return;
248
- this.unRegisterEvents();
249
- this.viewer._element.style.cursor = 'pointer';
250
- this.viewer.enableCursorStyle = true;
251
- this.isMeasure = false;
252
- }
253
-
254
- clear() {
255
- this.viewer.entities.remove(this.lineEntity);
256
- this.lineEntity = undefined;
257
-
258
- this.viewer.entities.remove(this.labelEntity);
259
- this.labelEntity = undefined;
260
-
261
- this.removeCircleEntity();
262
-
263
- this.vertexEntities.forEach(item => {
264
- this.viewer.entities.remove(item);
265
- });
266
- this.vertexEntities = [];
267
- }
268
-
269
- createLineEntity() {
270
- this.lineEntity = this.viewer.entities.add({
271
- polyline: {
272
- positions: new Cesium.CallbackProperty(e => {
273
- return this.positions;
274
- }, false),
275
- width: 3,
276
- material: Cesium.Color.fromCssColorString("rgb(249, 157, 11)")
277
- }
278
- })
279
- }
280
-
281
- createVertex(index) {
282
- const vertexEntity = this.viewer.entities.add({
283
- position: new Cesium.CallbackProperty(e => {
284
- return this.positions[index];
285
- }, false),
286
- type: "MeasureHeightVertex",
287
- point: {
288
- color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
289
- outlineColor: Cesium.Color.WHITE,
290
- outlineWidth: 2,
291
- pixelSize: 10
292
- }
293
- });
294
- this.vertexEntities.push(vertexEntity);
295
- }
296
-
297
- createLabel() {
298
- this.labelEntity = this.viewer.entities.add({
299
- position: new Cesium.CallbackProperty(e => {
300
- return this.positions[1];
301
- }, false),
302
- label: {
303
- text: "",
304
- scale: 0.5,
305
- font: 'normal 28px MicroSoft YaHei',
306
- style: Cesium.LabelStyle.FILL_AND_OUTLINE,
307
- pixelOffset: new Cesium.Cartesian2(0, -30),
308
- outlineWidth: 9,
309
- outlineColor: Cesium.Color.WHITE,
310
- heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
311
- disableDepthTestDistance: Number.POSITIVE_INFINITY
312
- }
313
- })
314
- }
315
-
316
- createCircleEntitiy() {
317
- this.circleEntity = this.viewer.entities.add({
318
- position: new Cesium.CallbackProperty(e => {
319
- return this.positions[this.positions.length - 1];
320
- }, false),
321
- ellipse: {
322
- height: new Cesium.CallbackProperty(e => {
323
- return this.getPositionHeight(this.positions[this.positions.length - 1]);
324
- }, false),
325
- semiMinorAxis: new Cesium.CallbackProperty(e => {
326
- return this.circleRadius;
327
- }, false),
328
- semiMajorAxis: new Cesium.CallbackProperty(e => {
329
- return this.circleRadius;
330
- }, false),
331
- material: Cesium.Color.YELLOW.withAlpha(0.5)
332
- },
333
- });
334
- }
335
-
336
- getPositionHeight(position) {
337
- const cartographic = Cesium.Cartographic.fromCartesian(position);
338
- return cartographic.height;
339
- }
340
-
341
- removeCircleEntity() {
342
- this.viewer.entities.remove(this.circleEntity);
343
- this.circleEntity = undefined;
344
- }
345
-
346
- registerEvents() {
347
- this.leftClickEvent();
348
- this.rightClickEvent();
349
- this.mouseMoveEvent();
350
- }
351
-
352
- leftClickEvent() {
353
- this.handler.setInputAction(e => {
354
- this.viewer._element.style.cursor = 'default';
355
- let position = this.viewer.scene.pickPosition(e.position);
356
- if (!position) {
357
- const ellipsoid = this.viewer.scene.globe.ellipsoid;
358
- position = this.viewer.scene.camera.pickEllipsoid(e.position, ellipsoid);
359
- }
360
- if (!position) return;
361
-
362
- if (this.positions.length == 0) {
363
- this.positions.push(position);
364
- this.createVertex(0);
365
- this.createLineEntity();
366
- this.createCircleEntitiy();
367
- this.createLabel();
368
- } else {
369
- this.measureEnd();
370
- this.removeCircleEntity()
371
- }
372
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
373
- }
374
-
375
- mouseMoveEvent() {
376
- this.handler.setInputAction(e => {
377
- if (!this.isMeasure) return;
378
- this.viewer._element.style.cursor = 'default';
379
- let position = this.viewer.scene.pickPosition(e.endPosition);
380
- if (!position) {
381
- position = this.viewer.scene.camera.pickEllipsoid(e.startPosition, this.viewer.scene.globe.ellipsoid);
382
- }
383
- if (!position) return;
384
- this.handleMoveEvent(position);
385
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
386
- }
387
-
388
- cartesian3Point3(pos) {
389
- const ellipsoid = this.viewer.scene.globe.ellipsoid;
390
- const cartographic = ellipsoid.cartesianToCartographic(pos);
391
- const lng = Cesium.Math.toDegrees(cartographic.longitude);
392
- const lat = Cesium.Math.toDegrees(cartographic.latitude);
393
- const alt = cartographic.height;
394
-
395
- return [lng, lat, alt]
396
- }
397
-
398
- getDistanceH(p1, p2) {
399
- if (!p1 || !p2) return 2;
400
- let distance = Cesium.Cartesian3.distance(p1, p2);
401
- let p2Car = this.cartesian3Point3(p2);
402
- let radius = Math.sqrt(Math.abs(Math.pow(distance, 2) - Math.pow(p2Car[2], 2)));
403
- return radius;
404
- }
405
-
406
- handleMoveEvent(position) {
407
- if (this.positions.length < 1) return;
408
- let firstPoint = this.cartesian3Point3(this.positions[0])
409
- let movePoint = this.cartesian3Point3(position)
410
- const h = movePoint[2] - firstPoint[2];
411
- firstPoint[2] = movePoint[2];
412
- const twoPosition = Cesium.Cartesian3.fromDegrees(firstPoint[0], firstPoint[1], movePoint[2]);
413
- if (this.positions.length < 2) {
414
- this.positions.push(twoPosition, position);
415
- this.createVertex(1);
416
- this.createVertex(2);
417
- } else {
418
- this.positions[1] = twoPosition;
419
- this.positions[2] = position;
420
- this.measureHeight = h.toFixed(3);
421
- this.labelEntity.label.text = "高度:" + this.measureHeight + " 米"
422
- }
423
-
424
- this.circleRadius = 20
425
- }
426
-
427
- rightClickEvent() {
428
- this.handler.setInputAction(e => {
429
- if (this.isMeasure) {
430
- this.deactivate();
431
- this.clear();
432
- }
433
- }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
434
- }
435
-
436
- measureEnd() {
437
- this.deactivate();
438
- this.MeasureEndEvent.raiseEvent(this.measureHeight);
439
- }
440
-
441
- unRegisterEvents() {
442
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
443
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
444
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
445
- }
446
- }
447
-
448
- class MeasureArea {
449
- constructor(viewer) {
450
- this.viewer = viewer;
451
- this.initEvents();
452
- this.positions = [];
453
- this.tempPositions = [];
454
- this.vertexEntities = [];
455
- this.labelEntity = undefined;
456
- this.measureArea = 0;
457
- }
458
-
459
- initEvents() {
460
- this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
461
- this.MeasureStartEvent = new Cesium.Event();
462
- this.MeasureEndEvent = new Cesium.Event();
463
- }
464
-
465
- activate() {
466
- this.deactivate();
467
- this.registerEvents();
468
- this.viewer.enableCursorStyle = false;
469
- this.viewer._element.style.cursor = 'default';
470
- this.isMeasure = true;
471
- this.measureArea = 0;
472
- }
473
-
474
- deactivate() {
475
- if (!this.isMeasure) return;
476
- this.unRegisterEvents();
477
- this.viewer._element.style.cursor = 'pointer';
478
- this.viewer.enableCursorStyle = true;
479
- this.isMeasure = false;
480
- this.tempPositions = [];
481
- this.positions = [];
482
- this.height = undefined;
483
- }
484
-
485
- clear() {
486
- this.viewer.entities.remove(this.polygonEntity);
487
- this.polygonEntity = undefined;
488
-
489
- this.vertexEntities.forEach(item => {
490
- this.viewer.entities.remove(item);
491
- });
492
- this.vertexEntities = [];
493
-
494
- this.viewer.entities.remove(this.mesureResultEntity);
495
- this.mesureResultEntity = undefined;
496
-
497
- this.height = undefined;
498
- }
499
-
500
- createPolygonEntity() {
501
- this.polygonEntity = this.viewer.entities.add({
502
- polygon: {
503
- hierarchy: new Cesium.CallbackProperty(e => {
504
- return new Cesium.PolygonHierarchy(this.tempPositions);
505
- }, false),
506
- material: Cesium.Color.fromCssColorString("rgb(249, 157, 11,.6)")
507
- },
508
- polyline: {
509
- clampToGround: true,
510
- positions: new Cesium.CallbackProperty(e => {
511
- return this.tempPositions.concat(this.tempPositions[0]);
512
- }, false),
513
- width: 1,
514
- material: new Cesium.PolylineOutlineMaterialProperty({
515
- outlineWidth: 2,
516
- outlineColor: Cesium.Color.WHITE,
517
- })
518
- }
519
- })
520
- }
521
-
522
- createVertex() {
523
- const vertexEntity = this.viewer.entities.add({
524
- position: this.positions[this.positions.length - 1],
525
- type: "MeasureAreaVertex",
526
- point: {
527
- color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
528
- outlineColor: Cesium.Color.WHITE,
529
- outlineWidth: 2,
530
- pixelSize: 10,
531
- heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
532
- }
533
- });
534
- this.vertexEntities.push(vertexEntity);
535
- }
536
-
537
- createResultLabel() {
538
- this.mesureResultEntity = this.viewer.entities.add({
539
- position: new Cesium.CallbackProperty(e => {
540
- return this.getCenterPosition()
541
- }, false),
542
- type: "MeasureAreaResult",
543
- label: {
544
- text: new Cesium.CallbackProperty(e => {
545
- return "面积" + this.computeArea(this.tempPositions);
546
- }, false),
547
- scale: 0.5,
548
- font: 'normal 28px MicroSoft YaHei',
549
- style: Cesium.LabelStyle.FILL_AND_OUTLINE,
550
- pixelOffset: new Cesium.Cartesian2(0, -30),
551
- outlineWidth: 9,
552
- outlineColor: Cesium.Color.WHITE,
553
- // heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
554
- disableDepthTestDistance: Number.POSITIVE_INFINITY
555
- }
556
- });
557
- }
558
-
559
- getCenterPosition() {
560
- let points = [];
561
- if (this.tempPositions.length < 3) return this.tempPositions[0];
562
- this.tempPositions.forEach(position => {
563
- const point3d = this.cartesian3ToPoint3D(position);
564
- points.push([point3d.x, point3d.y]);
565
- })
566
- let geo = turf.lineString(points);
567
- let bbox = turf.bbox(geo);
568
- let bboxPolygon = turf.bboxPolygon(bbox);
569
- let pointOnFeature = turf.center(bboxPolygon);
570
- let lonLat = pointOnFeature.geometry.coordinates;
571
-
572
- return Cesium.Cartesian3.fromDegrees(lonLat[0], lonLat[1], this.height + 0.3);
573
- }
574
-
575
- registerEvents() {
576
- this.leftClickEvent();
577
- this.rightClickEvent();
578
- this.mouseMoveEvent();
579
- }
580
-
581
- leftClickEvent() {
582
- this.handler.setInputAction(e => {
583
- this.viewer._element.style.cursor = 'default';
584
- let position = this.viewer.scene.pickPosition(e.position);
585
- if (!position) {
586
- const ellipsoid = this.viewer.scene.globe.ellipsoid;
587
- position = this.viewer.scene.camera.pickEllipsoid(e.position, ellipsoid);
588
- }
589
- if (!position) return;
590
- this.positions.push(position);
591
- this.height = this.unifiedHeight(this.positions, this.height);
592
- if (this.positions.length == 1) {
593
- this.createPolygonEntity();
594
- }
595
- this.createVertex();
596
-
597
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
598
- }
599
-
600
- mouseMoveEvent() {
601
- this.handler.setInputAction(e => {
602
- if (!this.isMeasure) return;
603
- this.viewer._element.style.cursor = 'default';
604
- let position = this.viewer.scene.pickPosition(e.endPosition);
605
- if (!position) {
606
- position = this.viewer.scene.camera.pickEllipsoid(e.startPosition, this.viewer.scene.globe.ellipsoid);
607
- }
608
- if (!position) return;
609
- this.handleMoveEvent(position);
610
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
611
- }
612
-
613
- handleMoveEvent(position) {
614
- if (this.positions.length < 1) return;
615
- this.height = this.unifiedHeight(this.positions, this.height);
616
- this.tempPositions = this.positions.concat(position);
617
- console.log(this.tempPositions.length,!this.mesureResultEntity);
618
-
619
- if (this.tempPositions.length >= 3 && !this.mesureResultEntity) {
620
- this.createResultLabel();
621
- }
622
- }
623
-
624
- unifiedHeight(positions, height) {
625
- if (!height) height = this.getPositionHeight(positions[0]);
626
- let point3d;
627
- for (let i = 0; i < positions.length; i++) {
628
- const element = positions[i];
629
- point3d = this.cartesian3ToPoint3D(element);
630
- positions[i] = Cesium.Cartesian3.fromDegrees(point3d.x, point3d.y, height)
631
- }
632
-
633
- return height;
634
- }
635
-
636
- getPositionHeight(position) {
637
- const cartographic = Cesium.Cartographic.fromCartesian(position);
638
- return cartographic.height;
639
- }
640
-
641
- cartesian3ToPoint3D(position) {
642
- const cartographic = Cesium.Cartographic.fromCartesian(position);
643
- const lon = Cesium.Math.toDegrees(cartographic.longitude);
644
- const lat = Cesium.Math.toDegrees(cartographic.latitude);
645
- return { x: lon, y: lat, z: cartographic.height };
646
- }
647
-
648
- Bearing(from, to) {
649
- from = Cesium.Cartographic.fromCartesian(from);
650
- to = Cesium.Cartographic.fromCartesian(to);
651
-
652
- const lat1 = from.latitude;
653
- const lon1 = from.longitude;
654
- const lat2 = to.latitude;
655
- const lon2 = to.longitude;
656
- let angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));
657
- if (angle < 0) {
658
- angle += Math.PI * 2.0;
659
- }
660
- const degreesPerRadian = 180.0 / Math.PI;
661
-
662
- angle = angle * degreesPerRadian;
663
- return angle;
664
- }
665
-
666
- Angle(p1, p2, p3) {
667
- const bearing21 = this.Bearing(p2, p1);
668
- const bearing23 = this.Bearing(p2, p3);
669
- let angle = bearing21 - bearing23;
670
- if (angle < 0) {
671
- angle += 360;
672
- }
673
- return angle;
674
- }
675
-
676
- distance(point1, point2) {
677
- const point1cartographic = Cesium.Cartographic.fromCartesian(point1);
678
- const point2cartographic = Cesium.Cartographic.fromCartesian(point2);
679
- const geodesic = new Cesium.EllipsoidGeodesic();
680
- geodesic.setEndPoints(point1cartographic, point2cartographic);
681
- let s = geodesic.surfaceDistance;
682
- s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
683
- return s;
684
- }
685
-
686
- computeArea(points) {
687
- let res = 0;
688
-
689
- for (let i = 0; i < points.length - 2; i++) {
690
- const j = (i + 1) % points.length;
691
- const k = (i + 2) % points.length;
692
- const totalAngle = this.Angle(points[i], points[j], points[k]);
693
-
694
- const dis_temp1 = this.distance(points[j], points[0]);
695
- const dis_temp2 = this.distance(points[k], points[0]);
696
-
697
- res += dis_temp1 * dis_temp2 * Math.sin(totalAngle) / 2;
698
- }
699
-
700
- if (res < 1000000) {
701
- res = Math.abs(res).toFixed(4) + " 平方米";
702
- } else {
703
- res = Math.abs((res / 1000000.0).toFixed(4)) + " 平方公里";
704
- }
705
-
706
- return res;
707
- }
708
-
709
- rightClickEvent() {
710
- this.handler.setInputAction(e => {
711
- if (!this.isMeasure || this.positions.length < 3) {
712
- this.deactivate();
713
- this.clear();
714
- } else {
715
- this.tempPositions = [...this.positions];
716
-
717
- this.polygonEntity.polyline = {
718
- clampToGround: true,
719
- positions: new Cesium.CallbackProperty(e => {
720
- return this.tempPositions.concat(this.tempPositions[0]);
721
- }, false),
722
- width: 1,
723
- material: new Cesium.PolylineOutlineMaterialProperty({
724
- color: Cesium.Color.ORANGE,
725
- outlineWidth: 2,
726
- outlineColor: Cesium.Color.WHITE
727
- })
728
- }
729
-
730
- this.polygonEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(this.tempPositions);
731
- this.mesureResultEntity.position = this.getCenterPosition();
732
- this.mesureResultEntity.label.text = "总面积为" + this.computeArea(this.positions)
733
- this.measureEnd();
734
- }
735
-
736
- }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
737
- }
738
-
739
- measureEnd() {
740
- this.deactivate();
741
- this.MeasureEndEvent.raiseEvent(this.measureArea);
742
- }
743
-
744
- unRegisterEvents() {
745
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
746
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
747
- this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
748
- }
749
- }
750
-
751
- export {
752
- MeasureDistance,
753
- MeasureHeight,
754
- MeasureArea
755
- }