Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove const generic from coord buffers #845

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions js/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions js/src/data/coord.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
use arrow_buffer::ScalarBuffer;
use geoarrow::datatypes::Dimension;
use wasm_bindgen::prelude::*;

// TODO: remove InterleavedCoordBuffer and SeparatedCoordBuffer structs?

/// An immutable buffer of interleaved coordinates in WebAssembly memory.
#[wasm_bindgen]
pub struct InterleavedCoordBuffer(pub(crate) geoarrow::array::InterleavedCoordBuffer<2>);
pub struct InterleavedCoordBuffer(pub(crate) geoarrow::array::InterleavedCoordBuffer);

#[wasm_bindgen]
impl InterleavedCoordBuffer {
#[wasm_bindgen(constructor)]
pub fn new(coords: Vec<f64>) -> Self {
Self(geoarrow::array::InterleavedCoordBuffer::new(coords.into()))
Self(geoarrow::array::InterleavedCoordBuffer::new(
coords.into(),
Dimension::XY,
))
}
}

/// An immutable buffer of separated coordinates in WebAssembly memory.
#[wasm_bindgen]
pub struct SeparatedCoordBuffer(pub(crate) geoarrow::array::SeparatedCoordBuffer<2>);
pub struct SeparatedCoordBuffer(pub(crate) geoarrow::array::SeparatedCoordBuffer);

#[wasm_bindgen]
impl SeparatedCoordBuffer {
#[wasm_bindgen(constructor)]
pub fn new(x: Vec<f64>, y: Vec<f64>) -> Self {
Self(geoarrow::array::SeparatedCoordBuffer::new([
x.into(),
y.into(),
]))
Self(geoarrow::array::SeparatedCoordBuffer::new(
[
x.into(),
y.into(),
ScalarBuffer::from(vec![]),
ScalarBuffer::from(vec![]),
],
Dimension::XY,
))
}
}

/// An immutable buffer of coordinates in WebAssembly memory, that can be either interleaved or
/// separated.
#[wasm_bindgen]
pub struct CoordBuffer(pub(crate) geoarrow::array::CoordBuffer<2>);
pub struct CoordBuffer(pub(crate) geoarrow::array::CoordBuffer);

