open_vector_tile/
vector_tile.rs1use crate::{
2 VectorGeometry, VectorLines3DWithOffset, VectorLinesWithOffset, VectorPoints, VectorPoints3D,
3 base::BaseVectorTile,
4 mapbox::MapboxVectorLayer,
5 open::{
6 ColumnCacheReader, ColumnCacheWriter, FeatureType, GridData, ImageData, OpenVectorLayer,
7 write_layer,
8 },
9};
10use alloc::{collections::BTreeMap, rc::Rc, string::String, vec::Vec};
11use core::cell::RefCell;
12use pbf::{ProtoRead, Protobuf};
13use s2json::{BBOX, Properties};
14
15pub trait VectorFeatureMethods {
17 fn id(&self) -> Option<u64>;
19 fn version(&self) -> u16;
21 fn properties(&self) -> Properties;
23 fn extent(&self) -> usize;
25 fn get_type(&self) -> FeatureType;
27 fn bbox(&self) -> Option<BBOX>;
29 fn has_m_values(&self) -> bool;
31 fn is_points(&self) -> bool;
33 fn is_lines(&self) -> bool;
35 fn is_polygons(&self) -> bool;
37 fn is_points_3d(&self) -> bool;
39 fn is_lines_3d(&self) -> bool;
41 fn is_polygons_3d(&self) -> bool;
43 fn load_points(&mut self) -> VectorPoints;
45 fn load_points_3d(&mut self) -> VectorPoints3D;
47 fn load_lines(&mut self) -> VectorLinesWithOffset;
49 fn load_lines_3d(&mut self) -> VectorLines3DWithOffset;
51 fn load_geometry_flat(&mut self) -> (Vec<f64>, Vec<u32>);
53 fn load_geometry(&mut self) -> VectorGeometry;
55 fn read_indices(&mut self) -> Vec<u32>;
57 fn add_tessellation(&mut self, geometry: &mut Vec<f64>, multiplier: f64);
59 fn add_tessellation_3d(&mut self, geometry: &mut Vec<f64>, multiplier: f64);
61}
62
63pub trait VectorLayerMethods {
65 fn version(&self) -> u16;
67 fn name(&self) -> String;
69 fn extent(&self) -> usize;
72 fn feature(&mut self, i: usize) -> Option<&mut dyn VectorFeatureMethods>;
74 fn len(&self) -> usize;
76 fn is_empty(&self) -> bool;
78}
79
80#[derive(Debug)]
82pub enum VectorLayer {
83 Mapbox(MapboxVectorLayer),
85 Open(OpenVectorLayer),
87}
88impl VectorLayerMethods for VectorLayer {
89 fn version(&self) -> u16 {
90 match self {
91 VectorLayer::Mapbox(layer) => layer.version(),
92 VectorLayer::Open(layer) => layer.version(),
93 }
94 }
95
96 fn name(&self) -> String {
97 match self {
98 VectorLayer::Mapbox(layer) => layer.name(),
99 VectorLayer::Open(layer) => layer.name(),
100 }
101 }
102
103 fn extent(&self) -> usize {
104 match self {
105 VectorLayer::Mapbox(layer) => layer.extent(),
106 VectorLayer::Open(layer) => layer.extent(),
107 }
108 }
109
110 fn feature(&mut self, i: usize) -> Option<&mut dyn VectorFeatureMethods> {
111 match self {
112 VectorLayer::Mapbox(layer) => layer.feature(i),
113 VectorLayer::Open(layer) => layer.feature(i),
114 }
115 }
116
117 fn len(&self) -> usize {
118 match self {
119 VectorLayer::Mapbox(layer) => layer.len(),
120 VectorLayer::Open(layer) => layer.len(),
121 }
122 }
123
124 fn is_empty(&self) -> bool {
125 match self {
126 VectorLayer::Mapbox(layer) => layer.is_empty(),
127 VectorLayer::Open(layer) => layer.is_empty(),
128 }
129 }
130}
131
132#[derive(Debug)]
180pub struct VectorTile {
181 pub layers: BTreeMap<String, VectorLayer>,
183 layer_indexes: Vec<usize>,
186 pbf: Rc<RefCell<Protobuf>>,
188 columns: Option<Rc<RefCell<ColumnCacheReader>>>,
190 pub grids: BTreeMap<String, GridData>,
192 pub images: BTreeMap<String, ImageData>,
194}
195impl VectorTile {
196 pub fn new(data: Vec<u8>, end: Option<usize>) -> Self {
198 let pbf = Rc::new(RefCell::new(data.into()));
199 let mut vt = VectorTile {
200 pbf: pbf.clone(),
201 columns: None,
202 layer_indexes: Vec::new(),
203 layers: BTreeMap::new(),
204 grids: BTreeMap::new(),
205 images: BTreeMap::new(),
206 };
207
208 pbf.borrow_mut().read_fields(&mut vt, end);
209
210 if !vt.layer_indexes.is_empty() {
211 vt.read_layers();
212 }
213
214 vt
215 }
216
217 pub fn read_layers(&mut self) -> Option<()> {
219 let layer_indexes = self.layer_indexes.clone();
220 let mut tmp_pbf = self.pbf.borrow_mut();
221 let cache = self.columns.as_ref()?.clone();
222
223 for pos in layer_indexes {
224 tmp_pbf.set_pos(pos);
225 let mut layer = OpenVectorLayer::new(cache.clone());
226 tmp_pbf.read_message(&mut layer);
227 self.layers.insert(layer.name.clone(), VectorLayer::Open(layer));
228 }
229
230 Some(())
231 }
232
233 pub fn layer(&mut self, name: &str) -> Option<&mut VectorLayer> {
235 self.layers.get_mut(name)
236 }
237}
238impl ProtoRead for VectorTile {
239 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
240 match tag {
241 1 | 3 => {
242 let mut layer = MapboxVectorLayer::new(self.pbf.clone(), tag == 1);
243 pb.read_message(&mut layer);
244 self.layers.insert(layer.name.clone(), VectorLayer::Mapbox(layer));
245 }
246 4 => {
247 self.layer_indexes.push(pb.get_pos());
250 }
251 5 => {
252 let mut column_reader = ColumnCacheReader::new();
253 pb.read_message(&mut column_reader);
254 self.columns = Some(Rc::new(RefCell::new(column_reader)));
255 }
256 6 => {
257 let mut grid = GridData::default();
258 pb.read_message(&mut grid);
259 self.grids.insert(grid.name.clone(), grid);
260 }
261 7 => {
262 let mut image = ImageData::default();
263 pb.read_message(&mut image);
264 self.images.insert(image.name.clone(), image);
265 }
266 #[tarpaulin::skip]
267 _ => panic!("unknown tag: {}", tag),
268 }
269 }
270}
271
272pub fn write_tile(
274 tile: Option<&mut BaseVectorTile>,
275 images: Option<Vec<&ImageData>>,
276 grids: Option<Vec<&GridData>>,
277) -> Vec<u8> {
278 let mut pbf = Protobuf::new();
279 let mut cache = ColumnCacheWriter::default();
280
281 if let Some(tile) = tile {
283 for layer in tile.layers.values_mut() {
284 pbf.write_bytes_field(4, &write_layer(layer, &mut cache));
285 }
286 pbf.write_message(5, &cache);
288 }
289 if let Some(grids) = grids {
291 for grid in grids.iter() {
292 pbf.write_message(6, *grid);
293 }
294 }
295 if let Some(images) = images {
297 for image in images.iter() {
298 pbf.write_message(7, *image);
299 }
300 }
301
302 pbf.take()
303}