Skip to content

Commit 955c34e

Browse files
authored
Support intersects for GET requests (#521)
* fix: support intersects for GET requests * chore: update changelog
1 parent 94f6e2c commit 955c34e

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* `self` link rel for `/collections/{c_id}/items` ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
2424
* Media type of the item collection endpoint ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
2525
* Manually exclude non-truthy optional values from sqlalchemy serialization of Collections ([#508](https://github.com/stac-utils/stac-fastapi/pull/508))
26+
* Support `intersects` in GET requests ([#521](https://github.com/stac-utils/stac-fastapi/pull/521))
2627
* Deleting items that had repeated ids in other collections ([#520](https://github.com/stac-utils/stac-fastapi/pull/520))
2728

2829
### Deprecated

stac_fastapi/pgstac/stac_fastapi/pgstac/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ async def get_search(
352352
sortby: Optional[str] = None,
353353
filter: Optional[str] = None,
354354
filter_lang: Optional[str] = None,
355+
intersects: Optional[str] = None,
355356
**kwargs,
356357
) -> ItemCollection:
357358
"""Cross catalog search (GET).
@@ -389,6 +390,9 @@ async def get_search(
389390
if datetime:
390391
base_args["datetime"] = datetime
391392

393+
if intersects:
394+
base_args["intersects"] = orjson.loads(unquote_plus(intersects))
395+
392396
if sortby:
393397
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
394398
sort_param = []

stac_fastapi/pgstac/tests/api/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ async def test_search_point_intersects(
329329
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
330330
assert resp.status_code == 200
331331

332+
new_coordinates = list()
333+
for coordinate in item["geometry"]["coordinates"][0]:
334+
new_coordinates.append([coordinate[0] * -1, coordinate[1] * -1])
335+
item["id"] = "test-item-other-hemispheres"
336+
item["geometry"]["coordinates"] = [new_coordinates]
337+
item["bbox"] = list(value * -1 for value in item["bbox"])
338+
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
339+
assert resp.status_code == 200
340+
332341
point = [150.04, -33.14]
333342
intersects = {"type": "Point", "coordinates": point}
334343

@@ -341,6 +350,12 @@ async def test_search_point_intersects(
341350
resp_json = resp.json()
342351
assert len(resp_json["features"]) == 1
343352

353+
params["intersects"] = orjson.dumps(params["intersects"]).decode("utf-8")
354+
resp = await app_client.get("/search", params=params)
355+
assert resp.status_code == 200
356+
resp_json = resp.json()
357+
assert len(resp_json["features"]) == 1
358+
344359

345360
async def test_search_line_string_intersects(
346361
load_test_data, app_client, load_test_collection

stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def get_search(
249249
token: Optional[str] = None,
250250
fields: Optional[List[str]] = None,
251251
sortby: Optional[str] = None,
252+
intersects: Optional[str] = None,
252253
**kwargs,
253254
) -> ItemCollection:
254255
"""GET search catalog."""
@@ -265,6 +266,9 @@ def get_search(
265266
if datetime:
266267
base_args["datetime"] = datetime
267268

269+
if intersects:
270+
base_args["intersects"] = json.loads(unquote_plus(intersects))
271+
268272
if sortby:
269273
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
270274
sort_param = []

stac_fastapi/sqlalchemy/tests/api/test_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ def test_search_point_intersects(load_test_data, app_client, postgres_transactio
276276
item["collection"], item, request=MockStarletteRequest
277277
)
278278

279+
new_coordinates = list()
280+
for coordinate in item["geometry"]["coordinates"][0]:
281+
new_coordinates.append([coordinate[0] * -1, coordinate[1] * -1])
282+
item["id"] = "test-item-other-hemispheres"
283+
item["geometry"]["coordinates"] = [new_coordinates]
284+
item["bbox"] = list(value * -1 for value in item["bbox"])
285+
postgres_transactions.create_item(
286+
item["collection"], item, request=MockStarletteRequest
287+
)
288+
279289
point = [150.04, -33.14]
280290
intersects = {"type": "Point", "coordinates": point}
281291

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

301+
params["intersects"] = orjson.dumps(params["intersects"]).decode("utf-8")
302+
resp = app_client.get("/search", params=params)
303+
assert resp.status_code == 200
304+
resp_json = resp.json()
305+
assert len(resp_json["features"]) == 1
306+
291307

292308
def test_datetime_non_interval(load_test_data, app_client, postgres_transactions):
293309
item = load_test_data("test_item.json")

stac_fastapi/types/stac_fastapi/types/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ def get_search(
433433
token: Optional[str] = None,
434434
fields: Optional[List[str]] = None,
435435
sortby: Optional[str] = None,
436+
intersects: Optional[str] = None,
436437
**kwargs,
437438
) -> stac_types.ItemCollection:
438439
"""Cross catalog search (GET).
@@ -627,6 +628,7 @@ async def get_search(
627628
token: Optional[str] = None,
628629
fields: Optional[List[str]] = None,
629630
sortby: Optional[str] = None,
631+
intersects: Optional[str] = None,
630632
**kwargs,
631633
) -> stac_types.ItemCollection:
632634
"""Cross catalog search (GET).

stac_fastapi/types/stac_fastapi/types/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class BaseSearchGetRequest(APIRequest):
7474
collections: Optional[str] = attr.ib(default=None, converter=str2list)
7575
ids: Optional[str] = attr.ib(default=None, converter=str2list)
7676
bbox: Optional[str] = attr.ib(default=None, converter=str2list)
77-
intersects: Optional[str] = attr.ib(default=None, converter=str2list)
77+
intersects: Optional[str] = attr.ib(default=None)
7878
datetime: Optional[str] = attr.ib(default=None)
7979
limit: Optional[int] = attr.ib(default=10)
8080

0 commit comments

Comments
 (0)