Skip to content

Commit a3c88c1

Browse files
jonhealy1lossyrobmoradology
authored
Test alternate geometries in pgstac and sqlalchemy (#264)
* add point, line string tests to sqlalchemy * add point, line string tests to pgstac * run pre-commit * change varianle name fromo poly to geom in sqlalchemy core * run pre-commit * lint Co-authored-by: Rob Emanuele <[email protected]> Co-authored-by: Nathan Zimmerman <[email protected]>
1 parent 27d2d2a commit a3c88c1

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

stac_fastapi/pgstac/tests/api/test_api.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,47 @@ async def test_app_search_response(load_test_data, app_client, load_test_collect
229229
# stac_version and stac_extensions were removed in v1.0.0-beta.3
230230
assert resp_json.get("stac_version") is None
231231
assert resp_json.get("stac_extensions") is None
232+
233+
234+
@pytest.mark.asyncio
235+
async def test_search_point_intersects(
236+
load_test_data, app_client, load_test_collection
237+
):
238+
coll = load_test_collection
239+
item = load_test_data("test_item.json")
240+
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
241+
assert resp.status_code == 200
242+
243+
point = [150.04, -33.14]
244+
intersects = {"type": "Point", "coordinates": point}
245+
246+
params = {
247+
"intersects": intersects,
248+
"collections": [item["collection"]],
249+
}
250+
resp = await app_client.post("/search", json=params)
251+
assert resp.status_code == 200
252+
resp_json = resp.json()
253+
assert len(resp_json["features"]) == 1
254+
255+
256+
@pytest.mark.asyncio
257+
async def test_search_line_string_intersects(
258+
load_test_data, app_client, load_test_collection
259+
):
260+
coll = load_test_collection
261+
item = load_test_data("test_item.json")
262+
resp = await app_client.post(f"/collections/{coll.id}/items", json=item)
263+
assert resp.status_code == 200
264+
265+
line = [[150.04, -33.14], [150.22, -33.89]]
266+
intersects = {"type": "LineString", "coordinates": line}
267+
268+
params = {
269+
"intersects": intersects,
270+
"collections": [item["collection"]],
271+
}
272+
resp = await app_client.post("/search", json=params)
273+
assert resp.status_code == 200
274+
resp_json = resp.json()
275+
assert len(resp_json["features"]) == 1

stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ def post_search(
312312

313313
else:
314314
# Spatial query
315-
poly = None
315+
geom = None
316316
if search_request.intersects is not None:
317-
poly = shape(search_request.intersects)
317+
geom = shape(search_request.intersects)
318318
elif search_request.bbox:
319319
if len(search_request.bbox) == 4:
320-
poly = ShapelyPolygon.from_bounds(*search_request.bbox)
320+
geom = ShapelyPolygon.from_bounds(*search_request.bbox)
321321
elif len(search_request.bbox) == 6:
322322
"""Shapely doesn't support 3d bounding boxes we'll just use the 2d portion"""
323323
bbox_2d = [
@@ -326,10 +326,10 @@ def post_search(
326326
search_request.bbox[3],
327327
search_request.bbox[4],
328328
]
329-
poly = ShapelyPolygon.from_bounds(*bbox_2d)
329+
geom = ShapelyPolygon.from_bounds(*bbox_2d)
330330

331-
if poly:
332-
filter_geom = ga.shape.from_shape(poly, srid=4326)
331+
if geom:
332+
filter_geom = ga.shape.from_shape(geom, srid=4326)
333333
query = query.filter(
334334
ga.func.ST_Intersects(self.item_table.geometry, filter_geom)
335335
)

stac_fastapi/sqlalchemy/tests/api/test_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ def test_search_invalid_date(load_test_data, app_client, postgres_transactions):
174174
assert resp.status_code == 400
175175

176176

177+
def test_search_point_intersects(load_test_data, app_client, postgres_transactions):
178+
item = load_test_data("test_item.json")
179+
postgres_transactions.create_item(item, request=MockStarletteRequest)
180+
181+
point = [150.04, -33.14]
182+
intersects = {"type": "Point", "coordinates": point}
183+
184+
params = {
185+
"intersects": intersects,
186+
"collections": [item["collection"]],
187+
}
188+
resp = app_client.post("/search", json=params)
189+
assert resp.status_code == 200
190+
resp_json = resp.json()
191+
assert len(resp_json["features"]) == 1
192+
193+
177194
def test_datetime_non_interval(load_test_data, app_client, postgres_transactions):
178195
item = load_test_data("test_item.json")
179196
postgres_transactions.create_item(item, request=MockStarletteRequest)
@@ -207,6 +224,24 @@ def test_bbox_3d(load_test_data, app_client, postgres_transactions):
207224
}
208225
resp = app_client.post("/search", json=params)
209226
assert resp.status_code == 200
227+
resp_json = resp.json()
228+
assert len(resp_json["features"]) == 1
229+
230+
231+
def test_search_line_string_intersects(
232+
load_test_data, app_client, postgres_transactions
233+
):
234+
item = load_test_data("test_item.json")
235+
postgres_transactions.create_item(item, request=MockStarletteRequest)
210236

237+
line = [[150.04, -33.14], [150.22, -33.89]]
238+
intersects = {"type": "LineString", "coordinates": line}
239+
240+
params = {
241+
"intersects": intersects,
242+
"collections": [item["collection"]],
243+
}
244+
resp = app_client.post("/search", json=params)
245+
assert resp.status_code == 200
211246
resp_json = resp.json()
212247
assert len(resp_json["features"]) == 1

0 commit comments

Comments
 (0)