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

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

03 May 2025 04:21AM UTC coverage: 98.815% (+0.1%) from 98.678%
#67

push

Mr Martian
migrate to stable

9510 of 9624 relevant lines covered (98.82%)

68.44 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 crate::{
2
    VectorLayerMethods,
3
    base::BaseVectorLayer,
4
    open::{
5
        ColumnCacheReader, ColumnCacheWriter, Extent, OpenVectorFeature, decode_shape,
6
        encode_shape, read_feature, write_feature,
7
    },
8
};
9
use alloc::{rc::Rc, string::String, vec::Vec};
10
use core::cell::RefCell;
11
use pbf::{ProtoRead, Protobuf};
12
use s2json::Shape;
13

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

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

21✔
104
    pbf.write_varint_field(1, layer.version);
21✔
105
    pbf.write_varint_field(2, cache.add_string(layer.name.clone()));
21✔
106
    pbf.write_varint_field(3, layer.extent);
21✔
107
    pbf.write_varint_field(5, encode_shape(&layer.shape, cache));
21✔
108
    if let Some(ref m_shape) = layer.m_shape {
21✔
109
        pbf.write_varint_field(6, encode_shape(m_shape, cache));
21✔
110
    }
21✔
111

112
    // sort by feature type
113
    layer.features.sort_by_key(|a| a.get_type());
42✔
114

115
    for feature in &layer.features {
63✔
116
        pbf.write_bytes_field(
42✔
117
            4,
42✔
118
            &write_feature(feature, &layer.shape, layer.m_shape.as_ref(), cache),
42✔
119
        );
42✔
120
    }
42✔
121

122
    pbf.take()
21✔
123
}
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