Skip to content
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

Support intersects for GET requests #521

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* `self` link rel for `/collections/{c_id}/items` ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
* Media type of the item collection endpoint ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
* Manually exclude non-truthy optional values from sqlalchemy serialization of Collections ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
* Support `intersects` in GET requests ([#521](https://github.com/stac-utils/stac-fastapi/pull/521))

## [2.4.3] - 2022-11-25

Expand Down
4 changes: 4 additions & 0 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async def get_search(
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_lang: Optional[str] = None,
intersects: Optional[str] = None,
**kwargs,
) -> ItemCollection:
"""Cross catalog search (GET).
Expand Down Expand Up @@ -389,6 +390,9 @@ async def get_search(
if datetime:
base_args["datetime"] = datetime

if intersects:
base_args["intersects"] = orjson.loads(unquote_plus(intersects))

if sortby:
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
sort_param = []
Expand Down
15 changes: 15 additions & 0 deletions stac_fastapi/pgstac/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ async def test_search_point_intersects(
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
assert resp.status_code == 200

new_coordinates = list()
for coordinate in item["geometry"]["coordinates"][0]:
new_coordinates.append([coordinate[0] * -1, coordinate[1] * -1])
item["id"] = "test-item-other-hemispheres"
item["geometry"]["coordinates"] = [new_coordinates]
item["bbox"] = list(value * -1 for value in item["bbox"])
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
assert resp.status_code == 200

point = [150.04, -33.14]
intersects = {"type": "Point", "coordinates": point}

Expand All @@ -322,6 +331,12 @@ async def test_search_point_intersects(
resp_json = resp.json()
assert len(resp_json["features"]) == 1

params["intersects"] = orjson.dumps(params["intersects"]).decode("utf-8")
resp = await app_client.get("/search", params=params)
assert resp.status_code == 200
resp_json = resp.json()
assert len(resp_json["features"]) == 1


async def test_search_line_string_intersects(
load_test_data, app_client, load_test_collection
Expand Down
4 changes: 4 additions & 0 deletions stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def get_search(
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
intersects: Optional[str] = None,
**kwargs,
) -> ItemCollection:
"""GET search catalog."""
Expand All @@ -265,6 +266,9 @@ def get_search(
if datetime:
base_args["datetime"] = datetime

if intersects:
base_args["intersects"] = json.loads(unquote_plus(intersects))

if sortby:
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
sort_param = []
Expand Down
16 changes: 16 additions & 0 deletions stac_fastapi/sqlalchemy/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ def test_search_point_intersects(load_test_data, app_client, postgres_transactio
item["collection"], item, request=MockStarletteRequest
)

new_coordinates = list()
for coordinate in item["geometry"]["coordinates"][0]:
new_coordinates.append([coordinate[0] * -1, coordinate[1] * -1])
item["id"] = "test-item-other-hemispheres"
item["geometry"]["coordinates"] = [new_coordinates]
item["bbox"] = list(value * -1 for value in item["bbox"])
postgres_transactions.create_item(
item["collection"], item, request=MockStarletteRequest
)

point = [150.04, -33.14]
intersects = {"type": "Point", "coordinates": point}

Expand All @@ -288,6 +298,12 @@ def test_search_point_intersects(load_test_data, app_client, postgres_transactio
resp_json = resp.json()
assert len(resp_json["features"]) == 1

params["intersects"] = orjson.dumps(params["intersects"]).decode("utf-8")
resp = app_client.get("/search", params=params)
assert resp.status_code == 200
resp_json = resp.json()
assert len(resp_json["features"]) == 1


def test_datetime_non_interval(load_test_data, app_client, postgres_transactions):
item = load_test_data("test_item.json")
Expand Down
2 changes: 2 additions & 0 deletions stac_fastapi/types/stac_fastapi/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ def get_search(
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
intersects: Optional[str] = None,
**kwargs,
) -> stac_types.ItemCollection:
"""Cross catalog search (GET).
Expand Down Expand Up @@ -627,6 +628,7 @@ async def get_search(
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
intersects: Optional[str] = None,
**kwargs,
) -> stac_types.ItemCollection:
"""Cross catalog search (GET).
Expand Down
2 changes: 1 addition & 1 deletion stac_fastapi/types/stac_fastapi/types/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BaseSearchGetRequest(APIRequest):
collections: Optional[str] = attr.ib(default=None, converter=str2list)
ids: Optional[str] = attr.ib(default=None, converter=str2list)
bbox: Optional[str] = attr.ib(default=None, converter=str2list)
intersects: Optional[str] = attr.ib(default=None, converter=str2list)
intersects: Optional[str] = attr.ib(default=None)
datetime: Optional[str] = attr.ib(default=None)
limit: Optional[int] = attr.ib(default=10)

Expand Down