@juun-roh/cesium-utils 0.3.7 → 0.3.9

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.
@@ -1,528 +0,0 @@
1
- import { DataSourceCollection, EntityCollection, ImageryLayerCollection, PrimitiveCollection, Billboard, Cesium3DTileset, GroundPrimitive, Label, PointPrimitive, Polyline, Primitive, DataSource, Entity, ImageryLayer } from 'cesium';
2
-
3
- /**
4
- * Runtime type validator to identify the non-function property.
5
- */
6
- type NonFunction<T> = {
7
- [K in keyof T]: T[K] extends Function ? never : K;
8
- }[keyof T];
9
- /**
10
- * Examine the property descriptors at runtime
11
- * to detect properties that only have getters.
12
- * (read-only accessor properties)
13
- * @param o The object to examine.
14
- * @param k The key value of the property.
15
- */
16
- declare function isGetterOnly(o: object, k: string | number | symbol): boolean;
17
-
18
- /**
19
- * @class
20
- * A wrapper class that enhances Cesium collection objects with tagging functionality.
21
- * This class provides a consistent API for working with different types of Cesium collections
22
- * and allows grouping and manipulating collection items by custom tags.
23
- *
24
- * @template C - The type of Cesium collection (e.g., EntityCollection, PrimitiveCollection)
25
- * @template I - The type of items in the collection (e.g., Entity, Primitive)
26
- *
27
- * @example
28
- * // Example 1: Managing Complex Scene with Multiple Object Types
29
- * class SceneOrganizer {
30
- * private entities: Collection<EntityCollection, Entity>;
31
- * private billboards: Collection<BillboardCollection, Billboard>;
32
- * private primitives: Collection<PrimitiveCollection, Primitive>;
33
- *
34
- * constructor(viewer: Viewer) {
35
- * this.entities = new Collection({ collection: viewer.entities });
36
- * this.billboards = new Collection({
37
- * collection: viewer.scene.primitives.add(new BillboardCollection())
38
- * });
39
- * this.primitives = new Collection({
40
- * collection: viewer.scene.primitives
41
- * });
42
- * }
43
- *
44
- * // Unified API across different collection types!
45
- * showLayer(layerName: string) {
46
- * this.entities.show(layerName);
47
- * this.billboards.show(layerName);
48
- * this.primitives.show(layerName);
49
- * }
50
- *
51
- * hideLayer(layerName: string) {
52
- * this.entities.hide(layerName);
53
- * this.billboards.hide(layerName);
54
- * this.primitives.hide(layerName);
55
- * }
56
- *
57
- * removeLayer(layerName: string) {
58
- * this.entities.remove(layerName);
59
- * this.billboards.remove(layerName);
60
- * this.primitives.remove(layerName);
61
- * }
62
- * }
63
- *
64
- * // Example 2: Extend the class for Domain-Specific Needs
65
- * class BuildingCollection extends Collection<EntityCollection, Entity> {
66
- * constructor(viewer: Viewer) {
67
- * super({ collection: viewer.entities, tag: 'buildings' });
68
- * }
69
- *
70
- * addBuilding(options: {
71
- * position: Cartesian3;
72
- * height: number;
73
- * floors: number;
74
- * type: 'residential' | 'commercial' | 'industrial';
75
- * }): Entity {
76
- * const building = new Entity({
77
- * position: options.position,
78
- * box: {
79
- * dimensions: new Cartesian3(50, 50, options.height),
80
- * material: this.getMaterialForType(options.type)
81
- * }
82
- * });
83
- *
84
- * // Tag by type AND by floor count
85
- * this.add(building, options.type);
86
- * this.add(building, `floors-${options.floors}`);
87
- *
88
- * return building;
89
- * }
90
- *
91
- * getByFloorRange(min: number, max: number): Entity[] {
92
- * const results: Entity[] = [];
93
- * for (let i = min; i <= max; i++) {
94
- * results.push(...this.get(`floors-${i}`));
95
- * }
96
- * return results;
97
- * }
98
- *
99
- * private getMaterialForType(type: string): Material {
100
- * const colors = {
101
- * residential: Color.GREEN,
102
- * commercial: Color.BLUE,
103
- * industrial: Color.YELLOW
104
- * };
105
- * return new ColorMaterialProperty(colors[type] || Color.WHITE);
106
- * }
107
- * }
108
- */
109
- declare class Collection<C extends Collection.Base, I extends Collection.ItemFor<C>> {
110
- /**
111
- * Symbol used as a property key to store tags on collection items.
112
- * Using a Symbol ensures no property naming conflicts with the item's own properties.
113
- * @readonly
114
- */
115
- static readonly symbol: unique symbol;
116
- /**
117
- * Default tag used when adding items without specifying a tag.
118
- * @protected
119
- */
120
- protected tag: Collection.Tag;
121
- /**
122
- * The underlying Cesium collection being wrapped.
123
- * @protected
124
- */
125
- protected collection: C;
126
- /**
127
- * Cache for values array to improve performance
128
- * @private
129
- */
130
- private _valuesCache;
131
- /**
132
- * Tag to items map for faster lookups
133
- * @private
134
- */
135
- private _tagMap;
136
- /**
137
- * Event listeners
138
- * @private
139
- */
140
- private _eventListeners;
141
- /**
142
- * For cleaning up the instances
143
- * @private
144
- */
145
- private _eventCleanupFunctions;
146
- /**
147
- * Creates a new Collection instance.
148
- *
149
- * @param options - Configuration options
150
- * @param options.collection - The Cesium collection to wrap
151
- * @param options.tag - The default tag to use for items (defaults to 'default')
152
- */
153
- constructor({ collection, tag }: {
154
- collection: C;
155
- tag?: Collection.Tag;
156
- });
157
- /**
158
- * Makes the collection directly iterable, allowing it to be used in `for...of` loops
159
- * and with spread operators.
160
- *
161
- * @returns An iterator for the items in the collection
162
- *
163
- * @example
164
- * // Iterate through all items in the collection
165
- * for (const entity of collection) {
166
- * console.log(entity.id);
167
- * }
168
- *
169
- * // Convert collection to array using spread syntax
170
- * const entitiesArray = [...collection];
171
- */
172
- [Symbol.iterator](): Iterator<I>;
173
- /**
174
- * Gets all item instances in the collection.
175
- * This array should not be modified directly.
176
- *
177
- * @returns An array of all items in the collection
178
- */
179
- get values(): I[];
180
- /**
181
- * Gets the number of items in the collection.
182
- *
183
- * @returns The item count
184
- */
185
- get length(): number;
186
- /**
187
- * Gets all unique tags currently in use in the collection.
188
- *
189
- * @returns An array of all unique tags
190
- *
191
- * @example
192
- * // Get all tags
193
- * const tags = collection.tags;
194
- * console.log(`Collection has these tags: ${tags.join(', ')}`);
195
- */
196
- get tags(): Collection.Tag[];
197
- /**
198
- * Registers an event listener for collection events.
199
- *
200
- * @param type - The event type to listen for
201
- * @param handler - The callback function
202
- * @returns The collection instance for method chaining
203
- */
204
- addEventListener(type: Collection.Event, handler: Collection.EventHandler<I>): this;
205
- /**
206
- * Removes an event listener.
207
- *
208
- * @param type - The event type
209
- * @param handler - The callback function to remove
210
- * @returns The collection instance for method chaining
211
- */
212
- removeEventListener(type: Collection.Event, handler: Collection.EventHandler<I>): this;
213
- /**
214
- * Adds a single item with a tag to the collection.
215
- *
216
- * @param item - The item to add to the collection
217
- * @param tag - Tag to associate with this item (defaults to the collection's default tag)
218
- * @param index - The index to add the item at (if supported by the collection)
219
- * @returns The collection instance for method chaining
220
- *
221
- * @example
222
- * const entity = collection.add(new Entity({ ... }), 'landmarks');
223
- */
224
- add(item: I, tag?: Collection.Tag, index?: number): this;
225
- /**
226
- * Adds multiple items with the same tag to the collection.
227
- *
228
- * @param items - The array of items to add to the collection
229
- * @param tag - Tag to associate with this item (defaults to the collection's default tag)
230
- * @returns The collection instance for method chaining
231
- *
232
- * @example
233
- * // Add multiple entities with the same tag
234
- * const entities = [new Entity({ ... }), new Entity({ ... })];
235
- * const addedEntities = collection.add(entities, 'buildings');
236
- */
237
- add(items: I[], tag?: Collection.Tag): this;
238
- /**
239
- * Returns true if the provided item is in this collection, false otherwise.
240
- *
241
- * @param item - The item instance to check for
242
- * @returns True if the item is in the collection, false otherwise
243
- */
244
- contains(item: I): boolean;
245
- /**
246
- * Checks if the collection has any items with the specified tag.
247
- *
248
- * @param tag - The tag to check for
249
- * @returns True if items with the tag exist, false otherwise
250
- *
251
- * @example
252
- * if (collection.contains('temporary')) {
253
- * console.log('Temporary items exist');
254
- * }
255
- */
256
- contains(tag: Collection.Tag): boolean;
257
- /**
258
- * Removes an item from the collection.
259
- *
260
- * @param item - The item to remove
261
- * @returns The collection instance for method chaining
262
- */
263
- remove(item: I): this;
264
- /**
265
- * Removes all items with the specified tag from the collection.
266
- *
267
- * @param by - The tag identifying which items to remove
268
- * @returns The collection instance for method chaining
269
- */
270
- remove(by: Collection.Tag): this;
271
- /**
272
- * Removes all items with the array of tags from the collection.
273
- *
274
- * @param by - The tags identifying which items to remove
275
- * @returns The collection instance for method chaining
276
- */
277
- remove(by: Collection.Tag[]): this;
278
- /**
279
- * Removes all items from the collection.
280
- */
281
- removeAll(): void;
282
- /**
283
- * Permanently destroys this Collection instance.
284
- * Removes all event listeners and clears internal state.
285
- * The Collection instance should not be used after calling this method.
286
- */
287
- destroy(): void;
288
- /**
289
- * Gets all items with the specified tag from the collection.
290
- * Uses an optimized internal map for faster lookups.
291
- *
292
- * @param by - The tag to filter by
293
- * @returns An array of items with the specified tag, or an empty array if none found
294
- *
295
- * @example
296
- * // Get all buildings
297
- * const buildings = collection.get('buildings');
298
- */
299
- get(by: Collection.Tag): I[];
300
- /**
301
- * Gets the first item matching the tag. More efficient than `get` when
302
- * you only need one item, especially for large collections.
303
- *
304
- * @param by - The tag to search for
305
- * @returns The first matching item or undefined if none found
306
- *
307
- * @example
308
- * // Get the first building
309
- * const firstBuilding = collection.first('buildings');
310
- */
311
- first(by: Collection.Tag): I | undefined;
312
- /**
313
- * Updates the tag for all items with the specified tag.
314
- *
315
- * @param from - The tag to replace
316
- * @param to - The new tag to assign
317
- * @returns The number of items updated
318
- *
319
- * @example
320
- * // Rename a tag
321
- * const count = collection.update('temp', 'temporary');
322
- * console.log(`Updated ${count} items`);
323
- */
324
- update(from: Collection.Tag, to: Collection.Tag): number;
325
- /**
326
- * Makes all items with the specified tag visible.
327
- * Only affects items that have a 'show' property.
328
- *
329
- * @param by - The tag identifying which items to show
330
- * @returns The collection itself.
331
- *
332
- * @example
333
- * // Show all buildings
334
- * collection.show('buildings');
335
- */
336
- show(by: Collection.Tag): this;
337
- /**
338
- * Hides all items with the specified tag.
339
- * Only affects items that have a 'show' property.
340
- *
341
- * @param by - The tag identifying which items to hide
342
- * @returns The collection itself.
343
- *
344
- * @example
345
- * // Hide all buildings
346
- * collection.hide('buildings');
347
- */
348
- hide(by: Collection.Tag): this;
349
- /**
350
- * Toggles visibility of all items with the specified tag.
351
- * Only affects items that have a 'show' property.
352
- *
353
- * @param by - The tag identifying which items to toggle
354
- * @returns The collection itself.
355
- *
356
- * @example
357
- * // Toggle visibility of all buildings
358
- * collection.toggle('buildings');
359
- */
360
- toggle(by: Collection.Tag): this;
361
- /**
362
- * Sets a property value on all items with the specified tag.
363
- *
364
- * @template K - A type
365
- *
366
- * @param property - The property name to set
367
- * @param value - The value to set
368
- * @param by - The tag identifying which items to update
369
- * @returns The collection itself.
370
- *
371
- * @example
372
- * // Change color of all buildings to red
373
- * collection.setProperty('color', Color.RED, 'buildings');
374
- */
375
- setProperty<K extends NonFunction<I>>(property: K, value: I[K], by?: Collection.Tag): this;
376
- /**
377
- * Filters items in the collection based on a predicate function.
378
- * Optionally only filters items with a specific tag.
379
- *
380
- * @param predicate - Function that tests each item
381
- * @param by - Optional tag to filter by before applying the predicate
382
- * @returns Array of items that pass the test
383
- *
384
- * @example
385
- * // Get all buildings taller than 100 meters
386
- * const tallBuildings = collection.filter(
387
- * entity => entity.properties?.height?.getValue() > 100,
388
- * 'buildings'
389
- * );
390
- */
391
- filter(predicate: (item: I) => boolean, by?: Collection.Tag): I[];
392
- /**
393
- * Executes a callback function for each item in the collection.
394
- * Optionally filters items by tag before execution.
395
- *
396
- * @param callback - Function to execute for each item
397
- * @param by - Optional tag to filter items (if not provided, processes all items)
398
- *
399
- * @example
400
- * // Highlight all buildings
401
- * collection.forEach((entity) => {
402
- * if (entity.polygon) {
403
- * entity.polygon.material = new ColorMaterialProperty(Color.YELLOW);
404
- * }
405
- * }, 'buildings');
406
- */
407
- forEach(callback: (value: I, index: number) => void, by?: Collection.Tag): void;
408
- /**
409
- * Creates a new array with the results of calling a provided function on every element
410
- * in the collection. Optionally filters by tag before mapping.
411
- *
412
- * @param callbackfn - Function that produces an element of the new array
413
- * @param by - Optional tag to filter items by before mapping
414
- * @returns A new array with each element being the result of the callback function
415
- *
416
- * @example
417
- * // Get all entity IDs
418
- * const entityIds = collection.map(entity => entity.id);
419
- *
420
- * // Get positions of all buildings
421
- * const buildingPositions = collection.map(
422
- * entity => entity.position.getValue(Cesium.JulianDate.now()),
423
- * 'buildings'
424
- * );
425
- */
426
- map<R>(callbackfn: (value: I, index: number) => R, by?: Collection.Tag): R[];
427
- /**
428
- * Returns the first element in the collection that satisfies the provided testing function.
429
- * Optionally filters by tag before searching.
430
- *
431
- * @param predicate - Function to test each element
432
- * @param by - Optional tag to filter items by before searching
433
- * @returns The first element that passes the test, or undefined if no elements pass
434
- *
435
- * @example
436
- * // Find the first entity with a specific name
437
- * const namedEntity = collection.find(entity => entity.name === 'Target');
438
- *
439
- * // Find the first building taller than 100 meters
440
- * const tallBuilding = collection.find(
441
- * entity => entity.properties?.height?.getValue() > 100,
442
- * 'buildings'
443
- * );
444
- */
445
- find(predicate: (value: I) => boolean, by?: Collection.Tag): I | undefined;
446
- /**
447
- * Emits an event to all registered listeners.
448
- *
449
- * @private
450
- * @param type - The event type
451
- * @param data - Additional event data
452
- */
453
- private _emit;
454
- /**
455
- * Adds an item to the internal tag map for quick lookups.
456
- *
457
- * @private
458
- * @param item - The item to add
459
- * @param tag - The tag to associate with the item
460
- */
461
- private _addToTagMap;
462
- /**
463
- * Removes an item from the internal tag map.
464
- *
465
- * @private
466
- * @param item - The item to remove
467
- */
468
- private _removeFromTagMap;
469
- /**
470
- * Invalidates the values cache when collection changes.
471
- *
472
- * @private
473
- */
474
- private _invalidateCache;
475
- /**
476
- * Sets up automatic cache invalidation by registering event listeners on the underlying Cesium collection.
477
- *
478
- * @private
479
- * @param collection - The Cesium collection to monitor for changes
480
- *
481
- * @see {@link destroy} For cleanup of event listeners
482
- * @see {@link _invalidateCache} For the actual cache invalidation logic
483
- */
484
- private _setupCacheInvalidator;
485
- }
486
- /**
487
- * @namespace
488
- */
489
- declare namespace Collection {
490
- /**
491
- * The underlying Cesium collection type being wrapped.
492
- */
493
- export type Base = DataSourceCollection | EntityCollection | ImageryLayerCollection | PrimitiveCollection;
494
- /**
495
- * The item types that can be added to the `PrimitiveCollection` instance.
496
- */
497
- type Primitives = Billboard | Cesium3DTileset | GroundPrimitive | Label | PointPrimitive | Polyline | Primitive;
498
- /**
499
- * Cesium item type that can be added to the {@link Collection.Base} instance.
500
- */
501
- export type Item = DataSource | Entity | ImageryLayer | Primitives;
502
- /**
503
- * Gets the item type for a given collection type
504
- */
505
- export type ItemFor<C extends Base> = C extends DataSourceCollection ? DataSource : C extends EntityCollection ? Entity : C extends ImageryLayerCollection ? ImageryLayer : C extends PrimitiveCollection ? Primitives : never;
506
- /**
507
- * Collection tag type.
508
- */
509
- export type Tag = string | number;
510
- export interface WithTag {
511
- [key: symbol]: Tag;
512
- }
513
- /**
514
- * Collection event types
515
- */
516
- export type Event = "add" | "remove" | "update" | "clear";
517
- /**
518
- * Event handler function type
519
- */
520
- export type EventHandler<I> = (event: {
521
- type: Event;
522
- items?: I[];
523
- tag?: Collection.Tag;
524
- }) => void;
525
- export { };
526
- }
527
-
528
- export { Collection as C, type NonFunction as N, isGetterOnly as i };