|
| 1 | +use crate::{Error, Json, Result}; |
| 2 | +use geoarrow::table::Table; |
| 3 | +use pyo3::{prelude::*, IntoPyObjectExt}; |
| 4 | +use pyo3_arrow::PyTable; |
| 5 | +use serde_json::Value; |
| 6 | +use stac::{Item, ItemCollection}; |
| 7 | + |
| 8 | +#[pyfunction] |
| 9 | +pub fn from_arrow(py: Python<'_>, table: PyTable) -> PyResult<Bound<PyAny>> { |
| 10 | + let (record_batches, mut schema) = table.into_inner(); |
| 11 | + let record_batches = record_batches |
| 12 | + .into_iter() |
| 13 | + .map(|record_batch| { |
| 14 | + let record_batch = stac::geoarrow::with_native_geometry(record_batch, "geometry")?; |
| 15 | + Ok(record_batch) |
| 16 | + }) |
| 17 | + .collect::<Result<Vec<_>>>()?; |
| 18 | + if !record_batches.is_empty() { |
| 19 | + schema = record_batches[0].schema(); |
| 20 | + } |
| 21 | + let table = Table::try_new(record_batches, schema).map_err(Error::from)?; |
| 22 | + let item_collection = stac::geoarrow::from_table(table).map_err(Error::from)?; |
| 23 | + let item_collection = Json(item_collection).into_pyobject(py)?; |
| 24 | + Ok(item_collection) |
| 25 | +} |
| 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 | + // TODO we might want to just allow use to go WKB right when we got to table? |
| 40 | + let (record_batches, mut schema) = stac::geoarrow::to_table(item_collection) |
| 41 | + .map_err(Error::from)? |
| 42 | + .into_inner(); |
| 43 | + let record_batches = record_batches |
| 44 | + .into_iter() |
| 45 | + .map(|record_batch| { |
| 46 | + stac::geoarrow::with_wkb_geometry(record_batch, "geometry").map_err(Error::from) |
| 47 | + }) |
| 48 | + .collect::<Result<Vec<_>>>()?; |
| 49 | + if !record_batches.is_empty() { |
| 50 | + schema = record_batches[0].schema(); |
| 51 | + } |
| 52 | + let table = PyTable::try_new(record_batches, schema)?; |
| 53 | + let table = table.to_arro3(py)?; |
| 54 | + Ok(table.into_py_any(py)?) |
| 55 | +} |
0 commit comments