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.
@@ -0,0 +1,270 @@
1
+ import * as Cesium from 'cesium'
2
+
3
+ const useProfileAnalysis = (viewer: Cesium.Viewer, options: {
4
+ collection: Cesium.CustomDataSource
5
+ }) => {
6
+
7
+ let handler = null
8
+ let positions = []
9
+ let positionsCartographic = []
10
+ let positions_Inter = []
11
+ let poly = null
12
+ let distance = null
13
+ let cartesian = null
14
+ let DistanceArray = []
15
+ let profileItem: {
16
+ distance: number
17
+ point: ReturnType<typeof cartesian3ToDegrees>
18
+ }[] = []
19
+
20
+ class PolyLinePrimitive {
21
+ options: Cesium.Entity.ConstructorOptions
22
+ positions: Cesium.Cartesian3[]
23
+
24
+ constructor(positions: Cesium.Cartesian3[]) {
25
+ this.options = {
26
+ polyline: {
27
+ show: true,
28
+ positions: [],
29
+ width: 3,
30
+ material: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
31
+ clampToGround: true
32
+ }
33
+ }
34
+ this.positions = positions
35
+ this.init()
36
+ }
37
+
38
+ init = () => {
39
+ const _self = this
40
+ const _update = function () {
41
+ return _self.positions
42
+ }
43
+ this.options.polyline.positions = new Cesium.CallbackProperty(_update, false)
44
+ options.collection.entities.add(this.options)
45
+ }
46
+ }
47
+
48
+ const draw = (cb: (data: {
49
+ min: number;
50
+ value: (number | string)[][]
51
+ }) => void) => {
52
+ if (handler) {
53
+ console.log('请使用右键结束上次测量!')
54
+ return
55
+ }
56
+ options.collection.entities.removeAll()
57
+ handler = new Cesium.ScreenSpaceEventHandler((viewer.scene as any)._imageryLayerCollection)
58
+ leftClickEvent()
59
+ mouseMoveEvent()
60
+ rightClickEvent(cb)
61
+ }
62
+
63
+ const stopDraw = () => {
64
+ if (handler) {
65
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
66
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
67
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
68
+ handler.destroy()
69
+ handler = null
70
+ }
71
+
72
+ positions = []
73
+ positionsCartographic = []
74
+ positions_Inter = []
75
+ poly = null
76
+ distance = null
77
+ cartesian = null
78
+ DistanceArray = []
79
+ profileItem = []
80
+ }
81
+
82
+ const removeAll = () => {
83
+ stopDraw()
84
+ options.collection.entities.removeAll()
85
+ }
86
+
87
+ const leftClickEvent = () => {
88
+ handler.setInputAction(((movement) => {
89
+ cartesian = viewer.scene.pickPosition(movement.position)
90
+ if (positions.length == 0) {
91
+ positions.push(cartesian.clone())
92
+ }
93
+ positions.push(cartesian)
94
+ if (poly) {
95
+ interPoints(poly.positions)
96
+ distance = getSpaceDistance(positions_Inter)
97
+ } else {
98
+ distance = getSpaceDistance(positions)
99
+ }
100
+
101
+ const textDisance = distance + "米"
102
+ DistanceArray.push(distance)
103
+ options.collection.entities.add({
104
+ position: positions[positions.length - 1],
105
+ point: {
106
+ color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
107
+ outlineColor: Cesium.Color.WHITE,
108
+ outlineWidth: 2,
109
+ pixelSize: 10
110
+ },
111
+ label: {
112
+ text: textDisance,
113
+ font: '18px sans-serif',
114
+ fillColor: Cesium.Color.GOLD,
115
+ style: Cesium.LabelStyle.FILL_AND_OUTLINE,
116
+ outlineWidth: 2,
117
+ verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
118
+ pixelOffset: new Cesium.Cartesian2(20, -20),
119
+ heightReference: Cesium.HeightReference.NONE
120
+ }
121
+ })
122
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.LEFT_CLICK)
123
+ }
124
+
125
+ const mouseMoveEvent = () => {
126
+ handler.setInputAction(((movement) => {
127
+ cartesian = viewer.scene.pickPosition(movement.endPosition)
128
+ if (positions.length >= 2) {
129
+ if (!Cesium.defined(poly)) {
130
+ poly = new PolyLinePrimitive(positions)
131
+ } else {
132
+ positions.pop()
133
+ positions.push(cartesian)
134
+ }
135
+ }
136
+ }) as Cesium.ScreenSpaceEventHandler.MotionEventCallback, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
137
+ }
138
+
139
+ const rightClickEvent = (cb: (data: {
140
+ min: number;
141
+ value: (number | string)[][]
142
+ }) => void) => {
143
+ handler.setInputAction((() => {
144
+ positions.pop()
145
+ createProfileChart(profileItem, cb)
146
+ stopDraw()
147
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
148
+ }
149
+
150
+ const cartesian3ToDegrees = (pos: Cesium.Cartesian3) => {
151
+ const ellipsoid = viewer.scene.globe.ellipsoid
152
+ const cartographic = ellipsoid.cartesianToCartographic(pos)
153
+ const lat = Cesium.Math.toDegrees(cartographic.latitude)
154
+ const lon = Cesium.Math.toDegrees(cartographic.longitude)
155
+ const height = cartographic.height
156
+
157
+ return {
158
+ lat,
159
+ lon,
160
+ height
161
+ }
162
+ }
163
+
164
+ const getSpaceDistance = (positions: Cesium.Cartesian3[]) => {
165
+ profileItem = [
166
+ {
167
+ point: cartesian3ToDegrees(positions[0]),
168
+ distance: 0
169
+ }
170
+ ]
171
+ let distance = 0
172
+ for (let i = 0; i < positions.length - 1; i++) {
173
+ const point1cartographic = Cesium.Cartographic.fromCartesian(positions[i])
174
+ const point2cartographic = Cesium.Cartographic.fromCartesian(positions[i + 1])
175
+ const geodesic = new Cesium.EllipsoidGeodesic()
176
+ geodesic.setEndPoints(point1cartographic, point2cartographic)
177
+ let s = geodesic.surfaceDistance
178
+ s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2))
179
+ distance = distance + s
180
+
181
+ const m_Item = {
182
+ point: cartesian3ToDegrees(positions[i + 1]),
183
+ distance: distance
184
+ }
185
+ profileItem.push(m_Item)
186
+ }
187
+ return distance.toFixed(2)
188
+ }
189
+
190
+ const interPoints = (positions: Cesium.Cartesian3[]) => {
191
+ positionsCartographic = []
192
+ var terrainSamplePositions = []
193
+ for (let index = 0; index < positions.length - 1; index++) {
194
+ const element = positions[index]
195
+ var ellipsoid = viewer.scene.globe.ellipsoid
196
+ var cartographic = ellipsoid.cartesianToCartographic(element)
197
+ positionsCartographic.push(cartographic)
198
+ }
199
+ for (let i = 0; i < positionsCartographic.length; i++) {
200
+ const m_Cartographic0 = positionsCartographic[i]
201
+ const m_Cartographic1 = positionsCartographic[i + 1]
202
+ if (m_Cartographic1) {
203
+ var a = Math.abs(m_Cartographic0.longitude - m_Cartographic1.longitude) * 10000000
204
+ var b = Math.abs(m_Cartographic0.latitude - m_Cartographic1.latitude) * 10000000
205
+ if (a > b) b = a
206
+ let length = parseInt((b / 2) as any)
207
+ if (length > 1000) length = 1000
208
+ if (length < 2) length = 2
209
+ for (var j = 0; j < length; j++) {
210
+ terrainSamplePositions.push(
211
+ new Cesium.Cartographic(
212
+ Cesium.Math.lerp(m_Cartographic0.longitude, m_Cartographic1.longitude, j / (length - 1)),
213
+ Cesium.Math.lerp(m_Cartographic0.latitude, m_Cartographic1.latitude, j / (length - 1))
214
+ )
215
+ )
216
+ }
217
+ terrainSamplePositions.pop()
218
+ } else {
219
+ terrainSamplePositions.push(m_Cartographic0)
220
+ }
221
+ }
222
+ positions_Inter = []
223
+ for (var n = 0; n < terrainSamplePositions.length; n++) {
224
+ var m_cartographic = terrainSamplePositions[n]
225
+ var height = viewer.scene.globe.getHeight(m_cartographic)
226
+ var point = Cesium.Cartesian3.fromDegrees(m_cartographic.longitude / Math.PI * 180, m_cartographic.latitude / Math.PI * 180, height)
227
+ positions_Inter.push(point)
228
+ }
229
+ }
230
+
231
+ const createProfileChart = (pos: typeof profileItem, cb: (data: {
232
+ min: number;
233
+ value: (number | string)[][]
234
+ }) => void) => {
235
+ const ProfileData = []
236
+ const ProfileData_Lon = []
237
+
238
+ let min = 0
239
+
240
+ for (let index = 0; index < pos.length; index++) {
241
+ const el = pos[index]
242
+ const m_distance = el.distance.toFixed(2)
243
+
244
+ const m_Lon = el.point.lon.toFixed(5)
245
+ const m_Lat = el.point.lat.toFixed(5)
246
+ const m_height = Number(el.point.height.toFixed(2))
247
+
248
+ if (m_height < min) {
249
+ min = m_height
250
+ }
251
+ var m_data = [m_distance, m_height]
252
+ ProfileData.push(m_data)
253
+ ProfileData_Lon.push([m_Lon, m_Lat])
254
+ }
255
+
256
+ cb({
257
+ min,
258
+ value: ProfileData
259
+ })
260
+ }
261
+
262
+ return {
263
+ draw,
264
+ removeAll
265
+ }
266
+ }
267
+
268
+ export {
269
+ useProfileAnalysis
270
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-three-utils",
3
- "version": "0.0.348",
3
+ "version": "0.0.350",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -8,5 +8,5 @@
8
8
  "author": "",
9
9
  "license": "ISC",
10
10
  "types": "./index.d.ts",
11
- "description": "更新长度测量"
11
+ "description": "更新j标完成绘制回显功能和按照id删除功能"
12
12
  }
@@ -1,191 +0,0 @@
1
- import CreateRemindertip from "./ReminderTip";
2
- import * as Cesium from 'cesium'
3
-
4
- const CreatePolygonOnGround = function (viewer, resultList, options, callback, cb) {
5
- if (!viewer) throw new Error("no viewer object!");
6
- options = options || {};
7
- let id = options.id || setSessionid(); //Polygon的id
8
- if (viewer.entities.getById(id))
9
- throw new Error("the id parameter is an unique value");
10
- let color = options.color || Cesium.Color.RED; //Polygon的填充色
11
- let outlineColor = options.outlineColor || color.withAlpha(1); //Polygon的轮廓线颜色
12
- let outlineWidth = options.outlineWidth || 2; //Polygon的轮廓线宽度
13
- const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
14
- cb && cb(handler)
15
- let toolTip = "左键点击开始绘制";
16
- let anchorpoints = [];
17
- let polygon = undefined;
18
- let drawStatus = true;
19
- handler.setInputAction(function (event) {
20
- let pixPos = event.position;
21
- let cartesian = getCatesian3FromPX(viewer, pixPos);
22
- if (anchorpoints.length == 0) {
23
- toolTip = "左键添加第二个点";
24
- anchorpoints.push(cartesian);
25
- let linePoints = new Cesium.CallbackProperty(function () {
26
- let verPoints = anchorpoints.concat([anchorpoints[0]]);
27
- return verPoints;
28
- }, false);
29
- let dynamicPositions = new Cesium.CallbackProperty(function () {
30
- return new Cesium.PolygonHierarchy(anchorpoints);
31
- }, false);
32
- polygon = viewer.entities.add({
33
- name: "Polygon",
34
- id: id,
35
- polyline: {
36
- positions: linePoints,
37
- width: outlineWidth,
38
- material: outlineColor,
39
- clampToGround: true,
40
- },
41
- polygon: {
42
- heightReference: Cesium.HeightReference.None,
43
- hierarchy: dynamicPositions,
44
- material: color,
45
- },
46
- });
47
- polygon.GeoType = "Polygon";
48
- } else {
49
- toolTip = "左键添加点,Ctrl+Z回退,右键完成绘制";
50
- }
51
- anchorpoints.push(cartesian);
52
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
53
-
54
- handler.setInputAction(function (movement) {
55
- let endPos = movement.endPosition;
56
- // CreateRemindertip(toolTip, endPos, true);
57
- if (Cesium.defined(polygon)) {
58
- anchorpoints.pop();
59
- let cartesian = getCatesian3FromPX(viewer, endPos);
60
- anchorpoints.push(cartesian);
61
- }
62
- if (anchorpoints.length === 3) {
63
- polygon.polygon.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND;
64
- }
65
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
66
-
67
- handler.setInputAction(function (event) {
68
- anchorpoints.pop();
69
- polygon.pottingPoint = anchorpoints;
70
- resultList.push(polygon);
71
- handler.destroy();
72
- // CreateRemindertip(toolTip, event.position, false);
73
- drawStatus = false;
74
- if (typeof callback == "function") callback(polygon);
75
- }, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
76
-
77
- //Ctrl + Z回退
78
- document.onkeydown = function (event) {
79
- if (event.ctrlKey && window.event.keyCode == 90) {
80
- if (!drawStatus) {
81
- return false;
82
- }
83
- anchorpoints.pop();
84
- if (anchorpoints.length == 2) {
85
- toolTip = "左键添加第二个点";
86
- }
87
- }
88
- };
89
- };
90
- function getCatesian3FromPX(viewer, px) {
91
- let picks = viewer.scene.drillPick(px);
92
- let cartesian = null;
93
- let isOn3dtiles = false,
94
- isOnTerrain = false;
95
- // drillPick
96
- for (let i in picks) {
97
- let pick = picks[i];
98
- if (
99
- (pick && pick.primitive instanceof Cesium.Cesium3DTileFeature) ||
100
- (pick && pick.primitive instanceof Cesium.Cesium3DTileset) ||
101
- (pick && pick.primitive instanceof Cesium.Model)
102
- ) {
103
- //模型上拾取
104
- isOn3dtiles = true;
105
- }
106
- // 3dtilset
107
- if (isOn3dtiles) {
108
- viewer.scene.pick(px);
109
- cartesian = viewer.scene.pickPosition(px);
110
- if (cartesian) {
111
- let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
112
- if (cartographic.height < 0) cartographic.height = 0;
113
- let lon = Cesium.Math.toDegrees(cartographic.longitude),
114
- lat = Cesium.Math.toDegrees(cartographic.latitude),
115
- height = cartographic.height;
116
- cartesian = transformWGS84ToCartesian(viewer, {
117
- lng: lon,
118
- lat: lat,
119
- alt: height,
120
- });
121
- }
122
- }
123
- }
124
- // 地形
125
- let boolTerrain =
126
- viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider;
127
- // Terrain
128
- if (!isOn3dtiles && !boolTerrain) {
129
- let ray = viewer.scene.camera.getPickRay(px);
130
- if (!ray) return null;
131
- cartesian = viewer.scene.globe.pick(ray, viewer.scene);
132
- isOnTerrain = true;
133
- }
134
- // 地球
135
- if (!isOn3dtiles && !isOnTerrain && boolTerrain) {
136
- cartesian = viewer.scene.camera.pickEllipsoid(
137
- px,
138
- viewer.scene.globe.ellipsoid
139
- );
140
- }
141
- if (cartesian) {
142
- let position = transformCartesianToWGS84(viewer, cartesian);
143
- if (position.alt < 0) {
144
- cartesian = transformWGS84ToCartesian(viewer, position, 0.1);
145
- }
146
- return cartesian;
147
- }
148
- return false;
149
- }
150
-
151
- /***
152
- * 坐标转换 84转笛卡尔
153
- * @param {Object} {lng,lat,alt} 地理坐标
154
- * @return {Object} Cartesian3 三维位置坐标
155
- */
156
- function transformWGS84ToCartesian(viewer, position, alt) {
157
- return position
158
- ? Cesium.Cartesian3.fromDegrees(
159
- position.lng || position.lon,
160
- position.lat,
161
- (position.alt = alt || position.alt),
162
- Cesium.Ellipsoid.WGS84
163
- )
164
- : Cesium.Cartesian3.ZERO;
165
- }
166
-
167
- /***
168
- * 坐标转换 笛卡尔转84
169
- * @param {Object} Cartesian3 三维位置坐标
170
- * @return {Object} {lng,lat,alt} 地理坐标
171
- */
172
- function transformCartesianToWGS84(viewer, cartesian) {
173
- let ellipsoid = Cesium.Ellipsoid.WGS84;
174
- let cartographic = ellipsoid.cartesianToCartographic(cartesian);
175
- return {
176
- lng: Cesium.Math.toDegrees(cartographic.longitude),
177
- lat: Cesium.Math.toDegrees(cartographic.latitude),
178
- alt: cartographic.height,
179
- };
180
- }
181
- function setSessionid(num) {
182
- let len = num || 32;
183
- let chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
184
- let maxPos = chars.length;
185
- let pwd = "";
186
- for (let i = 0; i < len; i++) {
187
- pwd += chars.charAt(Math.floor(Math.random() * maxPos));
188
- }
189
- return pwd;
190
- }
191
- export default CreatePolygonOnGround;
@@ -1,71 +0,0 @@
1
- const CreateRemindertip = function (arr, position, show) {
2
- let tooltip = document.getElementById("toolTip");
3
- let style, _x, _y, _color;
4
- if (arr && typeof arr === "object") {
5
- style = arr;
6
- }
7
- if (style && style.origin) {
8
- style.origin === "center" && ((_x = 15), (_y = -12));
9
- style.origin === "top" && ((_x = 15), (_y = -44));
10
- style.origin === "bottom" && ((_x = 15), (_y = 20));
11
- } else {
12
- (_x = 15), (_y = 20);
13
- }
14
- if (style && style.color) {
15
- style.color === "white" &&
16
- (_color = "background: rgba(255, 255, 255, 0.8);color: black;");
17
- style.color === "black" &&
18
- (_color = "background: rgba(0, 0, 0, 0.5);color: white;");
19
- style.color === "yellow" &&
20
- (_color =
21
- "color: black;background-color: #ffcc33;border: 1px solid white;");
22
- } else {
23
- _color = "background: rgba(0, 0, 0, 0.5);color: white;";
24
- }
25
- if (!tooltip) {
26
- const viewerDom = document.getElementsByClassName("cesium-viewer")[0];
27
- let elementbottom = document.createElement("div");
28
- viewerDom.append(elementbottom);
29
- let html =
30
- '<div id="toolTip" style="display: none;pointer-events: none;position: absolute;z-index: 1000;opacity: 0.8;border-radius: 4px;padding: 4px 8px;white-space: nowrap;font-family:黑体;color:white;font-weight: bolder;font-size: 14px;' +
31
- _color +
32
- '"></div>';
33
- viewerDom.insertAdjacentHTML("beforeend", html);
34
- tooltip = document.getElementById("toolTip");
35
- }
36
- if (show) {
37
- tooltip.innerHTML = arr;
38
- tooltip.style.left = position.x + _x + "px";
39
- tooltip.style.top = position.y + _y + "px";
40
- tooltip.style.display = "block";
41
- } else {
42
- tooltip.style.display = "none";
43
- }
44
- return {
45
- tooltip: tooltip,
46
- style: style,
47
- showAt: function (position, text) {
48
- this.tooltip.innerHTML = text;
49
- if (this.style && this.style.origin) {
50
- this.style.origin === "center" &&
51
- ((_x = 15), (_y = -this.tooltip.offsetHeight / 2));
52
- this.style.origin === "top" &&
53
- ((_x = 15), (_y = -this.tooltip.offsetHeight - 20));
54
- this.style.origin === "bottom" && ((_x = 15), (_y = 20));
55
- } else {
56
- (_x = 15), (_y = -this.tooltip.offsetHeight / 2);
57
- }
58
- this.tooltip.style.left = position.x + _x + "px";
59
- this.tooltip.style.top = position.y + _y + "px";
60
- this.tooltip.style.display = "block";
61
- },
62
- show: function (show) {
63
- if (show) {
64
- this.tooltip.style.display = "block";
65
- } else {
66
- this.tooltip.style.display = "none";
67
- }
68
- },
69
- };
70
- };
71
- export default CreateRemindertip;