easy-three-utils 0.0.348 → 0.0.350

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,208 +0,0 @@
1
- import * as Cesium from 'cesium'
2
-
3
- const useMeasureTools = (viewer: Cesium.Viewer) => {
4
- let handler = new Cesium.ScreenSpaceEventHandler((viewer.scene as any)._imageryLayerCollection)
5
- let positions = []
6
- let positionsCartographic = []
7
- let positions_Inter = []
8
- let poly = null
9
- let distance = null
10
- let cartesian = null
11
- let DistanceArray = []
12
- let profileItem = []
13
-
14
- const reset = () => {
15
- handler = new Cesium.ScreenSpaceEventHandler((viewer.scene as any)._imageryLayerCollection)
16
- positions = []
17
- positionsCartographic = []
18
- positions_Inter = []
19
- poly = null
20
- distance = null
21
- cartesian = null
22
- DistanceArray = []
23
- profileItem = []
24
- }
25
-
26
- const measureDistance = () => {
27
- reset()
28
-
29
- handler.setInputAction(((movement) => {
30
- cartesian = viewer.scene.pickPosition(movement.endPosition);
31
- if (positions.length >= 2) {
32
- if (!Cesium.defined(poly)) {
33
- poly = new PolyLinePrimitive(positions);
34
- } else {
35
- positions.pop();
36
- positions.push(cartesian);
37
- }
38
- }
39
- }) as Cesium.ScreenSpaceEventHandler.MotionEventCallback, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
40
-
41
- handler.setInputAction(((movement) => {
42
- cartesian = viewer.scene.pickPosition(movement.position);
43
- if (positions.length == 0) {
44
- positions.push(cartesian.clone());
45
- }
46
- positions.push(cartesian);
47
- if (poly) {
48
- interPoints(poly.positions);
49
- distance = getSpaceDistance(positions_Inter);
50
- } else {
51
- distance = getSpaceDistance(positions);
52
- }
53
-
54
- const textDisance = distance + "米";
55
- DistanceArray.push(distance);
56
- viewer.entities.add({
57
- position: positions[positions.length - 1],
58
- point: {
59
- color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
60
- outlineColor: Cesium.Color.WHITE,
61
- outlineWidth: 2,
62
- pixelSize: 10
63
- },
64
- label: {
65
- text: textDisance,
66
- font: '18px sans-serif',
67
- fillColor: Cesium.Color.GOLD,
68
- style: Cesium.LabelStyle.FILL_AND_OUTLINE,
69
- outlineWidth: 2,
70
- verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
71
- pixelOffset: new Cesium.Cartesian2(20, -20),
72
- heightReference: Cesium.HeightReference.NONE
73
- }
74
- });
75
- }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.LEFT_CLICK);
76
-
77
- handler.setInputAction((() => {
78
- destoryHandler()
79
- }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
80
-
81
- class PolyLinePrimitive {
82
- options: any;
83
- positions: any;
84
-
85
- constructor(positions) {
86
- this.options = {
87
- polyline: {
88
- show: true,
89
- positions: [],
90
- material: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
91
- width: 2,
92
- clampToGround: true
93
- }
94
- };
95
- this.positions = positions;
96
- this.init()
97
- }
98
-
99
- init = () => {
100
- const _self = this;
101
- const _update = function () {
102
- return _self.positions;
103
- };
104
- this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
105
- viewer.entities.add(this.options);
106
- }
107
- }
108
-
109
- const cartesian3ToDegrees = (pos) => {
110
- const ellipsoid = viewer.scene.globe.ellipsoid;
111
- const cartographic = ellipsoid.cartesianToCartographic(pos);
112
- const lat = Cesium.Math.toDegrees(cartographic.latitude);
113
- const lon = Cesium.Math.toDegrees(cartographic.longitude);
114
- const height = cartographic.height;
115
-
116
- return {
117
- lat,
118
- lon,
119
- height
120
- }
121
- }
122
-
123
- const getSpaceDistance = (positions) => {
124
- profileItem = [
125
- {
126
- point: cartesian3ToDegrees(positions[0]),
127
- distance: 0
128
- }
129
- ];
130
- let distance = 0;
131
- for (let i = 0; i < positions.length - 1; i++) {
132
- const point1cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
133
- const point2cartographic = Cesium.Cartographic.fromCartesian(positions[i + 1]);
134
- const geodesic = new Cesium.EllipsoidGeodesic();
135
- geodesic.setEndPoints(point1cartographic, point2cartographic);
136
- let s = geodesic.surfaceDistance;
137
- s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
138
- distance = distance + s;
139
-
140
- const m_Item = {
141
- point: cartesian3ToDegrees(positions[i + 1]),
142
- distance: distance
143
- };
144
- profileItem.push(m_Item);
145
- }
146
- return distance.toFixed(2);
147
- }
148
-
149
- const interPoints = (positions) => {
150
- positionsCartographic = [];
151
- var terrainSamplePositions = [];
152
- for (let index = 0; index < positions.length - 1; index++) {
153
- const element = positions[index];
154
- var ellipsoid = viewer.scene.globe.ellipsoid;
155
- var cartographic = ellipsoid.cartesianToCartographic(element);
156
- positionsCartographic.push(cartographic);
157
- }
158
- for (let i = 0; i < positionsCartographic.length; i++) {
159
- const m_Cartographic0 = positionsCartographic[i];
160
- const m_Cartographic1 = positionsCartographic[i + 1];
161
- if (m_Cartographic1) {
162
- var a = Math.abs(m_Cartographic0.longitude - m_Cartographic1.longitude) * 10000000;
163
- var b = Math.abs(m_Cartographic0.latitude - m_Cartographic1.latitude) * 10000000;
164
- if (a > b) b = a;
165
- let length = parseInt((b / 2) as any);
166
- if (length > 1000) length = 1000;
167
- if (length < 2) length = 2;
168
- for (var j = 0; j < length; j++) {
169
- terrainSamplePositions.push(
170
- new Cesium.Cartographic(
171
- Cesium.Math.lerp(m_Cartographic0.longitude, m_Cartographic1.longitude, j / (length - 1)),
172
- Cesium.Math.lerp(m_Cartographic0.latitude, m_Cartographic1.latitude, j / (length - 1))
173
- )
174
- );
175
- }
176
- terrainSamplePositions.pop();
177
- } else {
178
- terrainSamplePositions.push(m_Cartographic0);
179
- }
180
- }
181
- positions_Inter = [];
182
- for (var n = 0; n < terrainSamplePositions.length; n++) {
183
- var m_cartographic = terrainSamplePositions[n];
184
- var height = viewer.scene.globe.getHeight(m_cartographic);
185
- var point = Cesium.Cartesian3.fromDegrees(m_cartographic.longitude / Math.PI * 180, m_cartographic.latitude / Math.PI * 180, height);
186
- positions_Inter.push(point);
187
- }
188
- }
189
-
190
- const destoryHandler = () => {
191
- if (handler) {
192
- handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
193
- handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
194
- handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
195
- handler.destroy()
196
- handler = null
197
- }
198
- }
199
- }
200
-
201
- return {
202
- measureDistance
203
- }
204
- }
205
-
206
- export {
207
- useMeasureTools
208
- }
@@ -1,379 +0,0 @@
1
- import CreatePolygonOnGround from "./lib/CreatePolygonOnGround";
2
- import CreateRemindertip from "./lib/ReminderTip";
3
- import * as turf from "@turf/turf";
4
- import * as Cesium from 'cesium'
5
-
6
- class SlopeAspect {
7
- constructor(viewer, cb, cb1) {
8
- if (!viewer) throw new Error("no viewer object!");
9
- this.viewer = viewer;
10
- this.result = []; //存储创建的坡度分析结果,primitive集合
11
- this.handler = undefined;
12
- this.toolTip = "";
13
- this.cb = cb
14
- this.cb1 = cb1
15
- }
16
- openTip() {
17
- const $this = this;
18
- const viewer = this.viewer;
19
- this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
20
- this.handler.setInputAction(function (movement) {
21
- let endPos = movement.endPosition;
22
- var pick = viewer.scene.pick(endPos);
23
- if (pick && pick.id && pick.id.type === "SlopeAspect") {
24
- $this.toolTip = pick.id.value.toFixed(2);
25
- // CreateRemindertip($this.toolTip, endPos, true);
26
- } else {
27
- $this.toolTip = "";
28
- // CreateRemindertip($this.toolTip, endPos, false);
29
- }
30
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
31
- }
32
- closeTip() {
33
- if (this.handler) {
34
- this.handler.destroy();
35
- this.handler = undefined;
36
- }
37
- }
38
- //等距离切分网格
39
- createNew4Distance(distance) {
40
- distance = distance || 0.1; //默认0.1km精度
41
- let width = distance * 200 > 35 ? 35 : distance * 200;
42
- this.arrowWidth = width < 15 ? 15 : width;
43
- const $this = this;
44
- const viewer = this.viewer;
45
- CreatePolygonOnGround(
46
- viewer,
47
- [],
48
- {
49
- color: Cesium.Color.RED.withAlpha(0.1),
50
- outlineColor: Cesium.Color.YELLOW,
51
- outlineWidth: 2,
52
- },
53
- function (polygon) {
54
- let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
55
- viewer.entities.remove(polygon);
56
- let boundary = [];
57
- let minX = 10000,
58
- minY = 10000,
59
- maxX = -10000,
60
- maxY = -1000;
61
- for (let index = 0; index < degrees.length; index++) {
62
- const element = degrees[index];
63
- const x = element.lng;
64
- const y = element.lat;
65
- boundary.push([x, y]);
66
- minX = x < minX ? x : minX;
67
- minY = y < minY ? y : minY;
68
- maxX = x > maxX ? x : maxX;
69
- maxY = y > maxY ? y : maxY;
70
- }
71
- boundary.push(boundary[0]);
72
- let bbox = [minX, minY, maxX, maxY];
73
- let mask = turf.polygon([boundary]);
74
- let gridSquare = turf.squareGrid(bbox, distance, { mask: mask });
75
- $this.createEllipse(gridSquare);
76
- $this.cb && $this.cb()
77
- },
78
- function (handler) {
79
- $this.cb1(handler)
80
- }
81
- );
82
- }
83
- // 等分切分网格,切分成一个num*num的网格
84
- createNew4Num(num) {
85
- const $this = this;
86
- const viewer = this.viewer;
87
- CreatePolygonOnGround(
88
- viewer,
89
- [],
90
- {
91
- color: Cesium.Color.RED.withAlpha(0.1),
92
- outlineColor: Cesium.Color.YELLOW,
93
- outlineWidth: 2,
94
- },
95
- function (polygon) {
96
- let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
97
- viewer.entities.remove(polygon);
98
- let boundary = [];
99
- let minX = 10000,
100
- minY = 10000,
101
- maxX = -10000,
102
- maxY = -1000;
103
- for (let index = 0; index < degrees.length; index++) {
104
- const element = degrees[index];
105
- const x = element.lng;
106
- const y = element.lat;
107
- boundary.push([x, y]);
108
- minX = x < minX ? x : minX;
109
- minY = y < minY ? y : minY;
110
- maxX = x > maxX ? x : maxX;
111
- maxY = y > maxY ? y : maxY;
112
- }
113
- boundary.push(boundary[0]);
114
- let bbox = [minX, minY, maxX, maxY];
115
- let a = maxX - minX;
116
- let b = maxY - minY;
117
- b = b > a ? b : a;
118
- const step = b / num;
119
- let width = step * 2000 > 35 ? 35 : step * 2000;
120
- $this.arrowWidth = width < 15 ? 15 : width;
121
- let mask = turf.polygon([boundary]);
122
- let gridSquare = turf.squareGrid(bbox, step, {
123
- units: "degrees",
124
- mask: mask,
125
- });
126
- $this.createEllipse(gridSquare);
127
- $this.cb && $this.cb()
128
- },
129
- function (handler) {
130
- $this.cb1(handler)
131
- }
132
- );
133
- }
134
- createEllipse(gridSquare) {
135
- let boxResults = [];
136
- for (let index = 0; index < gridSquare.features.length; index++) {
137
- const feature = gridSquare.features[index];
138
- const coordinates = feature.geometry.coordinates[0];
139
- const centerdegree = [
140
- (coordinates[0][0] + coordinates[2][0]) / 2,
141
- (coordinates[0][1] + coordinates[2][1]) / 2,
142
- ];
143
- let centerCartographic = Cesium.Cartographic.fromDegrees(
144
- centerdegree[0],
145
- centerdegree[1]
146
- );
147
- boxResults.push(centerCartographic);
148
- for (let i = 0; i < coordinates.length; i++) {
149
- const coord = coordinates[i];
150
- let cartographic = Cesium.Cartographic.fromDegrees(coord[0], coord[1]);
151
- boxResults.push(cartographic);
152
- const coord1 = coordinates[i + 1];
153
- if (coord1) {
154
- let newCoord = [
155
- (coord[0] + coord1[0]) / 2,
156
- (coord[1] + coord1[1]) / 2,
157
- ];
158
- let newCartographic = Cesium.Cartographic.fromDegrees(
159
- newCoord[0],
160
- newCoord[1]
161
- );
162
- boxResults.push(newCartographic);
163
- }
164
- }
165
- }
166
- Cesium.sampleTerrainMostDetailed(
167
- this.viewer.scene.terrainProvider,
168
- boxResults
169
- ).then((updatePositions) => {
170
- let arrr = [];
171
- let ellipseResults = updatePositions.reduce(function (
172
- pre,
173
- item,
174
- index,
175
- updatePositions
176
- ) {
177
- var begin = index * 10;
178
- var end = begin + 10;
179
- var res = updatePositions.slice(begin, end);
180
- if (res.length != 0) {
181
- arrr[index] = res;
182
- }
183
- return arrr;
184
- },
185
- []);
186
- this.calculateSlope(ellipseResults);
187
- });
188
- }
189
-
190
- createPolygonInsrance(points, color) {
191
- let positions = [];
192
- for (let index = 1; index < points.length - 1; index++) {
193
- const element = points[index];
194
- positions.push(Cesium.Cartographic.toCartesian(element));
195
- }
196
- let polygon = new Cesium.PolygonGeometry({
197
- polygonHierarchy: new Cesium.PolygonHierarchy(positions),
198
- });
199
-
200
- let polygonInstance = new Cesium.GeometryInstance({
201
- geometry: polygon,
202
- attributes: {
203
- color: Cesium.ColorGeometryInstanceAttribute.fromColor(
204
- Cesium.Color.fromCssColorString(color)
205
- ),
206
- show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
207
- },
208
- });
209
- return polygonInstance;
210
- }
211
- createArrowInstance(
212
- targetPoint,
213
- center,
214
- diagonalPoint,
215
- heightDifference,
216
- curSlope
217
- ) {
218
- let cartographic_0 = new Cesium.Cartographic(
219
- (targetPoint.longitude + center.longitude) / 2,
220
- (targetPoint.latitude + center.latitude) / 2,
221
- (targetPoint.height + center.height) / 2
222
- );
223
- let cartographic_1 = new Cesium.Cartographic(
224
- (diagonalPoint.longitude + center.longitude) / 2,
225
- (diagonalPoint.latitude + center.latitude) / 2,
226
- (diagonalPoint.height + center.height) / 2
227
- );
228
- //偏移的
229
- let positions1 =
230
- heightDifference > 0
231
- ? [
232
- Cesium.Cartographic.toCartesian(cartographic_0),
233
- Cesium.Cartographic.toCartesian(cartographic_1),
234
- ]
235
- : [
236
- Cesium.Cartographic.toCartesian(cartographic_1),
237
- Cesium.Cartographic.toCartesian(cartographic_0),
238
- ];
239
- //箭头线
240
- const instance = new Cesium.GeometryInstance({
241
- id: {
242
- type: "SlopeAspect",
243
- value: curSlope,
244
- },
245
- geometry: new Cesium.GroundPolylineGeometry({
246
- positions: positions1,
247
- width: this.arrowWidth,
248
- }),
249
- attributes: {
250
- color: Cesium.ColorGeometryInstanceAttribute.fromColor(
251
- Cesium.Color.BLUE.withAlpha(0.6)
252
- ),
253
- show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
254
- },
255
- });
256
- return instance;
257
- }
258
- calculateSlope(ellipseResults) {
259
- let instances = [];
260
- let polygonInstance = [];
261
- for (let index = 0; index < ellipseResults.length; index++) {
262
- const ellipse = ellipseResults[index];
263
-
264
- const center = ellipse[0];
265
- let heightDifference = 0;
266
- let maxIndex = 0;
267
- for (let i = 1; i < ellipse.length - 1; i++) {
268
- const point = ellipse[i];
269
- let curHD = point.height - center.height;
270
- if (Math.abs(curHD) > heightDifference) {
271
- heightDifference = curHD;
272
- maxIndex = i;
273
- }
274
- }
275
- let pos0 = new Cesium.Cartographic(center.longitude, center.latitude, 0);
276
- let pos1 = new Cesium.Cartographic(
277
- ellipse[maxIndex].longitude,
278
- ellipse[maxIndex].latitude,
279
- 0
280
- );
281
- let distance = Cesium.Cartesian3.distance(
282
- Cesium.Cartographic.toCartesian(pos0),
283
- Cesium.Cartographic.toCartesian(pos1)
284
- );
285
- let curSlope = Math.abs(heightDifference / distance); //坡度的tan值
286
- let curColor = this.calculateSlopeColor(curSlope, 0.4);
287
- const curPolygonInstance = this.createPolygonInsrance(ellipse, curColor);
288
- polygonInstance.push(curPolygonInstance);
289
-
290
- let diagonalPoint =
291
- maxIndex > 4 ? ellipse[maxIndex - 4] : ellipse[maxIndex + 4]; //对角点
292
- let targetPoint = ellipse[maxIndex];
293
- const arrowInstance = this.createArrowInstance(
294
- targetPoint,
295
- center,
296
- diagonalPoint,
297
- heightDifference,
298
- curSlope
299
- );
300
- instances.push(arrowInstance);
301
- }
302
- const mapPrimitive = this.viewer.scene.primitives.add(
303
- new Cesium.GroundPrimitive({
304
- geometryInstances: polygonInstance,
305
- appearance: new Cesium.PerInstanceColorAppearance({
306
- translucent: true, //false时透明度无效
307
- closed: false,
308
- }),
309
- })
310
- );
311
- const arrowPrimitive = this.viewer.scene.primitives.add(
312
- new Cesium.GroundPolylinePrimitive({
313
- geometryInstances: instances,
314
- appearance: new Cesium.PolylineMaterialAppearance({
315
- material: new Cesium.Material({
316
- fabric: {
317
- type: "PolylineArrow",
318
- uniforms: {
319
- color: new Cesium.Color(1.0, 1.0, 0.0, 0.8),
320
- },
321
- },
322
- }),
323
- }),
324
- })
325
- );
326
- this.result.push(arrowPrimitive, mapPrimitive);
327
- }
328
- clearAll() {
329
- this.result.forEach((element) => {
330
- this.viewer.scene.primitives.remove(element);
331
- });
332
- this.result = [];
333
- }
334
- //根据坡度值赋值颜色
335
- calculateSlopeColor(value, alpha) {
336
- // 0°~0.5°为平原0.00872686779075879,rgb(85,182,43)
337
- // 0.5°~2°为微斜坡0.03492076949174773,rgb(135,211,43)
338
- // 2°~5°为缓斜坡0.08748866352592401,rgb(204,244,44)
339
- // 5°~15°为斜坡0.2679491924311227,rgb(245,233,44)
340
- // 15°~35°为陡坡0.7002075382097097,rgb(255,138,43)
341
- // 35°~55°为峭坡1.4281480067421144,rgb(255,84,43)
342
- // 55°~90°为垂直壁,rgb(255,32,43)
343
- if (value < 0.00872686779075879) {
344
- return "rgba(85,182,43," + alpha + ")";
345
- } else if (value < 0.03492076949174773) {
346
- return "rgba(135,211,43," + alpha + ")";
347
- } else if (value < 0.08748866352592401) {
348
- return "rgba(204,244,44," + alpha + ")";
349
- } else if (value < 0.2679491924311227) {
350
- return "rgba(245,233,44," + alpha + ")";
351
- } else if (value < 0.7002075382097097) {
352
- return "rgba(255,138,43," + alpha + ")";
353
- } else if (value < 1.4281480067421144) {
354
- return "rgba(255,84,43," + alpha + ")";
355
- } else {
356
- return "rgba(255,32,43," + alpha + ")";
357
- }
358
- }
359
- /**
360
- * 笛卡尔坐标数组转WGS84
361
- * @param {Array} cartesianList 笛卡尔坐标数组
362
- * @returns {Array} WGS84经纬度坐标数组
363
- */
364
- Cartesian3ListToWGS84(cartesianList) {
365
- let ellipsoid = Cesium.Ellipsoid.WGS84;
366
- let result = [];
367
- for (let index = 0; index < cartesianList.length; index++) {
368
- const cartesian = cartesianList[index];
369
- let cartographic = ellipsoid.cartesianToCartographic(cartesian);
370
- result.push({
371
- lng: Cesium.Math.toDegrees(cartographic.longitude),
372
- lat: Cesium.Math.toDegrees(cartographic.latitude),
373
- alt: cartographic.height,
374
- });
375
- }
376
- return result;
377
- }
378
- }
379
- export default SlopeAspect;