Skip to content

Commit faaa18b

Browse files
committed
wip: need to go back to wkb?
1 parent b8a1394 commit faaa18b

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/arrow.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::{Error, Json, Result};
22
use geoarrow::table::Table;
3-
use pyo3::prelude::*;
3+
use pyo3::{prelude::*, IntoPyObjectExt};
44
use pyo3_arrow::PyTable;
5+
use serde_json::Value;
6+
use stac::{Item, ItemCollection};
57

68
#[pyfunction]
79
pub fn from_arrow(py: Python<'_>, table: PyTable) -> PyResult<Bound<PyAny>> {
@@ -21,3 +23,23 @@ pub fn from_arrow(py: Python<'_>, table: PyTable) -> PyResult<Bound<PyAny>> {
2123
let item_collection = Json(item_collection).into_pyobject(py)?;
2224
Ok(item_collection)
2325
}
26+
27+
#[pyfunction]
28+
pub fn to_arrow(py: Python<'_>, items: Bound<PyAny>) -> PyResult<PyObject> {
29+
let value: Value = pythonize::depythonize(&items)?;
30+
let item_collection = if let Value::Array(array) = value {
31+
let items = array
32+
.into_iter()
33+
.map(|value| serde_json::from_value::<Item>(value).map_err(Error::from))
34+
.collect::<Result<Vec<_>>>()?;
35+
ItemCollection::from(items)
36+
} else {
37+
serde_json::from_value(value).map_err(Error::from)?
38+
};
39+
let (record_batches, schema) = stac::geoarrow::to_table(item_collection)
40+
.map_err(Error::from)?
41+
.into_inner();
42+
let table = PyTable::try_new(record_batches, schema)?;
43+
let table = table.to_arro3(py)?;
44+
Ok(table.into_py_any(py)?)
45+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn stacrs(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
2222
m.add_class::<duckdb::DuckdbClient>()?;
2323

2424
m.add_function(wrap_pyfunction!(arrow::from_arrow, m)?)?;
25+
m.add_function(wrap_pyfunction!(arrow::to_arrow, m)?)?;
2526
m.add_function(wrap_pyfunction!(cli::main, m)?)?;
2627
m.add_function(wrap_pyfunction!(migrate::migrate, m)?)?;
2728
m.add_function(wrap_pyfunction!(migrate::migrate_href, m)?)?;

stacrs.pyi

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async def from_arrow(
216216
table: arro3.core.Table,
217217
) -> dict[str, Any]:
218218
"""
219-
Converts an [arrow3.core.table][] to a STAC item collection.
219+
Converts an [arro3.core.table][] to a STAC item collection.
220220
221221
Requires **stacrs** to be installed with the `arrow` extra.
222222
@@ -227,6 +227,21 @@ async def from_arrow(
227227
dict[str, Any]: The STAC item collection
228228
"""
229229

230+
async def to_arrow(
231+
items: list[dict[str, Any]] | dict[str, Any],
232+
) -> arro3.core.Table:
233+
"""
234+
Converts items to an [arro3.core.table][].
235+
236+
Requires **stacrs** to be installed with the `arrow` extra.
237+
238+
Args:
239+
items: Either an iterable of items or a item collection
240+
241+
Returns:
242+
arro3.core.Table: The table
243+
"""
244+
230245
async def search(
231246
href: str,
232247
*,

tests/test_arrow.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Any
2+
3+
import pytest
4+
import stacrs
5+
from geopandas import GeoDataFrame
6+
7+
pytest.importorskip("arro3.core")
8+
9+
10+
def test_to_arrow(item: dict[str, Any]) -> None:
11+
table = stacrs.to_arrow([item])
12+
data_frame = GeoDataFrame.from_arrow(table)
13+
assert len(data_frame) == 1

0 commit comments

Comments
 (0)