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

macro for impl dyn geometry #326

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions src/algorithm/geo/scale.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::Arc;

use crate::algorithm::broadcasting::BroadcastablePrimitive;
use crate::array::LineStringArray;
use crate::array::*;
use crate::GeometryArrayTrait;
use arrow_array::types::Float64Type;
use arrow_array::OffsetSizeTrait;
use geo::Scale as _Scale;
Expand Down Expand Up @@ -250,6 +253,12 @@ iter_geo_impl!(
MultiPolygonBuilder<O>,
push_multi_polygon
);
// iter_geo_impl!(
// MixedGeometryArray<O>,
// MixedGeometryBuilder<O>,
// push_geometry
// );


impl<O: OffsetSizeTrait> Scale for GeometryArray<O> {
crate::geometry_array_delegate_impl! {
Expand All @@ -269,3 +278,22 @@ impl<O: OffsetSizeTrait> Scale for GeometryArray<O> {
) -> Self;
}
}

// impl Scale for Arc<dyn GeometryArrayTrait> {
// crate::geometry_dyn_array_delegate_impl! r{
// fn scale(&self, scale_factor: BroadcastablePrimitive<Float64Type>) -> Self;

// fn scale_xy(
// &self,
// x_factor: BroadcastablePrimitive<Float64Type>,
// y_factor: BroadcastablePrimitive<Float64Type>
// ) -> Self;

// fn scale_around_point(
// &self,
// x_factor: BroadcastablePrimitive<Float64Type>,
// y_factor: BroadcastablePrimitive<Float64Type>,
// origin: geo::Point
// ) -> Self;
// }
// }
109 changes: 109 additions & 0 deletions src/algorithm/geo/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,112 @@ macro_rules! __geometry_array_delegate_impl_helper {
)+
};
}

/// Implements the common pattern where a [`GeometryArray`][crate::array::GeometryArray] enum
/// simply delegates its trait impl to it's inner type.
///
// This is derived from geo https://github.com/georust/geo/blob/d4c858308ba910f69beab175e08af263b17c5f9f/geo/src/types.rs#L119-L158
#[macro_export]
macro_rules! geometry_dyn_array_delegate_impl {
($($a:tt)*) => { $crate::__geometry_dyn_array_delegate_impl_helper!{ $($a)* } }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __geometry_dyn_array_delegate_impl_helper {
(
$(
$(#[$outer:meta])*
fn $func_name: ident(&$($self_life:lifetime)?self $(, $arg_name: ident: $arg_type: ty)*) -> $return: ty;
)+
) => {
$(
$(#[$outer])*
fn $func_name(&$($self_life)? self, $($arg_name: $arg_type),*) -> $return {
use $crate::array::*;
use $crate::datatypes::GeoDataType;

match self.data_type() {
GeoDataType::Point(_) => {
let arr = self.as_any().downcast_ref::<PointArray>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
},
GeoDataType::LineString(_) => {
let arr = self.as_any().downcast_ref::<LineStringArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeLineString(_) => {
let arr = self.as_any().downcast_ref::<LineStringArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::Polygon(_) => {
let arr = self.as_any().downcast_ref::<PolygonArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargePolygon(_) => {
let arr = self.as_any().downcast_ref::<PolygonArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::MultiPoint(_) => {
let arr = self.as_any().downcast_ref::<MultiPointArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeMultiPoint(_) => {
let arr = self.as_any().downcast_ref::<MultiPointArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::MultiLineString(_) => {
let arr = self.as_any().downcast_ref::<MultiLineStringArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeMultiLineString(_) => {
let arr = self.as_any().downcast_ref::<MultiLineStringArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::MultiPolygon(_) => {
let arr = self.as_any().downcast_ref::<MultiPolygonArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeMultiPolygon(_) => {
let arr = self.as_any().downcast_ref::<MultiPolygonArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::Mixed(_) => {
let arr = self.as_any().downcast_ref::<MixedGeometryArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeMixed(_) => {
let arr = self.as_any().downcast_ref::<MixedGeometryArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::GeometryCollection(_) => {
let arr = self.as_any().downcast_ref::<GeometryCollectionArray<i32>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::LargeGeometryCollection(_) => {
let arr = self.as_any().downcast_ref::<GeometryCollectionArray<i64>>().unwrap();
let new_arr = arr.$func_name($($arg_name),*);
new_arr.into()
}
GeoDataType::Rect => todo!(),
GeoDataType::WKB => todo!(),
GeoDataType::LargeWKB => todo!(),
}
}
)+
};
}
4 changes: 4 additions & 0 deletions src/trait_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use crate::array::{CoordBuffer, CoordType};
use crate::datatypes::GeoDataType;
use crate::error::Result;
use crate::geo_traits::GeometryTrait;
use arrow_array::{Array, ArrayRef};
use arrow_buffer::{NullBuffer, NullBufferBuilder};
use arrow_schema::{DataType, Field};
Expand Down Expand Up @@ -270,4 +272,6 @@ pub trait GeometryArrayBuilder: std::fmt::Debug + Send + Sync {
// fn shrink_to_fit(&mut self);

fn into_array_ref(self) -> Arc<dyn Array>;

fn push_geometry(&mut self, value: Option<&impl GeometryTrait<T = f64>>) -> Result<()>;
}
Loading