@wind-cesium/wind-cesium-plugin 1.0.0 → 1.0.2

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,7 @@
1
+ import * as Cesium from 'cesium';
2
+ export declare function addOne(num: number): number;
3
+ declare const calculateVolume: (lngLatList: {
4
+ lng: number;
5
+ lat: number;
6
+ }[], viewer: Cesium.Viewer) => Promise<number | void>;
7
+ export { calculateVolume };
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /*
11
+ * @Author: zhubj | ''
12
+ * @Date: 2026-02-12 10:00:35
13
+ * @LastEditors: zhubj | ''
14
+ * @LastEditTime: 2026-02-24 13:12:26
15
+ * @FilePath: \wind-cesium-plugin\src\index.ts
16
+ * @Description:
17
+ */
18
+ import * as turf from '@turf/turf';
19
+ import * as Cesium from 'cesium';
20
+ import Delaunator from 'delaunator';
21
+ export function addOne(num) {
22
+ return num + 1;
23
+ }
24
+ console.log(addOne(3)); // 输出4
25
+ // 计算体积
26
+ const calculateVolume = (lngLatList, viewer) => __awaiter(void 0, void 0, void 0, function* () {
27
+ // 判断长度是否大于3
28
+ if (lngLatList.length < 4) {
29
+ return console.warn('计算体积至少需要4个点');
30
+ }
31
+ // turf 面
32
+ const polygonFeature = turf.polygon([lngLatList.map((item) => [item.lng, item.lat])]);
33
+ // 沿边界插点
34
+ const lineFeature = turf.lineString(lngLatList.map((item) => [item.lng, item.lat]));
35
+ const boundaryPoints = turf.lineChunk(lineFeature, 5, { units: 'meters' });
36
+ // 在面内部插点
37
+ const bbox = turf.bbox(polygonFeature);
38
+ const grid = turf.pointGrid(bbox, 5, { units: 'meters' });
39
+ // 只保留在 polygonFeature 内的点
40
+ const innerPoints = turf.pointsWithinPolygon(grid, polygonFeature);
41
+ // 把边界点转为点类型
42
+ const boundaryLinePoints = boundaryPoints.features.map((item) => turf.point(item.geometry.coordinates[0]));
43
+ // 合并所有点
44
+ const points = turf.featureCollection([...innerPoints.features, ...boundaryLinePoints]);
45
+ const coords = points.features.map((p) => p.geometry.coordinates);
46
+ const delaunay = Delaunator.from(coords);
47
+ const triangles = [];
48
+ for (let i = 0; i < delaunay.triangles.length; i += 3) {
49
+ const tri = [
50
+ coords[delaunay.triangles[i]],
51
+ coords[delaunay.triangles[i + 1]],
52
+ coords[delaunay.triangles[i + 2]],
53
+ coords[delaunay.triangles[i]]
54
+ ];
55
+ triangles.push(turf.polygon([tri]));
56
+ }
57
+ const cartesiansList = [];
58
+ triangles.forEach((item) => {
59
+ cartesiansList.push(Cesium.Cartesian3.fromDegrees(item.geometry.coordinates[0][0][0], item.geometry.coordinates[0][0][1]));
60
+ cartesiansList.push(Cesium.Cartesian3.fromDegrees(item.geometry.coordinates[0][1][0], item.geometry.coordinates[0][1][1]));
61
+ cartesiansList.push(Cesium.Cartesian3.fromDegrees(item.geometry.coordinates[0][2][0], item.geometry.coordinates[0][2][1]));
62
+ });
63
+ // 获取模型高度
64
+ const modelSamples = yield (viewer === null || viewer === void 0 ? void 0 : viewer.scene.clampToHeightMostDetailed(cartesiansList));
65
+ // 绘面
66
+ console.log('modelSamples', modelSamples);
67
+ let minHeight = Infinity;
68
+ let maxHeight = -Infinity;
69
+ let totalArea = 0;
70
+ let totalVolum = 0;
71
+ const modelSamplesRadList = [];
72
+ if (modelSamples && modelSamples.length) {
73
+ // 计算出高度
74
+ for (let i = 0; i < modelSamples.length; i++) {
75
+ const lngLatRad = Cesium.Cartographic.fromCartesian(modelSamples[i]);
76
+ minHeight = Math.min(minHeight, lngLatRad.height);
77
+ maxHeight = Math.max(maxHeight, lngLatRad.height);
78
+ modelSamplesRadList.push({
79
+ lng: Cesium.Math.toDegrees(lngLatRad.longitude),
80
+ lat: Cesium.Math.toDegrees(lngLatRad.latitude),
81
+ height: lngLatRad.height
82
+ });
83
+ }
84
+ console.log('最高高度和最低高度为', minHeight, maxHeight);
85
+ }
86
+ if (modelSamples && modelSamples.length) {
87
+ for (let i = 0; i < modelSamples.length; i = i + 3) {
88
+ const v1 = Cesium.Cartesian3.subtract(modelSamples[i + 1], modelSamples[i], new Cesium.Cartesian3());
89
+ const v2 = Cesium.Cartesian3.subtract(modelSamples[i + 2], modelSamples[i], new Cesium.Cartesian3());
90
+ const cross = Cesium.Cartesian3.cross(v1, v2, new Cesium.Cartesian3());
91
+ const area1 = Cesium.Cartesian3.magnitude(cross) * 0.5;
92
+ // 面积
93
+ totalArea = totalArea + area1;
94
+ // 体积
95
+ const sjPolygon = turf.polygon([
96
+ [
97
+ [modelSamplesRadList[i].lng, modelSamplesRadList[i].lat],
98
+ [modelSamplesRadList[i + 1].lng, modelSamplesRadList[i + 1].lat],
99
+ [modelSamplesRadList[i + 2].lng, modelSamplesRadList[i + 2].lat],
100
+ [modelSamplesRadList[i].lng, modelSamplesRadList[i].lat]
101
+ ]
102
+ ]);
103
+ const sjArea = turf.area(sjPolygon);
104
+ const sjMinHeight = Math.min(modelSamplesRadList[i].height, modelSamplesRadList[i + 1].height, modelSamplesRadList[i + 2].height);
105
+ const sjMaxHeight = Math.max(modelSamplesRadList[i].height, modelSamplesRadList[i + 1].height, modelSamplesRadList[i + 2].height);
106
+ totalVolum =
107
+ totalVolum +
108
+ (sjArea * (sjMinHeight - minHeight) + (sjArea * (sjMaxHeight - sjMinHeight)) / 3);
109
+ // const avaHeight =
110
+ // (modelSamplesRadList[i].height +
111
+ // modelSamplesRadList[i + 1].height +
112
+ // modelSamplesRadList[i + 2].height) /
113
+ // 3 -
114
+ // minHeight
115
+ // totalVolum = totalVolum + avaHeight * area1
116
+ }
117
+ console.log('totalVolum', totalVolum);
118
+ return totalVolum;
119
+ }
120
+ // 判断起始点和结束点是否一致,定位闭合
121
+ return 0;
122
+ });
123
+ export { calculateVolume };
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@wind-cesium/wind-cesium-plugin",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "cesium 的一些扩展方案实现",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "test": "vitest",
9
+ "test:run": "vitest run",
10
+ "build": "tsc",
11
+ "prepublishOnly": "npm run build"
8
12
  },
