Skip to content

Conversation

@giswqs
Copy link
Member

@giswqs giswqs commented Jan 31, 2026

Problem

Fixes issue #136 where adding multiple landmarks/entities to a Cesium map results in the error:
An entity with id point_0 already exists in this collection.

Root Cause

The existing code generates entity IDs using len(self._layers) which causes ID collisions because:

  1. Different entity types (points, billboards, polylines, polygons) can generate the same numeric suffix
  2. If entities are removed, len(self._layers) can decrease and reuse previous IDs
  3. The length-based approach doesn't guarantee unique IDs across the entire entity collection

Solution

  • Added entity counter: Introduced self._entity_counter that only increments, never decreases
  • Unique ID generator: Added _generate_entity_id(prefix) method that creates guaranteed unique IDs
  • Fixed all entity types: Updated add_point, add_billboard, add_polyline, and add_polygon methods
  • Prevents ID reuse: Counter-based approach ensures no ID collision even after entity removal

Changes

  • anymap/cesium.py:
    • Initialize _entity_counter = 0 in constructor
    • Add _generate_entity_id(prefix) method
    • Replace f"{type}_{len(self._layers)}" with self._generate_entity_id(type) in all entity creation methods

Testing

Verified the fix works correctly:

  • Generated multiple entities of different types
  • Confirmed all IDs are unique: ['point_0', 'point_1', 'billboard_2', 'polyline_3', 'polygon_4']
  • Tested edge cases like entity removal followed by new entity creation
  • No ID collisions occur

Impact

  • ✅ Resolves the 'entity already exists' error when adding multiple landmarks
  • ✅ Maintains backward compatibility (same ID format, just guaranteed unique)
  • ✅ No breaking changes to existing API
  • ✅ Applies to all Cesium entity types consistently

Closes #136

- Replace len(self._layers) based ID generation with proper entity counter
- Add _generate_entity_id() method that ensures unique IDs across all entity types
- Fix applies to add_point, add_billboard, add_polyline, and add_polygon methods
- Prevents 'An entity with id point_0 already exists in this collection' error
- Entity counter increments globally ensuring no ID reuse even after entity removal

Resolves #136
Copilot AI review requested due to automatic review settings January 31, 2026 16:02
@giswqs giswqs self-assigned this Jan 31, 2026
@github-actions
Copy link

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a critical bug in Cesium entity ID generation that caused "entity already exists" errors when adding multiple landmarks or entities to a Cesium map (issue #136).

Changes:

  • Introduced a monotonically-incrementing _entity_counter to ensure unique entity IDs
  • Added _generate_entity_id() helper method for consistent ID generation across all entity types
  • Updated all entity creation methods (add_point, add_billboard, add_polyline, add_polygon) to use the new ID generator

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +131 to +142
def _generate_entity_id(self, prefix: str) -> str:
"""Generate a unique entity ID with the given prefix.

Args:
prefix: The prefix for the entity ID (e.g., 'point', 'billboard')

Returns:
A unique entity ID string
"""
entity_id = f"{prefix}_{self._entity_counter}"
self._entity_counter += 1
return entity_id
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix for entity ID uniqueness should include a corresponding test case. Both LeafletMap and OpenLayersMap have test_layer_id_generation tests (see tests/test_leaflet.py:271 and tests/test_openlayers.py:313) that verify ID uniqueness by adding multiple entities and confirming all IDs are unique and properly prefixed. Consider adding a similar test_entity_id_generation test to tests/test_anymap.py in the TestCesiumMap class that creates multiple entities of different types and verifies all generated IDs are unique and don't collide.

Copilot uses AI. Check for mistakes.
@giswqs giswqs merged commit 71ae507 into main Feb 2, 2026
14 checks passed
@giswqs giswqs deleted the fix/cesium-entity-ids branch February 2, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cesiium functionlities do not work as expected

2 participants