ovtile/base/
vector_layer.rs1use crate::{
2 base::BaseVectorFeature,
3 mapbox::MapboxVectorLayer,
4 open::{Extent, Shape},
5};
6
7use alloc::string::String;
8use alloc::vec::Vec;
9
10#[derive(Debug)]
13pub struct BaseVectorLayer {
14 pub version: u8,
16 pub name: String,
18 pub extent: Extent,
20 pub shape_defined: bool,
22 pub m_shape_defined: bool,
24 pub shape: Shape,
26 pub m_shape: Option<Shape>,
28 pub features: Vec<BaseVectorFeature>,
30}
31impl BaseVectorLayer {
32 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 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 pub fn feature(&self, i: usize) -> &BaseVectorFeature {
73 &self.features[i]
74 }
75
76 pub fn len(&self) -> usize {
78 self.features.len()
79 }
80
81 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}