geoarrow_array/scalar/
geometry.rs

1use geo_traits::{GeometryTrait, GeometryType, UnimplementedLine, UnimplementedTriangle};
2
3use crate::eq::geometry_eq;
4use crate::scalar::*;
5
6/// An Arrow equivalent of a Geometry
7///
8/// This implements [GeometryTrait], which you can use to extract data.
9#[derive(Debug)]
10pub enum Geometry<'a> {
11    /// Point geometry
12    Point(crate::scalar::Point<'a>),
13    /// LineString geometry
14    LineString(crate::scalar::LineString<'a>),
15    /// Polygon geometry
16    Polygon(crate::scalar::Polygon<'a>),
17    /// MultiPoint geometry
18    MultiPoint(crate::scalar::MultiPoint<'a>),
19    /// MultiLineString geometry
20    MultiLineString(crate::scalar::MultiLineString<'a>),
21    /// MultiPolygon geometry
22    MultiPolygon(crate::scalar::MultiPolygon<'a>),
23    /// GeometryCollection geometry
24    GeometryCollection(crate::scalar::GeometryCollection<'a>),
25    /// Rect geometry
26    Rect(crate::scalar::Rect<'a>),
27}
28
29impl GeometryTrait for Geometry<'_> {
30    type T = f64;
31    type PointType<'b>
32        = Point<'b>
33    where
34        Self: 'b;
35    type LineStringType<'b>
36        = LineString<'b>
37    where
38        Self: 'b;
39    type PolygonType<'b>
40        = Polygon<'b>
41    where
42        Self: 'b;
43    type MultiPointType<'b>
44        = MultiPoint<'b>
45    where
46        Self: 'b;
47    type MultiLineStringType<'b>
48        = MultiLineString<'b>
49    where
50        Self: 'b;
51    type MultiPolygonType<'b>
52        = MultiPolygon<'b>
53    where
54        Self: 'b;
55    type GeometryCollectionType<'b>
56        = GeometryCollection<'b>
57    where
58        Self: 'b;
59    type RectType<'b>
60        = Rect<'b>
61    where
62        Self: 'b;
63    type LineType<'b>
64        = UnimplementedLine<f64>
65    where
66        Self: 'b;
67    type TriangleType<'b>
68        = UnimplementedTriangle<f64>
69    where
70        Self: 'b;
71
72    fn dim(&self) -> geo_traits::Dimensions {
73        match self {
74            Geometry::Point(p) => p.dim(),
75            Geometry::LineString(p) => p.dim(),
76            Geometry::Polygon(p) => p.dim(),
77            Geometry::MultiPoint(p) => p.dim(),
78            Geometry::MultiLineString(p) => p.dim(),
79            Geometry::MultiPolygon(p) => p.dim(),
80            Geometry::GeometryCollection(p) => p.dim(),
81            Geometry::Rect(p) => p.dim(),
82        }
83    }
84
85    fn as_type(
86        &self,
87    ) -> geo_traits::GeometryType<
88        '_,
89        Self::PointType<'_>,
90        Self::LineStringType<'_>,
91        Self::PolygonType<'_>,
92        Self::MultiPointType<'_>,
93        Self::MultiLineStringType<'_>,
94        Self::MultiPolygonType<'_>,
95        Self::GeometryCollectionType<'_>,
96        Self::RectType<'_>,
97        Self::TriangleType<'_>,
98        Self::LineType<'_>,
99    > {
100        match self {
101            Geometry::Point(p) => GeometryType::Point(p),
102            Geometry::LineString(p) => GeometryType::LineString(p),
103            Geometry::Polygon(p) => GeometryType::Polygon(p),
104            Geometry::MultiPoint(p) => GeometryType::MultiPoint(p),
105            Geometry::MultiLineString(p) => GeometryType::MultiLineString(p),
106            Geometry::MultiPolygon(p) => GeometryType::MultiPolygon(p),
107            Geometry::GeometryCollection(p) => GeometryType::GeometryCollection(p),
108            Geometry::Rect(p) => GeometryType::Rect(p),
109        }
110    }
111}
112
113impl<'a> GeometryTrait for &'a Geometry<'a> {
114    type T = f64;
115    type PointType<'b>
116        = Point<'b>
117    where
118        Self: 'b;
119    type LineStringType<'b>
120        = LineString<'b>
121    where
122        Self: 'b;
123    type PolygonType<'b>
124        = Polygon<'b>
125    where
126        Self: 'b;
127    type MultiPointType<'b>
128        = MultiPoint<'b>
129    where
130        Self: 'b;
131    type MultiLineStringType<'b>
132        = MultiLineString<'b>
133    where
134        Self: 'b;
135    type MultiPolygonType<'b>
136        = MultiPolygon<'b>
137    where
138        Self: 'b;
139    type GeometryCollectionType<'b>
140        = GeometryCollection<'b>
141    where
142        Self: 'b;
143    type RectType<'b>
144        = Rect<'b>
145    where
146        Self: 'b;
147    type LineType<'b>
148        = UnimplementedLine<f64>
149    where
150        Self: 'b;
151    type TriangleType<'b>
152        = UnimplementedTriangle<f64>
153    where
154        Self: 'b;
155
156    fn dim(&self) -> geo_traits::Dimensions {
157        match self {
158            Geometry::Point(p) => p.dim(),
159            Geometry::LineString(p) => p.dim(),
160            Geometry::Polygon(p) => p.dim(),
161            Geometry::MultiPoint(p) => p.dim(),
162            Geometry::MultiLineString(p) => p.dim(),
163            Geometry::MultiPolygon(p) => p.dim(),
164            Geometry::GeometryCollection(p) => p.dim(),
165            Geometry::Rect(p) => p.dim(),
166        }
167    }
168
169    fn as_type(
170        &self,
171    ) -> geo_traits::GeometryType<
172        '_,
173        Self::PointType<'_>,
174        Self::LineStringType<'_>,
175        Self::PolygonType<'_>,
176        Self::MultiPointType<'_>,
177        Self::MultiLineStringType<'_>,
178        Self::MultiPolygonType<'_>,
179        Self::GeometryCollectionType<'_>,
180        Self::RectType<'_>,
181        Self::TriangleType<'_>,
182        Self::LineType<'_>,
183    > {
184        match self {
185            Geometry::Point(p) => GeometryType::Point(p),
186            Geometry::LineString(p) => GeometryType::LineString(p),
187            Geometry::Polygon(p) => GeometryType::Polygon(p),
188            Geometry::MultiPoint(p) => GeometryType::MultiPoint(p),
189            Geometry::MultiLineString(p) => GeometryType::MultiLineString(p),
190            Geometry::MultiPolygon(p) => GeometryType::MultiPolygon(p),
191            Geometry::GeometryCollection(p) => GeometryType::GeometryCollection(p),
192            Geometry::Rect(p) => GeometryType::Rect(p),
193        }
194    }
195}
196
197impl<G: GeometryTrait<T = f64>> PartialEq<G> for Geometry<'_> {
198    fn eq(&self, other: &G) -> bool {
199        geometry_eq(self, other)
200    }
201}