|
1 | 1 | import stacrs |
| 2 | +from pystac import Collection as PystacCollection |
| 3 | +from pystac import Extent, Item |
2 | 4 |
|
3 | 5 | from tistac.backends import Backend |
4 | | -from tistac.models.item_collection import ItemCollection |
5 | | -from tistac.models.search import Search |
| 6 | +from tistac.models import Collection, ItemCollection, Search |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class StacGeoparquetBackend(Backend): |
9 | 10 | """A stac-geoparquet backend, using DuckDB under the hood.""" |
10 | 11 |
|
11 | 12 | def __init__(self, href: str): |
| 13 | + # TODO support multiple collections |
| 14 | + # TODO store collection information in the **stac-geoparquet** |
| 15 | + items_as_dicts = stacrs.search(href) |
| 16 | + collection_ids = set() |
| 17 | + items = list() |
| 18 | + for item_as_dict in items_as_dicts: |
| 19 | + item = Item.from_dict(item_as_dict) |
| 20 | + if item.collection_id: |
| 21 | + collection_ids.add(item.collection_id) |
| 22 | + items.append(item) |
| 23 | + if len(collection_ids) != 1: |
| 24 | + raise Exception( |
| 25 | + "Only one collection id is supported by the " |
| 26 | + f"stac-geoparquet backend: {collection_ids}" |
| 27 | + ) |
| 28 | + extent = Extent.from_items(items) |
| 29 | + collection = PystacCollection( |
| 30 | + id=collection_ids.pop(), |
| 31 | + description="An auto-generated stac-geoparquet Collection", |
| 32 | + extent=extent, |
| 33 | + ) |
| 34 | + d = collection.to_dict(include_self_link=False, transform_hrefs=False) |
| 35 | + d["stac_version"] = "1.1.0" |
| 36 | + self.collections = [Collection.model_validate(d)] |
12 | 37 | self.href = href |
13 | 38 |
|
| 39 | + async def get_collections(self) -> list[Collection]: |
| 40 | + return self.collections |
| 41 | + |
| 42 | + async def get_collection(self, collection_id: str) -> Collection | None: |
| 43 | + return next((c for c in self.collections if c.id == collection_id), None) |
| 44 | + |
14 | 45 | async def search(self, search: Search) -> ItemCollection: |
15 | 46 | items = stacrs.search(self.href, limit=search.limit) |
16 | 47 | return ItemCollection(features=items) |
0 commit comments