Skip to content

Commit 9c5eff1

Browse files
authored
Merge pull request #51 from Jena-Earth-Observation-School/filter_collection_ids
Allow filtering Sentinel-2 L2A data by STAC Collection IDs
2 parents 2417ef4 + ab2c97f commit 9c5eff1

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

sdc/products/_query.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def filter_stac_catalog(catalog: Catalog,
1212
bbox: Optional[tuple[float, float, float, float]] = None,
13+
collection_ids: Optional[list[str]] = None,
1314
time_range: Optional[tuple[str, str]] = None,
1415
time_pattern: Optional[str] = None
1516
) -> tuple[list[Collection], Iterable[Item]]:
@@ -24,6 +25,9 @@ def filter_stac_catalog(catalog: Catalog,
2425
The STAC Catalog to filter.
2526
bbox : tuple of float, optional
2627
The bounding box of the area of interest in the format (minx, miny, maxx, maxy).
28+
collection_ids : list of str, optional
29+
A list of collection IDs to filter. If not None, this will override the `bbox`
30+
option.
2731
time_range : tuple of str, optional
2832
Time range to load as a tuple of (start_time, stop_time), where start_time and
2933
stop_time are strings in the format specified by `time_pattern`. Default is
@@ -39,13 +43,14 @@ def filter_stac_catalog(catalog: Catalog,
3943
filtered_items : list of Item
4044
A list of filtered items.
4145
"""
42-
filtered_collections = filter_collections(catalog, bbox)
46+
filtered_collections = filter_collections(catalog, bbox, collection_ids)
4347
filtered_items = filter_items(filtered_collections, time_range, time_pattern)
4448
return filtered_collections, filtered_items
4549

4650

4751
def filter_collections(catalog: Catalog,
48-
bbox: Optional[tuple[float, float, float, float]] = None
52+
bbox: Optional[tuple[float, float, float, float]] = None,
53+
collection_ids: Optional[list[str]] = None
4954
) -> list[Collection]:
5055
"""
5156
Filters the collections in a STAC Catalog based on a bounding box.
@@ -56,21 +61,28 @@ def filter_collections(catalog: Catalog,
5661
The STAC Catalog to filter.
5762
bbox : tuple of float, optional
5863
The bounding box of the area of interest in the format (minx, miny, maxx, maxy).
64+
collection_ids : list of str, optional
65+
A list of collection IDs to filter. If not None, this will override the `bbox`
66+
option.
5967
6068
Returns
6169
-------
6270
list of Collection
6371
A list of filtered collections.
6472
"""
65-
if bbox is None:
73+
if collection_ids is not None:
6674
return [collection for collection in catalog.get_children()
67-
if isinstance(collection, Collection)]
68-
else:
75+
if isinstance(collection, Collection) and
76+
collection.id in collection_ids]
77+
elif bbox is not None:
6978
return [collection for collection in catalog.get_children() if
7079
isinstance(collection, Collection) and
7180
collection.extent.spatial.bboxes is not None and
7281
any(_bbox_intersection(list(bbox), b) is not None
7382
for b in collection.extent.spatial.bboxes)]
83+
else:
84+
return [collection for collection in catalog.get_children()
85+
if isinstance(collection, Collection)]
7486

7587

7688
def _bbox_intersection(bbox1: list[float, float, float, float],

sdc/products/s2.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from sdc.products import _query as query
1212

1313

14-
def load_s2_l2a(bounds: tuple[float, float, float, float],
14+
def load_s2_l2a(bounds: tuple[float, float, float, float] = None,
15+
collection_ids: Optional[list[str]] = None,
1516
time_range: Optional[tuple[str, str]] = None,
1617
time_pattern: Optional[str] = None,
1718
apply_mask: bool = True,
@@ -25,6 +26,9 @@ def load_s2_l2a(bounds: tuple[float, float, float, float],
2526
bounds: tuple of float
2627
The bounding box of the area of interest in the format (minx, miny, maxx, maxy).
2728
Will be used to filter the STAC Catalog for intersecting STAC Collections.
29+
collection_ids : list of str, optional
30+
A list of collection IDs to filter. If not None, this will override the `bbox`
31+
option.
2832
time_range : tuple of str, optional
2933
The time range in the format (start_time, end_time) to filter STAC Items by.
3034
Defaults to None, which will load all STAC Items in the filtered STAC
@@ -67,9 +71,14 @@ def load_s2_l2a(bounds: tuple[float, float, float, float],
6771
'B09', # Water Vapour (60 m)
6872
'B11', 'B12'] # SWIR 1, SWIR 2 (20 m)
6973

74+
if bounds is None and collection_ids is None:
75+
raise ValueError("Either `bounds` or `collection_ids` must be provided.")
76+
7077
# Load and filter STAC Items
7178
catalog = Catalog.from_file(anc.get_catalog_path(product=product))
72-
_, items = query.filter_stac_catalog(catalog=catalog, bbox=bounds,
79+
_, items = query.filter_stac_catalog(catalog=catalog,
80+
bbox=bounds,
81+
collection_ids=collection_ids,
7382
time_range=time_range,
7483
time_pattern=time_pattern)
7584

0 commit comments

Comments
 (0)