Skip to content

Commit

Permalink
Import from 3d arrayref (#671)
Browse files Browse the repository at this point in the history
For #662
  • Loading branch information
kylebarron authored Jul 22, 2024
1 parent fbd495b commit d571c20
Show file tree
Hide file tree
Showing 18 changed files with 376 additions and 143 deletions.
26 changes: 26 additions & 0 deletions src/array/binary/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ impl<O: OffsetSizeTrait> GeometryArrayTrait for WKBArray<O> {
self.metadata.clone()
}

fn with_metadata(&self, metadata: Arc<ArrayMetadata>) -> crate::trait_::GeometryArrayRef {
let mut arr = self.clone();
arr.metadata = metadata;
Arc::new(arr)
}

/// Returns the number of geometries in this array
#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -275,6 +281,26 @@ impl TryFrom<&dyn Array> for WKBArray<i64> {
}
}

impl TryFrom<(&dyn Array, &Field)> for WKBArray<i32> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl TryFrom<(&dyn Array, &Field)> for WKBArray<i64> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl From<WKBArray<i32>> for WKBArray<i64> {
fn from(value: WKBArray<i32>) -> Self {
let binary_array = value.array;
Expand Down
7 changes: 7 additions & 0 deletions src/array/coord/combined/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ impl<const D: usize> GeometryArrayTrait for CoordBuffer<D> {
panic!()
}

fn with_metadata(
&self,
_metadata: Arc<crate::array::metadata::ArrayMetadata>,
) -> crate::trait_::GeometryArrayRef {
panic!()
}

fn into_array_ref(self) -> Arc<dyn Array> {
self.into_arrow()
}
Expand Down
7 changes: 7 additions & 0 deletions src/array/coord/interleaved/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ impl<const D: usize> GeometryArrayTrait for InterleavedCoordBuffer<D> {
panic!()
}

fn with_metadata(
&self,
_metadata: Arc<crate::array::metadata::ArrayMetadata>,
) -> crate::trait_::GeometryArrayRef {
panic!()
}

fn into_array_ref(self) -> Arc<dyn Array> {
Arc::new(self.into_arrow())
}
Expand Down
7 changes: 7 additions & 0 deletions src/array/coord/separated/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ impl<const D: usize> GeometryArrayTrait for SeparatedCoordBuffer<D> {
panic!()
}

fn with_metadata(
&self,
_metadata: Arc<crate::array::metadata::ArrayMetadata>,
) -> crate::trait_::GeometryArrayRef {
panic!()
}

fn len(&self) -> usize {
self.buffers[0].len()
}
Expand Down
26 changes: 26 additions & 0 deletions src/array/geometrycollection/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryArrayTrait for GeometryCollecti
self.metadata.clone()
}

fn with_metadata(&self, metadata: Arc<ArrayMetadata>) -> crate::trait_::GeometryArrayRef {
let mut arr = self.clone();
arr.metadata = metadata;
Arc::new(arr)
}

/// Returns the number of geometries in this array
#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -309,6 +315,26 @@ impl<const D: usize> TryFrom<&dyn Array> for GeometryCollectionArray<i64, D> {
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for GeometryCollectionArray<i32, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for GeometryCollectionArray<i64, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<O: OffsetSizeTrait, G: GeometryCollectionTrait<T = f64>> From<&[G]>
for GeometryCollectionArray<O, 2>
{
Expand Down
26 changes: 26 additions & 0 deletions src/array/linestring/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryArrayTrait for LineStringArray<
self.metadata.clone()
}

fn with_metadata(&self, metadata: Arc<ArrayMetadata>) -> crate::trait_::GeometryArrayRef {
let mut arr = self.clone();
arr.metadata = metadata;
Arc::new(arr)
}

/// Returns the number of geometries in this array
#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -363,6 +369,26 @@ impl<const D: usize> TryFrom<&dyn Array> for LineStringArray<i64, D> {
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for LineStringArray<i32, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for LineStringArray<i64, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<O: OffsetSizeTrait, G: LineStringTrait<T = f64>> From<Vec<Option<G>>>
for LineStringArray<O, 2>
{
Expand Down
15 changes: 15 additions & 0 deletions src/array/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
//!
//! This metadata is [defined by the GeoArrow specification](https://geoarrow.org/extension-types).
use arrow_schema::Field;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::GeoArrowError;

/// If present, instructs consumers that edges follow a spherical path rather than a planar one. If
/// this value is omitted, edges will be interpreted as planar.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -32,3 +35,15 @@ pub struct ArrayMetadata {
/// one. If this value is omitted, edges will be interpreted as planar.
pub edges: Option<Edges>,
}

impl TryFrom<&Field> for ArrayMetadata {
type Error = GeoArrowError;

fn try_from(value: &Field) -> Result<Self, Self::Error> {
if let Some(ext_meta) = value.metadata().get("ARROW:extension:metadata") {
Ok(serde_json::from_str(ext_meta)?)
} else {
Ok(Default::default())
}
}
}
26 changes: 26 additions & 0 deletions src/array/mixed/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryArrayTrait for MixedGeometryArr
self.metadata.clone()
}

fn with_metadata(&self, metadata: Arc<ArrayMetadata>) -> crate::trait_::GeometryArrayRef {
let mut arr = self.clone();
arr.metadata = metadata;
Arc::new(arr)
}

/// Returns the number of geometries in this array
#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -698,6 +704,26 @@ impl<const D: usize> TryFrom<&dyn Array> for MixedGeometryArray<i64, D> {
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for MixedGeometryArray<i32, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<const D: usize> TryFrom<(&dyn Array, &Field)> for MixedGeometryArray<i64, D> {
type Error = GeoArrowError;

fn try_from((arr, field): (&dyn Array, &Field)) -> Result<Self> {
let mut arr: Self = arr.try_into()?;
arr.metadata = Arc::new(ArrayMetadata::try_from(field)?);
Ok(arr)
}
}

impl<O: OffsetSizeTrait, G: GeometryTrait<T = f64>> TryFrom<&[G]> for MixedGeometryArray<O, 2> {
type Error = GeoArrowError;

Expand Down
Loading

0 comments on commit d571c20

Please sign in to comment.