-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoint.rs
95 lines (85 loc) · 2.26 KB
/
point.rs
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
use crate::common::WKBDimension;
use crate::reader::coord::Coord;
use crate::Endianness;
use geo_traits::Dimensions;
use geo_traits::{CoordTrait, PointTrait};
/// A WKB Point.
///
/// This has been preprocessed, so access to any internal coordinate is `O(1)`.
///
/// See page 66 of <https://portal.ogc.org/files/?artifact_id=25355>.
#[derive(Debug, Clone, Copy)]
pub struct Point<'a> {
/// The coordinate inside this Point
coord: Coord<'a>,
dim: WKBDimension,
is_empty: bool,
}
impl<'a> Point<'a> {
pub fn new(buf: &'a [u8], byte_order: Endianness, offset: u64, dim: WKBDimension) -> Self {
// The space of the byte order + geometry type
let offset = offset + 5;
let coord = Coord::new(buf, byte_order, offset, dim);
let is_empty = (0..coord.dim().size()).all(|coord_dim| {
{
// Safety:
// We just checked the number of dimensions, and coord_dim is less than
// coord.dim().size()
unsafe { coord.nth_unchecked(coord_dim) }
}
.is_nan()
});
Self {
coord,
dim,
is_empty,
}
}
/// The number of bytes in this object, including any header
///
/// Note that this is not the same as the length of the underlying buffer
pub fn size(&self) -> u64 {
// - 1: byteOrder
// - 4: wkbType
// - 4: numPoints
// - dim size * 8: two f64s
1 + 4 + (self.dim.size() as u64 * 8)
}
pub fn dimension(&self) -> WKBDimension {
self.dim
}
}
impl<'a> PointTrait for Point<'a> {
type T = f64;
type CoordType<'b>
= Coord<'a>
where
Self: 'b;
fn dim(&self) -> Dimensions {
self.dim.into()
}
fn coord(&self) -> Option<Self::CoordType<'_>> {
if self.is_empty {
None
} else {
Some(self.coord)
}
}
}
impl<'a> PointTrait for &Point<'a> {
type T = f64;
type CoordType<'b>
= Coord<'a>
where
Self: 'b;
fn dim(&self) -> Dimensions {
self.dim.into()
}
fn coord(&self) -> Option<Self::CoordType<'_>> {
if self.is_empty {
None
} else {
Some(self.coord)
}
}
}