Skip to content

Add remove_layer method to Python GISDocument API #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,36 @@ def add_heatmap_layer(

return self._add_layer(OBJECT_FACTORY.create_layer(layer, self))

def remove_layer(self, layer_id: str):
"""
Remove a layer from the GIS document.

:param layer_id: The ID of the layer to remove.
:raises KeyError: If the layer does not exist.
"""

layer = self._layers.get(layer_id)

if layer is None:
raise KeyError(f"No layer found with ID: {layer_id}")

del self._layers[layer_id]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also remove the corresponding source if it is not used by another layer ?

Copy link
Member Author

Choose a reason for hiding this comment

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

That seems sensible! I'll do a bit of test-driven development today (or more realistically, tomorrow. No meetings on Fridays!) :)

self._remove_source_if_orphaned(layer["parameters"]["source"])

def _remove_source_if_orphaned(self, source_id: str):
source = self._sources.get(source_id)

if source is None:
raise KeyError(f"No source found with ID: {source_id}")

source_is_orphan = not any(
layer["parameters"]["source"] == source_id
for layer in self._layers.values()
)

if source_is_orphan:
del self._sources[source_id]

def create_color_expr(
self,
color_stops: Dict,
Expand Down
35 changes: 24 additions & 11 deletions python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import os
import unittest

from jupytergis_lab import GISDocument
import pytest

from jupytergis_lab import GISDocument

class VectorTileTests(unittest.TestCase):
def setUp(self):
self.doc = GISDocument()
TEST_TIF = "https://s2downloads.eox.at/demo/EOxCloudless/2020/rgbnir/s2cloudless2020-16bits_sinlge-file_z0-4.tif"


class TiffLayerTests(unittest.TestCase):
def setUp(self):
class TestDocument:
def setup_method(self):
self.doc = GISDocument()


class TestTiffLayer(TestDocument):
def test_sourcelayer(self):
color = self.doc.create_color_expr(
interpolation_type="linear",
Expand All @@ -26,8 +26,21 @@ def test_sourcelayer(self):
},
)

tif_layer = self.doc.add_tiff_layer(
url="https://s2downloads.eox.at/demo/EOxCloudless/2020/rgbnir/s2cloudless2020-16bits_sinlge-file_z0-4.tif",
color_expr=color,
)
tif_layer = self.doc.add_tiff_layer(url=TEST_TIF, color_expr=color)
assert self.doc.layers[tif_layer]["parameters"]["color"] == color


class TestLayerManipulation(TestDocument):
def test_add_and_remove_layer_and_source(self):
layer_id = self.doc.add_tiff_layer(url=TEST_TIF)
assert len(self.doc.layers) == 1

# After removing the layer, the source is not associated with any layer, so we
# expect it to be removed as well.
self.doc.remove_layer(layer_id)
assert len(self.doc.layers) == 0
assert len(self.doc._sources) == 0

def test_remove_nonexistent_layer_raises(self):
with pytest.raises(KeyError):
self.doc.remove_layer("foo")
Loading