#[wasm_bindgen]
impl CoordBuffer {
/// Create a new CoordBuffer from a `Float64Array` of interleaved XY coordinates
#[wasm_bindgen(js_name = fromInterleaved)]
pub fn from_interleaved(coords: Vec<f64>) -> Self {
let buffer = geoarrow::array::InterleavedCoordBuffer::new(coords.into());
let buffer = geoarrow::array::InterleavedCoordBuffer::new(coords.into(), Dimension::XY);
Self(geoarrow::array::CoordBuffer::Interleaved(buffer))
}

/// Create a new CoordBuffer from two `Float64Array`s of X and Y
#[wasm_bindgen(js_name = fromSeparated)]
pub fn from_separated(x: Vec<f64>, y: Vec<f64>) -> Self {
let buffer = geoarrow::array::SeparatedCoordBuffer::new([x.into(), y.into()]);
let buffer = geoarrow::array::SeparatedCoordBuffer::new(
[
x.into(),
y.into(),
ScalarBuffer::from(vec![]),
ScalarBuffer::from(vec![]),
],
Dimension::XY,
);
Self(geoarrow::array::CoordBuffer::Separated(buffer))
}

Expand Down
142 changes: 44 additions & 98 deletions python/geoarrow-core/src/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ fn create_array_metadata(crs: Option<CRS>) -> Arc<ArrayMetadata> {
#[pyo3(signature = (coords, *, crs = None))]
pub fn points(coords: PyCoordBuffer, crs: Option<CRS>) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = PointArray::new(coords, None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = PointArray::new(coords, None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
// TODO: remove const generic
let array = PointArray::<2>::new(coords.into_inner(), None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}

#[pyfunction]
Expand All @@ -41,16 +34,14 @@ pub fn linestrings(
crs: Option<CRS>,
) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = LineStringArray::new(coords, geom_offsets.into_inner(), None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = LineStringArray::new(coords, geom_offsets.into_inner(), None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
// TODO: remove const generic
let array = LineStringArray::<2>::new(
coords.into_inner(),
geom_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}

#[pyfunction]
Expand All @@ -62,28 +53,15 @@ pub fn polygons(
crs: Option<CRS>,
) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = PolygonArray::new(
coords,
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = PolygonArray::new(
coords,
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
// TODO: remove const generic
let array = PolygonArray::<2>::new(
coords.into_inner(),
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}

#[pyfunction]
Expand All @@ -94,16 +72,13 @@ pub fn multipoints(
crs: Option<CRS>,
) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = MultiPointArray::new(coords, geom_offsets.into_inner(), None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = MultiPointArray::new(coords, geom_offsets.into_inner(), None, metadata);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
let array = MultiPointArray::<2>::new(
coords.into_inner(),
geom_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}

#[pyfunction]
Expand All @@ -115,28 +90,14 @@ pub fn multilinestrings(
crs: Option<CRS>,
) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = MultiLineStringArray::new(
coords,
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = MultiLineStringArray::new(
coords,
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
let array = MultiLineStringArray::<2>::new(
coords.into_inner(),
geom_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}

#[pyfunction]
Expand All @@ -149,28 +110,13 @@ pub fn multipolygons(
crs: Option<CRS>,
) -> PyGeoArrowResult<PyNativeArray> {
let metadata = create_array_metadata(crs);
match coords {
PyCoordBuffer::TwoD(coords) => {
let array = MultiPolygonArray::new(
coords,
geom_offsets.into_inner(),
polygon_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
PyCoordBuffer::ThreeD(coords) => {
let array = MultiPolygonArray::new(
coords,
geom_offsets.into_inner(),
polygon_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
}
let array = MultiPolygonArray::<2>::new(
coords.into_inner(),
geom_offsets.into_inner(),
polygon_offsets.into_inner(),
ring_offsets.into_inner(),
None,
metadata,
);
Ok(PyNativeArray::new(NativeArrayDyn::new(Arc::new(array))))
}
18 changes: 8 additions & 10 deletions python/geoarrow-core/src/interop/shapely/to_shapely.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ fn check_nulls(nulls: Option<&NullBuffer>) -> PyGeoArrowResult<()> {
}

/// Copy a CoordBuffer to a numpy array of shape `(length, D)`
fn coords_to_numpy<const D: usize>(
py: Python,
coords: &CoordBuffer<D>,
) -> PyGeoArrowResult<PyObject> {
fn coords_to_numpy(py: Python, coords: &CoordBuffer) -> PyGeoArrowResult<PyObject> {
match coords {
CoordBuffer::Interleaved(cb) => {
let size = cb.dim().size();
let scalar_buffer = cb.coords();
let numpy_coords = scalar_buffer
.to_pyarray_bound(py)
.reshape([scalar_buffer.len() / D, D])?;
.reshape([scalar_buffer.len() / size, size])?;

Ok(numpy_coords.to_object(py))
}
CoordBuffer::Separated(cb) => {
let buffers = cb.coords();
let buffers = cb.buffers();
let numpy_buffers = buffers
.iter()
.map(|buf| buf.to_pyarray_bound(py).to_object(py))
Expand Down Expand Up @@ -258,10 +256,10 @@ fn rect_arr(py: Python, arr: geoarrow::array::RectArray<2>) -> PyGeoArrowResult<
let lower = arr.lower();
let upper = arr.upper();

let xmin = &lower.coords()[0].to_pyarray_bound(py);
let ymin = &lower.coords()[1].to_pyarray_bound(py);
let xmax = &upper.coords()[0].to_pyarray_bound(py);
let ymax = &upper.coords()[1].to_pyarray_bound(py);
let xmin = &lower.buffers()[0].to_pyarray_bound(py);
let ymin = &lower.buffers()[1].to_pyarray_bound(py);
let xmax = &upper.buffers()[0].to_pyarray_bound(py);
let ymax = &upper.buffers()[1].to_pyarray_bound(py);

let args = (xmin, ymin, xmax, ymax);
Ok(shapely_mod.call_method1(intern!(py, "box"), args)?)
Expand Down
Loading
Loading