Skip to content

Commit 89f92e4

Browse files
authored
feat: add collection_from_id_and_items (#109)
1 parent bc9f185 commit 89f92e4

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

python/rustac/rustac.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ class DuckdbClient:
165165
A list of STAC Collections
166166
"""
167167

168+
def collection_from_id_and_items(id: str, items: list[Item]) -> Collection:
169+
"""Creates a collection from an id and some items.
170+
171+
The extents will be calculated from the items, and the items will be linked.
172+
173+
Args:
174+
id: The collection id
175+
items: A list of STAC items
176+
177+
Returns:
178+
A STAC collection
179+
"""
180+
168181
def migrate(value: dict[str, Any], version: Optional[str] = None) -> dict[str, Any]:
169182
"""
170183
Migrates a STAC dictionary to another version.

src/collection.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use pyo3::{
2+
Bound, PyResult, Python, pyfunction,
3+
types::{PyAny, PyAnyMethods, PyDict},
4+
};
5+
use stac::{Collection, Item};
6+
7+
#[pyfunction]
8+
pub fn collection_from_id_and_items<'py>(
9+
py: Python<'py>,
10+
id: String,
11+
items: Bound<'_, PyAny>,
12+
) -> PyResult<Bound<'py, PyDict>> {
13+
let items: Vec<Item> = pythonize::depythonize(&items)?;
14+
let collection = Collection::from_id_and_items(id, &items);
15+
let collection = pythonize::pythonize(py, &collection)?.extract()?;
16+
Ok(collection)
17+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod arrow;
44
mod cli;
5+
mod collection;
56
mod duckdb;
67
mod error;
78
mod migrate;
@@ -27,6 +28,10 @@ fn rustac(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
2728
m.add_function(wrap_pyfunction!(arrow::from_arrow, m)?)?;
2829
m.add_function(wrap_pyfunction!(arrow::to_arrow, m)?)?;
2930
m.add_function(wrap_pyfunction!(cli::main, m)?)?;
31+
m.add_function(wrap_pyfunction!(
32+
collection::collection_from_id_and_items,
33+
m
34+
)?)?;
3035
m.add_function(wrap_pyfunction!(migrate::migrate, m)?)?;
3136
m.add_function(wrap_pyfunction!(read::read, m)?)?;
3237
m.add_function(wrap_pyfunction!(search::search, m)?)?;

tests/test_collection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pathlib import Path
2+
3+
import rustac
4+
from pystac import Collection
5+
6+
7+
async def test_collection_from_id_and_items(data: Path) -> None:
8+
items = await rustac.read(str(data / "100-sentinel-2-items.parquet"))
9+
collection = Collection.from_dict(
10+
rustac.collection_from_id_and_items("a-collection", items["features"]) # type: ignore
11+
)
12+
collection.validate()

0 commit comments

Comments
 (0)