easy-three-utils 0.0.331 → 0.0.332

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,191 @@
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;
@@ -0,0 +1,71 @@
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;