open-vector-tile 1.11.1

This library reads/writes The Open Vector Tiles 1.0 Specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use super::decode_value;
use crate::{
    Point, Point3D, VectorFeatureMethods, VectorGeometry, VectorLine3DWithOffset,
    VectorLineWithOffset, VectorLines3DWithOffset, VectorLinesWithOffset, VectorPoints,
    VectorPoints3D,
    base::{BaseVectorFeature, TessellationWrapper, decode_offset},
    mapbox::FeatureType as MapboxFeatureType,
    open::{ColumnCacheReader, ColumnCacheWriter, encode_value},
    unweave_2d, unweave_3d, zagzig,
};
use alloc::{rc::Rc, vec, vec::Vec};
use core::cell::RefCell;
use pbf::{BitCast, Protobuf};
use s2json::{BBOX, Properties, Shape};
use serde::{Deserialize, Serialize};

/// Extent guide for how the geometry data is stored
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Extent {
    /// 512x512
    Extent512 = 512,
    /// 1024x1024
    Extent1024 = 1_024,
    /// 2048x2048
    Extent2048 = 2_048,
    /// 4096x4096 (default)
    #[default]
    Extent4096 = 4_096,
    /// 8_192x8_192
    Extent8192 = 8_192,
    /// 16_384x16_384
    Extent16384 = 16_384,
}
impl BitCast for Extent {
    fn to_u64(&self) -> u64 {
        match self {
            Extent::Extent512 => 0,
            Extent::Extent1024 => 1,
            Extent::Extent2048 => 2,
            Extent::Extent4096 => 3,
            Extent::Extent8192 => 4,
            Extent::Extent16384 => 5,
        }
    }

    fn from_u64(value: u64) -> Self {
        match value {
            1 => Extent::Extent1024,
            2 => Extent::Extent2048,
            3 => Extent::Extent4096,
            4 => Extent::Extent8192,
            5 => Extent::Extent16384,
            _ => Extent::Extent512,
        }
    }
}
impl From<usize> for Extent {
    fn from(extent: usize) -> Self {
        match extent {
            512 => Extent::Extent512,
            1_024 => Extent::Extent1024,
            2_048 => Extent::Extent2048,
            4_096 => Extent::Extent4096,
            8_192 => Extent::Extent8192,
            16_384 => Extent::Extent16384,
            _ => Extent::Extent512,
        }
    }
}
impl From<Extent> for usize {
    fn from(extent: Extent) -> Self {
        extent as i32 as usize
    }
}
impl From<Extent> for f64 {
    fn from(extent: Extent) -> Self {
        extent as i32 as f64
    }
}

/// Open Vector Tile Feature types.
#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub enum FeatureType {
    /// Points Feature
    #[default]
    Points = 1,
    /// Lines Feature
    Lines = 2,
    /// Polygons Feature
    Polygons = 3,
    /// Points3D Feature
    Points3D = 4,
    /// Lines3D Feature
    Lines3D = 5,
    /// Polygons3D Feature
    Polygons3D = 6,
}
impl BitCast for FeatureType {
    fn to_u64(&self) -> u64 {
        (*self) as u64
    }
    fn from_u64(value: u64) -> Self {
        match value {
            1 => FeatureType::Points,
            2 => FeatureType::Lines,
            3 => FeatureType::Polygons,
            4 => FeatureType::Points3D,
            5 => FeatureType::Lines3D,
            6 => FeatureType::Polygons3D,
            _ => panic!("unknown value: {}", value),
        }
    }
}
impl From<&MapboxFeatureType> for FeatureType {
    fn from(mft: &MapboxFeatureType) -> Self {
        match mft {
            MapboxFeatureType::Point => FeatureType::Points,
            MapboxFeatureType::Line => FeatureType::Lines,
            MapboxFeatureType::Polygon => FeatureType::Polygons,
            MapboxFeatureType::MultiPolygon => FeatureType::Polygons,
        }
    }
}

