Skip to content

Commit ce01428

Browse files
committed
Error on adding dimension with existing name (Open-EO/openeo-geopyspark-driver#205)
1 parent 3c6f11a commit ce01428

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
`Connection.execute()` and `Connection.create_job()`
1414
- Add support for reverse math operators on DataCube in `apply` mode ([#323](https://github.com/Open-EO/openeo-python-client/issues/323))
1515
- Add `DataCube.print_json()` to simplify exporting process graphs in Jupyter or other interactive environments ([#324](https://github.com/Open-EO/openeo-python-client/issues/324))
16-
16+
- Raise `DimensionAlreadyExistsException` when trying to `add_dimension()` a dimension with existing name ([Open-EO/openeo-geopyspark-driver#205](https://github.com/Open-EO/openeo-geopyspark-driver/issues/205))
1717

1818
### Changed
1919

openeo/metadata.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class MetadataException(Exception):
1010
pass
1111

1212

13+
class DimensionAlreadyExistsException(MetadataException):
14+
pass
15+
16+
1317
class Dimension:
1418
"""Base class for dimensions."""
1519

@@ -412,6 +416,8 @@ def reduce_dimension(self, dimension_name: str) -> 'CollectionMetadata':
412416

413417
def add_dimension(self, name: str, label: Union[str, float], type: str = None) -> 'CollectionMetadata':
414418
"""Create new metadata object with added dimension"""
419+
if any(d.name == name for d in self._dimensions):
420+
raise DimensionAlreadyExistsException(f"Dimension with name {name!r} already exists")
415421
if type == "bands":
416422
dim = BandDimension(name=name, bands=[Band(label, None, None)])
417423
elif type == "spatial":

tests/rest/datacube/test_bandmath.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,13 @@ def test_fuzzy_mask(connection, api_version):
342342
assert mask.flat_graph() == load_json_resource('data/%s/fuzzy_mask.json' % api_version)
343343

344344

345-
def test_fuzzy_mask_band_math(connection, api_version):
346-
s2 = connection.load_collection("SENTINEL2_SCF")
345+
def test_fuzzy_mask_band_math(con100):
346+
s2 = con100.load_collection("SENTINEL2_SCF")
347347
scf_band = s2.band("SCENECLASSIFICATION")
348348
clouds = scf_band == 4
349349
fuzzy = clouds.apply_kernel(kernel=0.1 * np.ones((3, 3)))
350350
mask = fuzzy.add_dimension("bands", "mask", "bands").band("mask") > 0.3
351-
assert mask.flat_graph() == load_json_resource('data/%s/fuzzy_mask_add_dim.json' % api_version)
351+
assert mask.flat_graph() == load_json_resource('data/1.0.0/fuzzy_mask_add_dim.json')
352352

353353

354354
def test_normalized_difference(connection, api_version):

tests/test_metadata.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from openeo.metadata import CollectionMetadata, Band, SpatialDimension, Dimension, TemporalDimension, BandDimension, \
7-
MetadataException
7+
MetadataException, DimensionAlreadyExistsException
88

99

1010
def test_metadata_get():
@@ -478,6 +478,17 @@ def test_metadata_add_band_dimension():
478478
assert new.band_names == ["red"]
479479

480480

481+
def test_metadata_add_band_dimension_duplicate():
482+
metadata = CollectionMetadata({
483+
"cube:dimensions": {
484+
"t": {"type": "temporal"}
485+
}
486+
})
487+
metadata = metadata.add_dimension("layer", "red", "bands")
488+
with pytest.raises(DimensionAlreadyExistsException, match="Dimension with name 'layer' already exists"):
489+
_ = metadata.add_dimension("layer", "red", "bands")
490+
491+
481492
def test_metadata_add_temporal_dimension():
482493
metadata = CollectionMetadata({
483494
"cube:dimensions": {
@@ -495,6 +506,17 @@ def test_metadata_add_temporal_dimension():
495506
assert new.temporal_dimension.extent == ["2020-05-15", "2020-05-15"]
496507

497508

509+
def test_metadata_add_temporal_dimension_duplicate():
510+
metadata = CollectionMetadata({
511+
"cube:dimensions": {
512+
"x": {"type": "spatial"}
513+
}
514+
})
515+
metadata = metadata.add_dimension("date", "2020-05-15", "temporal")
516+
with pytest.raises(DimensionAlreadyExistsException, match="Dimension with name 'date' already exists"):
517+
_ = metadata.add_dimension("date", "2020-05-15", "temporal")
518+
519+
498520
def test_metadata_drop_dimension():
499521
metadata = CollectionMetadata({
500522
"cube:dimensions": {

0 commit comments

Comments
 (0)