diff --git a/python/rustac/rustac.pyi b/python/rustac/rustac.pyi index b194f72..258766b 100644 --- a/python/rustac/rustac.pyi +++ b/python/rustac/rustac.pyi @@ -165,6 +165,19 @@ class DuckdbClient: A list of STAC Collections """ +def collection_from_id_and_items(id: str, items: list[Item]) -> Collection: + """Creates a collection from an id and some items. + + The extents will be calculated from the items, and the items will be linked. + + Args: + id: The collection id + items: A list of STAC items + + Returns: + A STAC collection + """ + def migrate(value: dict[str, Any], version: Optional[str] = None) -> dict[str, Any]: """ Migrates a STAC dictionary to another version. diff --git a/src/collection.rs b/src/collection.rs new file mode 100644 index 0000000..b75d5b0 --- /dev/null +++ b/src/collection.rs @@ -0,0 +1,17 @@ +use pyo3::{ + Bound, PyResult, Python, pyfunction, + types::{PyAny, PyAnyMethods, PyDict}, +}; +use stac::{Collection, Item}; + +#[pyfunction] +pub fn collection_from_id_and_items<'py>( + py: Python<'py>, + id: String, + items: Bound<'_, PyAny>, +) -> PyResult> { + let items: Vec = pythonize::depythonize(&items)?; + let collection = Collection::from_id_and_items(id, &items); + let collection = pythonize::pythonize(py, &collection)?.extract()?; + Ok(collection) +} diff --git a/src/lib.rs b/src/lib.rs index 5b970d8..f7d36ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ mod arrow; mod cli; +mod collection; mod duckdb; mod error; mod migrate; @@ -27,6 +28,10 @@ fn rustac(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(arrow::from_arrow, m)?)?; m.add_function(wrap_pyfunction!(arrow::to_arrow, m)?)?; m.add_function(wrap_pyfunction!(cli::main, m)?)?; + m.add_function(wrap_pyfunction!( + collection::collection_from_id_and_items, + m + )?)?; m.add_function(wrap_pyfunction!(migrate::migrate, m)?)?; m.add_function(wrap_pyfunction!(read::read, m)?)?; m.add_function(wrap_pyfunction!(search::search, m)?)?; diff --git a/tests/test_collection.py b/tests/test_collection.py new file mode 100644 index 0000000..7a6264a --- /dev/null +++ b/tests/test_collection.py @@ -0,0 +1,12 @@ +from pathlib import Path + +import rustac +from pystac import Collection + + +async def test_collection_from_id_and_items(data: Path) -> None: + items = await rustac.read(str(data / "100-sentinel-2-items.parquet")) + collection = Collection.from_dict( + rustac.collection_from_id_and_items("a-collection", items["features"]) # type: ignore + ) + collection.validate()