open_vector_tile/base/
vector_layer.rs1use crate::{base::BaseVectorFeature, mapbox::MapboxVectorLayer, open::Extent};
2use alloc::{string::String, vec::Vec};
3use s2json::Shape;
4
5#[derive(Debug)]
8pub struct BaseVectorLayer {
9 pub version: u8,
11 pub name: String,
13 pub extent: Extent,
15 pub shape_defined: bool,
17 pub m_shape_defined: bool,
19 pub shape: Shape,
21 pub m_shape: Option<Shape>,
23 pub features: Vec<BaseVectorFeature>,
25}
26impl BaseVectorLayer {
27 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 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 pub fn feature(&self, i: usize) -> &BaseVectorFeature {
68 &self.features[i]
69 }
70
71 pub fn len(&self) -> usize {
73 self.features.len()
74 }
75
76 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}