Skip to main content

mlt_core/decoder/geometry/
model.rs

1use derive_debug::Dbg;
2use num_enum::TryFromPrimitive;
3use serde::{Deserialize, Serialize};
4
5use crate::decoder::RawStream;
6use crate::utils::formatter::{opt_vec_seq, vec_seq};
7use crate::{DecodeState, Lazy};
8
9/// Geometry column representation, parameterized by decode state.
10///
11/// - `Geometry<'a>` / `Geometry<'a, Lazy>` - either raw bytes or decoded, in an [`crate::LazyParsed`] enum.
12/// - `Geometry<'a, Parsed>` - decoded [`GeometryValues`] directly (no enum wrapper).
13pub type Geometry<'a, S = Lazy> = <S as DecodeState>::LazyOrParsed<RawGeometry<'a>, GeometryValues>;
14
15/// Raw geometry data as read directly from the tile (borrows from input bytes)
16#[derive(Debug, PartialEq, Clone)]
17pub struct RawGeometry<'a> {
18    pub(crate) types: GeoTypes<'a>,
19    pub(crate) items: Vec<RawStream<'a>>,
20}
21
22/// Where a geometry section's per-feature [`GeometryType`]s come from.
23///
24/// A v1 section always spells them out. A v2 section may instead name one type in
25/// its [layer header byte](crate::decoder::LayerHeader02), which is all a layer of
26/// a single geometry type has to say.
27#[derive(Debug, PartialEq, Clone)]
28pub enum GeoTypes<'a> {
29    /// The leading stream of the section, one value per feature.
30    Stream(RawStream<'a>),
31    /// No stream: every one of `feature_count` features has this type.
32    #[cfg(feature = "unstable-v2")]
33    Uniform {
34        geometry_type: GeometryType,
35        feature_count: u32,
36    },
37}
38
39/// Parsed (decoded) geometry data
40#[derive(Clone, Dbg, Default, PartialEq, Eq)]
41pub struct GeometryValues {
42    #[dbg(formatter = "vec_seq")]
43    pub(crate) vector_types: Vec<GeometryType>,
44    #[dbg(formatter = "opt_vec_seq")]
45    pub(crate) geometry_offsets: Option<Vec<u32>>,
46    #[dbg(formatter = "opt_vec_seq")]
47    pub(crate) part_offsets: Option<Vec<u32>>,
48    #[dbg(formatter = "opt_vec_seq")]
49    pub(crate) ring_offsets: Option<Vec<u32>>,
50    #[dbg(formatter = "opt_vec_seq")]
51    pub(crate) index_buffer: Option<Vec<u32>>,
52    #[dbg(formatter = "opt_vec_seq")]
53    pub(crate) triangles: Option<Vec<u32>>,
54    #[dbg(formatter = "opt_vec_seq")]
55    pub(crate) vertices: Option<Vec<i32>>,
56}
57
58/// Types of geometries supported in MLT
59#[derive(
60    Debug,
61    Clone,
62    Copy,
63    PartialEq,
64    PartialOrd,
65    Eq,
66    Hash,
67    Ord,
68    TryFromPrimitive,
69    strum::Display,
70    strum::IntoStaticStr,
71    Serialize,
72    Deserialize,
73)]
74#[repr(u8)]
75#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
76pub enum GeometryType {
77    /*
78        ATTENTION: Do not modify the order of this enum - it is being used in geometry decoding
79    */
80    Point,
81    LineString,
82    Polygon,
83    MultiPoint,
84    MultiLineString,
85    MultiPolygon,
86}