WrapArray trait
Oh that's a valid point about defining the trait in geoarrow-array. We can have:
use arrow_array::Array;
use geoarrow_schema::PointType;
use crate::error::Result;
use crate::GeoArrowArray;
pub trait WrapArray {
type Output: GeoArrowArray;
fn wrap(&self, array: &dyn Array) -> Result<Self::Output>;
}
impl WrapArray for PointType {
type Output = PointArray;
fn wrap(&self, array: &dyn Array) -> Result<Self::Output> {
(array, self.clone()).try_into()
}
}
fn example(typ: PointType, array: Arc<dyn Array>) {
let point_array: PointArray = typ.wrap(array.as_ref()).unwrap();
}
Originally posted by @kylebarron in https://github.com/geoarrow/geoarrow-rs/issues/1023#issuecomment-2784833501
cc @paleolimbot
Oh nice, I didn't know the trick about setting the output type!
For what it's worth, this comes from Arrow C++/pyarrow, where "extension types" have a wrap_array()/WrapArray() method. It also creates some dependency issues for geoarrow-pyarrow because we want the same kind of setup there too (array depends on schema).
While it's not explicitly called a "WrapArray" trait, there's equivalent functionality in geoarrow-array to achieve this. Every geometry array type has several TryFrom implementations that convert data from arrow arrays into geoarrow arrays: https://docs.rs/geoarrow-array/latest/geoarrow_array/array/struct.PointArray.html#impl-TryFrom%3C(%26StructArray,+PointType)%3E-for-PointArray
One downside of the current from_arrow_array top-level function is that it always uses GeoArrowType::from_arrow_field (interpreting any geometry-like column, loosely) instead of using GeoArrowType::from_extension_field (actively requiring GeoArrow extension metadata to be set on the field to allow the conversion).
Perhaps it would be worth it to have this WrapArray trait so that users could choose which method to use. GeoArrowType::from_extension_field(field).unwrap().wrap_array(&array_ref)?
Perhaps we should have a wrap_array method both on each extension type, so that PointType::wrap_array returns a PointArray. But then also implement that on GeoArrowType which returns an Arc<dyn GeoArrowArray>.