/// Open Vector Tile Feature specification
#[derive(Debug)]
pub struct OpenVectorFeature {
    /// the id of the feature
    pub id: Option<u64>,
    /// the properties of the feature
    pub properties: Properties,
    /// the type of the feature
    pub r#type: FeatureType,
    cache: Rc<RefCell<ColumnCacheReader>>,
    m_shape: Shape,
    extent: Extent,
    geometry_indices: Vec<u32>,
    geometry: Option<VectorGeometry>,
    single: bool,
    bbox_index: Option<usize>,
    has_offsets: bool,
    has_m_values: bool,
    indices_index: Option<usize>,
    tessellation_index: Option<usize>,
}
impl OpenVectorFeature {
    fn _load_geometry_points(&mut self) -> VectorPoints {
        let mut cache = self.cache.borrow_mut();

        let mut index_pos = 0;
        let geometry_index = self.geometry_indices[index_pos];
        index_pos += 1;
        if self.single {
            let (a, b) = unweave_2d(geometry_index);
            vec![Point::new(zagzig(a as u32), zagzig(b as u32))]
        } else {
            let mut geometry = cache.get_points(geometry_index as usize);

            if self.has_m_values {
                let length = geometry.len();
                geometry.iter_mut().take(length).for_each(|p| {
                    let value_index = self.geometry_indices[index_pos];
                    p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                    index_pos += 1;
                });
            }

            geometry
        }
    }

    fn _load_geometry_points_3d(&mut self) -> VectorPoints3D {
        let mut cache = self.cache.borrow_mut();

        let mut index_pos = 0;
        let geometry_index = self.geometry_indices[index_pos];
        index_pos += 1;
        if self.single {
            let (a, b, c) = unweave_3d(geometry_index as u64);
            vec![Point3D::new(zagzig(a), zagzig(b), zagzig(c))]
        } else {
            let mut geometry = cache.get_points_3d(geometry_index as usize);

            if self.has_m_values {
                let length = geometry.len();
                geometry.iter_mut().take(length).for_each(|p| {
                    let value_index = self.geometry_indices[index_pos];
                    p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                    index_pos += 1;
                });
            }

            geometry
        }
    }

    fn _load_geometry_lines(&mut self) -> VectorLinesWithOffset {
        let mut cache = self.cache.borrow_mut();

        let mut res: VectorLinesWithOffset = vec![];

        let mut index_pos = 0;
        let mut line_count = 1;
        if !self.single {
            line_count = self.geometry_indices[index_pos];
            index_pos += 1;
        };
        for _ in 0..line_count {
            // get offset if it exists
            let mut offset = 0.0;
            if self.has_offsets {
                offset = decode_offset(self.geometry_indices[index_pos]);
                index_pos += 1;
            }
            // get geometry
            let mut geometry = cache.get_points(self.geometry_indices[index_pos] as usize);
            index_pos += 1;
            // inject m values if they exist
            if self.has_m_values {
                let length = geometry.len();
                geometry.iter_mut().take(length).for_each(|p| {
                    let value_index = self.geometry_indices[index_pos];
                    p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                    index_pos += 1;
                });
            }
            res.push(VectorLineWithOffset::new(offset, geometry));
        }

        res
    }

    fn _load_geometry_lines_3d(&mut self) -> VectorLines3DWithOffset {
        let mut cache = self.cache.borrow_mut();

        let mut res: VectorLines3DWithOffset = vec![];

        let mut index_pos = 0;
        let mut line_count = 1;
        if !self.single {
            line_count = self.geometry_indices[index_pos];
            index_pos += 1;
        };
        for _ in 0..line_count {
            // get offset if it exists
            let mut offset = 0.0;
            if self.has_offsets {
                offset = decode_offset(self.geometry_indices[index_pos]);
                index_pos += 1;
            }
            // get geometry
            let mut geometry = cache.get_points_3d(self.geometry_indices[index_pos] as usize);
            index_pos += 1;
            // inject m values if they exist
            if self.has_m_values {
                let length = geometry.len();
                geometry.iter_mut().take(length).for_each(|p| {
                    let value_index = self.geometry_indices[index_pos];
                    p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                    index_pos += 1;
                });
            }
            res.push(VectorLine3DWithOffset::new(offset, geometry));
        }

        res
    }

