geoarrow_schema/
type_id.rs

1//! Contains helpers for working with GeoArrow Type IDs.
2
3use crate::{
4    Dimension, GeometryCollectionType, LineStringType, MultiLineStringType, MultiPointType,
5    MultiPolygonType, PointType, PolygonType,
6};
7
8/// Compute the Type ID for an array type-dimension combination.
9///
10/// The GeoArrow specification defines a Type ID for each geometry type and dimension combination.
11/// <https://geoarrow.org/format.html#geometry>
12pub trait GeometryTypeId {
13    /// The integer offset for this geometry type.
14    ///
15    /// This matches the 2D geometry type IDs defined in the GeoArrow specification. For example,
16    /// Point is 1, LineString is 2, etc.
17    const GEOMETRY_TYPE_OFFSET: i8;
18
19    /// The dimension of this geometry type.
20    fn dimension(&self) -> Dimension;
21
22    /// The Type ID for this geometry type and dimension.
23    fn geometry_type_id(&self) -> i8 {
24        (dimension_order(self.dimension()) * 10) + Self::GEOMETRY_TYPE_OFFSET
25    }
26}
27
28fn dimension_order(dim: Dimension) -> i8 {
29    match dim {
30        Dimension::XY => 0,
31        Dimension::XYZ => 1,
32        Dimension::XYM => 2,
33        Dimension::XYZM => 3,
34    }
35}
36
37impl GeometryTypeId for PointType {
38    const GEOMETRY_TYPE_OFFSET: i8 = 1;
39
40    fn dimension(&self) -> Dimension {
41        self.dimension()
42    }
43}
44
45impl GeometryTypeId for LineStringType {
46    const GEOMETRY_TYPE_OFFSET: i8 = 2;
47
48    fn dimension(&self) -> Dimension {
49        self.dimension()
50    }
51}
52
53impl GeometryTypeId for PolygonType {
54    const GEOMETRY_TYPE_OFFSET: i8 = 3;
55
56    fn dimension(&self) -> Dimension {
57        self.dimension()
58    }
59}
60
61impl GeometryTypeId for MultiPointType {
62    const GEOMETRY_TYPE_OFFSET: i8 = 4;
63
64    fn dimension(&self) -> Dimension {
65        self.dimension()
66    }
67}
68
69impl GeometryTypeId for MultiLineStringType {
70    const GEOMETRY_TYPE_OFFSET: i8 = 5;
71
72    fn dimension(&self) -> Dimension {
73        self.dimension()
74    }
75}
76
77impl GeometryTypeId for MultiPolygonType {
78    const GEOMETRY_TYPE_OFFSET: i8 = 6;
79
80    fn dimension(&self) -> Dimension {
81        self.dimension()
82    }
83}
84
85impl GeometryTypeId for GeometryCollectionType {
86    const GEOMETRY_TYPE_OFFSET: i8 = 7;
87
88    fn dimension(&self) -> Dimension {
89        self.dimension()
90    }
91}