• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-S2 / open-vector-tile / #27

06 Dec 2024 02:32PM UTC coverage: 98.708% (+1.3%) from 97.451%
#27

push

Mr Martian
fix coveralls

8783 of 8898 relevant lines covered (98.71%)

58.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.63
/rust/open/vector_layer.rs
1
use pbf::{ProtoRead, Protobuf};
2

3
use crate::{
4
    base::BaseVectorLayer,
5
    open::{
6
        decode_shape, encode_shape, read_feature, write_feature, ColumnCacheReader,
7
        ColumnCacheWriter, Extent, OpenVectorFeature, Shape,
8
    },
9
    VectorLayerMethods,
10
};
11

12
use core::cell::RefCell;
13

14
use alloc::rc::Rc;
15
use alloc::string::String;
16
use alloc::vec::Vec;
17

18
/// The Open Vector Layer class represents a layer in an Open Vector Tile.
19
/// Contains an extent, name, version, and features.
20
/// The features will utilize the layer extent to decode geometry.
21
#[derive(Debug)]
22
pub struct OpenVectorLayer {
23
    /// the version of the vector tile
24
    pub version: u16,
25
    /// the name of the layer
26
    pub name: String,
27
    /// the extent of the vector layer
28
    pub extent: Extent,
29
    /// the features in the layer
30
    pub features: Vec<OpenVectorFeature>,
31
    shape: Option<Shape>,
32
    m_shape: Option<Shape>,
33
    cache: Rc<RefCell<ColumnCacheReader>>,
34
}
35
impl OpenVectorLayer {
36
    /// Create a new OpenVectorLayer
37
    pub fn new(cache: Rc<RefCell<ColumnCacheReader>>) -> OpenVectorLayer {
21✔
38
        OpenVectorLayer {
21✔
39
            version: 1,
21✔
40
            name: String::new(),
21✔
41
            extent: Extent::default(),
21✔
42
            shape: None,
21✔
43
            m_shape: None,
21✔
44
            features: Vec::new(),
21✔
45
            cache,
21✔
46
        }
21✔
47
    }
21✔
48
}
49
impl VectorLayerMethods for OpenVectorLayer {
50
    fn version(&self) -> u16 {
18✔
51
        self.version
18✔
52
    }
18✔
53
    fn name(&self) -> String {
18✔
54
        self.name.clone()
18✔
55
    }
18✔
56
    fn extent(&self) -> usize {
18✔
57
        self.extent.into()
18✔
58
    }
18✔
59
    fn len(&self) -> usize {
18✔
60
        self.features.len()
18✔
61
    }
18✔
62
    fn is_empty(&self) -> bool {
3✔
63
        self.features.is_empty()
3✔
64
    }
3✔
65
    fn feature(&mut self, i: usize) -> Option<&mut dyn crate::VectorFeatureMethods> {
36✔
66
        self.features.get_mut(i).map(|f| f as &mut dyn crate::VectorFeatureMethods)
36✔
67
    }
36✔
68
}
69
impl ProtoRead for OpenVectorLayer {
70
    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
147✔
71
        match tag {
147✔
72
            1 => self.version = pb.read_varint::<u16>(),
21✔
73
            2 => {
74
                self.name = {
21✔
75
                    let mut cache = self.cache.borrow_mut();
21✔
76
                    cache.get_string(pb.read_varint())
21✔
77
                }
21✔
78
            }
79
            3 => self.extent = pb.read_varint::<Extent>(),
21✔
80
            4 => self.features.push(read_feature(
42✔
81
                pb.read_bytes(),
42✔
82
                self.extent,
42✔
83
                self.cache.clone(),
42✔
84
                &self.shape.clone().unwrap_or_default(),
42✔
85
                self.m_shape.clone().unwrap_or_default(),
42✔
86
            )),
42✔
87
            5 => {
88
                self.shape = {
21✔
89
                    let mut cache = self.cache.borrow_mut();
21✔
90
                    Some(decode_shape(pb.read_varint(), &mut cache))
21✔
91
                }
21✔
92
            }
93
            6 => {
94
                self.m_shape = {
21✔
95
                    let mut cache: core::cell::RefMut<ColumnCacheReader> = self.cache.borrow_mut();
21✔
96
                    Some(decode_shape(pb.read_varint(), &mut cache))
21✔
97
                }
21✔
98
            }
99
            #[tarpaulin::skip]
100
            _ => panic!("unknown tag: {}", tag),
×
101
        }
102
    }
147✔
103
}
104

105
/// Write the layer to a protobuf
106
pub fn write_layer(layer: &mut BaseVectorLayer, cache: &mut ColumnCacheWriter) -> Vec<u8> {
21✔
107
    let mut pbf = Protobuf::new();
21✔
108

21✔
109
    pbf.write_varint_field(1, layer.version);
21✔
110
    pbf.write_varint_field(2, cache.add_string(layer.name.clone()));
21✔
111
    pbf.write_varint_field(3, layer.extent);
21✔
112
    pbf.write_varint_field(5, encode_shape(&layer.shape, cache));
21✔
113
    if let Some(ref m_shape) = layer.m_shape {
21✔
114
        pbf.write_varint_field(6, encode_shape(m_shape, cache));
21✔
115
    }
21✔
116

117
    // sort by feature type
118
    layer.features.sort_by_key(|a| a.get_type());
42✔
119

120
    for feature in &layer.features {
63✔
121
        pbf.write_bytes_field(
42✔
122
            4,
42✔
123
            &write_feature(feature, &layer.shape, layer.m_shape.as_ref(), cache),
42✔
124
        );
42✔
125
    }
42✔
126

127
    pbf.take()
21✔
128
}
21✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc