cesium-alpha-earth 1.0.0
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.
- package/dist/index.js +214371 -0
- package/index.html +16 -0
- package/package.json +29 -0
- package/src/core/CesiumAlphaGeoJsonLayer/index.ts +815 -0
- package/src/core/CesiumAlphaGeoJsonLayer/method/geojson-util.ts +374 -0
- package/src/core/CesiumAlphaGeoJsonLayer/type.ts +108 -0
- package/src/core/CesiumAlphaViewer/index.ts +106 -0
- package/src/core/CesiumAlphaViewer/method/clusterFun.ts +116 -0
- package/src/core/CesiumAlphaViewer/method/createViewer.ts +53 -0
- package/src/core/CesiumAlphaViewer/method/goTo.ts +108 -0
- package/src/core/CesiumAlphaViewer/method/load3DtilestModel.ts +56 -0
- package/src/core/CesiumAlphaViewer/method/loadGeoJsonLayer.ts +33 -0
- package/src/core/CesiumAlphaViewer/type.ts +141 -0
- package/src/index.ts +10 -0
- package/src/util/PrimitiveCuster/index.js +1000 -0
- package/src/util/getPositionCenters/getPositionCenter.ts +25 -0
- package/src/util/parseColor/index.ts +39 -0
- package/tsconfig.json +28 -0
- package/types/core/CesiumAlphaViewer/index.d.ts +7 -0
- package/types/core/CesiumAlphaViewer/method/createViewer.d.ts +3 -0
- package/types/core/CesiumAlphaViewer/method/goTo.d.ts +3 -0
- package/types/core/CesiumAlphaViewer/type.d.ts +77 -0
- package/types/index.d.ts +2 -0
- package/viewTest/App.vue +38 -0
- package/viewTest/main.ts +10 -0
- package/viewTest/style/style.scss +4 -0
- package/vite.config.ts +69 -0
|
@@ -0,0 +1,815 @@
|
|
|
1
|
+
import { GeoJsonLayerType } from "./type";
|
|
2
|
+
import { nanoid } from "nanoid";
|
|
3
|
+
import * as Cesium from "cesium";
|
|
4
|
+
import { parseColorToCesium } from "@/util/parseColor";
|
|
5
|
+
import { getPositionsCenter } from "@/util/getPositionCenters/getPositionCenter";
|
|
6
|
+
import {
|
|
7
|
+
crsLinkHrefs,
|
|
8
|
+
crsLinkTypes,
|
|
9
|
+
crsNames,
|
|
10
|
+
geoJsonObjectTypes,
|
|
11
|
+
defaultCrsFunction,
|
|
12
|
+
} from "./method/geojson-util";
|
|
13
|
+
const DefaultColor = "rgb(255,0,0)";
|
|
14
|
+
|
|
15
|
+
const DefaultOptions: GeoJsonLayerType.GeoJsonLayerOptions = {
|
|
16
|
+
name: nanoid(),
|
|
17
|
+
pointSize: 32,
|
|
18
|
+
pointColor: DefaultColor,
|
|
19
|
+
pointClampToGround: false,
|
|
20
|
+
pointLabelFields: "",
|
|
21
|
+
lineColor: DefaultColor,
|
|
22
|
+
lineWidth: 2,
|
|
23
|
+
lineLabelFields: undefined,
|
|
24
|
+
lineClampToGround: false,
|
|
25
|
+
gonColor: DefaultColor,
|
|
26
|
+
gonClampToGround: false,
|
|
27
|
+
gonLabelFields: "",
|
|
28
|
+
labelDistanceDisplay: [0, 5000],
|
|
29
|
+
labelColor: "rgb(50,200,0)",
|
|
30
|
+
labelFont: "24px sans-serif",
|
|
31
|
+
showLevel: 0,
|
|
32
|
+
sourceUri: "",
|
|
33
|
+
pointSymbol: "",
|
|
34
|
+
depthTest: false,
|
|
35
|
+
bathSize: 30,
|
|
36
|
+
show: true,
|
|
37
|
+
zIndex: 0,
|
|
38
|
+
pixelOffset: [0, 0],
|
|
39
|
+
allowPick: true,
|
|
40
|
+
finishFun: () => {},
|
|
41
|
+
popupShowFields: ["*"],
|
|
42
|
+
fieldsAlias: [],
|
|
43
|
+
popupType:'attribute'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export class GeoJsonLayer {
|
|
47
|
+
//图层名称
|
|
48
|
+
private _name: string;
|
|
49
|
+
//图层ID
|
|
50
|
+
private _layerId: string;
|
|
51
|
+
//当前加载状态
|
|
52
|
+
public _isLoading: boolean;
|
|
53
|
+
//点线面的管理容器
|
|
54
|
+
private _primitiveCollection: Cesium.PrimitiveCollection;
|
|
55
|
+
//数据请求的promiss对象
|
|
56
|
+
readonly _promises: Promise<any>[];
|
|
57
|
+
//所有点线面的数据容器
|
|
58
|
+
private _featureItems: GeoJsonLayerType.PrimitiveItem[] = [];
|
|
59
|
+
//geojson数据对象
|
|
60
|
+
private _geojson: GeoJSON.FeatureCollection | undefined;
|
|
61
|
+
//当前图层的点线面的相关配置
|
|
62
|
+
private _options: Required<GeoJsonLayerType.GeoJsonLayerOptions>;
|
|
63
|
+
//点的数据基元集合
|
|
64
|
+
private _pointCollection: Cesium.PointPrimitiveCollection;
|
|
65
|
+
private _billboardCollection: Cesium.BillboardCollection;
|
|
66
|
+
private _labelCollection: Cesium.LabelCollection;
|
|
67
|
+
//线的基元对象集合
|
|
68
|
+
private _polylinePrimitive: Cesium.PrimitiveCollection | undefined;
|
|
69
|
+
private _polylineInstances: Cesium.GeometryInstance[] = [];
|
|
70
|
+
private _polylineFinish: boolean = false;
|
|
71
|
+
//面的基元对象集合
|
|
72
|
+
private _polygonPrimitive: Cesium.PrimitiveCollection | undefined;
|
|
73
|
+
private _polygonInstances: Cesium.GeometryInstance[] = [];
|
|
74
|
+
private _polygonFinish: boolean = false;
|
|
75
|
+
|
|
76
|
+
public cluster: any = undefined;
|
|
77
|
+
//================================
|
|
78
|
+
private _showLevel: number;
|
|
79
|
+
private _isDestroyed = false;
|
|
80
|
+
|
|
81
|
+
constructor(options: {
|
|
82
|
+
name?: string;
|
|
83
|
+
options?: GeoJsonLayerType.GeoJsonLayerOptions;
|
|
84
|
+
}) {
|
|
85
|
+
this._isLoading = false;
|
|
86
|
+
this._primitiveCollection = new Cesium.PrimitiveCollection();
|
|
87
|
+
this._billboardCollection = new Cesium.BillboardCollection();
|
|
88
|
+
this._labelCollection = new Cesium.LabelCollection();
|
|
89
|
+
this._pointCollection = new Cesium.PointPrimitiveCollection({
|
|
90
|
+
blendOption: Cesium.BlendOption.TRANSLUCENT,
|
|
91
|
+
});
|
|
92
|
+
//@ts-ignore
|
|
93
|
+
this._options = { ...DefaultOptions, ...options.options };
|
|
94
|
+
|
|
95
|
+
this._promises = [];
|
|
96
|
+
this._showLevel = 0;
|
|
97
|
+
this._isLoading = true;
|
|
98
|
+
this._name = options.name || "未知图层";
|
|
99
|
+
this._layerId = this._generateId();
|
|
100
|
+
//@ts-ignore
|
|
101
|
+
this.show = this._options.show;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get layerId() {
|
|
105
|
+
return this._layerId;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get primitiveCollection() {
|
|
109
|
+
return this._primitiveCollection;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get billboardCollection() {
|
|
113
|
+
return this._billboardCollection;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get labelCollection() {
|
|
117
|
+
return this._labelCollection;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get pointPrimitiveCollection() {
|
|
121
|
+
return this._pointCollection;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get polygonPrimitive() {
|
|
125
|
+
return this._polygonPrimitive;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get polylinePrimitive() {
|
|
129
|
+
return this._polylinePrimitive;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get featureItems() {
|
|
133
|
+
return this._featureItems;
|
|
134
|
+
}
|
|
135
|
+
get zIndex() {
|
|
136
|
+
return this._options.zIndex;
|
|
137
|
+
}
|
|
138
|
+
//**
|
|
139
|
+
// 图层名称
|
|
140
|
+
// */
|
|
141
|
+
get name() {
|
|
142
|
+
return this._name;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
set name(value) {
|
|
146
|
+
if (this._name !== value) {
|
|
147
|
+
this._name = value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//**
|
|
151
|
+
// 是否正在加载
|
|
152
|
+
// */
|
|
153
|
+
get loading() {
|
|
154
|
+
return this._isLoading;
|
|
155
|
+
}
|
|
156
|
+
//**
|
|
157
|
+
// 整个图层的显隐控制器
|
|
158
|
+
// */
|
|
159
|
+
get show() {
|
|
160
|
+
return this._primitiveCollection.show;
|
|
161
|
+
}
|
|
162
|
+
set show(value) {
|
|
163
|
+
this._primitiveCollection.show = value;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
*获取一个对象,该对象将crs的名称映射到一个接受GeoJSON坐标的回调函数
|
|
167
|
+
*并将其转换为WGS84地球固定笛卡尔坐标系。旧版本的GeoJSON
|
|
168
|
+
*通过指定完整的EPSG名称,
|
|
169
|
+
*例如“EPSG:4326”。.
|
|
170
|
+
* @type {Object}
|
|
171
|
+
*/
|
|
172
|
+
get crsNames() {
|
|
173
|
+
return crsNames;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
*获取一个对象,该对象将crs链接的href属性映射到回调函数
|
|
178
|
+
*它接受crs属性对象并返回一个Promise,该Promise解析
|
|
179
|
+
*该函数接受GeoJSON坐标并将其转换为WGS84地球固定笛卡尔坐标。
|
|
180
|
+
*此对象中的项目优先于<code>crsLinkHrefs</code>中定义的项目,假设
|
|
181
|
+
*链接具有指定的类型。
|
|
182
|
+
* @type {Object}
|
|
183
|
+
*/
|
|
184
|
+
get crsLinkHrefs() {
|
|
185
|
+
return crsLinkHrefs;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
get option() {
|
|
189
|
+
return this._options;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
*获取一个对象,该对象将crs链接的类型属性映射到回调函数
|
|
194
|
+
*它接受crs属性对象并返回一个Promise,该Promise解析
|
|
195
|
+
*该函数接受GeoJSON坐标并将其转换为WGS84地球固定笛卡尔坐标。
|
|
196
|
+
*<code>crsLinkHrefs</code>中的项优先于此对象。
|
|
197
|
+
* @type {Object}
|
|
198
|
+
*/
|
|
199
|
+
get crsLinkTypes() {
|
|
200
|
+
return crsLinkTypes;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
get isDestroyed() {
|
|
204
|
+
return this._isDestroyed;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
get geojson() {
|
|
208
|
+
return this._geojson;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
public _generateId() {
|
|
212
|
+
return nanoid();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
addBillboard(item: GeoJsonLayerType.BillboardPrimitiveItem) {
|
|
216
|
+
const { position, style, properties, type, uid } = item;
|
|
217
|
+
const id = {
|
|
218
|
+
uid: uid,
|
|
219
|
+
type,
|
|
220
|
+
properties,
|
|
221
|
+
popupAttributes: {
|
|
222
|
+
title: this._name,
|
|
223
|
+
attributes: createAttribute(
|
|
224
|
+
properties,
|
|
225
|
+
this.option.fieldsAlias,
|
|
226
|
+
this.option.popupShowFields
|
|
227
|
+
),
|
|
228
|
+
},
|
|
229
|
+
layer: this,
|
|
230
|
+
allowPick: this.option.allowPick,
|
|
231
|
+
};
|
|
232
|
+
const instance = this._billboardCollection.add({
|
|
233
|
+
id,
|
|
234
|
+
...(style as Cesium.Billboard.ConstructorOptions),
|
|
235
|
+
position,
|
|
236
|
+
});
|
|
237
|
+
this.addFeatureItem({
|
|
238
|
+
...item,
|
|
239
|
+
id,
|
|
240
|
+
instance,
|
|
241
|
+
center: { cartesian3: position },
|
|
242
|
+
});
|
|
243
|
+
return instance;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
addLabel(item: GeoJsonLayerType.LabelPrimitiveItem) {
|
|
247
|
+
const { position, style, uid } = item;
|
|
248
|
+
const id = uid;
|
|
249
|
+
const instance = this._labelCollection.add({
|
|
250
|
+
id,
|
|
251
|
+
...(style as Cesium.Label.ConstructorOptions),
|
|
252
|
+
position,
|
|
253
|
+
});
|
|
254
|
+
return instance;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
addPoint(item: GeoJsonLayerType.PointPrimitiveItem) {
|
|
258
|
+
const { position, style, uid, type, properties } = item;
|
|
259
|
+
const id = {
|
|
260
|
+
uid: uid,
|
|
261
|
+
type,
|
|
262
|
+
popupAttributes: {
|
|
263
|
+
title: this._name,
|
|
264
|
+
attributes: createAttribute(
|
|
265
|
+
properties,
|
|
266
|
+
this.option.fieldsAlias,
|
|
267
|
+
this.option.popupShowFields
|
|
268
|
+
),
|
|
269
|
+
},
|
|
270
|
+
properties,
|
|
271
|
+
layer: this,
|
|
272
|
+
allowPick: this.option.allowPick,
|
|
273
|
+
};
|
|
274
|
+
const instance = this._pointCollection.add({
|
|
275
|
+
id,
|
|
276
|
+
position,
|
|
277
|
+
...style,
|
|
278
|
+
color: parseColorToCesium(style?.color || this._options.pointColor),
|
|
279
|
+
});
|
|
280
|
+
this.addFeatureItem({
|
|
281
|
+
...item,
|
|
282
|
+
id,
|
|
283
|
+
instance,
|
|
284
|
+
center: { cartesian3: position },
|
|
285
|
+
});
|
|
286
|
+
return instance;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
addPolygon(item: GeoJsonLayerType.PolygonPrimitiveItem) {
|
|
290
|
+
const { positions, style, holes, uid, type, properties } = item;
|
|
291
|
+
const id = {
|
|
292
|
+
uid: uid,
|
|
293
|
+
type,
|
|
294
|
+
popupAttributes: {
|
|
295
|
+
title: this._name,
|
|
296
|
+
attributes: createAttribute(
|
|
297
|
+
properties,
|
|
298
|
+
this.option.fieldsAlias,
|
|
299
|
+
this.option.popupShowFields
|
|
300
|
+
),
|
|
301
|
+
},
|
|
302
|
+
properties,
|
|
303
|
+
layer: this,
|
|
304
|
+
allowPick: this.option.allowPick,
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const geometry = new Cesium.PolygonGeometry({
|
|
308
|
+
polygonHierarchy: new Cesium.PolygonHierarchy(
|
|
309
|
+
positions,
|
|
310
|
+
holes.map((hole) => new Cesium.PolygonHierarchy(hole))
|
|
311
|
+
),
|
|
312
|
+
vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
|
|
313
|
+
extrudedHeight: style?.extrudedHeight,
|
|
314
|
+
arcType: Cesium.ArcType.RHUMB,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const instance = new Cesium.GeometryInstance({
|
|
318
|
+
geometry,
|
|
319
|
+
attributes: {
|
|
320
|
+
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
|
321
|
+
Cesium.Color.fromCssColorString(
|
|
322
|
+
style?.material ?? this._options.gonColor ?? DefaultOptions.gonColor
|
|
323
|
+
)
|
|
324
|
+
),
|
|
325
|
+
},
|
|
326
|
+
id,
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
this._polygonInstances.push(instance);
|
|
330
|
+
|
|
331
|
+
this.addFeatureItem({
|
|
332
|
+
...item,
|
|
333
|
+
id,
|
|
334
|
+
instance: geometry,
|
|
335
|
+
center: getPositionsCenter(item.positions, style?.extrudedHeight),
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
this.addPolyline(
|
|
339
|
+
{
|
|
340
|
+
type: "Polyline",
|
|
341
|
+
positions,
|
|
342
|
+
uid: this._generateId(),
|
|
343
|
+
center: {
|
|
344
|
+
cartesian3: item.positions[Math.floor(item.positions.length / 2)],
|
|
345
|
+
},
|
|
346
|
+
style: {
|
|
347
|
+
width: style?.outlineWidth,
|
|
348
|
+
material: style?.outlineColor,
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
false
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
return instance;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
addPolyline(item: GeoJsonLayerType.PolylinePrimitiveItem, addFeature = true) {
|
|
358
|
+
const { positions, style, uid, properties, type } = item;
|
|
359
|
+
const id = {
|
|
360
|
+
uid: uid,
|
|
361
|
+
type,
|
|
362
|
+
popupAttributes: {
|
|
363
|
+
title: this._name,
|
|
364
|
+
attributes: createAttribute(
|
|
365
|
+
properties,
|
|
366
|
+
this.option.fieldsAlias,
|
|
367
|
+
this.option.popupShowFields
|
|
368
|
+
),
|
|
369
|
+
},
|
|
370
|
+
properties,
|
|
371
|
+
layer: this,
|
|
372
|
+
allowPick: this.option.allowPick,
|
|
373
|
+
};
|
|
374
|
+
let geometry = null;
|
|
375
|
+
|
|
376
|
+
if (this._options.lineClampToGround) {
|
|
377
|
+
geometry = new Cesium.GroundPolylineGeometry({
|
|
378
|
+
positions: positions,
|
|
379
|
+
width: style?.width,
|
|
380
|
+
arcType: Cesium.ArcType.RHUMB,
|
|
381
|
+
});
|
|
382
|
+
} else {
|
|
383
|
+
geometry = new Cesium.PolylineGeometry({
|
|
384
|
+
positions,
|
|
385
|
+
vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
|
|
386
|
+
width: style?.width,
|
|
387
|
+
arcType: Cesium.ArcType.RHUMB,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const instance = new Cesium.GeometryInstance({
|
|
392
|
+
geometry,
|
|
393
|
+
attributes: {
|
|
394
|
+
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
|
395
|
+
Cesium.Color.fromCssColorString(
|
|
396
|
+
style?.material ??
|
|
397
|
+
this._options.lineColor ??
|
|
398
|
+
DefaultOptions.lineColor
|
|
399
|
+
)
|
|
400
|
+
),
|
|
401
|
+
},
|
|
402
|
+
id,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
this._polylineInstances.push(instance);
|
|
406
|
+
|
|
407
|
+
if (addFeature)
|
|
408
|
+
this.addFeatureItem({
|
|
409
|
+
...item,
|
|
410
|
+
id,
|
|
411
|
+
instance: geometry,
|
|
412
|
+
center: {
|
|
413
|
+
cartesian3: item.positions[Math.floor(item.positions.length / 2)],
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
return instance;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
addFeatureItem(item: GeoJsonLayerType.PrimitiveItem) {
|
|
421
|
+
this._featureItems.push(item);
|
|
422
|
+
return item;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
findFeatureItemByUID(uid: string) {
|
|
426
|
+
return this.featureItems.find((feature) => {
|
|
427
|
+
return uid == feature.uid;
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static async load(
|
|
432
|
+
url: any,
|
|
433
|
+
options: GeoJsonLayerType.GeoJsonLayerOptions = {}
|
|
434
|
+
): Promise<GeoJsonLayer> {
|
|
435
|
+
let data = url;
|
|
436
|
+
|
|
437
|
+
let name = options.name || nanoid();
|
|
438
|
+
//创建图层对象
|
|
439
|
+
const layer = new GeoJsonLayer({ name: name, options });
|
|
440
|
+
//查看data是否定义
|
|
441
|
+
if (!Cesium.defined(data)) {
|
|
442
|
+
throw new Cesium.DeveloperError("数据无内容.");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
let promise: any = data;
|
|
446
|
+
let sourceUri = options.sourceUri;
|
|
447
|
+
|
|
448
|
+
if (typeof data === "string") {
|
|
449
|
+
data = new Cesium.Resource({ url: data });
|
|
450
|
+
}
|
|
451
|
+
if (data instanceof Cesium.Resource) {
|
|
452
|
+
promise = data.fetchJson();
|
|
453
|
+
|
|
454
|
+
sourceUri = sourceUri ?? data.getUrlComponent();
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
try {
|
|
458
|
+
const geoJson = await Promise.resolve(promise);
|
|
459
|
+
layer._geojson = geoJson;
|
|
460
|
+
await layer.preload(geoJson, options, sourceUri, true);
|
|
461
|
+
return layer;
|
|
462
|
+
} catch (error: any) {
|
|
463
|
+
layer._isLoading = false;
|
|
464
|
+
throw error;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
//前置处理函数
|
|
469
|
+
async preload(
|
|
470
|
+
geoJson: GeoJSON.GeoJSON,
|
|
471
|
+
layerOptions: GeoJsonLayerType.GeoJsonLayerOptions,
|
|
472
|
+
sourceUri: string | undefined,
|
|
473
|
+
clear: boolean
|
|
474
|
+
) {
|
|
475
|
+
const options = { ...this._options, ...layerOptions };
|
|
476
|
+
|
|
477
|
+
let name: string | undefined;
|
|
478
|
+
|
|
479
|
+
if (options.name) {
|
|
480
|
+
name = options.name;
|
|
481
|
+
} else if (sourceUri) {
|
|
482
|
+
name = Cesium.getFilenameFromUri(sourceUri);
|
|
483
|
+
} else {
|
|
484
|
+
name = "未知图层";
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (name && this._name !== name) {
|
|
488
|
+
this._name = name;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const typeHandler = geoJsonObjectTypes[geoJson.type];
|
|
492
|
+
if (!Cesium.defined(typeHandler)) {
|
|
493
|
+
throw new Cesium.RuntimeError(
|
|
494
|
+
`Unsupported GeoJSON object type: ${geoJson.type}`
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
//检查坐标系是否合规
|
|
499
|
+
const crs = (geoJson as any).crs;
|
|
500
|
+
let crsFunction = crs !== null ? defaultCrsFunction : null;
|
|
501
|
+
|
|
502
|
+
if (Cesium.defined(crs)) {
|
|
503
|
+
if (!Cesium.defined(crs.properties)) {
|
|
504
|
+
throw new Cesium.RuntimeError("crs.properties is undefined.");
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const properties = crs.properties;
|
|
508
|
+
if (crs.type === "name") {
|
|
509
|
+
crsFunction = crsNames[properties.name];
|
|
510
|
+
if (!Cesium.defined(crsFunction)) {
|
|
511
|
+
throw new Cesium.RuntimeError(`Unknown crs name: ${properties.name}`);
|
|
512
|
+
}
|
|
513
|
+
} else if (crs.type === "link") {
|
|
514
|
+
let handler = crsLinkHrefs[properties.href];
|
|
515
|
+
if (!Cesium.defined(handler)) {
|
|
516
|
+
handler = crsLinkTypes[properties.type];
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (!Cesium.defined(handler)) {
|
|
520
|
+
throw new Cesium.RuntimeError(
|
|
521
|
+
`Unable to resolve crs link: ${JSON.stringify(properties)}`
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
crsFunction = handler(properties);
|
|
526
|
+
} else if (crs.type === "EPSG") {
|
|
527
|
+
crsFunction = crsNames[`EPSG:${properties.code}`];
|
|
528
|
+
if (!Cesium.defined(crsFunction)) {
|
|
529
|
+
throw new Cesium.RuntimeError(
|
|
530
|
+
`Unknown crs EPSG code: ${properties.code}`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
} else {
|
|
534
|
+
throw new Cesium.RuntimeError(`Unknown crs type: ${crs.type}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return Promise.resolve(crsFunction).then(async (crsFunc) => {
|
|
539
|
+
if (clear) {
|
|
540
|
+
this._primitiveCollection.removeAll();
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// null is a valid value for the crs, but means the entire load process becomes a no-op
|
|
544
|
+
// because we can't assume anything about the coordinates.
|
|
545
|
+
if (crsFunc !== null) {
|
|
546
|
+
typeHandler(
|
|
547
|
+
this,
|
|
548
|
+
geoJson,
|
|
549
|
+
geoJson,
|
|
550
|
+
crsFunc,
|
|
551
|
+
options as Required<GeoJsonLayerType.GeoJsonLayerOptions>
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
await Promise.all(this._promises);
|
|
556
|
+
this._promises.length = 0;
|
|
557
|
+
await this.reloadPrimitive();
|
|
558
|
+
return this;
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async reloadPrimitive(depthTest: boolean = this._options.depthTest ?? false) {
|
|
563
|
+
let that = this;
|
|
564
|
+
const appearance = depthTest
|
|
565
|
+
? new Cesium.PerInstanceColorAppearance({
|
|
566
|
+
translucent: false,
|
|
567
|
+
renderState: {
|
|
568
|
+
depthTest: {
|
|
569
|
+
enabled: true,
|
|
570
|
+
},
|
|
571
|
+
depthMask: true,
|
|
572
|
+
blending: Cesium.BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
|
|
573
|
+
},
|
|
574
|
+
})
|
|
575
|
+
: new Cesium.PerInstanceColorAppearance({
|
|
576
|
+
flat: true,
|
|
577
|
+
translucent: false,
|
|
578
|
+
closed: true,
|
|
579
|
+
renderState: {
|
|
580
|
+
depthTest: false,
|
|
581
|
+
depthMask: false,
|
|
582
|
+
blending: Cesium.BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
|
|
583
|
+
},
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
this._polygonPrimitive = new Cesium.PrimitiveCollection();
|
|
587
|
+
let gonCollection = this._polygonPrimitive;
|
|
588
|
+
if (this._polygonInstances.length == 0) {
|
|
589
|
+
that._polygonFinish = true;
|
|
590
|
+
}
|
|
591
|
+
//贴地面或者不贴地面(由面贴地属性决定)
|
|
592
|
+
if (this._options.gonClampToGround) {
|
|
593
|
+
let taskArr = splitArrayIntoGroups(
|
|
594
|
+
this._polygonInstances,
|
|
595
|
+
this._options.bathSize
|
|
596
|
+
);
|
|
597
|
+
let index = 0;
|
|
598
|
+
async function executeBatch() {
|
|
599
|
+
gonCollection.add(
|
|
600
|
+
new Cesium.GroundPrimitive({
|
|
601
|
+
geometryInstances: taskArr[index],
|
|
602
|
+
appearance,
|
|
603
|
+
asynchronous: true,
|
|
604
|
+
releaseGeometryInstances: true,
|
|
605
|
+
})
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
index++;
|
|
609
|
+
if (index >= taskArr.length) {
|
|
610
|
+
that._polygonFinish = true;
|
|
611
|
+
|
|
612
|
+
that.checkLoadingStatus();
|
|
613
|
+
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
await new Promise(requestAnimationFrame);
|
|
618
|
+
await executeBatch();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
await new Promise(requestAnimationFrame);
|
|
622
|
+
await executeBatch();
|
|
623
|
+
} else {
|
|
624
|
+
let taskArr = splitArrayIntoGroups(
|
|
625
|
+
this._polygonInstances,
|
|
626
|
+
this._options.bathSize
|
|
627
|
+
);
|
|
628
|
+
let index = 0;
|
|
629
|
+
async function executeBatch() {
|
|
630
|
+
gonCollection.add(
|
|
631
|
+
new Cesium.Primitive({
|
|
632
|
+
geometryInstances: taskArr[index],
|
|
633
|
+
appearance,
|
|
634
|
+
asynchronous: true,
|
|
635
|
+
releaseGeometryInstances: true,
|
|
636
|
+
})
|
|
637
|
+
);
|
|
638
|
+
|
|
639
|
+
index++;
|
|
640
|
+
if (index >= taskArr.length) {
|
|
641
|
+
that._polygonFinish = true;
|
|
642
|
+
that.checkLoadingStatus();
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
await new Promise(requestAnimationFrame);
|
|
646
|
+
await executeBatch();
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
await new Promise(requestAnimationFrame);
|
|
650
|
+
await executeBatch();
|
|
651
|
+
}
|
|
652
|
+
//必须初始化否则没有地形无法加载地球
|
|
653
|
+
// await Cesium.GroundPolylinePrimitive.initializeTerrainHeights();
|
|
654
|
+
this._polylinePrimitive = new Cesium.PrimitiveCollection();
|
|
655
|
+
//贴地线或者不贴地线(由线贴地属性决定)
|
|
656
|
+
if (this._polylineInstances.length == 0) {
|
|
657
|
+
that._polylineFinish = true;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
let lineCollection = this._polylinePrimitive;
|
|
661
|
+
|
|
662
|
+
let appearanceLine = this.option.lineMaterial
|
|
663
|
+
? new Cesium.PolylineMaterialAppearance({
|
|
664
|
+
material: this.option.lineMaterial,
|
|
665
|
+
})
|
|
666
|
+
: new Cesium.PolylineColorAppearance({
|
|
667
|
+
translucent: false,
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
if (this._options.lineClampToGround) {
|
|
671
|
+
let taskArr = splitArrayIntoGroups(
|
|
672
|
+
this._polylineInstances,
|
|
673
|
+
this._options.bathSize
|
|
674
|
+
);
|
|
675
|
+
let index = 0;
|
|
676
|
+
async function executeBatch() {
|
|
677
|
+
lineCollection.add(
|
|
678
|
+
new Cesium.GroundPolylinePrimitive({
|
|
679
|
+
geometryInstances: taskArr[index],
|
|
680
|
+
appearance: appearanceLine,
|
|
681
|
+
asynchronous: true,
|
|
682
|
+
releaseGeometryInstances: true,
|
|
683
|
+
classificationType: Cesium.ClassificationType.TERRAIN,
|
|
684
|
+
})
|
|
685
|
+
);
|
|
686
|
+
|
|
687
|
+
index++;
|
|
688
|
+
if (index >= taskArr.length) {
|
|
689
|
+
that._polylineFinish = true;
|
|
690
|
+
that.checkLoadingStatus();
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
await new Promise(requestAnimationFrame);
|
|
694
|
+
await executeBatch();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
await new Promise(requestAnimationFrame);
|
|
698
|
+
|
|
699
|
+
await executeBatch();
|
|
700
|
+
} else {
|
|
701
|
+
let taskArr = splitArrayIntoGroups(
|
|
702
|
+
this._polylineInstances,
|
|
703
|
+
this._options.bathSize
|
|
704
|
+
);
|
|
705
|
+
let index = 0;
|
|
706
|
+
|
|
707
|
+
async function executeBatch() {
|
|
708
|
+
lineCollection.add(
|
|
709
|
+
new Cesium.Primitive({
|
|
710
|
+
geometryInstances: taskArr[index],
|
|
711
|
+
appearance: appearanceLine,
|
|
712
|
+
asynchronous: true,
|
|
713
|
+
releaseGeometryInstances: true,
|
|
714
|
+
})
|
|
715
|
+
);
|
|
716
|
+
|
|
717
|
+
index++;
|
|
718
|
+
if (index >= taskArr.length) {
|
|
719
|
+
that._polylineFinish = true;
|
|
720
|
+
that.checkLoadingStatus();
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
await new Promise(requestAnimationFrame);
|
|
724
|
+
await executeBatch();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
await new Promise(requestAnimationFrame);
|
|
728
|
+
await executeBatch();
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
this._primitiveCollection.add(this._polygonPrimitive);
|
|
732
|
+
//@ts-ignore
|
|
733
|
+
this._polygonPrimitive.featureType = "polygonFeature";
|
|
734
|
+
|
|
735
|
+
this._primitiveCollection.add(this._polylinePrimitive);
|
|
736
|
+
//@ts-ignore
|
|
737
|
+
this._polylinePrimitive.featureType = "polylineFeature";
|
|
738
|
+
|
|
739
|
+
this._primitiveCollection.add(this._billboardCollection);
|
|
740
|
+
//@ts-ignore
|
|
741
|
+
this._billboardCollection.featureType = "pointFeature";
|
|
742
|
+
|
|
743
|
+
this._primitiveCollection.add(this._labelCollection);
|
|
744
|
+
//@ts-ignore
|
|
745
|
+
this._labelCollection.featureType = "labelFeature";
|
|
746
|
+
|
|
747
|
+
this._primitiveCollection.add(this._pointCollection);
|
|
748
|
+
//@ts-ignore
|
|
749
|
+
this._pointCollection.featureType = "pointFeature";
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
destroy() {
|
|
753
|
+
this._pointCollection.removeAll();
|
|
754
|
+
this._billboardCollection.removeAll();
|
|
755
|
+
this._polygonPrimitive?.removeAll();
|
|
756
|
+
this._polylinePrimitive?.removeAll();
|
|
757
|
+
this._labelCollection.removeAll();
|
|
758
|
+
this._primitiveCollection.removeAll();
|
|
759
|
+
this._featureItems = [];
|
|
760
|
+
|
|
761
|
+
this._geojson = undefined;
|
|
762
|
+
|
|
763
|
+
this._polylineInstances = [];
|
|
764
|
+
this._polygonInstances = [];
|
|
765
|
+
|
|
766
|
+
this._isDestroyed = true;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
//检查加载状态
|
|
770
|
+
checkLoadingStatus() {
|
|
771
|
+
if (this._polygonFinish && this._polylineFinish) {
|
|
772
|
+
this._isLoading = false;
|
|
773
|
+
this._options.finishFun && this._options.finishFun();
|
|
774
|
+
return true;
|
|
775
|
+
} else {
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
function createAttribute(properties: any, aliasArr: any[], showAttr: any[]) {
|
|
782
|
+
let attribute: any = {};
|
|
783
|
+
|
|
784
|
+
for (let key in properties) {
|
|
785
|
+
let attrName = showAttr?.find((attr) => {
|
|
786
|
+
return attr == key || attr == "*";
|
|
787
|
+
});
|
|
788
|
+
if (!attrName) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (properties.hasOwnProperty(key)) {
|
|
793
|
+
let alias = aliasArr.find((ali) => {
|
|
794
|
+
return ali.name == key;
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
if (alias) {
|
|
798
|
+
attribute[alias.alias] = properties[key];
|
|
799
|
+
} else {
|
|
800
|
+
attribute[key] = properties[key];
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return attribute;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function splitArrayIntoGroups(array: any, groupSize: any) {
|
|
809
|
+
let result: any = [];
|
|
810
|
+
for (let i = 0; i < array.length; i += groupSize) {
|
|
811
|
+
result.push(array.slice(i, i + groupSize));
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return result;
|
|
815
|
+
}
|