open_vector_tile/
geometry.rs1use alloc::vec::Vec;
2use core::cmp::Ordering;
3use libm::round;
4use s2json::{LineStringMValues, MValue, MValueCompatible, VectorPoint};
5
6#[derive(Debug, Clone, PartialEq)]
9pub struct Point {
10 pub x: i32,
12 pub y: i32,
14 pub m: Option<MValue>,
16}
17impl Point {
18 pub fn new(x: i32, y: i32) -> Point {
20 Point { x, y, m: None }
21 }
22
23 pub fn new_with_m(x: i32, y: i32, m: MValue) -> Point {
25 Point { x, y, m: Some(m) }
26 }
27}
28impl<D: MValueCompatible> From<&VectorPoint<D>> for Point {
29 fn from(vp: &VectorPoint<D>) -> Self {
30 Point { x: round(vp.x) as i32, y: round(vp.y) as i32, m: vp.m.clone().map(|m| m.into()) }
31 }
32}
33impl PartialOrd for Point {
34 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35 Some(core::cmp::Ord::cmp(self, other))
37 }
38}
39impl Eq for Point {}
40impl Ord for Point {
41 fn cmp(&self, other: &Self) -> Ordering {
42 self.x.cmp(&other.x).then(self.y.cmp(&other.y))
44 }
45}
46#[derive(Debug, Clone, PartialEq)]
49pub struct Point3D {
50 pub x: i32,
52 pub y: i32,
54 pub z: i32,
56 pub m: Option<MValue>,
58}
59impl Point3D {
60 pub fn new(x: i32, y: i32, z: i32) -> Point3D {
62 Point3D { x, y, z, m: None }
63 }
64
65 pub fn new_with_m(x: i32, y: i32, z: i32, m: MValue) -> Point3D {
67 Point3D { x, y, z, m: Some(m) }
68 }
69}
70impl<D: MValueCompatible> From<&VectorPoint<D>> for Point3D {
71 fn from(vp: &VectorPoint<D>) -> Self {
72 Point3D {
73 x: round(vp.x) as i32,
74 y: round(vp.y) as i32,
75 z: round(vp.z.unwrap_or_default()) as i32,
76 m: vp.m.clone().map(|m| m.into()),
77 }
78 }
79}
80impl PartialOrd for Point3D {
81 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
82 Some(core::cmp::Ord::cmp(self, other))
84 }
85}
86impl Eq for Point3D {}
87impl Ord for Point3D {
88 fn cmp(&self, other: &Self) -> Ordering {
89 self.x.cmp(&other.x).then(self.y.cmp(&other.y)).then(self.z.cmp(&other.z))
91 }
92}
93
94#[derive(Debug, Clone, PartialEq)]
96pub struct VectorLineWithOffset {
97 pub offset: f64,
99 pub geometry: VectorLine,
101}
102impl From<&[Point]> for VectorLineWithOffset {
103 fn from(p: &[Point]) -> Self {
104 Self { offset: 0.0, geometry: p.to_vec() }
105 }
106}
107impl VectorLineWithOffset {
108 pub fn new(offset: f64, geometry: VectorLine) -> Self {
110 Self { offset, geometry }
111 }
112
113 pub fn has_offset(&self) -> bool {
115 self.offset != 0.0
116 }
117
118 pub fn has_m_values(&self) -> bool {
120 self.geometry.iter().any(|p| p.m.is_some())
121 }
122
123 pub fn m_values(&self) -> Option<LineStringMValues> {
125 if !self.has_m_values() {
126 return None;
127 }
128 Some(self.geometry.iter().map(|p| p.m.clone().unwrap_or_default()).collect())
129 }
130}
131pub type VectorLinesWithOffset = Vec<VectorLineWithOffset>;
133
134#[derive(Debug, Clone, PartialEq)]
136pub struct VectorLine3DWithOffset {
137 pub offset: f64,
139 pub geometry: VectorLine3D,
141}
142impl VectorLine3DWithOffset {
143 pub fn new(offset: f64, geometry: VectorLine3D) -> Self {
145 Self { offset, geometry }
146 }
147
148 pub fn has_offset(&self) -> bool {
150 self.offset != 0.0
151 }
152
153 pub fn has_m_values(&self) -> bool {
155 self.geometry.iter().any(|p| p.m.is_some())
156 }
157
158 pub fn m_values(&self) -> Option<LineStringMValues> {
160 if !self.has_m_values() {
161 return None;
162 }
163 Some(self.geometry.iter().map(|p| p.m.clone().unwrap_or_default()).collect())
164 }
165}
166pub type VectorLines3DWithOffset = Vec<VectorLine3DWithOffset>;
168
169pub type VectorPoints = Vec<Point>;
171pub type VectorPoints3D = Vec<Point3D>;
173pub type VectorLine = Vec<Point>;
175pub type VectorLine3D = Vec<Point3D>;
177pub type VectorLines = Vec<VectorLine>;
179pub type VectorLines3D = Vec<VectorLine3D>;
181pub type VectorPoly = Vec<VectorLine>;
183pub type VectorPoly3D = Vec<VectorLine3D>;
185pub type VectorMultiPoly = Vec<VectorPoly>;
187pub type VectorMultiPoly3D = Vec<VectorPoly3D>;
189#[derive(Debug, Clone, PartialEq)]
191pub enum VectorGeometry {
192 VectorPoints(VectorPoints),
194 VectorLines(VectorLinesWithOffset),
196 VectorPolys(Vec<VectorLinesWithOffset>),
198 VectorPoints3D(VectorPoints3D),
200 VectorLines3D(VectorLines3DWithOffset),
202 VectorPolys3D(Vec<VectorLines3DWithOffset>),
204}