open_vector_tile/
vector_feature.rs

1use crate::{
2    FeatureType, OpenVectorFeature, VectorGeometry, VectorLines3DWithOffset, VectorLinesWithOffset,
3    VectorPoints, VectorPoints3D, mapbox::MapboxVectorFeature,
4};
5use alloc::vec::Vec;
6use s2json::{BBOX, Properties};
7
8/// Methods that all vector features should have
9pub trait VectorFeatureMethods {
10    /// the id of the feature
11    fn id(&self) -> Option<u64>;
12    /// the version of the vector tile
13    fn version(&self) -> u16;
14    /// the properties
15    fn properties(&self) -> Properties;
16    /// the extent
17    fn extent(&self) -> usize;
18    /// the feature type
19    fn get_type(&self) -> FeatureType;
20    /// the bounding box
21    fn bbox(&self) -> Option<BBOX>;
22    /// whether the feature has m values
23    fn has_m_values(&self) -> bool;
24    /// whether the feature is a points type
25    fn is_points(&self) -> bool;
26    /// whether the feature is a line type
27    fn is_lines(&self) -> bool;
28    /// whether the feature is a polygon type
29    fn is_polygons(&self) -> bool;
30    /// whether the feature is a points 3D type
31    fn is_points_3d(&self) -> bool;
32    /// whether the feature is a line 3D type
33    fn is_lines_3d(&self) -> bool;
34    /// whether the feature is a polygon 3D type
35    fn is_polygons_3d(&self) -> bool;
36    /// regardless of the type, we return a flattend point array
37    fn load_points(&mut self) -> VectorPoints;
38    /// regardless of the type, we return a flattend point3D array
39    fn load_points_3d(&mut self) -> VectorPoints3D;
40    /// an array of lines.
41    fn load_lines(&mut self) -> VectorLinesWithOffset;
42    /// an array of 3D lines.
43    fn load_lines_3d(&mut self) -> VectorLines3DWithOffset;
44    /// an array of polygons.
45    fn load_polys(&mut self) -> Vec<VectorLinesWithOffset>;
46    /// an array of 3D polygons.
47    fn load_polys_3d(&mut self) -> Vec<VectorLines3DWithOffset>;
48    /// (flattened geometry & tesslation if applicable, indices)
49    fn load_geometry_flat(&mut self) -> (Vec<f64>, Vec<u32>);
50    /// load the geometry
51    fn load_geometry(&mut self) -> VectorGeometry;
52    /// load the indices
53    fn read_indices(&mut self) -> Vec<u32>;
54    /// Add tessellation data to the geometry
55    fn add_tessellation(&mut self, geometry: &mut Vec<f64>, multiplier: f64);
56    /// Add 3D tessellation data to the geometry
57    fn add_tessellation_3d(&mut self, geometry: &mut Vec<f64>, multiplier: f64);
58}
59
60/// Either a mapbox or open vector feature. Implements the [`VectorFeatureMethods`] trait
61#[derive(Debug)]
62pub enum VectorFeature<'a> {
63    /// Mapbox Vector Feature
64    Mapbox(&'a mut MapboxVectorFeature),
65    /// Open Vector Feature
66    Open(&'a mut OpenVectorFeature),
67}
68impl<'a> From<&'a mut MapboxVectorFeature> for VectorFeature<'a> {
69    fn from(value: &'a mut MapboxVectorFeature) -> Self {
70        VectorFeature::Mapbox(value)
71    }
72}
73impl<'a> From<&'a mut OpenVectorFeature> for VectorFeature<'a> {
74    fn from(value: &'a mut OpenVectorFeature) -> Self {
75        VectorFeature::Open(value)
76    }
77}
78impl VectorFeatureMethods for VectorFeature<'_> {
79    fn id(&self) -> Option<u64> {
80        match self {
81            VectorFeature::Mapbox(feature) => feature.id(),
82            VectorFeature::Open(feature) => feature.id(),
83        }
84    }
85    fn version(&self) -> u16 {
86        match self {
87            VectorFeature::Mapbox(feature) => feature.version(),
88            VectorFeature::Open(feature) => feature.version(),
89        }
90    }
91    fn properties(&self) -> Properties {
92        match self {
93            VectorFeature::Mapbox(feature) => feature.properties(),
94            VectorFeature::Open(feature) => feature.properties(),
95        }
96    }
97    fn extent(&self) -> usize {
98        match self {
99            VectorFeature::Mapbox(feature) => feature.extent(),
100            VectorFeature::Open(feature) => feature.extent(),
101        }
102    }
103    fn get_type(&self) -> FeatureType {
104        match self {
105            VectorFeature::Mapbox(feature) => feature.get_type(),
106            VectorFeature::Open(feature) => feature.get_type(),
107        }
108    }
109    fn bbox(&self) -> Option<BBOX> {
110        match self {
111            VectorFeature::Mapbox(feature) => feature.bbox(),
112            VectorFeature::Open(feature) => feature.bbox(),
113        }
114    }
115    fn has_m_values(&self) -> bool {
116        match self {
117            VectorFeature::Mapbox(feature) => feature.has_m_values(),
118            VectorFeature::Open(feature) => feature.has_m_values(),
119        }
120    }
121    fn is_points(&self) -> bool {
122        match self {
123            VectorFeature::Mapbox(feature) => feature.is_points(),
124            VectorFeature::Open(feature) => feature.is_points(),
125        }
126    }
127    fn is_lines(&self) -> bool {
128        match self {
129            VectorFeature::Mapbox(feature) => feature.is_lines(),
130            VectorFeature::Open(feature) => feature.is_lines(),
131        }
132    }
133    fn is_polygons(&self) -> bool {
134        match self {
135            VectorFeature::Mapbox(feature) => feature.is_polygons(),
136            VectorFeature::Open(feature) => feature.is_polygons(),
137        }
138    }
139    fn is_points_3d(&self) -> bool {
140        match self {
141            VectorFeature::Mapbox(feature) => feature.is_points_3d(),
142            VectorFeature::Open(feature) => feature.is_points_3d(),
143        }
144    }
145    fn is_lines_3d(&self) -> bool {
146        match self {
147            VectorFeature::Mapbox(feature) => feature.is_lines_3d(),
148            VectorFeature::Open(feature) => feature.is_lines_3d(),
149        }
150    }
151    fn is_polygons_3d(&self) -> bool {
152        match self {
153            VectorFeature::Mapbox(feature) => feature.is_polygons_3d(),
154            VectorFeature::Open(feature) => feature.is_polygons_3d(),
155        }
156    }
157    fn load_points(&mut self) -> VectorPoints {
158        match self {
159            VectorFeature::Mapbox(feature) => feature.load_points(),
160            VectorFeature::Open(feature) => feature.load_points(),
161        }
162    }
163    fn load_points_3d(&mut self) -> VectorPoints3D {
164        match self {
165            VectorFeature::Mapbox(feature) => feature.load_points_3d(),
166            VectorFeature::Open(feature) => feature.load_points_3d(),
167        }
168    }
169    fn load_lines(&mut self) -> VectorLinesWithOffset {
170        match self {
171            VectorFeature::Mapbox(feature) => feature.load_lines(),
172            VectorFeature::Open(feature) => feature.load_lines(),
173        }
174    }
175    fn load_lines_3d(&mut self) -> VectorLines3DWithOffset {
176        match self {
177            VectorFeature::Mapbox(feature) => feature.load_lines_3d(),
178            VectorFeature::Open(feature) => feature.load_lines_3d(),
179        }
180    }
181    fn load_polys(&mut self) -> Vec<VectorLinesWithOffset> {
182        match self {
183            VectorFeature::Mapbox(feature) => feature.load_polys(),
184            VectorFeature::Open(feature) => feature.load_polys(),
185        }
186    }
187    fn load_polys_3d(&mut self) -> Vec<VectorLines3DWithOffset> {
188        match self {
189            VectorFeature::Mapbox(feature) => feature.load_polys_3d(),
190            VectorFeature::Open(feature) => feature.load_polys_3d(),
191        }
192    }
193    fn load_geometry_flat(&mut self) -> (Vec<f64>, Vec<u32>) {
194        match self {
195            VectorFeature::Mapbox(feature) => feature.load_geometry_flat(),
196            VectorFeature::Open(feature) => feature.load_geometry_flat(),
197        }
198    }
199    fn load_geometry(&mut self) -> VectorGeometry {
200        match self {
201            VectorFeature::Mapbox(feature) => feature.load_geometry(),
202            VectorFeature::Open(feature) => feature.load_geometry(),
203        }
204    }
205    fn read_indices(&mut self) -> Vec<u32> {
206        match self {
207            VectorFeature::Mapbox(feature) => feature.read_indices(),
208            VectorFeature::Open(feature) => feature.read_indices(),
209        }
210    }
211    fn add_tessellation(&mut self, geometry: &mut Vec<f64>, multiplier: f64) {
212        match self {
213            VectorFeature::Mapbox(feature) => feature.add_tessellation(geometry, multiplier),
214            VectorFeature::Open(feature) => feature.add_tessellation(geometry, multiplier),
215        }
216    }
217    fn add_tessellation_3d(&mut self, geometry: &mut Vec<f64>, multiplier: f64) {
218        match self {
219            VectorFeature::Mapbox(feature) => feature.add_tessellation_3d(geometry, multiplier),
220            VectorFeature::Open(feature) => feature.add_tessellation_3d(geometry, multiplier),
221        }
222    }
223}