-
Notifications
You must be signed in to change notification settings - Fork 113
add POST - /collections collection-search #739
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
"""Collection-Search extension module.""" | ||
|
||
from .collection_search import CollectionSearchExtension, ConformanceClasses | ||
from .collection_search import ( | ||
CollectionSearchExtension, | ||
CollectionSearchPostExtension, | ||
ConformanceClasses, | ||
) | ||
|
||
__all__ = ["CollectionSearchExtension", "ConformanceClasses"] | ||
__all__ = [ | ||
"CollectionSearchExtension", | ||
"CollectionSearchPostExtension", | ||
"ConformanceClasses", | ||
] |
49 changes: 49 additions & 0 deletions
49
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""collection-search extensions clients.""" | ||
|
||
import abc | ||
|
||
import attr | ||
|
||
from stac_fastapi.types import stac | ||
|
||
from .request import BaseCollectionSearchPostRequest | ||
|
||
|
||
@attr.s | ||
class AsyncBaseCollectionSearchClient(abc.ABC): | ||
"""Defines a pattern for implementing the STAC collection-search POST extension.""" | ||
|
||
@abc.abstractmethod | ||
async def post_all_collections( | ||
self, | ||
search_request: BaseCollectionSearchPostRequest, | ||
**kwargs, | ||
) -> stac.ItemCollection: | ||
"""Get all available collections. | ||
|
||
Called with `POST /collections`. | ||
|
||
Returns: | ||
A list of collections. | ||
|
||
""" | ||
... | ||
|
||
|
||
@attr.s | ||
class BaseCollectionSearchClient(abc.ABC): | ||
"""Defines a pattern for implementing the STAC collection-search POST extension.""" | ||
|
||
@abc.abstractmethod | ||
def post_all_collections( | ||
self, search_request: BaseCollectionSearchPostRequest, **kwargs | ||
) -> stac.ItemCollection: | ||
"""Get all available collections. | ||
|
||
Called with `POST /collections`. | ||
|
||
Returns: | ||
A list of collections. | ||
|
||
""" | ||
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 116 additions & 4 deletions
120
stac_fastapi/extensions/stac_fastapi/extensions/core/collection_search/request.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,139 @@ | ||
"""Request models for the Collection-Search extension.""" | ||
|
||
from typing import Optional | ||
from datetime import datetime as dt | ||
from typing import List, Optional, Tuple, cast | ||
|
||
import attr | ||
from fastapi import Query | ||
from pydantic import BaseModel, Field, field_validator | ||
from stac_pydantic.api.search import SearchDatetime | ||
from stac_pydantic.shared import BBox | ||
from typing_extensions import Annotated | ||
|
||
from stac_fastapi.types.rfc3339 import DateTimeType | ||
from stac_fastapi.types.search import APIRequest, _bbox_converter, _datetime_converter | ||
from stac_fastapi.types.search import ( | ||
APIRequest, | ||
Limit, | ||
_bbox_converter, | ||
_datetime_converter, | ||
) | ||
|
||
|
||
@attr.s | ||
class CollectionSearchExtensionGetRequest(APIRequest): | ||
class BaseCollectionSearchGetRequest(APIRequest): | ||
"""Basics additional Collection-Search parameters for the GET request.""" | ||
|
||
bbox: Optional[BBox] = attr.ib(default=None, converter=_bbox_converter) | ||
datetime: Optional[DateTimeType] = attr.ib( | ||
default=None, converter=_datetime_converter | ||
) | ||
limit: Annotated[ | ||
Optional[int], | ||
Optional[Limit], | ||
Query( | ||
description="Limits the number of results that are included in each page of the response." # noqa: E501 | ||
), | ||
] = attr.ib(default=10) | ||
|
||
|
||
class BaseCollectionSearchPostRequest(BaseModel): | ||
"""Collection-Search POST model.""" | ||
|
||
bbox: Optional[BBox] = None | ||
datetime: Optional[str] = None | ||
limit: Optional[Limit] = Field( | ||
10, | ||
description="Limits the number of results that are included in each page of the response (capped to 10_000).", # noqa: E501 | ||
) | ||
|
||
# Private properties to store the parsed datetime values. | ||
# Not part of the model schema. | ||
_start_date: Optional[dt] = None | ||
_end_date: Optional[dt] = None | ||
|
||
# Properties to return the private values | ||
@property | ||
def start_date(self) -> Optional[dt]: | ||
"""start date.""" | ||
return self._start_date | ||
|
||
@property | ||
def end_date(self) -> Optional[dt]: | ||
"""end date.""" | ||
return self._end_date | ||
|
||
@field_validator("bbox") | ||
@classmethod | ||
def validate_bbox(cls, v: BBox) -> BBox: | ||
"""validate bbox.""" | ||
if v: | ||
# Validate order | ||
if len(v) == 4: | ||
xmin, ymin, xmax, ymax = cast(Tuple[int, int, int, int], v) | ||
else: | ||
xmin, ymin, min_elev, xmax, ymax, max_elev = cast( | ||
Tuple[int, int, int, int, int, int], v | ||
) | ||
if max_elev < min_elev: | ||
raise ValueError( | ||
"Maximum elevation must greater than minimum elevation" | ||
) | ||
|
||
if xmax < xmin: | ||
raise ValueError( | ||
"Maximum longitude must be greater than minimum longitude" | ||
) | ||
|
||
if ymax < ymin: | ||
raise ValueError( | ||
"Maximum longitude must be greater than minimum longitude" | ||
) | ||
|
||
# Validate against WGS84 | ||
if xmin < -180 or ymin < -90 or xmax > 180 or ymax > 90: | ||
raise ValueError("Bounding box must be within (-180, -90, 180, 90)") | ||
|
||
return v | ||
|
||
@field_validator("datetime") | ||
@classmethod | ||
def validate_datetime(cls, value: str) -> str: | ||
"""validate datetime.""" | ||
# Split on "/" and replace no value or ".." with None | ||
values = [v if v and v != ".." else None for v in value.split("/")] | ||
|
||
# If there are more than 2 dates, it's invalid | ||
if len(values) > 2: | ||
raise ValueError( | ||
"""Invalid datetime range. Too many values. | ||
Must match format: {begin_date}/{end_date}""" | ||
) | ||
|
||
# If there is only one date, duplicate to use for both start and end dates | ||
if len(values) == 1: | ||
values = [values[0], values[0]] | ||
|
||
# Cast because pylance gets confused by the type adapter and annotated type | ||
dates = cast( | ||
List[Optional[dt]], | ||
[ | ||
# Use the type adapter to validate the datetime strings, | ||
# strict is necessary due to pydantic issues #8736 and #8762 | ||
SearchDatetime.validate_strings(v, strict=True) if v else None | ||
for v in values | ||
], | ||
) | ||
|
||
# If there is a start and end date, | ||
# check that the start date is before the end date | ||
if dates[0] and dates[1] and dates[0] > dates[1]: | ||
raise ValueError( | ||
"Invalid datetime range. Begin date after end date. " | ||
"Must match format: {begin_date}/{end_date}" | ||
) | ||
|
||
# Store the parsed dates | ||
cls._start_date = dates[0] | ||
cls._end_date = dates[1] | ||
|
||
# Return the original string value | ||
return value |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.