easy-three-utils 0.0.347 → 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.
@@ -0,0 +1,300 @@
1
+ import * as Cesium from "cesium"
2
+ import * as turf from "@turf/turf"
3
+ import CreatePolygonOnGround from "./lib/CreatePolygonOnGround"
4
+ // import CreateRemindertip from "./lib/ReminderTip"
5
+
6
+ const useSlope = (viewer: Cesium.Viewer, options: {
7
+ collection: Cesium.CustomDataSource
8
+ }) => {
9
+
10
+ let result = []
11
+ // let handler = null
12
+ // let toolTip = ""
13
+ let arrowWidth = 20
14
+
15
+ const Cartesian3ListToWGS84 = (cartesianList) => {
16
+ const ellipsoid = Cesium.Ellipsoid.WGS84
17
+ return cartesianList.map((cartesian) => {
18
+ const cartographic = ellipsoid.cartesianToCartographic(cartesian)
19
+ return {
20
+ lng: Cesium.Math.toDegrees(cartographic.longitude),
21
+ lat: Cesium.Math.toDegrees(cartographic.latitude),
22
+ alt: cartographic.height,
23
+ }
24
+ })
25
+ }
26
+
27
+ const calculateSlopeColor = (value, alpha) => {
28
+ if (value < 0.0087) return `rgba(85,182,43,${alpha})`
29
+ if (value < 0.0349) return `rgba(135,211,43,${alpha})`
30
+ if (value < 0.0874) return `rgba(204,244,44,${alpha})`
31
+ if (value < 0.2679) return `rgba(245,233,44,${alpha})`
32
+ if (value < 0.7002) return `rgba(255,138,43,${alpha})`
33
+ if (value < 1.4281) return `rgba(255,84,43,${alpha})`
34
+ return `rgba(255,32,43,${alpha})`
35
+ }
36
+
37
+ // const openTip = () => {
38
+ // handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
39
+ // handler.setInputAction((movement) => {
40
+ // const pick = viewer.scene.pick(movement.endPosition)
41
+ // if (pick && pick.id && pick.id.type === "SlopeAspect") {
42
+ // toolTip = pick.id.value.toFixed(2)
43
+ // CreateRemindertip(toolTip, movement.endPosition, true)
44
+ // } else {
45
+ // toolTip = ""
46
+ // CreateRemindertip(toolTip, movement.endPosition, false)
47
+ // }
48
+ // }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
49
+ // }
50
+
51
+ // const closeTip = () => {
52
+ // if (handler) {
53
+ // handler.destroy()
54
+ // handler = null
55
+ // }
56
+ // }
57
+
58
+ const drawByDistance = (distance = 0.1, onDrawStart?: (ev: () => void) => void, onComplete?: () => void) => {
59
+ const width = distance * 200 > 35 ? 35 : distance * 200
60
+ arrowWidth = width < 15 ? 15 : width
61
+
62
+ CreatePolygonOnGround({
63
+ viewer,
64
+ collection: options.collection
65
+ }, [],
66
+ {
67
+ color: Cesium.Color.RED.withAlpha(0.1),
68
+ outlineColor: Cesium.Color.YELLOW,
69
+ outlineWidth: 2,
70
+ },
71
+ (polygon) => {
72
+ const degrees = Cartesian3ListToWGS84(polygon.pottingPoint)
73
+ options.collection.entities.remove(polygon)
74
+
75
+ const boundary = degrees.map((p) => [p.lng, p.lat])
76
+ const bbox = turf.bbox(turf.polygon([boundary.concat([boundary[0]])]))
77
+ const mask = turf.polygon([boundary.concat([boundary[0]])])
78
+ const gridSquare = turf.squareGrid(bbox, distance, { mask })
79
+ createEllipse(gridSquare)
80
+
81
+ onComplete && onComplete()
82
+ },
83
+ (drawHandler: () => void) => onDrawStart && onDrawStart(drawHandler)
84
+ )
85
+ }
86
+
87
+ const drawByNum = (num = 10, onDrawStart?: (ev: () => void) => void, onComplete?: () => void) => {
88
+ CreatePolygonOnGround({
89
+ viewer,
90
+ collection: options.collection
91
+ }, [],
92
+ {
93
+ color: Cesium.Color.RED.withAlpha(0.1),
94
+ outlineColor: Cesium.Color.YELLOW,
95
+ outlineWidth: 2,
96
+ },
97
+ (polygon) => {
98
+ const degrees = Cartesian3ListToWGS84(polygon.pottingPoint)
99
+ options.collection.entities.remove(polygon)
100
+
101
+ const boundary = degrees.map((p) => [p.lng, p.lat])
102
+ const bbox = turf.bbox(turf.polygon([boundary.concat([boundary[0]])]))
103
+
104
+ const [minX, minY, maxX, maxY] = bbox
105
+ const step = Math.max(maxX - minX, maxY - minY) / num
106
+
107
+ const width = step * 2000 > 35 ? 35 : step * 2000
108
+ arrowWidth = width < 15 ? 15 : width
109
+
110
+ const mask = turf.polygon([boundary.concat([boundary[0]])])
111
+ const gridSquare = turf.squareGrid(bbox, step, {
112
+ units: "degrees",
113
+ mask,
114
+ })
115
+ createEllipse(gridSquare)
116
+
117
+ onComplete && onComplete()
118
+ },
119
+ (drawHandler: () => void) => { onDrawStart && onDrawStart(drawHandler) }
120
+ )
121
+ }
122
+
123
+ const createEllipse = (gridSquare) => {
124
+ const boxResults = []
125
+
126
+ for (const feature of gridSquare.features) {
127
+ const coordinates = feature.geometry.coordinates[0]
128
+ const centerdegree = [
129
+ (coordinates[0][0] + coordinates[2][0]) / 2,
130
+ (coordinates[0][1] + coordinates[2][1]) / 2,
131
+ ]
132
+ const centerCarto = Cesium.Cartographic.fromDegrees(
133
+ centerdegree[0],
134
+ centerdegree[1]
135
+ )
136
+ boxResults.push(centerCarto)
137
+
138
+ for (let i = 0; i < coordinates.length; i++) {
139
+ const coord = coordinates[i]
140
+ const carto = Cesium.Cartographic.fromDegrees(coord[0], coord[1])
141
+ boxResults.push(carto)
142
+
143
+ const next = coordinates[i + 1]
144
+ if (next) {
145
+ const mid = Cesium.Cartographic.fromDegrees(
146
+ (coord[0] + next[0]) / 2,
147
+ (coord[1] + next[1]) / 2
148
+ )
149
+ boxResults.push(mid)
150
+ }
151
+ }
152
+ }
153
+
154
+ Cesium.sampleTerrainMostDetailed(viewer.scene.terrainProvider, boxResults)
155
+ .then((updatePositions) => {
156
+ const group = []
157
+ for (let i = 0; i < updatePositions.length; i += 10) {
158
+ const slice = updatePositions.slice(i, i + 10)
159
+ if (slice.length) group.push(slice)
160
+ }
161
+ calculateSlope(group)
162
+ })
163
+ }
164
+
165
+ const createPolygonInstance = (points, color) => {
166
+ const positions = points
167
+ .slice(1, -1)
168
+ .map((p) => Cesium.Cartographic.toCartesian(p))
169
+ const polygon = new Cesium.PolygonGeometry({
170
+ polygonHierarchy: new Cesium.PolygonHierarchy(positions),
171
+ })
172
+
173
+ return new Cesium.GeometryInstance({
174
+ geometry: polygon,
175
+ attributes: {
176
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
177
+ Cesium.Color.fromCssColorString(color)
178
+ ),
179
+ show: new Cesium.ShowGeometryInstanceAttribute(true),
180
+ },
181
+ })
182
+ }
183
+
184
+ const createArrowInstance = (target, center, diagonal, heightDiff, slope) => {
185
+ const mid0 = new Cesium.Cartographic(
186
+ (target.longitude + center.longitude) / 2,
187
+ (target.latitude + center.latitude) / 2,
188
+ (target.height + center.height) / 2
189
+ )
190
+ const mid1 = new Cesium.Cartographic(
191
+ (diagonal.longitude + center.longitude) / 2,
192
+ (diagonal.latitude + center.latitude) / 2,
193
+ (diagonal.height + center.height) / 2
194
+ )
195
+
196
+ const positions =
197
+ heightDiff > 0
198
+ ? [
199
+ Cesium.Cartographic.toCartesian(mid0),
200
+ Cesium.Cartographic.toCartesian(mid1),
201
+ ]
202
+ : [
203
+ Cesium.Cartographic.toCartesian(mid1),
204
+ Cesium.Cartographic.toCartesian(mid0),
205
+ ]
206
+
207
+ return new Cesium.GeometryInstance({
208
+ id: { type: "SlopeAspect", value: slope },
209
+ geometry: new Cesium.GroundPolylineGeometry({
210
+ positions,
211
+ width: arrowWidth,
212
+ }),
213
+ attributes: {
214
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
215
+ Cesium.Color.BLUE.withAlpha(0.6)
216
+ ),
217
+ show: new Cesium.ShowGeometryInstanceAttribute(true),
218
+ },
219
+ })
220
+ }
221
+
222
+ const calculateSlope = (groups) => {
223
+ const polygons = []
224
+ const arrows = []
225
+
226
+ for (const ellipse of groups) {
227
+ const center = ellipse[0]
228
+ let maxHD = 0
229
+ let maxIndex = 0
230
+
231
+ for (let i = 1; i < ellipse.length; i++) {
232
+ const hd = ellipse[i].height - center.height
233
+ if (Math.abs(hd) > Math.abs(maxHD)) {
234
+ maxHD = hd
235
+ maxIndex = i
236
+ }
237
+ }
238
+
239
+ const p0 = new Cesium.Cartographic(center.longitude, center.latitude, 0)
240
+ const p1 = new Cesium.Cartographic(
241
+ ellipse[maxIndex].longitude,
242
+ ellipse[maxIndex].latitude,
243
+ 0
244
+ )
245
+
246
+ const dist = Cesium.Cartesian3.distance(
247
+ Cesium.Cartographic.toCartesian(p0),
248
+ Cesium.Cartographic.toCartesian(p1)
249
+ )
250
+ const slope = Math.abs(maxHD / dist)
251
+ const color = calculateSlopeColor(slope, 0.4)
252
+
253
+ polygons.push(createPolygonInstance(ellipse, color))
254
+
255
+ const diag = ellipse[maxIndex > 4 ? maxIndex - 4 : maxIndex + 4]
256
+ arrows.push(createArrowInstance(ellipse[maxIndex], center, diag, maxHD, slope))
257
+ }
258
+
259
+ const polyPrimitive = viewer.scene.primitives.add(
260
+ new Cesium.GroundPrimitive({
261
+ geometryInstances: polygons,
262
+ appearance: new Cesium.PerInstanceColorAppearance({
263
+ translucent: true,
264
+ closed: false,
265
+ }),
266
+ })
267
+ )
268
+
269
+ const arrowPrimitive = viewer.scene.primitives.add(
270
+ new Cesium.GroundPolylinePrimitive({
271
+ geometryInstances: arrows,
272
+ appearance: new Cesium.PolylineMaterialAppearance({
273
+ material: new Cesium.Material({
274
+ fabric: {
275
+ type: "PolylineArrow",
276
+ uniforms: { color: new Cesium.Color(1, 1, 0, 0.8) },
277
+ },
278
+ }),
279
+ }),
280
+ })
281
+ )
282
+
283
+ result.push(polyPrimitive, arrowPrimitive)
284
+ }
285
+
286
+ const removeAll = () => {
287
+ result.forEach((p) => viewer.scene.primitives.remove(p))
288
+ result = []
289
+ }
290
+
291
+ return {
292
+ drawByDistance,
293
+ drawByNum,
294
+ removeAll
295
+ }
296
+ }
297
+
298
+ export {
299
+ useSlope
300
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-three-utils",
3
- "version": "0.0.347",
3
+ "version": "0.0.349",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"