Skip to content

Commit

Permalink
added unit tests for STAC splitter. Let STAC splitter raise KeyError …
Browse files Browse the repository at this point in the history
…when proj:epsg property is not present in STAC item.
  • Loading branch information
VincentVerelst committed Aug 9, 2024
1 parent a51b223 commit 1127650
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/openeo_gfmap/utils/split_stac.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility function to split a STAC collection into multiple STAC collections based on CRS.
Requires the "proj:epsg" property to be present in all the STAC items.
"""

import os
Expand All @@ -16,13 +17,17 @@ def _extract_epsg_from_stac_item(stac_item: pystac.Item) -> int:
stac_item (pystac.Item): The STAC item.
Returns:
int: The EPSG code, or None if not found.
int: The EPSG code.
Raises:
KeyError: If the "proj:epsg" property is missing from the STAC item.
"""

try:
epsg_code = stac_item.properties.get("proj:epsg")
epsg_code = stac_item.properties["proj:epsg"]
return epsg_code
except KeyError:
return None
raise KeyError("The 'proj:epsg' property is missing from the STAC item.")


def _create_item_by_epsg_dict(collection: pystac.Collection) -> dict:
Expand Down
99 changes: 98 additions & 1 deletion tests/test_openeo_gfmap/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from pathlib import Path

import pystac
import pytest
from netCDF4 import Dataset

from openeo_gfmap import Backend, BackendContext, BoundingBoxExtent, TemporalContext
from openeo_gfmap.utils import update_nc_attributes
from openeo_gfmap.utils import split_collection_by_epsg, update_nc_attributes
from openeo_gfmap.utils.catalogue import s1_area_per_orbitstate, select_S1_orbitstate

# Region of Paris, France
Expand Down Expand Up @@ -76,3 +77,99 @@ def test_update_nc_attributes(temp_nc_file):
assert getattr(nc, attr_name) == attr_value
assert "existing_attribute" in nc.ncattrs()
assert nc.getncattr("existing_attribute") == "existing_value"


def test_split_collection_by_epsg(tmp_path):
collection = pystac.collection.Collection.from_dict(
{
"type": "Collection",
"id": "test-collection",
"stac_version": "1.0.0",
"description": "Test collection",
"links": [],
"title": "Test Collection",
"extent": {
"spatial": {"bbox": [[-180.0, -90.0, 180.0, 90.0]]},
"temporal": {
"interval": [["2020-01-01T00:00:00Z", "2020-01-10T00:00:00Z"]]
},
},
"license": "proprietary",
"summaries": {"eo:bands": [{"name": "B01"}, {"name": "B02"}]},
}
)
first_item = pystac.item.Item.from_dict(
{
"type": "Feature",
"stac_version": "1.0.0",
"id": "4326-item",
"properties": {
"datetime": "2020-05-22T00:00:00Z",
"eo:bands": [{"name": "SCL"}, {"name": "B08"}],
"proj:epsg": 4326,
},
"geometry": {
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
"type": "Polygon",
},
"links": [],
"assets": {},
"bbox": [0, 1, 0, 1],
"stac_extensions": [],
}
)
second_item = pystac.item.Item.from_dict(
{
"type": "Feature",
"stac_version": "1.0.0",
"id": "3857-item",
"properties": {
"datetime": "2020-05-22T00:00:00Z",
"eo:bands": [{"name": "SCL"}, {"name": "B08"}],
"proj:epsg": 3857,
},
"geometry": {
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
"type": "Polygon",
},
"links": [],
"assets": {},
"bbox": [0, 1, 0, 1],
"stac_extensions": [],
}
)
collection.add_items([first_item, second_item])
input_dir = str(tmp_path / "collection.json")
output_dir = str(tmp_path / "split_collections")

collection.normalize_and_save(input_dir)
split_collection_by_epsg(path=input_dir, output_dir=output_dir)

# Collection contains two different EPSG codes, so 2 collections should be created
assert len([p for p in Path(output_dir).iterdir() if p.is_dir()]) == 2

missing_epsg_item = pystac.item.Item.from_dict(
{
"type": "Feature",
"stac_version": "1.0.0",
"id": "3857-item",
"properties": {
"datetime": "2020-05-22T00:00:00Z",
"eo:bands": [{"name": "SCL"}, {"name": "B08"}],
},
"geometry": {
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
"type": "Polygon",
},
"links": [],
"assets": {},
"bbox": [0, 1, 0, 1],
"stac_extensions": [],
}
)

# Collection contains item without EPSG, so KeyError should be raised
with pytest.raises(KeyError):
collection.add_item(missing_epsg_item)
collection.normalize_and_save(input_dir)
split_collection_by_epsg(path=input_dir, output_dir=output_dir)

0 comments on commit 1127650

Please sign in to comment.