Skip to content

Commit f2a2cc2

Browse files
add stac search helper methods (#70)
* add property to return search geometry * add test, fix nesting * rename to spatial tiler
1 parent b9f420e commit f2a2cc2

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

stac_pydantic/api/search.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
MultiPolygon,
99
Point,
1010
Polygon,
11+
_GeometryBase,
1112
)
1213
from pydantic import BaseModel, Field, validator
1314

@@ -87,3 +88,25 @@ def validate_datetime(cls, v):
8788
"Invalid datetime range, must match format (begin_date, end_date)"
8889
)
8990
return v
91+
92+
@property
93+
def spatial_filter(self) -> Optional[_GeometryBase]:
94+
"""Return a geojson-pydantic object representing the spatial filter for the search request.
95+
96+
Check for both because the ``bbox`` and ``intersects`` parameters are mutually exclusive.
97+
"""
98+
if self.bbox:
99+
return Polygon(
100+
coordinates=[
101+
[
102+
[self.bbox[0], self.bbox[3]],
103+
[self.bbox[2], self.bbox[3]],
104+
[self.bbox[2], self.bbox[1]],
105+
[self.bbox[0], self.bbox[1]],
106+
[self.bbox[0], self.bbox[3]],
107+
]
108+
]
109+
)
110+
if self.intersects:
111+
return self.intersects
112+
return

tests/test_api_extensions.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from datetime import datetime
22

3-
import pytest
4-
from shapely.geometry import Polygon
3+
from shapely.geometry import Polygon, shape
54

65
from stac_pydantic import Item
76
from stac_pydantic.api.extensions.fields import FieldsExtension
7+
from stac_pydantic.api.search import Search
88

99

1010
def test_fields_filter():
@@ -29,3 +29,10 @@ def test_fields_filter():
2929

3030
assert not props.get("bar")
3131
assert not d
32+
33+
34+
def test_search_geometry_bbox():
35+
search = Search(collections=["foo", "bar"], bbox=[0, 0, 1, 1])
36+
geom1 = shape(search.spatial_filter)
37+
geom2 = Polygon.from_bounds(*search.bbox)
38+
assert (geom1.intersection(geom2).area / geom1.union(geom2).area) == 1.0

0 commit comments

Comments
 (0)