open_vector_tile/base/
vector_layer.rs

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