Cesium Utils - v0.0.9
    Preparing search index...

    Class Collection<C, I>

    Abstract class that enhances Cesium collection objects with tagging functionality. This class provides a consistent API for working with different types of Cesium collections and allows grouping and manipulating collection items by custom tags.

    // Creating a specialized collection for entities
    class MyEntities extends Collection<EntityCollection, Entity> {
    constructor(viewer) {
    super({ collection: viewer.entities, tag: 'myEntities' });
    }

    get values() {
    return this.collection.values;
    }
    }

    const entities = new MyEntities(viewer);
    entities.add(new Entity({ ... }), 'buildings');
    entities.add(new Entity({ ... }), 'roads');

    // Later, show only buildings
    entities.show('buildings').hide('roads');

    Type Parameters

    • C extends CesiumCollection

      The type of Cesium collection (e.g., EntityCollection, PrimitiveCollection)

    • I extends CesiumCollectionItem

      The type of items in the collection (e.g., Entity, Primitive)

    Index

    Constructors

    Properties

    _eventListeners: Map<CollectionEventType, Set<EventHandler<I>>> = ...

    Event listeners

    _tagMap: Map<Tag, Set<I>> = ...

    Tag to items map for faster lookups

    _valuesCache: null | I[] = null

    Cache for values array to improve performance

    collection: C

    The underlying Cesium collection being wrapped.

    tag: Tag

    Default tag used when adding items without specifying a tag.

    symbol: typeof symbol = ...

    Symbol used as a property key to store tags on collection items. Using a Symbol ensures no property naming conflicts with the item's own properties.

    Accessors

    • get tags(): Tag[]

      Gets all unique tags currently in use in the collection.

      Returns Tag[]

      An array of all unique tags

      // Get all tags
      const tags = collection.tags;
      console.log(`Collection has these tags: ${tags.join(', ')}`);
    • get values(): I[]

      Gets all item instances in the collection. This array should not be modified directly.

      Returns I[]

      An array of all items in the collection

    Methods

    • Private

      Adds an item to the internal tag map for quick lookups.

      Parameters

      • item: I

        The item to add

      • tag: Tag

        The tag to associate with the item

      Returns void

    • Makes the collection directly iterable, allowing it to be used in for...of loops and with spread operators.

      Returns Iterator<I>

      An iterator for the items in the collection

      // Iterate through all items in the collection
      for (const entity of collection) {
      console.log(entity.id);
      }

      // Convert collection to array using spread syntax
      const entitiesArray = [...collection];
    • Adds a single item with a tag to the collection.

      Parameters

      • item: I

        The item to add to the collection

      • Optionaltag: Tag

        Tag to associate with this item (defaults to the collection's default tag)

      • Optionalindex: number

        The index to add the item at (if supported by the collection)

      Returns I

      The added item for chaining

      const entity = collection.add(new Entity({ ... }), 'landmarks');
      
    • Adds multiple items with the same tag to the collection.

      Parameters

      • items: I[]

        The array of items to add to the collection

      • Optionaltag: Tag

        Tag to associate with this item (defaults to the collection's default tag)

      Returns I[]

      The array of added items

      // Add multiple entities with the same tag
      const entities = [new Entity({ ... }), new Entity({ ... })];
      const addedEntities = collection.add(entities, 'buildings');
    • Returns true if the provided item is in this collection, false otherwise.

      Parameters

      • item: I

        The item instance to check for

      Returns boolean

      True if the item is in the collection, false otherwise

    • Checks if the collection has any items with the specified tag.

      Parameters

      • tag: Tag

        The tag to check for

      Returns boolean

      True if items with the tag exist, false otherwise

      if (collection.contains('temporary')) {
      console.log('Temporary items exist');
      }
    • Filters items in the collection based on a predicate function. Optionally only filters items with a specific tag.

      Parameters

      • predicate: (item: I) => boolean

        Function that tests each item

      • Optionalby: Tag

        Optional tag to filter by before applying the predicate

      Returns I[]

      Array of items that pass the test

      // Get all buildings taller than 100 meters
      const tallBuildings = collection.filter(
      entity => entity.properties?.height?.getValue() > 100,
      'buildings'
      );
    • Returns the first element in the collection that satisfies the provided testing function. Optionally filters by tag before searching.

      Parameters

      • predicate: (value: I) => boolean

        Function to test each element

      • Optionalby: Tag

        Optional tag to filter items by before searching

      Returns undefined | I

      The first element that passes the test, or undefined if no elements pass

      // Find the first entity with a specific name
      const namedEntity = collection.find(entity => entity.name === 'Target');

      // Find the first building taller than 100 meters
      const tallBuilding = collection.find(
      entity => entity.properties?.height?.getValue() > 100,
      'buildings'
      );
    • Gets the first item matching the tag. More efficient than get when you only need one item, especially for large collections.

      Parameters

      • by: Tag

        The tag to search for

      Returns undefined | I

      The first matching item or undefined if none found

      // Get the first building
      const firstBuilding = collection.first('buildings');
    • Executes a callback function for each item in the collection. Optionally filters items by tag before execution.

      Parameters

      • callback: (value: I, index: number) => void

        Function to execute for each item

      • Optionalby: Tag

        Optional tag to filter items (if not provided, processes all items)

      Returns void

      // Highlight all buildings
      collection.forEach((entity) => {
      if (entity.polygon) {
      entity.polygon.material = new ColorMaterialProperty(Color.YELLOW);
      }
      }, 'buildings');
    • Gets all items with the specified tag from the collection. Uses an optimized internal map for faster lookups.

      Parameters

      • by: Tag

        The tag to filter by

      Returns I[]

      An array of items with the specified tag, or an empty array if none found

      // Get all buildings
      const buildings = collection.get('buildings');
    • Hides all items with the specified tag. Only affects items that have a 'show' property.

      Parameters

      • by: Tag

        The tag identifying which items to hide

      Returns this

      The collection itself.

      // Hide all buildings
      collection.hide('buildings');
    • Creates a new array with the results of calling a provided function on every element in the collection. Optionally filters by tag before mapping.

      Type Parameters

      • R

      Parameters

      • callbackfn: (value: I, index: number) => R

        Function that produces an element of the new array

      • Optionalby: Tag

        Optional tag to filter items by before mapping

      Returns R[]

      A new array with each element being the result of the callback function

      // Get all entity IDs
      const entityIds = collection.map(entity => entity.id);

      // Get positions of all buildings
      const buildingPositions = collection.map(
      entity => entity.position.getValue(Cesium.JulianDate.now()),
      'buildings'
      );
    • Removes an item from the collection.

      Parameters

      • item: I

        The item to remove

      Returns boolean

      True if the item was removed, false if it wasn't found

    • Removes all items with the specified tag from the collection.

      Parameters

      • by: Tag

        The tag identifying which items to remove

      Returns boolean

      True if the item was removed, false if it wasn't found

    • Removes all items with the array of tags from the collection.

      Parameters

      • by: Tag[]

        The tags identifying which items to remove

      Returns boolean

      True if the item was removed, false if it wasn't found

    • Sets a property value on all items with the specified tag.

      Type Parameters

      • K extends string | number | symbol
      • V

      Parameters

      • property: K

        The property name to set

      • value: V

        The value to set

      • by: Tag = ...

        The tag identifying which items to update

      Returns this

      The number of items updated

      // Change color of all buildings to red
      collection.setProperty('buildings', 'color', Color.RED);
    • Makes all items with the specified tag visible. Only affects items that have a 'show' property.

      Parameters

      • by: Tag

        The tag identifying which items to show

      Returns this

      The collection itself.

      // Show all buildings
      collection.show('buildings');
    • Toggles visibility of all items with the specified tag. Only affects items that have a 'show' property.

      Parameters

      • by: Tag

        The tag identifying which items to toggle

      Returns this

      The collection itself.

      // Toggle visibility of all buildings
      collection.toggle('buildings');
    • Updates the tag for all items with the specified tag.

      Parameters

      • from: Tag

        The tag to replace

      • to: Tag

        The new tag to assign

      Returns number

      The number of items updated

      // Rename a tag
      const count = collection.update('temp', 'temporary');
      console.log(`Updated ${count} items`);