    fn _load_geometry_polys(&mut self) -> Vec<VectorLinesWithOffset> {
        let mut cache = self.cache.borrow_mut();

        let mut res: Vec<VectorLinesWithOffset> = vec![];

        let mut index_pos = 0;
        let mut poly_count = 1;
        if !self.single {
            poly_count = self.geometry_indices[index_pos];
            index_pos += 1;
        };
        for _ in 0..poly_count {
            let line_count = self.geometry_indices[index_pos];
            index_pos += 1;
            let mut lines: VectorLinesWithOffset = vec![];
            for _ in 0..line_count {
                // get offset if it exists
                let mut offset = 0.0;
                if self.has_offsets {
                    offset = decode_offset(self.geometry_indices[index_pos]);
                    index_pos += 1;
                }
                // get geometry
                let mut geometry = cache.get_points(self.geometry_indices[index_pos] as usize);
                index_pos += 1;
                // inject m values if they exist
                if self.has_m_values {
                    let length = geometry.len();
                    geometry.iter_mut().take(length).for_each(|p| {
                        let value_index = self.geometry_indices[index_pos];
                        p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                        index_pos += 1;
                    });
                }
                lines.push(VectorLineWithOffset::new(offset, geometry));
            }
            res.push(lines);
        }

        res
    }

    fn _load_geometry_polys_3d(&mut self) -> Vec<VectorLines3DWithOffset> {
        let mut cache = self.cache.borrow_mut();

        let mut res: Vec<VectorLines3DWithOffset> = vec![];

        let mut index_pos = 0;
        let mut poly_count = 1;
        if !self.single {
            poly_count = self.geometry_indices[index_pos];
            index_pos += 1;
        };
        for _ in 0..poly_count {
            let line_count = self.geometry_indices[index_pos];
            index_pos += 1;
            let mut lines: VectorLines3DWithOffset = vec![];
            for _ in 0..line_count {
                // get offset if it exists
                let mut offset = 0.0;
                if self.has_offsets {
                    offset = decode_offset(self.geometry_indices[index_pos]);
                    index_pos += 1;
                }
                // get geometry
                let mut geometry = cache.get_points_3d(self.geometry_indices[index_pos] as usize);
                index_pos += 1;
                // inject m values if they exist
                if self.has_m_values {
                    let length = geometry.len();
                    geometry.iter_mut().take(length).for_each(|p| {
                        let value_index = self.geometry_indices[index_pos];
                        p.m = Some(decode_value(value_index as usize, &self.m_shape, &mut cache));
                        index_pos += 1;
                    });
                }
                lines.push(VectorLine3DWithOffset::new(offset, geometry));
            }
            res.push(lines);
        }

        res
    }
}
impl VectorFeatureMethods for OpenVectorFeature {
    /// get the id of the feature
    fn id(&self) -> Option<u64> {
        self.id
    }

    /// get the version of the feature
    fn version(&self) -> u16 {
        1
    }

    /// get the extent of the feature
    fn extent(&self) -> usize {
        self.extent.into()
    }

    fn properties(&self) -> Properties {
        self.properties.clone()
    }

    /// Create a new OpenVectorFeature
    fn get_type(&self) -> FeatureType {
        self.r#type
    }

    /// get the bbox of the feature
    fn bbox(&self) -> Option<BBOX> {
        if let Some(index) = self.bbox_index {
            let mut cache = self.cache.borrow_mut();
            Some(cache.get_bbox(index))
        } else {
            None
        }
    }

