ovtile/base/
vector_layer.rs

1use crate::{
2    base::BaseVectorFeature,
3    mapbox::MapboxVectorLayer,
4    open::{Extent, Shape},
5};
6
7use alloc::string::String;
8use alloc::vec::Vec;
9
10/// Base Vector Layer
11/// This is an intermediary for storing layer data in the Open Vector Tile format.
12#[derive(Debug)]
13pub struct BaseVectorLayer {
14    /// the version of the vector tile. This is a number that tracks the OVT specification. and shouldn't be tampered with
15    pub version: u8,
16    /// the name of the layer
17    pub name: String,
18    /// the extent of the vector tile (only **512**, **1_024**, **2_048**, **4_096**, and **8_192** are supported)
19    pub extent: Extent,
20    /// if the shape was already passed in to the constructor
21    pub shape_defined: bool,
22    /// if the M-Shape was already passed in to the constructor
23    pub m_shape_defined: bool,
24    /// The shape used to describe the features properties in the layer
25    pub shape: Shape,
26    /// the shape of each feature's M-Values
27    pub m_shape: Option<Shape>,
28    /// the features in the layer
29    pub features: Vec<BaseVectorFeature>,
30}
31impl BaseVectorLayer {
32    /// Create a new BaseVectorLayer
33    pub fn new(
34        name: String,
35        extent: Extent,
36        features: Vec<BaseVectorFeature>,
37        shape: Option<Shape>,
38        m_shape: Option<Shape>,
39    ) -> Self {
40        Self {
41            version: 1,
42            name,
43            extent,
44            shape_defined: shape.is_some(),
45            m_shape_defined: m_shape.is_some(),
46            shape: shape.unwrap_or_default(),
47            m_shape,
48            features,
49        }
50    }
51
52    /// Add a new feature to the layer
53    pub fn add_feature(&mut self, feature: BaseVectorFeature) {
54        if !self.shape_defined {
55            let prop_shape = (feature.properties()).clone().into();
56            self.shape.merge(&prop_shape);
57        }
58        if !self.m_shape_defined {
59            if let Some(m_values) = feature.m_values() {
60                let feature_shape: Shape = (&m_values[..]).into();
61                match self.m_shape {
62                    Some(ref mut m_shape) => m_shape.merge(&feature_shape),
63                    None => self.m_shape = Some(feature_shape),
64                }
65            }
66        }
67
68        self.features.push(feature);
69    }
70
71    /// Get the feature at the given index
72    pub fn feature(&self, i: usize) -> &BaseVectorFeature {
73        &self.features[i]
74    }
75
76    /// Get the number of features
77    pub fn len(&self) -> usize {
78        self.features.len()
79    }
80
81    /// Check if the layer is empty
82    pub fn is_empty(&self) -> bool {
83        self.features.is_empty()
84    }
85}
86impl From<&mut MapboxVectorLayer> for BaseVectorLayer {
87    fn from(mvt: &mut MapboxVectorLayer) -> Self {
88        let mut bvt = Self {
89            version: 1,
90            name: mvt.name.clone(),
91            extent: mvt.extent.into(),
92            shape_defined: false,
93            m_shape_defined: false,
94            shape: Shape::default(),
95            m_shape: None,
96            features: Vec::new(),
97        };
98
99        for feature in mvt.features.values_mut() {
100            bvt.add_feature(feature.into());
101        }
102
103        bvt
104    }
105}