-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Convert WKT and EWKB to geoarrow (#369)
- Loading branch information
1 parent
4f5f617
commit 62fef84
Showing
25 changed files
with
346 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::sync::Arc; | ||
|
||
use geoarrow::array::{from_arrow_array, AsGeometryArray, CoordType}; | ||
use geoarrow::datatypes::GeoDataType; | ||
use geoarrow::io::geozero::FromEWKB; | ||
use geoarrow::GeometryArrayTrait; | ||
use pyo3::exceptions::PyTypeError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyType; | ||
|
||
use crate::array::*; | ||
use crate::ffi::from_python::import_arrow_c_array; | ||
use crate::ffi::to_python::geometry_array_to_pyobject; | ||
|
||
/// Convert an Arrow BinaryArray from EWKB to its GeoArrow-native counterpart. | ||
#[pyfunction] | ||
pub fn from_ewkb(ob: &PyAny) -> PyResult<PyObject> { | ||
let (array, field) = import_arrow_c_array(ob)?; | ||
// TODO: need to improve crate's error handling | ||
let array = from_arrow_array(&array, &field).unwrap(); | ||
let ref_array = array.as_ref(); | ||
let geo_array: Arc<dyn GeometryArrayTrait> = match array.data_type() { | ||
GeoDataType::WKB => { | ||
FromEWKB::from_ewkb(ref_array.as_wkb(), CoordType::Interleaved).unwrap() | ||
} | ||
GeoDataType::LargeWKB => { | ||
FromEWKB::from_ewkb(ref_array.as_large_wkb(), CoordType::Interleaved).unwrap() | ||
} | ||
other => { | ||
return Err(PyTypeError::new_err(format!( | ||
"Unexpected array type {:?}", | ||
other | ||
))) | ||
} | ||
}; | ||
Python::with_gil(|py| geometry_array_to_pyobject(py, geo_array)) | ||
} | ||
|
||
macro_rules! impl_from_ewkb { | ||
($py_array:ty, $geoarrow_array:ty) => { | ||
#[pymethods] | ||
impl $py_array { | ||
/// Parse from EWKB | ||
#[classmethod] | ||
pub fn from_ewkb(_cls: &PyType, ob: &PyAny) -> PyResult<$py_array> { | ||
let (array, field) = import_arrow_c_array(ob)?; | ||
let array = from_arrow_array(&array, &field).unwrap(); | ||
let ref_array = array.as_ref(); | ||
match array.data_type() { | ||
GeoDataType::WKB => Ok(<$geoarrow_array>::from_ewkb( | ||
ref_array.as_wkb(), | ||
CoordType::Interleaved, | ||
) | ||
.unwrap() | ||
.into()), | ||
GeoDataType::LargeWKB => Ok(<$geoarrow_array>::from_ewkb( | ||
ref_array.as_large_wkb(), | ||
CoordType::Interleaved, | ||
) | ||
.unwrap() | ||
.into()), | ||
other => Err(PyTypeError::new_err(format!( | ||
"Unexpected array type {:?}", | ||
other | ||
))), | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from_ewkb!(MixedGeometryArray, geoarrow::array::MixedGeometryArray<i32>); | ||
impl_from_ewkb!( | ||
GeometryCollectionArray, | ||
geoarrow::array::GeometryCollectionArray<i32> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod csv; | ||
pub mod ewkb; | ||
pub mod flatgeobuf; | ||
pub mod geojson; | ||
pub mod wkb; | ||
pub mod wkt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow::datatypes::DataType; | ||
use arrow_array::cast::AsArray; | ||
use geoarrow::array::CoordType; | ||
use geoarrow::io::geozero::FromWKT; | ||
use geoarrow::GeometryArrayTrait; | ||
use pyo3::exceptions::PyTypeError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyType; | ||
|
||
use crate::array::*; | ||
use crate::ffi::from_python::import_arrow_c_array; | ||
use crate::ffi::to_python::geometry_array_to_pyobject; | ||
|
||
/// Convert an Arrow StringArray from WKT to its GeoArrow-native counterpart. | ||
#[pyfunction] | ||
pub fn from_wkt(ob: &PyAny) -> PyResult<PyObject> { | ||
let (array, _field) = import_arrow_c_array(ob)?; | ||
let geo_array: Arc<dyn GeometryArrayTrait> = match array.data_type() { | ||
DataType::Utf8 => { | ||
FromWKT::from_wkt(array.as_string::<i32>(), CoordType::Interleaved).unwrap() | ||
} | ||
DataType::LargeUtf8 => { | ||
FromWKT::from_wkt(array.as_string::<i64>(), CoordType::Interleaved).unwrap() | ||
} | ||
other => { | ||
return Err(PyTypeError::new_err(format!( | ||
"Unexpected array type {:?}", | ||
other | ||
))) | ||
} | ||
}; | ||
Python::with_gil(|py| geometry_array_to_pyobject(py, geo_array)) | ||
} | ||
|
||
macro_rules! impl_from_wkt { | ||
($py_array:ty, $geoarrow_array:ty) => { | ||
#[pymethods] | ||
impl $py_array { | ||
/// Parse from WKT | ||
#[classmethod] | ||
pub fn from_wkt(_cls: &PyType, ob: &PyAny) -> PyResult<$py_array> { | ||
let (array, _field) = import_arrow_c_array(ob)?; | ||
match array.data_type() { | ||
DataType::Utf8 => Ok(<$geoarrow_array>::from_wkt( | ||
array.as_string::<i32>(), | ||
CoordType::Interleaved, | ||
) | ||
.unwrap() | ||
.into()), | ||
DataType::LargeUtf8 => Ok(<$geoarrow_array>::from_wkt( | ||
array.as_string::<i64>(), | ||
CoordType::Interleaved, | ||
) | ||
.unwrap() | ||
.into()), | ||
other => Err(PyTypeError::new_err(format!( | ||
"Unexpected array type {:?}", | ||
other | ||
))), | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from_wkt!(MixedGeometryArray, geoarrow::array::MixedGeometryArray<i32>); | ||
impl_from_wkt!( | ||
GeometryCollectionArray, | ||
geoarrow::array::GeometryCollectionArray<i32> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.