@nsc-earth-2/layer 1.0.3 → 1.0.5

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.
Files changed (3) hide show
  1. package/dist/index.js +1 -633
  2. package/package.json +35 -35
  3. package/LICENSE +0 -21
package/dist/index.js CHANGED
@@ -1,633 +1 @@
1
- import { defaultValue, Terrain, CesiumTerrainProvider, EllipsoidTerrainProvider, Rectangle, Cesium3DTileset, Matrix4, Cartographic, Cartesian3, Transforms, Matrix3, Math, ArcGisMapServerImageryProvider, ImageryLayer, GeographicTilingScheme, Ellipsoid, UrlTemplateImageryProvider, WebMapServiceImageryProvider, WebMapTileServiceImageryProvider, KmlDataSource } from 'cesium';
2
- import { GeoJsonPrimitiveLayer } from '@nsc-earth-2/primitive-geojson';
3
-
4
- class TERRAIN {
5
- constructor(viewer) {
6
- this._viewer = viewer;
7
- }
8
- load(options) {
9
- options = defaultValue(options, {});
10
- const type = defaultValue(options.type, void 0);
11
- if (type !== "TERRAIN") {
12
- throw new Error(`the layer type is not 'TERRAIN'`);
13
- }
14
- const url = defaultValue(options.url, void 0);
15
- const terrain = new Terrain(CesiumTerrainProvider.fromUrl(url));
16
- this._viewer.scene.setTerrain(terrain);
17
- this._viewer.scene.requestRender();
18
- return Promise.resolve(terrain);
19
- }
20
- /**
21
- * 移除地形,不加载任何地形
22
- * @param terrain 待移除的地形
23
- */
24
- remove(terrain) {
25
- const privider = new EllipsoidTerrainProvider();
26
- this._viewer.terrainProvider = privider;
27
- this._viewer.scene.requestRender();
28
- }
29
- locate(options, terrain) {
30
- options = defaultValue(options, {});
31
- const url = defaultValue(options.url, void 0);
32
- fetch(`${url}/layer.json`).then((response) => response.json()).then((res) => {
33
- if (res.valid_bounds && Array.isArray(res.valid_bounds) && res.valid_bounds.length === 4) {
34
- this._viewer.camera.flyTo({
35
- destination: Rectangle.fromDegrees(res.valid_bounds[0], res.valid_bounds[1], res.valid_bounds[2], res.valid_bounds[3])
36
- });
37
- } else if (res.bounds && Array.isArray(res.bounds) && res.bounds.length == 4) {
38
- this._viewer.camera.flyTo({
39
- destination: Rectangle.fromDegrees(res.bounds[0], res.bounds[1], res.bounds[2], res.bounds[3])
40
- });
41
- }
42
- });
43
- }
44
- show(terrain, show) {
45
- if (terrain instanceof Terrain) {
46
- if (show) {
47
- this._viewer.scene.setTerrain(terrain);
48
- } else {
49
- const privider = new EllipsoidTerrainProvider();
50
- this._viewer.terrainProvider = privider;
51
- }
52
- this._viewer.scene.requestRender();
53
- }
54
- }
55
- }
56
-
57
- class TILES {
58
- constructor(viewer) {
59
- this._viewer = viewer;
60
- }
61
- async load(options) {
62
- options = defaultValue(options, {});
63
- const type = defaultValue(options.type, void 0);
64
- if (type !== "3DTILES") {
65
- throw new Error(`the layer type is not '3DTILES'`);
66
- }
67
- const url = options.url;
68
- if (!url) {
69
- throw new Error(`the url is needed`);
70
- }
71
- const longitude = defaultValue(options.longitude, 0);
72
- const latitude = defaultValue(options.latitude, 0);
73
- const altitude = defaultValue(options.altitude, 0);
74
- const azimuth = defaultValue(options.azimuth, 0);
75
- const transfer = { longitude, latitude, altitude, azimuth };
76
- const tileset = await Cesium3DTileset.fromUrl(url, {
77
- dynamicScreenSpaceError: true,
78
- dynamicScreenSpaceErrorDensity: 278e-5,
79
- dynamicScreenSpaceErrorFactor: 4,
80
- dynamicScreenSpaceErrorHeightFalloff: 0.25
81
- });
82
- this.transferTileSet(tileset, transfer);
83
- this._viewer.scene.primitives.add(tileset);
84
- this._viewer.scene.requestRender();
85
- return tileset;
86
- }
87
- transferTileSet(tileset, transfer) {
88
- const { longitude, latitude, altitude, azimuth } = transfer;
89
- let rotationAndTransMt4 = new Matrix4();
90
- if (longitude && latitude) {
91
- let height = altitude;
92
- if (altitude == 0) {
93
- height = this._viewer.scene.globe.getHeight(Cartographic.fromDegrees(longitude, latitude)) || 0;
94
- }
95
- const _car3 = Cartesian3.fromDegrees(longitude, latitude, height);
96
- const translationMt4 = Transforms.eastNorthUpToFixedFrame(_car3);
97
- const mat3 = new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
98
- const mat3z = Matrix3.fromRotationZ(Math.toRadians(azimuth));
99
- Matrix3.multiply(mat3, mat3z, mat3);
100
- rotationAndTransMt4 = Matrix4.multiplyByMatrix3(translationMt4, mat3, new Matrix4());
101
- } else if (altitude) {
102
- const tran = new Cartesian3();
103
- Matrix4.getTranslation(tileset.root.transform, tran);
104
- const cartographic = Cartographic.fromCartesian(tran);
105
- if (cartographic) {
106
- const lat2 = Math.toDegrees(cartographic.latitude);
107
- const lon2 = Math.toDegrees(cartographic.longitude);
108
- const _car32 = Cartesian3.fromDegrees(lon2, lat2, altitude);
109
- const translationMt42 = Transforms.eastNorthUpToFixedFrame(_car32);
110
- const mat3 = new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
111
- const mat3z = Matrix3.fromRotationZ(Math.toRadians(azimuth));
112
- Matrix3.multiply(mat3, mat3z, mat3);
113
- rotationAndTransMt4 = Matrix4.multiplyByMatrix3(translationMt42, mat3, new Matrix4());
114
- }
115
- } else if (azimuth) {
116
- const mat3 = new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
117
- const mat3z = Matrix3.fromRotationZ(Math.toRadians(azimuth));
118
- Matrix3.multiply(mat3, mat3z, mat3);
119
- rotationAndTransMt4 = Matrix4.multiplyByMatrix3(tileset.root.transform, mat3, new Matrix4());
120
- } else {
121
- rotationAndTransMt4 = tileset.root.transform;
122
- }
123
- tileset.root.transform = rotationAndTransMt4;
124
- }
125
- remove(tileset) {
126
- if (tileset instanceof Cesium3DTileset) {
127
- this._viewer.scene.primitives.remove(tileset);
128
- this._viewer.scene.requestRender();
129
- }
130
- }
131
- locate(options, tileset) {
132
- if (tileset instanceof Cesium3DTileset) {
133
- this._viewer.flyTo(tileset);
134
- }
135
- }
136
- show(tileset, show) {
137
- if (tileset instanceof Cesium3DTileset) {
138
- tileset.show = show;
139
- this._viewer.scene.requestRender();
140
- }
141
- }
142
- }
143
-
144
- class ARCGIS {
145
- constructor(viewer) {
146
- this._viewer = viewer;
147
- }
148
- async load(options) {
149
- options = defaultValue(options, {});
150
- const type = defaultValue(options.type, void 0);
151
- if (type !== "ARCGIS") {
152
- throw new Error(`the layer type is not 'ARCGIS'`);
153
- }
154
- const { url, alpha, token } = options;
155
- if (!url) {
156
- throw new Error(`the url is needed`);
157
- }
158
- let jsonUrl = url;
159
- if (token) {
160
- jsonUrl = url + "/info/iteminfo?f=pjson&token=" + token;
161
- } else {
162
- jsonUrl = url + "/info/iteminfo?f=pjson";
163
- }
164
- const option = await fetch(jsonUrl, {
165
- method: "GET"
166
- }).then((response) => response.json()).then((res) => {
167
- const option2 = {};
168
- if (token) {
169
- option2.token = token;
170
- }
171
- if (res && res.extent && Array.isArray(res.extent)) {
172
- option2.rectangle = Rectangle.fromDegrees(res.extent[0][0], res.extent[0][1], res.extent[1][0], res.extent[1][1]);
173
- }
174
- return option2;
175
- });
176
- const esri = ArcGisMapServerImageryProvider.fromUrl(url, option);
177
- const imageryLayer = ImageryLayer.fromProviderAsync(esri, {});
178
- if (alpha)
179
- imageryLayer.alpha = alpha;
180
- this._viewer.imageryLayers.add(imageryLayer);
181
- this._viewer.scene.requestRender();
182
- return imageryLayer;
183
- }
184
- async remove(imageryLayer) {
185
- if (imageryLayer instanceof ImageryLayer) {
186
- this._viewer.imageryLayers.remove(imageryLayer, true);
187
- this._viewer.scene.requestRender();
188
- }
189
- }
190
- locate(options, imageryLayer) {
191
- if (imageryLayer instanceof ImageryLayer) {
192
- if (imageryLayer.ready) {
193
- const rectangle = imageryLayer.getImageryRectangle();
194
- this._viewer.scene.camera.flyTo({
195
- destination: rectangle
196
- });
197
- } else {
198
- imageryLayer.readyEvent.addEventListener((provider) => {
199
- const rectangle = imageryLayer.getImageryRectangle();
200
- this._viewer.scene.camera.flyTo({
201
- destination: rectangle
202
- });
203
- });
204
- }
205
- }
206
- }
207
- show(imageryLayer, show) {
208
- if (imageryLayer instanceof ImageryLayer) {
209
- imageryLayer.show = show;
210
- this._viewer.scene.requestRender();
211
- }
212
- }
213
- }
214
-
215
- class URLTemplate {
216
- constructor(viewer) {
217
- this._viewer = viewer;
218
- }
219
- async load(options) {
220
- options = defaultValue(options, {});
221
- const type = defaultValue(options.type, void 0);
222
- if (type !== "URLTemplate") {
223
- throw new Error(`the layer type is not 'URLTemplate'`);
224
- }
225
- const { url, alpha } = options;
226
- if (!url) {
227
- throw new Error(`the url is needed`);
228
- }
229
- let str = "{z}/{x}/{y}.png";
230
- let mateUrl = url.substr(0, url.indexOf(str)) + "meta.json";
231
- const option = await fetch(mateUrl, {
232
- method: "GET"
233
- }).then((response) => response.json()).then((res) => {
234
- const option2 = { url };
235
- if (res && res.bounds) {
236
- option2.rectangle = Rectangle.fromDegrees(res.bounds.west, res.bounds.south, res.bounds.east, res.bounds.north);
237
- }
238
- if (res && res.proj == 4326) {
239
- option2.tilingScheme = new GeographicTilingScheme({
240
- ellipsoid: Ellipsoid.WGS84
241
- });
242
- }
243
- if (res && res.maxzoom) {
244
- option2.maximumLevel = res.maxzoom;
245
- }
246
- if (res && res.minzoom) {
247
- option2.minimumLevel = res.minzoom;
248
- }
249
- if (res && res.tilesize) {
250
- option2.tileWidth = option2.tileHeight = res.tilesize;
251
- }
252
- return option2;
253
- });
254
- const provider = new UrlTemplateImageryProvider(option);
255
- const imageryLayer = new ImageryLayer(provider, {});
256
- if (alpha)
257
- imageryLayer.alpha = alpha;
258
- this._viewer.imageryLayers.add(imageryLayer);
259
- this._viewer.scene.requestRender();
260
- return imageryLayer;
261
- }
262
- async remove(imageryLayer) {
263
- if (imageryLayer instanceof ImageryLayer) {
264
- this._viewer.imageryLayers.remove(imageryLayer, true);
265
- this._viewer.scene.requestRender();
266
- }
267
- }
268
- locate(options, imageryLayer) {
269
- if (imageryLayer instanceof ImageryLayer) {
270
- if (imageryLayer.ready) {
271
- const rectangle = imageryLayer.getImageryRectangle();
272
- this._viewer.scene.camera.flyTo({
273
- destination: rectangle
274
- });
275
- } else {
276
- imageryLayer.readyEvent.addEventListener((provider) => {
277
- const rectangle = imageryLayer.getImageryRectangle();
278
- this._viewer.scene.camera.flyTo({
279
- destination: rectangle
280
- });
281
- });
282
- }
283
- }
284
- }
285
- show(imageryLayer, show) {
286
- if (imageryLayer instanceof ImageryLayer) {
287
- imageryLayer.show = show;
288
- this._viewer.scene.requestRender();
289
- }
290
- }
291
- }
292
-
293
- class GEOJSON {
294
- constructor(viewer) {
295
- this._viewer = viewer;
296
- }
297
- async load(options) {
298
- options = defaultValue(options, {});
299
- const type = defaultValue(options.type, void 0);
300
- if (type !== "GEOJSON") {
301
- throw new Error(`the layer type is not 'GEOJSON'`);
302
- }
303
- const { url, clampToGround } = options;
304
- if (!url) {
305
- throw new Error(`the url is needed`);
306
- }
307
- const layer = new GeoJsonPrimitiveLayer({ options: { useGroundGeometry: clampToGround ?? false, clampToGround: clampToGround ?? false, scene: this._viewer.scene } });
308
- await layer.load(url);
309
- this._viewer.scene.primitives.add(layer.primitiveCollection);
310
- this._viewer.scene.requestRender();
311
- return layer;
312
- }
313
- async remove(geojson) {
314
- if (geojson instanceof GeoJsonPrimitiveLayer) {
315
- this._viewer.scene.primitives.remove(geojson.primitiveCollection);
316
- this._viewer.scene.requestRender();
317
- }
318
- }
319
- locate(options, geojson) {
320
- if (geojson instanceof GeoJsonPrimitiveLayer) {
321
- geojson.flyTo(this._viewer, { tolerance: 0.015 });
322
- }
323
- }
324
- show(geojson, show) {
325
- if (geojson instanceof GeoJsonPrimitiveLayer) {
326
- geojson.primitiveCollection.show = show;
327
- this._viewer.scene.requestRender();
328
- }
329
- }
330
- }
331
-
332
- class WMS {
333
- constructor(viewer) {
334
- this._viewer = viewer;
335
- }
336
- async load(options) {
337
- options = defaultValue(options, {});
338
- const type = defaultValue(options.type, void 0);
339
- if (type !== "WMS") {
340
- throw new Error(`the layer type is not 'WMS'`);
341
- }
342
- const { url, layers } = options;
343
- if (!url) {
344
- throw new Error(`the url is needed`);
345
- }
346
- if (!layers) {
347
- throw new Error(`the layers is needed`);
348
- }
349
- const option = { url };
350
- const provider = new WebMapServiceImageryProvider(option);
351
- const imageryLayer = new ImageryLayer(provider, {});
352
- this._viewer.imageryLayers.add(imageryLayer);
353
- this._viewer.scene.requestRender();
354
- return imageryLayer;
355
- }
356
- async remove(imageryLayer) {
357
- if (imageryLayer instanceof ImageryLayer) {
358
- this._viewer.imageryLayers.remove(imageryLayer, true);
359
- this._viewer.scene.requestRender();
360
- }
361
- }
362
- locate(options, imageryLayer) {
363
- if (imageryLayer instanceof ImageryLayer) {
364
- if (imageryLayer.ready) {
365
- const rectangle = imageryLayer.getImageryRectangle();
366
- this._viewer.scene.camera.flyTo({
367
- destination: rectangle
368
- });
369
- } else {
370
- imageryLayer.readyEvent.addEventListener((provider) => {
371
- const rectangle = imageryLayer.getImageryRectangle();
372
- this._viewer.scene.camera.flyTo({
373
- destination: rectangle
374
- });
375
- });
376
- }
377
- }
378
- }
379
- show(imageryLayer, show) {
380
- if (imageryLayer instanceof ImageryLayer) {
381
- imageryLayer.show = show;
382
- this._viewer.scene.requestRender();
383
- }
384
- }
385
- }
386
-
387
- class WMTS {
388
- constructor(viewer) {
389
- this._viewer = viewer;
390
- }
391
- async load(options) {
392
- options = defaultValue(options, {});
393
- const type = defaultValue(options.type, void 0);
394
- if (type !== "WMTS") {
395
- throw new Error(`the layer type is not 'WMTS'`);
396
- }
397
- const { url, layer, style, tileMatrixSetID, subdomains, format, tilingScheme, tileMatrixLabels } = options;
398
- if (!url) {
399
- throw new Error(`the url is needed`);
400
- }
401
- if (!layer) {
402
- throw new Error(`the layer is needed`);
403
- }
404
- if (!style) {
405
- throw new Error(`the style is needed`);
406
- }
407
- if (!tileMatrixSetID) {
408
- throw new Error(`the tileMatrixSetID is needed`);
409
- }
410
- const option = { url, layer, style, tileMatrixSetID };
411
- if (subdomains)
412
- option.subdomains = subdomains;
413
- if (format)
414
- option.format = format;
415
- if (tilingScheme)
416
- option.tilingScheme = tilingScheme;
417
- if (tileMatrixLabels)
418
- option.tileMatrixLabels = tileMatrixLabels;
419
- const provider = new WebMapTileServiceImageryProvider(option);
420
- const imageryLayer = new ImageryLayer(provider, {});
421
- this._viewer.imageryLayers.add(imageryLayer);
422
- this._viewer.scene.requestRender();
423
- return imageryLayer;
424
- }
425
- async remove(imageryLayer) {
426
- if (imageryLayer instanceof ImageryLayer) {
427
- this._viewer.imageryLayers.remove(imageryLayer, true);
428
- this._viewer.scene.requestRender();
429
- }
430
- }
431
- locate(options, imageryLayer) {
432
- if (imageryLayer instanceof ImageryLayer) {
433
- if (imageryLayer.ready) {
434
- const rectangle = imageryLayer.getImageryRectangle();
435
- this._viewer.scene.camera.flyTo({
436
- destination: rectangle
437
- });
438
- } else {
439
- imageryLayer.readyEvent.addEventListener((provider) => {
440
- const rectangle = imageryLayer.getImageryRectangle();
441
- this._viewer.scene.camera.flyTo({
442
- destination: rectangle
443
- });
444
- });
445
- }
446
- }
447
- }
448
- show(imageryLayer, show) {
449
- if (imageryLayer instanceof ImageryLayer) {
450
- imageryLayer.show = show;
451
- this._viewer.scene.requestRender();
452
- }
453
- }
454
- }
455
-
456
- class KML {
457
- constructor(viewer) {
458
- this._viewer = viewer;
459
- }
460
- async load(options) {
461
- options = defaultValue(options, {});
462
- const type = defaultValue(options.type, void 0);
463
- if (type !== "KML" && type !== "KMZ") {
464
- throw new Error(`the layer type is not 'KML' or 'KMZ`);
465
- }
466
- const { url, clampToGround } = options;
467
- if (!url) {
468
- throw new Error(`the url is needed`);
469
- }
470
- const layer = this._viewer.dataSources.add(KmlDataSource.load(url, {
471
- camera: this._viewer.scene.camera,
472
- canvas: this._viewer.scene.canvas,
473
- clampToGround: clampToGround ?? false
474
- }));
475
- return layer;
476
- }
477
- async remove(layer) {
478
- if (layer instanceof Promise) {
479
- layer.then((res) => {
480
- this._viewer.dataSources.remove(res);
481
- this._viewer.scene.requestRender();
482
- });
483
- }
484
- if (layer instanceof KmlDataSource) {
485
- this._viewer.dataSources.remove(layer);
486
- this._viewer.scene.requestRender();
487
- }
488
- }
489
- locate(options, layer) {
490
- if (layer instanceof Promise) {
491
- layer.then((res) => {
492
- this._viewer.flyTo(res);
493
- });
494
- }
495
- if (layer instanceof KmlDataSource) {
496
- this._viewer.flyTo(layer);
497
- }
498
- }
499
- show(layer, show) {
500
- if (layer instanceof Promise) {
501
- layer.then((res) => {
502
- res.show = show;
503
- this._viewer.scene.requestRender();
504
- });
505
- }
506
- if (layer instanceof KmlDataSource) {
507
- layer.show = show;
508
- this._viewer.scene.requestRender();
509
- }
510
- }
511
- }
512
-
513
- class Layer {
514
- constructor(viewer) {
515
- if (!viewer)
516
- throw new Error("\u8BF7\u8F93\u5165Viewer\u5BF9\u8C61\uFF01");
517
- this._viewer = viewer;
518
- this._layersMap = /* @__PURE__ */ new Map();
519
- this._typeClassMap = /* @__PURE__ */ new Map();
520
- }
521
- _initLoader() {
522
- const typeClass = this._typeClassMap.get(this._type);
523
- if (typeClass) {
524
- this._typeClass = typeClass;
525
- } else {
526
- if (this._type === "TERRAIN") {
527
- this._typeClass = new TERRAIN(this._viewer);
528
- } else if (this._type === "3DTILES") {
529
- this._typeClass = new TILES(this._viewer);
530
- } else if (this._type === "ARCGIS") {
531
- this._typeClass = new ARCGIS(this._viewer);
532
- } else if (this._type === "URLTemplate") {
533
- this._typeClass = new URLTemplate(this._viewer);
534
- } else if (this._type === "WMS") {
535
- this._typeClass = new WMS(this._viewer);
536
- } else if (this._type === "WMTS") {
537
- this._typeClass = new WMTS(this._viewer);
538
- } else if (this._type === "KML") {
539
- this._typeClass = new KML(this._viewer);
540
- } else if (this._type === "GEOJSON") {
541
- this._typeClass = new GEOJSON(this._viewer);
542
- }
543
- this._typeClassMap.set(this._type, this._typeClass);
544
- }
545
- this._load = this._typeClass.load.bind(this._typeClass);
546
- this._remove = this._typeClass.remove.bind(this._typeClass);
547
- this._locate = this._typeClass.locate.bind(this._typeClass);
548
- this._show = this._typeClass.show.bind(this._typeClass);
549
- }
550
- /**
551
- * 加载地图图层
552
- * @param options
553
- */
554
- async loadAsync(options) {
555
- options = defaultValue(options, {});
556
- this._id = defaultValue(options.id, void 0);
557
- if (this._layersMap.has(this._id)) {
558
- throw new Error(`the layer with id '${this._id}' is already loaded`);
559
- }
560
- this._url = defaultValue(options.url, void 0);
561
- if (!this._url) {
562
- throw new Error(`the url is needed`);
563
- }
564
- this._type = defaultValue(options.type, void 0);
565
- if (!this._isSupport(options.type)) {
566
- throw new Error(`the type '${options.type}' is not support`);
567
- }
568
- this._initLoader();
569
- if (options.type === "KML") {
570
- const layer = this._load(options);
571
- if (layer instanceof Promise) {
572
- this._layersMap.set(options.id, { options, layer });
573
- }
574
- } else {
575
- const layer = await this._load(options);
576
- this._layersMap.set(options.id, { options, layer });
577
- }
578
- }
579
- _isSupport(type) {
580
- return [
581
- "TERRAIN",
582
- //地形
583
- "3DTILES",
584
- //模型
585
- "ARCGIS",
586
- "URLTemplate",
587
- "WMS",
588
- "WMTS",
589
- //地图服务
590
- "KML",
591
- "GEOJSON"
592
- // 矢量
593
- ].includes(type);
594
- }
595
- removeById(id) {
596
- const map = this._layersMap.get(id);
597
- this._layersMap.delete(id);
598
- if (!map)
599
- return;
600
- const { options, layer } = map;
601
- if (layer) {
602
- this._type = options.type;
603
- this._initLoader();
604
- this._remove(layer);
605
- }
606
- }
607
- locateById(id) {
608
- const map = this._layersMap.get(id);
609
- if (!map)
610
- return;
611
- const { options, layer } = map;
612
- if (layer) {
613
- this._type = options.type;
614
- this._initLoader();
615
- this._locate(options, layer);
616
- }
617
- }
618
- showById(id, show) {
619
- const map = this._layersMap.get(id);
620
- if (!map)
621
- return;
622
- const { options, layer } = map;
623
- if (layer) {
624
- this._type = options.type;
625
- this._initLoader();
626
- this._show(layer, show);
627
- }
628
- }
629
- destroy() {
630
- }
631
- }
632
-
633
- export { Layer as default };
1
+ import{defaultValue as t,Terrain as s,CesiumTerrainProvider as i,EllipsoidTerrainProvider as e,Rectangle as n,Cesium3DTileset as o,Matrix4 as r,Cartographic as h,Cartesian3 as a,Transforms as c,Matrix3 as l,Math as w,ArcGisMapServerImageryProvider as d,ImageryLayer as f,GeographicTilingScheme as y,Ellipsoid as u,UrlTemplateImageryProvider as m,WebMapServiceImageryProvider as p,WebMapTileServiceImageryProvider as E,KmlDataSource as S}from"cesium";import{GeoJsonPrimitiveLayer as v}from"@nsc-earth-2/primitive-geojson";class T{constructor(t){this.t=t}load(e){e=t(e,{});if("TERRAIN"!==t(e.type,void 0))throw new Error("the layer type is not 'TERRAIN'");const n=t(e.url,void 0),o=new s(i.i(n));return this.t.h.o(o),this.t.h.l(),Promise.resolve(o)}remove(t){const s=new e;this.t.u=s,this.t.h.l()}m(s,i){s=t(s,{});const e=t(s.url,void 0);fetch(`${e}/layer.json`).then((t=>t.json())).then((t=>{t.p&&Array.isArray(t.p)&&4===t.p.length?this.t.v.S({destination:n.T(t.p[0],t.p[1],t.p[2],t.p[3])}):t.bounds&&Array.isArray(t.bounds)&&4==t.bounds.length&&this.t.v.S({destination:n.T(t.bounds[0],t.bounds[1],t.bounds[2],t.bounds[3])})}))}show(t,i){if(t instanceof s){if(i)this.t.h.o(t);else{const t=new e;this.t.u=t}this.t.h.l()}}}class M{constructor(t){this.t=t}async load(s){s=t(s,{});if("3DTILES"!==t(s.type,void 0))throw new Error("the layer type is not '3DTILES'");const i=s.url;if(!i)throw new Error("the url is needed");const e={longitude:t(s.longitude,0),latitude:t(s.latitude,0),altitude:t(s.altitude,0),azimuth:t(s.azimuth,0)},n=await o.i(i,{M:!0,I:.00278,G:4,R:.25});return this.L(n,e),this.t.h.A.add(n),this.t.h.l(),n}L(t,s){const{longitude:i,latitude:e,altitude:n,azimuth:o}=s;let d=new r;if(i&&e){let t=n;0==n&&(t=this.t.h.N.D(h.T(i,e))||0);const s=a.T(i,e,t),f=c.O(s),y=new l(1,0,0,0,1,0,0,0,1),u=l.W(w.K(o));l.multiply(y,u,y),d=r.j(f,y,new r)}else if(n){const s=new a;r.P(t.root.transform,s);const i=h.C(s);if(i){const t=w.J(i.latitude),s=w.J(i.longitude),e=a.T(s,t,n),h=c.O(e),f=new l(1,0,0,0,1,0,0,0,1),y=l.W(w.K(o));l.multiply(f,y,f),d=r.j(h,f,new r)}}else if(o){const s=new l(1,0,0,0,1,0,0,0,1),i=l.W(w.K(o));l.multiply(s,i,s),d=r.j(t.root.transform,s,new r)}else d=t.root.transform;t.root.transform=d}remove(t){t instanceof o&&(this.t.h.A.remove(t),this.t.h.l())}m(t,s){s instanceof o&&this.t.S(s)}show(t,s){t instanceof o&&(t.show=s,this.t.h.l())}}class I{constructor(t){this.t=t}async load(s){s=t(s,{});if("ARCGIS"!==t(s.type,void 0))throw new Error("the layer type is not 'ARCGIS'");const{url:i,alpha:e,U:o}=s;if(!i)throw new Error("the url is needed");let r=i;r=o?i+"/info/iteminfo?f=pjson&token="+o:i+"/info/iteminfo?f=pjson";const h=await fetch(r,{method:"GET"}).then((t=>t.json())).then((t=>{const s={};return o&&(s.U=o),t&&t.B&&Array.isArray(t.B)&&(s.$=n.T(t.B[0][0],t.B[0][1],t.B[1][0],t.B[1][1])),s})),a=d.i(i,h),c=f.k(a,{});return e&&(c.alpha=e),this.t.F.add(c),this.t.h.l(),c}async remove(t){t instanceof f&&(this.t.F.remove(t,!0),this.t.h.l())}m(t,s){if(s instanceof f)if(s.ready){const t=s.Z();this.t.h.v.S({destination:t})}else s._.addEventListener((t=>{const i=s.Z();this.t.h.v.S({destination:i})}))}show(t,s){t instanceof f&&(t.show=s,this.t.h.l())}}class G{constructor(t){this.t=t}async load(s){s=t(s,{});if("URLTemplate"!==t(s.type,void 0))throw new Error("the layer type is not 'URLTemplate'");const{url:i,alpha:e}=s;if(!i)throw new Error("the url is needed");let o=i.substr(0,i.indexOf("{z}/{x}/{y}.png"))+"meta.json";const r=await fetch(o,{method:"GET"}).then((t=>t.json())).then((t=>{const s={url:i};return t&&t.bounds&&(s.$=n.T(t.bounds.H,t.bounds.V,t.bounds.q,t.bounds.X)),t&&4326==t.Y&&(s.tt=new y({st:u.it})),t&&t.et&&(s.nt=t.et),t&&t.ot&&(s.rt=t.ot),t&&t.ht&&(s.ct=s.lt=t.ht),s})),h=new m(r),a=new f(h,{});return e&&(a.alpha=e),this.t.F.add(a),this.t.h.l(),a}async remove(t){t instanceof f&&(this.t.F.remove(t,!0),this.t.h.l())}m(t,s){if(s instanceof f)if(s.ready){const t=s.Z();this.t.h.v.S({destination:t})}else s._.addEventListener((t=>{const i=s.Z();this.t.h.v.S({destination:i})}))}show(t,s){t instanceof f&&(t.show=s,this.t.h.l())}}class R{constructor(t){this.t=t}async load(s){s=t(s,{});if("GEOJSON"!==t(s.type,void 0))throw new Error("the layer type is not 'GEOJSON'");const{url:i,wt:e}=s;if(!i)throw new Error("the url is needed");const n=new v({options:{dt:e??!1,wt:e??!1,h:this.t.h}});return await n.load(i),this.t.h.A.add(n.ft),this.t.h.l(),n}async remove(t){t instanceof v&&(this.t.h.A.remove(t.ft),this.t.h.l())}m(t,s){s instanceof v&&s.S(this.t,{yt:.015})}show(t,s){t instanceof v&&(t.ft.show=s,this.t.h.l())}}class L{constructor(t){this.t=t}async load(s){s=t(s,{});if("WMS"!==t(s.type,void 0))throw new Error("the layer type is not 'WMS'");const{url:i,ut:e}=s;if(!i)throw new Error("the url is needed");if(!e)throw new Error("the layers is needed");const n=new p({url:i}),o=new f(n,{});return this.t.F.add(o),this.t.h.l(),o}async remove(t){t instanceof f&&(this.t.F.remove(t,!0),this.t.h.l())}m(t,s){if(s instanceof f)if(s.ready){const t=s.Z();this.t.h.v.S({destination:t})}else s._.addEventListener((t=>{const i=s.Z();this.t.h.v.S({destination:i})}))}show(t,s){t instanceof f&&(t.show=s,this.t.h.l())}}class A{constructor(t){this.t=t}async load(s){s=t(s,{});if("WMTS"!==t(s.type,void 0))throw new Error("the layer type is not 'WMTS'");const{url:i,Et:e,style:n,St:o,vt:r,format:h,tt:a,Tt:c}=s;if(!i)throw new Error("the url is needed");if(!e)throw new Error("the layer is needed");if(!n)throw new Error("the style is needed");if(!o)throw new Error("the tileMatrixSetID is needed");const l={url:i,Et:e,style:n,St:o};r&&(l.vt=r),h&&(l.format=h),a&&(l.tt=a),c&&(l.Tt=c);const w=new E(l),d=new f(w,{});return this.t.F.add(d),this.t.h.l(),d}async remove(t){t instanceof f&&(this.t.F.remove(t,!0),this.t.h.l())}m(t,s){if(s instanceof f)if(s.ready){const t=s.Z();this.t.h.v.S({destination:t})}else s._.addEventListener((t=>{const i=s.Z();this.t.h.v.S({destination:i})}))}show(t,s){t instanceof f&&(t.show=s,this.t.h.l())}}class D{constructor(t){this.t=t}async load(s){s=t(s,{});const i=t(s.type,void 0);if("KML"!==i&&"KMZ"!==i)throw new Error("the layer type is not 'KML' or 'KMZ");const{url:e,wt:n}=s;if(!e)throw new Error("the url is needed");return this.t.Mt.add(S.load(e,{v:this.t.h.v,canvas:this.t.h.canvas,wt:n??!1}))}async remove(t){t instanceof Promise&&t.then((t=>{this.t.Mt.remove(t),this.t.h.l()})),t instanceof S&&(this.t.Mt.remove(t),this.t.h.l())}m(t,s){s instanceof Promise&&s.then((t=>{this.t.S(t)})),s instanceof S&&this.t.S(s)}show(t,s){t instanceof Promise&&t.then((t=>{t.show=s,this.t.h.l()})),t instanceof S&&(t.show=s,this.t.h.l())}}class N{constructor(t){if(!t)throw new Error("请输入Viewer对象!");this.t=t,this.It=new Map,this.Gt=new Map}Rt(){const t=this.Gt.get(this.Lt);t?this.At=t:("TERRAIN"===this.Lt?this.At=new T(this.t):"3DTILES"===this.Lt?this.At=new M(this.t):"ARCGIS"===this.Lt?this.At=new I(this.t):"URLTemplate"===this.Lt?this.At=new G(this.t):"WMS"===this.Lt?this.At=new L(this.t):"WMTS"===this.Lt?this.At=new A(this.t):"KML"===this.Lt?this.At=new D(this.t):"GEOJSON"===this.Lt&&(this.At=new R(this.t)),this.Gt.set(this.Lt,this.At)),this.Dt=this.At.load.bind(this.At),this.Nt=this.At.remove.bind(this.At),this.Ot=this.At.m.bind(this.At),this.Wt=this.At.show.bind(this.At)}async Kt(s){if(s=t(s,{}),this.gt=t(s.id,void 0),this.It.has(this.gt))throw new Error(`the layer with id '${this.gt}' is already loaded`);if(this.xt=t(s.url,void 0),!this.xt)throw new Error("the url is needed");if(this.Lt=t(s.type,void 0),!this.jt(s.type))throw new Error(`the type '${s.type}' is not support`);if(this.Rt(),"KML"===s.type){const t=this.Dt(s);t instanceof Promise&&this.It.set(s.id,{options:s,Et:t})}else{const t=await this.Dt(s);this.It.set(s.id,{options:s,Et:t})}}jt(t){return["TERRAIN","3DTILES","ARCGIS","URLTemplate","WMS","WMTS","KML","GEOJSON"].includes(t)}Pt(t){const s=this.It.get(t);if(this.It.delete(t),!s)return;const{options:i,Et:e}=s;e&&(this.Lt=i.type,this.Rt(),this.Nt(e))}Ct(t){const s=this.It.get(t);if(!s)return;const{options:i,Et:e}=s;e&&(this.Lt=i.type,this.Rt(),this.Ot(i,e))}Jt(t,s){const i=this.It.get(t);if(!i)return;const{options:e,Et:n}=i;n&&(this.Lt=e.type,this.Rt(),this.Wt(n,s))}destroy(){}}export{N as default};
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "@nsc-earth-2/layer",
3
- "version": "1.0.3",
4
- "type": "module",
5
- "main": "./dist/index.js",
6
- "module": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "files": [
9
- "dist"
10
- ],
11
- "publishConfig": {
12
- "access": "public"
13
- },
14
- "repository": {
15
- "directory": "packages/layer"
16
- },
17
- "keywords": [
18
- "cesium",
19
- "layer"
20
- ],
21
- "author": "wangfang",
22
- "license": "MIT",
23
- "peerDependencies": {
24
- "cesium": "*"
25
- },
26
- "devDependencies": {
27
- "cesium": "^1.104.0"
28
- },
29
- "dependencies": {
30
- "@nsc-earth-2/primitive-geojson": "1.0.5"
31
- },
32
- "scripts": {
33
- "build": "rimraf dist && rollup -c",
34
- "dev": "rollup -c --watch"
35
- }
1
+ {
2
+ "name": "@nsc-earth-2/layer",
3
+ "version": "1.0.5",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "repository": {
15
+ "directory": "packages/layer"
16
+ },
17
+ "scripts": {
18
+ "build": "rimraf dist && rollup -c",
19
+ "dev": "rollup -c --watch"
20
+ },
21
+ "keywords": [
22
+ "cesium",
23
+ "layer"
24
+ ],
25
+ "author": "wangfang",
26
+ "license": "MIT",
27
+ "peerDependencies": {
28
+ "cesium": "*"
29
+ },
30
+ "devDependencies": {
31
+ "cesium": "^1.104.0"
32
+ },
33
+ "dependencies": {
34
+ "@nsc-earth-2/primitive-geojson": "1.0.7"
35
+ }
36
36
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 練氣士
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.