Skip to content

Commit 90108f8

Browse files
committed
add test item collection bbox
1 parent 9b3fa3f commit 90108f8

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,38 @@ async def item_collection(
101101
) -> ItemCollection:
102102
"""Read an item collection from the database."""
103103
request: Request = kwargs["request"]
104-
base_url = str(kwargs["request"].base_url)
104+
base_url = str(request.base_url)
105+
106+
collection = await self.get_collection(collection_id=collection_id, request=request)
107+
try:
108+
collection_id = collection["id"]
109+
except:
110+
raise HTTPException(status_code=404, detail="Collection not found")
111+
112+
search = self.database.make_search()
113+
search = self.database.apply_collections_filter(
114+
search=search, collection_ids=[collection_id]
115+
)
116+
117+
if datetime:
118+
datetime_search = self._return_date(datetime)
119+
search = self.database.apply_datetime_filter(
120+
search=search, datetime_search=datetime_search
121+
)
122+
123+
if bbox:
124+
bbox = [float(x) for x in bbox]
125+
if len(bbox) == 6:
126+
bbox = [bbox[0], bbox[1], bbox[3], bbox[4]]
127+
128+
search = self.database.apply_bbox_filter(search=search, bbox=bbox)
105129

106130
items, maybe_count, next_token = await self.database.execute_search(
107-
search=self.database.apply_collections_filter(
108-
self.database.make_search(), [collection_id]
109-
),
131+
search=search,
110132
limit=limit,
111-
token=token,
112133
sort=None,
134+
token=token, # type: ignore
113135
collection_ids=[collection_id],
114-
ignore_unavailable=False,
115136
)
116137

117138
items = [

stac_fastapi/elasticsearch/tests/resources/test_item.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ async def test_get_item_collection(app_client, ctx, txn_client):
191191
assert matched == item_count + 1
192192

193193

194+
async def test_item_collection_filter_bbox(app_client, ctx, txn_client):
195+
item = ctx.item
196+
collection = item["collection"]
197+
198+
bbox = "100,-50,170,-20"
199+
resp = await app_client.get(f"/collections/{collection}/items", params={"bbox": bbox})
200+
assert resp.status_code == 200
201+
resp_json = resp.json()
202+
assert len(resp_json["features"]) == 1
203+
204+
bbox = "1,2,3,4"
205+
resp = await app_client.get(f"/collections/{collection}/items", params={"bbox": bbox})
206+
assert resp.status_code == 200
207+
resp_json = resp.json()
208+
assert len(resp_json["features"]) == 0
209+
210+
211+
# def test_item_collection_filter_datetime(
212+
# load_test_data, app_client, postgres_transactions
213+
# ):
214+
# item = load_test_data("test_item.json")
215+
# collection = item["collection"]
216+
# postgres_transactions.create_item(
217+
# item["collection"], item, request=MockStarletteRequest
218+
# )
219+
220+
# datetime_range = "2020-01-01T00:00:00.00Z/.."
221+
# resp = app_client.get(
222+
# f"/collections/{collection}/items", params={"datetime": datetime_range}
223+
# )
224+
# assert resp.status_code == 200
225+
# resp_json = resp.json()
226+
# assert len(resp_json["features"]) == 1
227+
228+
# datetime_range = "2018-01-01T00:00:00.00Z/2019-01-01T00:00:00.00Z"
229+
# resp = app_client.get(
230+
# f"/collections/{collection}/items", params={"datetime": datetime_range}
231+
# )
232+
# assert resp.status_code == 200
233+
# resp_json = resp.json()
234+
# assert len(resp_json["features"]) == 0
235+
194236
@pytest.mark.skip(reason="Pagination extension not implemented")
195237
async def test_pagination(app_client, load_test_data):
196238
"""Test item collection pagination (paging extension)"""

0 commit comments

Comments
 (0)