9
13
  "keywords": [],
10
14
  "author": "with-the-winds",
@@ -12,5 +16,15 @@
12
16
  "repository": {
13
17
  "type": "git",
14
18
  "url": "https://gitee.com/with_the_winds/wind-cesium-plugin.git"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^25.2.3",
22
+ "typescript": "^5.9.3",
23
+ "vitest": "^4.0.18"
24
+ },
25
+ "dependencies": {
26
+ "@turf/turf": "^7.3.4",
27
+ "cesium": "^1.138.0",
28
+ "delaunator": "^5.0.1"
15
29
  }
16
30
  }
package/src/index.ts ADDED
@@ -0,0 +1,174 @@
1
+ /*
2
+ * @Author: zhubj | ''
3
+ * @Date: 2026-02-12 10:00:35
4
+ * @LastEditors: zhubj | ''
5
+ * @LastEditTime: 2026-02-24 13:12:26
6
+ * @FilePath: \wind-cesium-plugin\src\index.ts
7
+ * @Description:
8
+ */
9
+ import * as turf from '@turf/turf'
10
+ import * as Cesium from 'cesium'
11
+ import Delaunator from 'delaunator'
12
+
13
+
14
+
15
+ export function addOne(num: number): number {
16
+ return num + 1;
17
+ }
18
+
19
+ console.log(addOne(3)); // 输出4
20
+
21
+
22
+ // 计算体积
23
+ const calculateVolume = async (lngLatList:{lng: number, lat: number}[],viewer:Cesium.Viewer) => {
24
+ // 判断长度是否大于3
25
+ if (lngLatList.length < 4) {
26
+ return console.warn('计算体积至少需要4个点');
27
+ }
28
+
29
+ // turf 面
30
+ const polygonFeature = turf.polygon([lngLatList.map((item) => [item.lng, item.lat])])
31
+
32
+ // 沿边界插点
33
+ const lineFeature = turf.lineString(lngLatList.map((item) => [item.lng, item.lat]))
34
+ const boundaryPoints = turf.lineChunk(lineFeature, 5, { units: 'meters' })
35
+
36
+ // 在面内部插点
37
+ const bbox = turf.bbox(polygonFeature)
38
+ const grid = turf.pointGrid(bbox, 5, { units: 'meters' })
39
+
40
+ // 只保留在 polygonFeature 内的点
41
+ const innerPoints = turf.pointsWithinPolygon(grid, polygonFeature)
42
+
43
+ // 把边界点转为点类型
44
+ const boundaryLinePoints = boundaryPoints.features.map((item) =>
45
+ turf.point(item.geometry.coordinates[0])
46
+ )
47
+
48
+ // 合并所有点
49
+ const points = turf.featureCollection([...innerPoints.features, ...boundaryLinePoints])
50
+
51
+ const coords = points.features.map((p) => p.geometry.coordinates)
52
+ const delaunay = Delaunator.from(coords)
53
+
54
+ const triangles = []
55
+
56
+ for (let i = 0; i < delaunay.triangles.length; i += 3) {
57
+ const tri: any = [
58
+ coords[delaunay.triangles[i]],
59
+ coords[delaunay.triangles[i + 1]],
60
+ coords[delaunay.triangles[i + 2]],
61
+ coords[delaunay.triangles[i]]
62
+ ]
63
+ triangles.push(turf.polygon([tri]))
64
+ }
65
+
66
+ const cartesiansList: Cesium.Cartesian3[] = []
67
+ triangles.forEach((item) => {
68
+ cartesiansList.push(
69
+ Cesium.Cartesian3.fromDegrees(
70
+ item.geometry.coordinates[0][0][0],
71
+ item.geometry.coordinates[0][0][1]
72
+ )
73
+ )
74
+ cartesiansList.push(
75
+ Cesium.Cartesian3.fromDegrees(
76
+ item.geometry.coordinates[0][1][0],
77
+ item.geometry.coordinates[0][1][1]
78
+ )
79
+ )
80
+ cartesiansList.push(
81
+ Cesium.Cartesian3.fromDegrees(
82
+ item.geometry.coordinates[0][2][0],
83
+ item.geometry.coordinates[0][2][1]
84
+ )
85
+ )
86
+ })
87
+
88
+ // 获取模型高度
89
+ const modelSamples = await viewer?.scene.clampToHeightMostDetailed(cartesiansList)
90
+
91
+ // 绘面
92
+ console.log('modelSamples', modelSamples)
93
+ let minHeight = Infinity
94
+ let maxHeight = -Infinity
95
+ let totalArea = 0
96
+ let totalVolum = 0
97
+ const modelSamplesRadList = []
98
+ if (modelSamples && modelSamples.length) {
99
+ // 计算出高度
100
+ for (let i = 0; i < modelSamples.length; i++) {
101
+ const lngLatRad = Cesium.Cartographic.fromCartesian(modelSamples[i])
102
+ minHeight = Math.min(minHeight, lngLatRad.height)
103
+ maxHeight = Math.max(maxHeight, lngLatRad.height)
104
+ modelSamplesRadList.push({
105
+ lng: Cesium.Math.toDegrees(lngLatRad.longitude),
106
+ lat: Cesium.Math.toDegrees(lngLatRad.latitude),
107
+ height: lngLatRad.height
108
+ })
109
+ }
110
+ console.log('最高高度和最低高度为', minHeight, maxHeight)
111
+ }
112
+
113
+ if (modelSamples && modelSamples.length) {
114
+ for (let i = 0; i < modelSamples.length; i = i + 3) {
115
+ const v1 = Cesium.Cartesian3.subtract(
116
+ modelSamples[i + 1],
117
+ modelSamples[i],
118
+ new Cesium.Cartesian3()
119
+ )
120
+ const v2 = Cesium.Cartesian3.subtract(
121
+ modelSamples[i + 2],
122
+ modelSamples[i],
123
+ new Cesium.Cartesian3()
124
+ )
125
+ const cross = Cesium.Cartesian3.cross(v1, v2, new Cesium.Cartesian3())
126
+ const area1 = Cesium.Cartesian3.magnitude(cross) * 0.5
127
+ // 面积
128
+ totalArea = totalArea + area1
129
+
130
+ // 体积
131
+ const sjPolygon = turf.polygon([
132
+ [
133
+ [modelSamplesRadList[i].lng, modelSamplesRadList[i].lat],
134
+ [modelSamplesRadList[i + 1].lng, modelSamplesRadList[i + 1].lat],
135
+ [modelSamplesRadList[i + 2].lng, modelSamplesRadList[i + 2].lat],
136
+ [modelSamplesRadList[i].lng, modelSamplesRadList[i].lat]
137
+ ]
138
+ ])
139
+ const sjArea = turf.area(sjPolygon)
140
+ const sjMinHeight = Math.min(
141
+ modelSamplesRadList[i].height,
142
+ modelSamplesRadList[i + 1].height,
143
+ modelSamplesRadList[i + 2].height
144
+ )
145
+ const sjMaxHeight = Math.max(
146
+ modelSamplesRadList[i].height,
147
+ modelSamplesRadList[i + 1].height,
148
+ modelSamplesRadList[i + 2].height
149
+ )
150
+
151
+ totalVolum =
152
+ totalVolum +
153
+ (sjArea * (sjMinHeight - minHeight) + (sjArea * (sjMaxHeight - sjMinHeight)) / 3)
154
+
155
+ // const avaHeight =
156
+ // (modelSamplesRadList[i].height +
157
+ // modelSamplesRadList[i + 1].height +
158
+ // modelSamplesRadList[i + 2].height) /
159
+ // 3 -
160
+ // minHeight
161
+ // totalVolum = totalVolum + avaHeight * area1
162
+ }
163
+ console.log('totalVolum', totalVolum)
164
+ return totalVolum
165
+ }
166
+
167
+
168
+
169
+ // 判断起始点和结束点是否一致,定位闭合
170
+ return 0
171
+
172
+ }
173
+
174
+ export {calculateVolume}
package/test/index.ts ADDED
@@ -0,0 +1,25 @@
1
+ import * as Cesium from 'cesium'
2
+ import {describe, it, expect} from 'vitest'
3
+ import {calculateVolume} from '../src/index'
4
+
5
+
6
+ const pointList = [
7
+ { lng: 120.06255, lat: 30.37728, value: 60 },
8
+ { lng: 120.06321, lat: 30.37711, value: 80 },
9
+ { lng: 120.06322, lat: 30.37745, value: 40 },
10
+ { lng: 120.06353, lat: 30.37791, value: 45 },
11
+ { lng: 120.06255, lat: 30.37728, value: 60 },
12
+ ]
13
+
14
+
15
+
16
+ describe('计算体积', () => {
17
+ const cesiumPolinList = pointList.map(item => {
18
+ return Cesium.Cartesian3.fromDegrees(item.lng, item.lat, 0)
19
+ })
20
+
21
+
22
+
23
+ calculateVolume(cesiumPolinList)
24
+ })
25
+
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "es6",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "rootDir": "./src",
8
+ "skipLibCheck": true,
9
+ "moduleResolution": "node",
10
+ "esModuleInterop": true,
11
+ "types": [
12
+ "vitest/globals"
13
+ ]
14
+ },
15
+ "include": [
16
+ "src/**/*"
17
+ ],
18
+ "exclude": [
19
+ "node_modules",
20
+ "**/*.spec.ts"
21
+ ]
22
+ }
@@ -0,0 +1,16 @@
1
+ /*
2
+ * @Author: zhubj | ''
3
+ * @Date: 2026-02-24 10:37:16
4
+ * @LastEditors: zhubj | ''
5
+ * @LastEditTime: 2026-02-24 10:38:37
6
+ * @FilePath: \wind-cesium-plugin\vitest.config.ts
7
+ * @Description:
8
+ */
9
+ import {defineConfig} from 'vitest/config'
10
+
11
+ export default defineConfig({
12
+ test: {
13
+ environment: 'node',
14
+ include: ['test/**/*.ts'],
15
+ },
16
+ })
package/index.js DELETED
@@ -1,5 +0,0 @@
1
- function sayHello() {
2
- console.log('Hello World!');
3
- }
4
-
5
- module.exports = sayHello;