Skip to content

feat: filter collection based on name #218

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions tests/routes/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,20 @@ def test_collections_temporal_extent_datetime_column(app):
assert len(intervals) == 4
assert intervals[0][0] == "2004-10-19T10:23:54+00:00"
assert intervals[0][1] == "2007-10-24T00:00:00+00:00"

def test_collections_collectionId_substring_filter(app):
"""Test /collections endpoint."""
response = app.get("/collections", params={"collectionId_substring": "_mgrs"})
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()

ids = [x["id"] for x in body["collections"]]

assert "public.sentinel_mgrs" in ids
assert "pg_temp.landsat_centroids" not in ids
assert "pg_temp.hexagons" not in ids
assert "pg_temp.squares" not in ids
assert "public.st_squaregrid" not in ids
assert "public.st_hexagongrid" not in ids
assert "public.st_subdivide" not in ids
25 changes: 25 additions & 0 deletions tipg/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ def datetime_query(

return None

def collectionId_substring_query(
collectionId_substring: Annotated[Optional[str], Query(description="Filter based on collectionId substring regex.")] = None
) -> Optional[str]:
"""collectionId substring dependency."""
compiled_substring = None
if collectionId_substring:
try:
# Attempt to compile the substring pattern provided by the user
compiled_substring = re.compile(collectionId_substring)
except re.error as e:
raise HTTPException(
status_code=422,
detail=f"Invalid substring '{collectionId_substring}' provided for 'collectionId_substring': {e}"
)
return compiled_substring

def properties_query(
properties: Annotated[
Expand Down Expand Up @@ -450,6 +465,7 @@ def CollectionsParams(
description="Starts the response at an offset.",
),
] = None,
collectionId_substring: Annotated[Optional[str], Depends(collectionId_substring_query)] = None
) -> CollectionList:
"""Return Collections Catalog."""
limit = limit or 0
Expand Down Expand Up @@ -487,6 +503,15 @@ def CollectionsParams(
and t_intersects(datetime_filter, collection.dt_bounds)
]

# collectionId substring filter
if collectionId_substring is not None:
collections_list = [
collection
for collection in collections_list
# Use search() to find the substring anywhere in the collection ID
if collectionId_substring.search(collection.id)
]

matched = len(collections_list)

if limit:
Expand Down
Loading