open_vector_tile/mapbox/
vector_tile.rs1use crate::{
2 base::BaseVectorTile,
3 mapbox::{MapboxVectorLayer, write_layer},
4};
5use alloc::{collections::BTreeMap, rc::Rc, string::String, vec::Vec};
6use core::cell::RefCell;
7use pbf::{ProtoRead, Protobuf};
8
9#[derive(Debug)]
11pub struct MapboxVectorTile {
12 pub layers: BTreeMap<String, MapboxVectorLayer>,
14 pbf: Rc<RefCell<Protobuf>>,
16}
17impl MapboxVectorTile {
18 pub fn new(data: Vec<u8>, end: Option<usize>) -> Self {
20 let pbf = Rc::new(RefCell::new(data.into()));
21 let mut vt = MapboxVectorTile { pbf: pbf.clone(), layers: BTreeMap::new() };
22
23 let mut tmp_pbf = pbf.borrow_mut();
24 tmp_pbf.read_fields(&mut vt, end);
25
26 vt
27 }
28
29 pub fn layer(&mut self, name: &str) -> Option<&mut MapboxVectorLayer> {
31 self.layers.get_mut(name)
32 }
33}
34impl ProtoRead for MapboxVectorTile {
35 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
36 match tag {
37 1 | 3 => {
38 let mut layer = MapboxVectorLayer::new(self.pbf.clone(), tag == 1);
39 pb.read_message(&mut layer);
40 self.layers.insert(layer.name.clone(), layer);
41 }
42 _ => panic!("unknown tag: {}", tag),
43 }
44 }
45}
46
47pub fn write_tile(tile: &mut BaseVectorTile, mapbox_support: bool) -> Vec<u8> {
49 let mut pbf = Protobuf::new();
50
51 for layer in tile.layers.values() {
53 pbf.write_bytes_field(
54 if mapbox_support { 3 } else { 1 },
55 &write_layer(layer, mapbox_support),
56 );
57 }
58
59 pbf.take()
60}