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

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

12 Aug 2024 09:02PM UTC coverage: 27.612% (+4.4%) from 23.175%
#17

push

Mr Martian
tests moving along

1915 of 2437 new or added lines in 17 files covered. (78.58%)

14 existing lines in 5 files now uncovered.

11082 of 40134 relevant lines covered (27.61%)

105.18 hits per line

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

0.0
/rust/vector_tile.rs
1
use pbf::{ProtoRead, Protobuf};
2

3
use alloc::collections::BTreeMap;
4
use alloc::rc::Rc;
5
use alloc::string::String;
6
use alloc::vec::Vec;
7

8
use core::cell::RefCell;
9

10
use crate::base::BaseVectorTile;
11
use crate::mapbox::MapboxVectorLayer;
12
use crate::open::{write_layer, ColumnCacheReader, ColumnCacheWriter, OpenVectorLayer};
13

14
/// Methods that all vector layers should have
15
pub trait VectorLayerMethods {
16
    /// the version of the vector tile layer.
17
    fn version(&self) -> u16;
18
    /// the name of the layer
19
    fn name(&self) -> String;
20
    /// the extent of the vector tile (only **512**, **1_024**, **2_048**, **4_096**, and **8_192**
21
    /// are supported for the open spec)
22
    fn extent(&self) -> usize;
23
}
24

25
/// Layer container supporting both mapbox and open vector layers
26
pub enum VectorLayer {
27
    /// Mapbox vector layer
28
    Mapbox(MapboxVectorLayer),
29
    /// Open vector layer
30
    Open(OpenVectorLayer),
31
}
32
impl VectorLayerMethods for VectorLayer {
33
    fn version(&self) -> u16 {
×
34
        match self {
×
35
            VectorLayer::Mapbox(layer) => layer.version(),
×
36
            VectorLayer::Open(layer) => layer.version(),
×
37
        }
38
    }
×
39

40
    fn name(&self) -> String {
×
41
        match self {
×
42
            VectorLayer::Mapbox(layer) => layer.name(),
×
43
            VectorLayer::Open(layer) => layer.name(),
×
44
        }
45
    }
×
46

47
    fn extent(&self) -> usize {
×
48
        match self {
×
49
            VectorLayer::Mapbox(layer) => layer.extent(),
×
50
            VectorLayer::Open(layer) => layer.extent(),
×
51
        }
52
    }
×
53
}
54

55
/// The vector tile struct that covers both "open" and "mapbox" specifications
56
pub struct VectorTile {
57
    /// the layers in the vector tile
58
    pub layers: BTreeMap<String, VectorLayer>,
59
    /// indexes to track the layers. Needed for the open spec because we need the cache before we can
60
    /// parse layers and features
61
    layer_indexes: Vec<usize>,
62
    /// the protobuf for the vector tile
63
    pbf: Rc<RefCell<Protobuf>>,
64
    /// the column cache
65
    columns: Option<Rc<RefCell<ColumnCacheReader>>>,
66
}
67
impl VectorTile {
68
    /// Create a new vector tile
NEW
69
    pub fn new(data: Vec<u8>, end: Option<usize>) -> Self {
×
NEW
70
        let pbf = Rc::new(RefCell::new(data.into()));
×
71
        let pbf_clone = pbf.clone();
×
72
        let mut vt = VectorTile {
×
73
            pbf,
×
74
            columns: None,
×
75
            layer_indexes: Vec::new(),
×
NEW
76
            layers: BTreeMap::new(),
×
77
        };
×
78

×
79
        let mut tmp_pbf = pbf_clone.borrow_mut();
×
80
        tmp_pbf.read_fields(&mut vt, end);
×
81

×
82
        vt.read_layers();
×
83

×
84
        vt
×
85
    }
×
86

87
    /// Read the layers
88
    pub fn read_layers(&mut self) -> Option<()> {
×
89
        let layer_indexes = self.layer_indexes.clone();
×
90
        let pbf_clone = self.pbf.clone();
×
91
        let mut tmp_pbf = pbf_clone.borrow_mut();
×
92
        let cache = self.columns.as_ref()?.clone();
×
93

94
        for pos in layer_indexes {
×
NEW
95
            tmp_pbf.set_pos(pos);
×
NEW
96
            let layer = OpenVectorLayer::new(self.pbf.clone(), cache.clone());
×
NEW
97
            self.layers
×
NEW
98
                .insert(layer.name.clone(), VectorLayer::Open(layer));
×
UNCOV
99
        }
×
100

101
        Some(())
×
102
    }
×
103
}
104
impl ProtoRead for VectorTile {
105
    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
×
106
        match tag {
×
107
            1 | 3 => {
×
108
                let layer = VectorLayer::Mapbox(MapboxVectorLayer::new(
×
109
                    self.pbf.clone(),
×
110
                    pb.read_varint::<usize>() + pb.get_pos(),
×
NEW
111
                    tag == 3,
×
112
                ));
×
113
                self.layers.insert(pb.read_string(), layer);
×
NEW
114
            }
×
115
            4 => {
×
116
                // store the position of each layer for later retrieval.
×
117
                // Columns must be prepped before reading the layer.
×
118
                self.layer_indexes.push(pb.get_pos());
×
NEW
119
            }
×
120
            5 => {
×
121
                // vectorTile.#columns = new ColumnCacheReader(pbf, pbf.readVarint() + pbf.pos);
×
122
                self.columns = Some(Rc::new(RefCell::new(ColumnCacheReader::new(
×
123
                    self.pbf.clone(),
×
NEW
124
                    pb.read_varint::<usize>() + pb.get_pos(),
×
125
                ))));
×
NEW
126
            }
×
127
            _ => panic!("unknown tag: {}", tag),
×
128
        }
129
    }
×
130
}
131

132
/// writer for converting a BaseVectorTile to encoded bytes of the Open Vector Tile format
133
pub fn write_tile(tile: &mut BaseVectorTile) -> Vec<u8> {
×
134
    let mut pbf = Protobuf::new();
×
135
    let mut cache = ColumnCacheWriter::default();
×
136

137
    // first write layers
138
    for layer in tile.layers.values_mut() {
×
139
        pbf.write_bytes_field(4, &write_layer(layer, &mut cache));
×
140
    }
×
141
    // now we can write columns
142
    pbf.write_message(5, &cache);
×
143

×
144
    pbf.take()
×
145
}
×
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