Skip to content

feat: filter collection based on name #1

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 11 commits into
base: main
Choose a base branch
from
Open
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

## [unreleased]

## [1.1.0] - 2025-05-06

* update geojson-pydantic requirement to `>=1.0,<3.0`

## [1.0.1] - 2025-03-17

* fix typo when using catalog_ttl
Expand Down Expand Up @@ -390,7 +394,8 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

- Initial release

[unreleased]: https://github.com/developmentseed/tipg/compare/1.0.1...HEAD
[unreleased]: https://github.com/developmentseed/tipg/compare/1.1.0...HEAD
[1.1.0]: https://github.com/developmentseed/tipg/compare/1.0.1...1.1.0
[1.0.1]: https://github.com/developmentseed/tipg/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/developmentseed/tipg/compare/0.10.0...1.0.0
[0.10.1]: https://github.com/developmentseed/tipg/compare/0.10.0...0.10.1
Expand Down
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies = [
"morecantile>=5.0,<7.0",
"pydantic>=2.4,<3.0",
"pydantic-settings~=2.0",
"geojson-pydantic>=1.0,<2.0",
"geojson-pydantic>=1.0,<3.0",
"pygeofilter>=0.2.0,<0.3.0",
"ciso8601~=2.3",
"starlette-cramjam>=0.4,<0.5",
Expand All @@ -44,8 +44,7 @@ test = [
"pytest-benchmark",
"httpx",
"pytest-postgresql",
"mapbox-vector-tile",
"protobuf>=3.0,<4.0",
"mapbox-vector-tile>=2.1",
"numpy",
]
dev = [
Expand Down Expand Up @@ -141,7 +140,7 @@ filterwarnings = [
]

[tool.bumpversion]
current_version = "1.0.1"
current_version = "1.1.0"

search = "{current_version}"
replace = "{new_version}"
Expand Down
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
2 changes: 1 addition & 1 deletion tipg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""tipg."""

__version__ = "1.0.1"
__version__ = "1.1.0"
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
26 changes: 15 additions & 11 deletions tipg/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@
)
from tipg.errors import MissingGeometryColumn, NoPrimaryKey, NotFound
from tipg.resources.enums import MediaType
from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse, orjsonDumps
from tipg.resources.response import (
GeoJSONResponse,
ORJSONResponse,
SchemaJSONResponse,
orjsonDumps,
)
from tipg.settings import FeaturesSettings, MVTSettings, TMSSettings

from fastapi import APIRouter, Depends, Path, Query
from fastapi.responses import ORJSONResponse

from starlette.datastructures import QueryParams
from starlette.requests import Request
Expand Down Expand Up @@ -596,17 +600,17 @@ def collections(
)
for collection in collection_list["collections"]
],
)
).model_dump(exclude_none=True, mode="json")

if output_type == MediaType.html:
return self._create_html_response(
request,
data.model_dump(exclude_none=True, mode="json"),
data,
template_name="collections",
title="Collections list",
)

return data
return ORJSONResponse(data)

def _collection_route(self):
@self.router.get(
Expand Down Expand Up @@ -689,17 +693,17 @@ def collection(
),
*self._additional_collection_tiles_links(request, collection),
],
)
).model_dump(exclude_none=True, mode="json")

if output_type == MediaType.html:
return self._create_html_response(
request,
data.model_dump(exclude_none=True, mode="json"),
data,
template_name="collection",
title=f"{collection.id} collection",
)

return data
return ORJSONResponse(data)

def _queryables_route(self):
@self.router.get(
Expand Down Expand Up @@ -735,17 +739,17 @@ def queryables(
title=collection.id,
link=self_url + qs,
properties=collection.queryables,
)
).model_dump(exclude_none=True, mode="json", by_alias=True)

if output_type == MediaType.html:
return self._create_html_response(
request,
data.model_dump(exclude_none=True, mode="json"),
data,
template_name="queryables",
title=f"{collection.id} queryables",
)

return data
return SchemaJSONResponse(data)

def _items_route(self): # noqa: C901
@self.router.get(
Expand Down