Skip to content

Commit

Permalink
Implement conversions to GEOS objects (#108)
Browse files Browse the repository at this point in the history
* Start implementing to GEOS

* Finish implementing to geos

* WIP convert from geos geometries to geoarrow arrays

* lint
  • Loading branch information
kylebarron committed Jul 28, 2023
1 parent 6ceb39a commit 32c3204
Show file tree
Hide file tree
Showing 38 changed files with 856 additions and 147 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ byteorder = "1"
gdal = { version = "0.15", optional = true }
geo = "0.26"
geodesy = { version = "0.10", optional = true }
geos = { version = "8.3", features = ["v3_8_0", "geo"], optional = true }
geos = { version = "8.3", features = ["v3_10_0", "geo"], optional = true }
geozero = { version = "0.9.4", features = ["with-wkb"], optional = true }
itertools = "0.11"
# Note: geo has a hard dependency on rstar, so there's no point in feature flagging it
Expand Down
90 changes: 45 additions & 45 deletions js/Cargo.lock

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

14 changes: 14 additions & 0 deletions src/array/coord/combined/geos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::array::CoordBuffer;
use crate::error::{GeoArrowError, Result};
use geos::CoordSeq;

impl TryFrom<CoordBuffer> for CoordSeq<'_> {
type Error = GeoArrowError;

fn try_from(value: CoordBuffer) -> Result<Self> {
match value {
CoordBuffer::Separated(cb) => cb.try_into(),
CoordBuffer::Interleaved(cb) => cb.try_into(),
}
}
}
2 changes: 2 additions & 0 deletions src/array/coord/combined/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod array;
#[cfg(feature = "geos")]
mod geos;
mod mutable;

pub use array::CoordBuffer;
Expand Down
17 changes: 17 additions & 0 deletions src/array/coord/interleaved/geos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::array::InterleavedCoordBuffer;
use crate::error::{GeoArrowError, Result};
use crate::GeometryArrayTrait;
use geos::CoordSeq;

impl TryFrom<InterleavedCoordBuffer> for CoordSeq<'_> {
type Error = GeoArrowError;

fn try_from(value: InterleavedCoordBuffer) -> Result<Self> {
Ok(CoordSeq::new_from_buffer(
value.coords.as_slice(),
value.len(),
false,
false,
)?)
}
}
2 changes: 2 additions & 0 deletions src/array/coord/interleaved/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod array;
#[cfg(feature = "geos")]
mod geos;
mod mutable;

pub use array::InterleavedCoordBuffer;
Expand Down
16 changes: 16 additions & 0 deletions src/array/coord/separated/geos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::array::SeparatedCoordBuffer;
use crate::error::{GeoArrowError, Result};
use geos::CoordSeq;

impl TryFrom<SeparatedCoordBuffer> for CoordSeq<'_> {
type Error = GeoArrowError;

fn try_from(value: SeparatedCoordBuffer) -> Result<Self> {
Ok(CoordSeq::new_from_arrays(
value.x.as_slice(),
value.y.as_slice(),
None,
None,
)?)
}
}
2 changes: 2 additions & 0 deletions src/array/coord/separated/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod array;
#[cfg(feature = "geos")]
mod geos;
mod mutable;

pub use array::SeparatedCoordBuffer;
Expand Down
4 changes: 2 additions & 2 deletions src/array/linestring/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<O: Offset> LineStringArray<O> {
/// Returns the value at slot `i` as a GEOS geometry.
#[cfg(feature = "geos")]
pub fn value_as_geos(&self, i: usize) -> geos::Geometry {
(&self.value_as_geo(i)).try_into().unwrap()
self.value(i).try_into().unwrap()
}

/// Gets the value at slot `i` as a GEOS geometry, additionally checking the validity bitmap
Expand All @@ -249,7 +249,7 @@ impl<O: Offset> LineStringArray<O> {
return None;
}

self.get_as_geo(i).as_ref().map(|g| g.try_into().unwrap())
Some(self.value_as_geos(i))
}

/// Iterator over GEOS geometry objects
Expand Down
23 changes: 23 additions & 0 deletions src/array/linestring/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ impl<O: Offset> TryFrom<WKBArray<O>> for MutableLineStringArray<O> {
}
}

// #[cfg(feature = "geos")]
// impl<O: Offset> TryFrom<Vec<Option<geos::Geometry<'_>>>> for MutableLineStringArray<O> {
// type Error = GeoArrowError;
// fn try_from(value: Vec<Option<geos::Geometry>>) -> std::result::Result<Self, Self::Error> {
// let length = value.len();
// let geos_linestring_objects: Vec<Option<GEOSLineString>> = value
// .iter()
// .map(|geom| {
// geom.map(|geom| GEOSLineString::new_unchecked(std::borrow::Cow::Owned(geom)))
// })
// .collect();
// let (coord_capacity, geom_capacity) = first_pass(
// geos_linestring_objects.iter().map(|item| item.as_ref()),
// length,
// );
// Ok(second_pass(
// geos_linestring_objects.iter().map(|item| item.as_ref()),
// coord_capacity,
// geom_capacity,
// ))
// }
// }

/// LineString and MultiPoint have the same layout, so enable conversions between the two to change
/// the semantic type
impl<O: Offset> From<MutableLineStringArray<O>> for MutableMultiPointArray<O> {
Expand Down
Loading

0 comments on commit 32c3204

Please sign in to comment.