Skip to content

Commit

Permalink
WIP: separate wkb array from native trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Sep 18, 2024
1 parent 9d620bc commit 7b56226
Show file tree
Hide file tree
Showing 136 changed files with 1,418 additions and 1,374 deletions.
2 changes: 1 addition & 1 deletion benches/nybb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use arrow_ipc::reader::FileReader;
use criterion::{criterion_group, criterion_main, Criterion};
use geoarrow::algorithm::geo::EuclideanDistance;
use geoarrow::array::{MultiPolygonArray, PointArray};
use geoarrow::trait_::NativeArrayAccessor;
use geoarrow::trait_::ArrayAccessor;

fn load_nybb() -> MultiPolygonArray<i32, 2> {
let file = File::open("fixtures/nybb.arrow").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion benches/wkb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::File;
use arrow::compute::concat;
use criterion::{criterion_group, criterion_main, Criterion};
use geoarrow::array::{MultiPolygonArray, WKBArray};
use geoarrow::trait_::NativeArrayAccessor;
use geoarrow::trait_::ArrayAccessor;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use parquet::arrow::ProjectionMask;

Expand Down
6 changes: 3 additions & 3 deletions python/geoarrow-core/src/interop/ewkb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use geoarrow::array::{from_arrow_array, AsNativeArray, CoordType};
use geoarrow::datatypes::GeoDataType;
use geoarrow::datatypes::NativeType;
use geoarrow::io::geozero::FromEWKB;
use geoarrow::NativeArray;
use pyo3::exceptions::PyTypeError;
Expand All @@ -17,13 +17,13 @@ pub fn from_ewkb(py: Python, input: PyArray) -> PyGeoArrowResult<PyObject> {
let array = from_arrow_array(&array, &field)?;
let ref_array = array.as_ref();
let geo_array: Arc<dyn NativeArray> = match array.data_type() {
GeoDataType::WKB => FromEWKB::from_ewkb(
NativeType::WKB => FromEWKB::from_ewkb(
ref_array.as_wkb(),
CoordType::Interleaved,
Default::default(),
false,
)?,
GeoDataType::LargeWKB => FromEWKB::from_ewkb(
NativeType::LargeWKB => FromEWKB::from_ewkb(
ref_array.as_large_wkb(),
CoordType::Interleaved,
Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion python/geoarrow-core/src/interop/numpy/to_numpy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use geoarrow::trait_::NativeArrayAccessor;
use geoarrow::trait_::ArrayAccessor;
use geoarrow::NativeArray;
use pyo3::exceptions::PyValueError;
use pyo3::intern;
Expand Down
4 changes: 2 additions & 2 deletions python/geoarrow-core/src/interop/shapely/from_shapely.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use arrow_array::builder::{BinaryBuilder, Int32BufferBuilder};
use arrow_buffer::OffsetBuffer;
use geoarrow::array::metadata::ArrayMetadata;
use geoarrow::array::InterleavedCoordBuffer;
use geoarrow::datatypes::{Dimension, GeoDataType};
use geoarrow::datatypes::{Dimension, NativeType};
use geoarrow::NativeArray;
use numpy::{PyReadonlyArray1, PyReadonlyArray2, PyUntypedArrayMethods};
use pyo3::exceptions::PyValueError;
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn from_shapely(
let wkb_arr = make_wkb_arr(py, input, metadata)?;
let geom_arr = geoarrow::io::wkb::from_wkb(
&wkb_arr,
GeoDataType::GeometryCollection(Default::default(), Dimension::XY),
NativeType::GeometryCollection(Default::default(), Dimension::XY),
false,
)?;
geometry_array_to_pyobject(py, geom_arr)
Expand Down
4 changes: 2 additions & 2 deletions python/geoarrow-core/src/interop/shapely/to_shapely.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::interop::shapely::utils::import_shapely;
use arrow_array::OffsetSizeTrait;
use arrow_buffer::NullBuffer;
use geoarrow::array::{from_arrow_array, AsNativeArray, CoordBuffer};
use geoarrow::datatypes::{Dimension, GeoDataType};
use geoarrow::datatypes::{Dimension, NativeType};
use geoarrow::io::wkb::to_wkb;
use geoarrow::NativeArray;
use numpy::PyArrayMethods;
Expand Down Expand Up @@ -87,7 +87,7 @@ fn pyarray_to_shapely(py: Python, input: PyArray) -> PyGeoArrowResult<Bound<PyAn
let array = from_arrow_array(&array, &field)?;

use Dimension::*;
use GeoDataType::*;
use NativeType::*;
match array.data_type() {
Point(_, XY) => point_arr(py, array.as_ref().as_point::<2>().clone()),
LineString(_, XY) => linestring_arr(py, array.as_ref().as_line_string::<2>().clone()),
Expand Down
12 changes: 5 additions & 7 deletions python/geoarrow-core/src/interop/wkb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use geoarrow::array::{AsChunkedNativeArray, AsNativeArray, NativeArrayDyn};
use geoarrow::chunked_array::ChunkedNativeArray;
use geoarrow::datatypes::GeoDataType;
use geoarrow::datatypes::NativeType;
use geoarrow::error::GeoArrowError;
use geoarrow::io::wkb::{to_wkb as _to_wkb, FromWKB, ToWKB};
use geoarrow::NativeArray;
Expand All @@ -27,10 +27,8 @@ pub fn from_wkb(
match input {
AnyGeometryInput::Array(arr) => {
let geo_array: Arc<dyn NativeArray> = match arr.as_ref().data_type() {
GeoDataType::WKB => FromWKB::from_wkb(arr.as_ref().as_wkb(), coord_type)?,
GeoDataType::LargeWKB => {
FromWKB::from_wkb(arr.as_ref().as_large_wkb(), coord_type)?
}
NativeType::WKB => FromWKB::from_wkb(arr.as_ref().as_wkb(), coord_type)?,
NativeType::LargeWKB => FromWKB::from_wkb(arr.as_ref().as_large_wkb(), coord_type)?,
other => {
return Err(GeoArrowError::IncorrectType(
format!("Unexpected array type {:?}", other).into(),
Expand All @@ -42,8 +40,8 @@ pub fn from_wkb(
}
AnyGeometryInput::Chunked(s) => {
let geo_array: Arc<dyn ChunkedNativeArray> = match s.as_ref().data_type() {
GeoDataType::WKB => FromWKB::from_wkb(s.as_ref().as_wkb(), coord_type)?,
GeoDataType::LargeWKB => FromWKB::from_wkb(s.as_ref().as_large_wkb(), coord_type)?,
NativeType::WKB => FromWKB::from_wkb(s.as_ref().as_wkb(), coord_type)?,
NativeType::LargeWKB => FromWKB::from_wkb(s.as_ref().as_large_wkb(), coord_type)?,
other => {
return Err(GeoArrowError::IncorrectType(
format!("Unexpected array type {:?}", other).into(),
Expand Down
4 changes: 2 additions & 2 deletions python/pyo3-geoarrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use arrow_array::RecordBatch;
use geoarrow::array::{from_arrow_array, NativeArrayDyn};

use geoarrow::error::GeoArrowError;
use geoarrow::scalar::GeometryScalar;
use geoarrow::scalar::NativeScalar;
use geoarrow::trait_::NativeArrayRef;
use geoarrow::NativeArray;
use geozero::ProcessToJson;
Expand Down Expand Up @@ -114,7 +114,7 @@ impl PyGeometryArray {
}

Ok(Some(PyGeometry(
GeometryScalar::try_new(self.0.slice(i, 1)).unwrap(),
NativeScalar::try_new(self.0.slice(i, 1)).unwrap(),
)))
}

Expand Down
4 changes: 2 additions & 2 deletions python/pyo3-geoarrow/src/chunked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use geoarrow::array::from_arrow_array;
use geoarrow::chunked_array::{from_arrow_chunks, ChunkedNativeArray};
use geoarrow::scalar::GeometryScalar;
use geoarrow::scalar::NativeScalar;
use pyo3::exceptions::PyIndexError;
use pyo3::intern;
use pyo3::prelude::*;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl PyChunkedGeometryArray {
let geom_chunks = sliced.geometry_chunks();
assert_eq!(geom_chunks.len(), 1);
Ok(Some(PyGeometry(
GeometryScalar::try_new(geom_chunks[0].clone()).unwrap(),
NativeScalar::try_new(geom_chunks[0].clone()).unwrap(),
)))
}

Expand Down
36 changes: 18 additions & 18 deletions python/pyo3-geoarrow/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::{PyGeoArrowError, PyGeoArrowResult};
use crate::{PyCoordType, PyDimension};

use geoarrow::array::CoordType;
use geoarrow::datatypes::{Dimension, GeoDataType};
use geoarrow::datatypes::{Dimension, NativeType};
use pyo3::exceptions::PyValueError;
use pyo3::intern;
use pyo3::prelude::*;
Expand All @@ -11,10 +11,10 @@ use pyo3_arrow::ffi::to_schema_pycapsule;
use pyo3_arrow::PyField;

#[pyclass(module = "geoarrow.rust.core._rust", name = "GeometryType", subclass)]
pub struct PyGeometryType(pub(crate) GeoDataType);
pub struct PyGeometryType(pub(crate) NativeType);

impl PyGeometryType {
pub fn new(data_type: GeoDataType) -> Self {
pub fn new(data_type: NativeType) -> Self {
Self(data_type)
}

Expand All @@ -23,7 +23,7 @@ impl PyGeometryType {
PyField::from_arrow_pycapsule(capsule)?.try_into()
}

pub fn into_inner(self) -> GeoDataType {
pub fn into_inner(self) -> NativeType {
self.0
}
}
Expand All @@ -37,40 +37,40 @@ impl PyGeometryType {
coord_type: Option<PyCoordType>,
) -> PyResult<Self> {
match r#type.to_lowercase().as_str() {
"point" => Ok(Self(GeoDataType::Point(
"point" => Ok(Self(NativeType::Point(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"linestring" => Ok(Self(GeoDataType::LineString(
"linestring" => Ok(Self(NativeType::LineString(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"polygon" => Ok(Self(GeoDataType::Polygon(
"polygon" => Ok(Self(NativeType::Polygon(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"multipoint" => Ok(Self(GeoDataType::MultiPoint(
"multipoint" => Ok(Self(NativeType::MultiPoint(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"multilinestring" => Ok(Self(GeoDataType::MultiLineString(
"multilinestring" => Ok(Self(NativeType::MultiLineString(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"multipolygon" => Ok(Self(GeoDataType::MultiPolygon(
"multipolygon" => Ok(Self(NativeType::MultiPolygon(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"geometry" => Ok(Self(GeoDataType::Mixed(
"geometry" => Ok(Self(NativeType::Mixed(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"geometrycollection" => Ok(Self(GeoDataType::GeometryCollection(
"geometrycollection" => Ok(Self(NativeType::GeometryCollection(
coord_type.unwrap().into(),
dimension.unwrap().into(),
))),
"wkb" => Ok(Self(GeoDataType::WKB)),
"box" | "rect" => Ok(Self(GeoDataType::Rect(dimension.unwrap().into()))),
"wkb" => Ok(Self(NativeType::WKB)),
"box" | "rect" => Ok(Self(NativeType::Rect(dimension.unwrap().into()))),
_ => Err(PyValueError::new_err("Unknown geometry type input")),
}
}
Expand All @@ -87,7 +87,7 @@ impl PyGeometryType {
}

fn __repr__(&self) -> String {
// TODO: implement Display for GeoDataType
// TODO: implement Display for NativeType
format!("geoarrow.rust.core.GeometryType({:?})", self.0)
}

Expand Down Expand Up @@ -130,13 +130,13 @@ impl PyGeometryType {
}
}

impl From<GeoDataType> for PyGeometryType {
fn from(value: GeoDataType) -> Self {
impl From<NativeType> for PyGeometryType {
fn from(value: NativeType) -> Self {
Self(value)
}
}

impl From<PyGeometryType> for GeoDataType {
impl From<PyGeometryType> for NativeType {
fn from(value: PyGeometryType) -> Self {
value.0
}
Expand Down
6 changes: 3 additions & 3 deletions python/pyo3-geoarrow/src/ffi/from_python/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::array::*;
use crate::scalar::*;
use geoarrow::array::MixedGeometryArray;
use geoarrow::io::geozero::ToMixedArray;
use geoarrow::scalar::GeometryScalar;
use geoarrow::scalar::NativeScalar;
use geozero::geojson::GeoJsonString;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
Expand All @@ -16,7 +16,7 @@ impl<'a> FromPyObject<'a> for PyGeometry {
let py = ob.py();
// TODO: direct shapely conversion not via __geo_interface__
if let Ok(geo_arr) = ob.extract::<PyGeometryArray>() {
let scalar = GeometryScalar::try_new(geo_arr.0.into_inner()).unwrap();
let scalar = NativeScalar::try_new(geo_arr.0.into_inner()).unwrap();
Ok(PyGeometry::new(scalar))
} else if ob.hasattr(intern!(py, "__geo_interface__"))? {
let json_string = call_geo_interface(py, ob)?;
Expand All @@ -29,7 +29,7 @@ impl<'a> FromPyObject<'a> for PyGeometry {
.to_mixed_geometry_array()
.map_err(|err| PyValueError::new_err(err.to_string()))?;
Ok(Self(
GeometryScalar::try_new(Arc::new(arr))
NativeScalar::try_new(Arc::new(arr))
.map_err(|err| PyValueError::new_err(err.to_string()))?,
))
} else {
Expand Down
16 changes: 8 additions & 8 deletions python/pyo3-geoarrow/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use geoarrow::algorithm::native::bounding_rect::bounding_rect_geometry;
use geoarrow::error::GeoArrowError;
use geoarrow::scalar::GeometryScalar;
use geoarrow::scalar::NativeScalar;
use geoarrow::NativeArray;
use geozero::svg::SvgWriter;
use geozero::{FeatureProcessor, GeozeroGeometry, ToJson};
Expand All @@ -14,18 +14,18 @@ use crate::error::PyGeoArrowResult;

/// This is modeled as a geospatial array of length 1
#[pyclass(module = "geoarrow.rust.core._rust", name = "Geometry", subclass)]
pub struct PyGeometry(pub(crate) GeometryScalar);
pub struct PyGeometry(pub(crate) NativeScalar);

impl PyGeometry {
pub fn new(array: GeometryScalar) -> Self {
pub fn new(array: NativeScalar) -> Self {
Self(array)
}

pub fn inner(&self) -> &GeometryScalar {
pub fn inner(&self) -> &NativeScalar {
&self.0
}

pub fn into_inner(self) -> GeometryScalar {
pub fn into_inner(self) -> NativeScalar {
self.0
}

Expand Down Expand Up @@ -114,13 +114,13 @@ impl PyGeometry {
}
}

impl From<GeometryScalar> for PyGeometry {
fn from(value: GeometryScalar) -> Self {
impl From<NativeScalar> for PyGeometry {
fn from(value: NativeScalar) -> Self {
Self(value)
}
}

impl From<PyGeometry> for GeometryScalar {
impl From<PyGeometry> for NativeScalar {
fn from(value: PyGeometry) -> Self {
value.0
}
Expand Down
10 changes: 5 additions & 5 deletions src/algorithm/geo/affine_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::sync::Arc;

use crate::array::*;
use crate::chunked_array::*;
use crate::datatypes::{Dimension, GeoDataType};
use crate::datatypes::{Dimension, NativeType};
use crate::error::{GeoArrowError, Result};
use crate::trait_::NativeArrayAccessor;
use crate::trait_::ArrayAccessor;
use crate::NativeArray;
use arrow_array::OffsetSizeTrait;
use geo::{AffineTransform, MapCoords};
Expand Down Expand Up @@ -132,7 +132,7 @@ impl AffineOps<&AffineTransform> for &dyn NativeArray {
};
}
use Dimension::*;
use GeoDataType::*;
use NativeType::*;

let result: Arc<dyn NativeArray> = match self.data_type() {
Point(_, XY) => impl_downcast!(as_point),
Expand Down Expand Up @@ -203,7 +203,7 @@ impl AffineOps<&AffineTransform> for &dyn ChunkedNativeArray {
};
}
use Dimension::*;
use GeoDataType::*;
use NativeType::*;

let result: Arc<dyn ChunkedNativeArray> = match self.data_type() {
Point(_, XY) => impl_downcast!(as_point),
Expand Down Expand Up @@ -313,7 +313,7 @@ impl AffineOps<&[AffineTransform]> for &dyn NativeArray {

fn affine_transform(&self, transform: &[AffineTransform]) -> Self::Output {
use Dimension::*;
use GeoDataType::*;
use NativeType::*;

let result: Arc<dyn NativeArray> = match self.data_type() {
Point(_, XY) => Arc::new(self.as_point::<2>().affine_transform(transform)),
Expand Down
Loading

0 comments on commit 7b56226

Please sign in to comment.