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 geo_type = GeoArrowType::from_arrow_field(field)?;
54    let result: Arc<dyn GeoArrowArray> = match geo_type {
55        Point(_) => Arc::new(PointArray::try_from((array, field))?),
56        LineString(_) => Arc::new(LineStringArray::try_from((array, field))?),
57        Polygon(_) => Arc::new(PolygonArray::try_from((array, field))?),
58        MultiPoint(_) => Arc::new(MultiPointArray::try_from((array, field))?),
59        MultiLineString(_) => Arc::new(MultiLineStringArray::try_from((array, field))?),
60        MultiPolygon(_) => Arc::new(MultiPolygonArray::try_from((array, field))?),
61        GeometryCollection(_) => Arc::new(GeometryCollectionArray::try_from((array, field))?),
62        Rect(_) => Arc::new(RectArray::try_from((array, field))?),
63        Geometry(_) => Arc::new(GeometryArray::try_from((array, field))?),
64        Wkb(_) => Arc::new(WkbArray::try_from((array, field))?),
65        LargeWkb(_) => Arc::new(LargeWkbArray::try_from((array, field))?),
66        WkbView(_) => Arc::new(WkbViewArray::try_from((array, field))?),
67        Wkt(_) => Arc::new(WktArray::try_from((array, field))?),
68        LargeWkt(_) => Arc::new(LargeWktArray::try_from((array, field))?),
69        WktView(_) => Arc::new(WktViewArray::try_from((array, field))?),
70    };
71    Ok(result)
72}
73
74// TODO: should we have an API to get the raw underlying string/&[u8] value?
75
76/// A trait for GeoArrow arrays that can hold WKB data.
77///
78/// Currently three types are supported:
79///
80/// - [`GenericWkbArray<i32>`]
81/// - [`GenericWkbArray<i64>`]
82/// - [`WkbViewArray`]
83///
84/// This trait helps to abstract over the different types of WKB arrays so that we don’t need to
85/// duplicate the implementation for each type.
86///
87/// This is modeled after the upstream [`BinaryArrayType`][arrow_array::array::BinaryArrayType]
88/// trait.
89pub trait GenericWkbArrayType<'a>:
90    Sized + crate::GeoArrowArrayAccessor<'a, Item = ::wkb::reader::Wkb<'a>>
91{
92}
93
94impl GenericWkbArrayType<'_> for GenericWkbArray<i32> {}
95impl GenericWkbArrayType<'_> for GenericWkbArray<i64> {}
96impl GenericWkbArrayType<'_> for WkbViewArray {}
97
98/// A trait for GeoArrow arrays that can hold WKT data.
99///
100/// Currently three types are supported:
101///
102/// - [`GenericWktArray<i32>`]
103/// - [`GenericWktArray<i64>`]
104/// - [`WktViewArray`]
105///
106/// This trait helps to abstract over the different types of WKT arrays so that we don’t need to
107/// duplicate the implementation for each type.
108///
109/// This is modeled after the upstream [`StringArrayType`][arrow_array::array::StringArrayType]
110/// trait.
111pub trait GenericWktArrayType:
112    Sized + for<'a> crate::GeoArrowArrayAccessor<'a, Item = ::wkt::Wkt>
113{
114}
115
116impl GenericWktArrayType for GenericWktArray<i32> {}
117impl GenericWktArrayType for GenericWktArray<i64> {}
118impl GenericWktArrayType for WktViewArray {}