geoarrow_array/array/
mod.rs

1//! The concrete array definitions.
2//!
3//! All arrays implement the core [GeoArrowArray] trait.
4
5mod coord;
6mod geometry;
7mod geometrycollection;
8mod linestring;
9mod mixed;
10mod multilinestring;
11mod multipoint;
12mod multipolygon;
13mod point;
14mod polygon;
15mod rect;
16mod wkb;
17mod wkb_view;
18mod wkt;
19mod wkt_view;
20
21use std::sync::Arc;
22
23use arrow_array::Array;
24use arrow_schema::Field;
25pub use coord::{CoordBuffer, InterleavedCoordBuffer, SeparatedCoordBuffer};
26use geoarrow_schema::GeoArrowType;
27use geoarrow_schema::error::GeoArrowResult;
28pub(crate) use geometry::DimensionIndex;
29pub use geometry::GeometryArray;
30pub use geometrycollection::GeometryCollectionArray;
31pub use linestring::LineStringArray;
32pub(crate) use mixed::MixedGeometryArray;
33pub use multilinestring::MultiLineStringArray;
34pub use multipoint::MultiPointArray;
35pub use multipolygon::MultiPolygonArray;
36pub use point::PointArray;
37pub use polygon::PolygonArray;
38pub use rect::RectArray;
39pub use wkb::{GenericWkbArray, LargeWkbArray, WkbArray};
40pub use wkb_view::WkbViewArray;
41pub use wkt::{GenericWktArray, LargeWktArray, WktArray};
42pub use wkt_view::WktViewArray;
43
44use crate::GeoArrowArray;
45
46/// Construct a new [GeoArrowArray] from an Arrow [Array] and [Field].
47pub fn from_arrow_array(
48    array: &dyn Array,
49    field: &Field,
50) -> GeoArrowResult<Arc<dyn GeoArrowArray>> {
51    use GeoArrowType::*;
52
53    let result: Arc<dyn GeoArrowArray> = match GeoArrowType::try_from(field)? {
54        Point(_) => Arc::new(PointArray::try_from((array, field))?),
55        LineString(_) => Arc::new(LineStringArray::try_from((array, field))?),
56        Polygon(_) => Arc::new(PolygonArray::try_from((array, field))?),
57        MultiPoint(_) => Arc::new(MultiPointArray::try_from((array, field))?),
58        MultiLineString(_) => Arc::new(MultiLineStringArray::try_from((array, field))?),
59        MultiPolygon(_) => Arc::new(MultiPolygonArray::try_from((array, field))?),
60        GeometryCollection(_) => Arc::new(GeometryCollectionArray::try_from((array, field))?),
61        Rect(_) => Arc::new(RectArray::try_from((array, field))?),
62        Geometry(_) => Arc::new(GeometryArray::try_from((array, field))?),
63        Wkb(_) => Arc::new(WkbArray::try_from((array, field))?),
64        LargeWkb(_) => Arc::new(LargeWkbArray::try_from((array, field))?),
65        WkbView(_) => Arc::new(WkbViewArray::try_from((array, field))?),
66        Wkt(_) => Arc::new(WktArray::try_from((array, field))?),
67        LargeWkt(_) => Arc::new(LargeWktArray::try_from((array, field))?),
68        WktView(_) => Arc::new(WktViewArray::try_from((array, field))?),
69    };
70    Ok(result)
71}
72
73// TODO: should we have an API to get the raw underlying string/&[u8] value?
74
75/// A trait for GeoArrow arrays that can hold WKB data.
76///
77/// Currently three types are supported:
78///
79/// - [`GenericWkbArray<i32>`]
80/// - [`GenericWkbArray<i64>`]
81/// - [`WkbViewArray`]
82///
83/// This trait helps to abstract over the different types of WKB arrays so that we don’t need to
84/// duplicate the implementation for each type.
85///
86/// This is modeled after the upstream [`BinaryArrayType`][arrow_array::array::BinaryArrayType]
87/// trait.
88pub trait GenericWkbArrayType<'a>:
89    Sized + crate::GeoArrowArrayAccessor<'a, Item = ::wkb::reader::Wkb<'a>>
90{
91}
92
93impl GenericWkbArrayType<'_> for GenericWkbArray<i32> {}
94impl GenericWkbArrayType<'_> for GenericWkbArray<i64> {}
95impl GenericWkbArrayType<'_> for WkbViewArray {}
96
97/// A trait for GeoArrow arrays that can hold WKT data.
98///
99/// Currently three types are supported:
100///
101/// - [`GenericWktArray<i32>`]
102/// - [`GenericWktArray<i64>`]
103/// - [`WktViewArray`]
104///
105/// This trait helps to abstract over the different types of WKT arrays so that we don’t need to
106/// duplicate the implementation for each type.
107///
108/// This is modeled after the upstream [`StringArrayType`][arrow_array::array::StringArrayType]
109/// trait.
110pub trait GenericWktArrayType:
111    Sized + for<'a> crate::GeoArrowArrayAccessor<'a, Item = ::wkt::Wkt>
112{
113}
114
115impl GenericWktArrayType for GenericWktArray<i32> {}
116impl GenericWktArrayType for GenericWktArray<i64> {}
117impl GenericWktArrayType for WktViewArray {}