    /// whether the feature has m values
    fn has_m_values(&self) -> bool {
        self.has_m_values
    }

    /// whether the feature is a points type
    fn is_points(&self) -> bool {
        self.r#type == FeatureType::Points
    }

    /// whether the feature is a line type
    fn is_lines(&self) -> bool {
        self.r#type == FeatureType::Lines
    }

    /// whether the feature is a polygon type
    fn is_polygons(&self) -> bool {
        self.r#type == FeatureType::Polygons
    }

    /// whether the feature is a points 3D type
    fn is_points_3d(&self) -> bool {
        self.r#type == FeatureType::Points3D
    }

    /// whether the feature is a line 3D type
    fn is_lines_3d(&self) -> bool {
        self.r#type == FeatureType::Lines3D
    }

    /// whether the feature is a polygon 3D type
    fn is_polygons_3d(&self) -> bool {
        self.r#type == FeatureType::Polygons3D
    }

    /// regardless of the type, we return a flattend point array
    fn load_points(&mut self) -> VectorPoints {
        match self.load_geometry() {
            VectorGeometry::VectorPoints(p) => p,
            VectorGeometry::VectorLines(lines) => {
                lines.iter().flat_map(|p| p.geometry.clone()).collect()
            }
            VectorGeometry::VectorPolys(polys) => polys
                .iter()
                .flat_map(|p| p.iter().flat_map(|p| p.geometry[..p.geometry.len() - 1].to_vec()))
                .collect(),
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// regardless of the type, we return a flattend point array
    fn load_points_3d(&mut self) -> VectorPoints3D {
        match self.load_geometry() {
            VectorGeometry::VectorPoints3D(p) => p,
            VectorGeometry::VectorLines3D(lines) => {
                lines.iter().flat_map(|p| p.geometry.clone()).collect()
            }
            VectorGeometry::VectorPolys3D(polys) => polys
                .iter()
                .flat_map(|p| p.iter().flat_map(|p| p.geometry[..p.geometry.len() - 1].to_vec()))
                .collect(),
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// an array of lines. The offsets will be set to 0
    fn load_lines(&mut self) -> VectorLinesWithOffset {
        match self.load_geometry() {
            VectorGeometry::VectorLines(lines) => lines,
            VectorGeometry::VectorPolys(polys) => polys.iter().flat_map(|p| p.clone()).collect(),
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// an array of lines. The offsets will be set to 0
    fn load_lines_3d(&mut self) -> VectorLines3DWithOffset {
        match self.load_geometry() {
            VectorGeometry::VectorLines3D(lines) => lines,
            VectorGeometry::VectorPolys3D(polys) => polys.iter().flat_map(|p| p.clone()).collect(),
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// an array of polys
    fn load_polys(&mut self) -> Vec<VectorLinesWithOffset> {
        match self.load_geometry() {
            VectorGeometry::VectorPolys(polys) => polys,
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// an array of 3d polys
    fn load_polys_3d(&mut self) -> Vec<VectorLines3DWithOffset> {
        match self.load_geometry() {
            VectorGeometry::VectorPolys3D(polys) => polys,
            _ => {
                panic!("unexpected geometry type")
            }
        }
    }

    /// returns the indices of the geometry
    fn read_indices(&mut self) -> Vec<u32> {
        if self.indices_index.is_none() {
            return vec![];
        }
        let mut cache = self.cache.borrow_mut();
        cache.get_indices(self.indices_index.unwrap())
    }

    /// Add tessellation data to the geometry
    fn add_tessellation(&mut self, geometry: &mut Vec<f64>, multiplier: f64) {
        let Some(tessellation_index) = self.tessellation_index else {
            return;
        };
        let mut cache = self.cache.borrow_mut();
        let data = cache.get_points(tessellation_index);
        for point in data {
            geometry.push(point.x as f64 * multiplier);
            geometry.push(point.y as f64 * multiplier);
        }
    }

    /// Add 3D tessellation data to the geometry
    fn add_tessellation_3d(&mut self, geometry: &mut Vec<f64>, multiplier: f64) {
        let Some(tessellation_index) = self.tessellation_index else {
            return;
        };
        let mut cache = self.cache.borrow_mut();
        let data = cache.get_points_3d(tessellation_index);
        for point in data {
            geometry.push(point.x as f64 * multiplier);
            geometry.push(point.y as f64 * multiplier);
            geometry.push(point.z as f64 * multiplier);
        }
    }

    /// (flattened geometry & tesslation if applicable, indices)
    fn load_geometry_flat(&mut self) -> (Vec<f64>, Vec<u32>) {
        // build a multiplier
        let multiplier: f64 = 1.0 / f64::from(self.extent);
        // grab the geometry, flatten it, and mutate to an f64
        let geometry: Vec<f64> = match self.load_geometry() {
            VectorGeometry::VectorPolys(polys) => {
                let mut geo = polys
                    .iter()
                    .flat_map(|p| {
                        p.iter().flat_map(|p| {
                            p.geometry.clone().into_iter().flat_map(|p| {
                                vec![p.x as f64 * multiplier, p.y as f64 * multiplier]
                            })
                        })
                    })
                    .collect();
                self.add_tessellation(&mut geo, multiplier);
                geo
            }
            VectorGeometry::VectorPolys3D(polys) => {
                let mut geo = polys
                    .iter()
                    .flat_map(|p| {
                        p.iter().flat_map(|p| {
                            p.geometry.clone().into_iter().flat_map(|p| {
                                vec![p.x as f64 * multiplier, p.y as f64 * multiplier]
                            })
                        })
                    })
                    .collect();
                self.add_tessellation_3d(&mut geo, multiplier);
                geo
            }
            _ => {
                panic!("unexpected geometry type")
            }
        };
        // if a poly, check if we should load indices
        let indices = self.read_indices();

        (geometry, indices)
    }

    /// load the geometry
    fn load_geometry(&mut self) -> VectorGeometry {
        if let Some(geometry) = &self.geometry {
            return geometry.clone();
        }

        self.geometry = Some(match self.r#type {
            FeatureType::Points => VectorGeometry::VectorPoints(self._load_geometry_points()),
            FeatureType::Points3D => {
                VectorGeometry::VectorPoints3D(self._load_geometry_points_3d())
            }
            FeatureType::Lines => VectorGeometry::VectorLines(self._load_geometry_lines()),
            FeatureType::Lines3D => VectorGeometry::VectorLines3D(self._load_geometry_lines_3d()),
            FeatureType::Polygons => VectorGeometry::VectorPolys(self._load_geometry_polys()),
            FeatureType::Polygons3D => {
                VectorGeometry::VectorPolys3D(self._load_geometry_polys_3d())
            }
        });

        self.load_geometry()
    }
}

/// Read a single feature given the encoded data
pub fn read_feature(
    data: Vec<u8>,
    extent: Extent,
    cache: Rc<RefCell<ColumnCacheReader>>,
    shape: &Shape,
    m_shape: Shape,
) -> OpenVectorFeature {
    let mut pbf = Protobuf::from_input(data);
    // pull in the type
    let r#type: FeatureType = pbf.read_varint();
    // next the flags
    let flags: u8 = pbf.read_varint();
    // read the id if it exists
    let id: Option<u64> = if flags & 1 > 0 { Some(pbf.read_varint()) } else { None };
    let has_bbox = flags & (1 << 1) > 0;
    let has_offsets = (flags & (1 << 2)) > 0;
    let has_indices = flags & (1 << 3) > 0;
    let has_tessellation = flags & (1 << 4) > 0;
    let has_m_values = flags & (1 << 5) > 0;
    let single = flags & (1 << 6) > 0;
    // read the properties
    let value_index: usize = pbf.read_varint();
    let properties = decode_value(value_index, shape, &mut cache.borrow_mut());
    // if type is 1 or 4, read geometry as a single index, otherwise as an array
    let mut geometry_indices: Vec<u32> = vec![];
    let mut indices_index: Option<usize> = None;
    let mut tessellation_index: Option<usize> = None;
    if r#type == FeatureType::Points || r#type == FeatureType::Points3D {
        if single {
            geometry_indices.push(pbf.read_varint())
        } else {
            geometry_indices = cache.borrow_mut().get_indices(pbf.read_varint());
        }
    } else {
        geometry_indices = cache.borrow_mut().get_indices(pbf.read_varint());
    }
    // read indices and tessellation if they exist
    if r#type == FeatureType::Polygons || r#type == FeatureType::Polygons3D {
        if has_indices {
            indices_index = Some(pbf.read_varint());
        }
        if has_tessellation {
            tessellation_index = Some(pbf.read_varint());
        }
    }
    let bbox_index = if has_bbox { Some(pbf.read_varint()) } else { None };

    OpenVectorFeature {
        id,
        properties,
        r#type,
        cache,
        m_shape,
        extent,
        geometry_indices,
        geometry: None,
        single,
        bbox_index,
        has_offsets,
        has_m_values,
        indices_index,
        tessellation_index,
    }
}

/// Write a single feature to the column cache and return the encoding indexes for lookup
pub fn write_feature(
    feature: &BaseVectorFeature,
    shape: &Shape,
    m_shape: Option<&Shape>,
    cache: &mut ColumnCacheWriter,
) -> Vec<u8> {
    // write id, type, properties, bbox, geometry, indices, tessellation, mValues
    let mut pbf = Protobuf::new();
    // type is just stored as a varint
    pbf.write_varint(feature.get_type());
    // store flags if each one exists or not into a single byte
    let id = feature.id();
    let has_id: bool = id.is_some();
    let indices = feature.indices();
    let has_indices = indices.is_some() && !indices.unwrap().is_empty();
    let tessellation = feature.tessellation();
    let has_tessellation = tessellation.is_some() && !tessellation.as_ref().unwrap().is_empty();
    let has_offsets = feature.has_offsets();
    let bbox = feature.bbox();
    let has_bbox = bbox.is_some();
    let has_m_values = feature.has_m_values();
    let single = feature.single();
    let mut flags: u8 = 0;
    if has_id {
        flags += 1;
    }
    if has_bbox {
        flags += 1 << 1;
    }
    if has_offsets {
        flags += 1 << 2;
    }
    if has_indices {
        flags += 1 << 3;
    }
    if has_tessellation {
        flags += 1 << 4;
    }
    if has_m_values {
        flags += 1 << 5;
    }
    if single {
        flags += 1 << 6;
    }
    pbf.write_varint(flags);
    // id is stored in unsigned column
    if has_id {
        pbf.write_varint(id.unwrap());
    }
    // index to values column
    let value_index = encode_value(feature.properties(), shape, cache);
    pbf.write_varint(value_index);
    // geometry
    let stored_geo = feature.encode_to_cache(cache, m_shape);
    pbf.write_varint(stored_geo);
    // indices
    if has_indices {
        pbf.write_varint(cache.add_indices(feature.indices().unwrap()));
    }
    // tessellation
    if has_tessellation {
        match tessellation.unwrap() {
            TessellationWrapper::Tessellation(t) => pbf.write_varint(cache.add_points(t)),
            TessellationWrapper::Tessellation3D(t) => pbf.write_varint(cache.add_points_3d(t)),
        }
    }
    // bbox is stored in double column.
    if has_bbox {
        pbf.write_varint(cache.add_bbox(bbox.unwrap()));
    }

    pbf.take()
}