-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolygon.rs
123 lines (104 loc) · 3.22 KB
/
polygon.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
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
use std::io::Cursor;
use crate::common::WKBDimension;
use crate::reader::linearring::WKBLinearRing;
use crate::reader::util::ReadBytesExt;
use crate::Endianness;
use geo_traits::Dimensions;
use geo_traits::PolygonTrait;
/// skip endianness and wkb type
const HEADER_BYTES: u64 = 5;
/// A WKB Polygon
///
/// This has been preprocessed, so access to any internal coordinate is `O(1)`.
#[derive(Debug, Clone)]
pub struct Polygon<'a> {
wkb_linear_rings: Vec<WKBLinearRing<'a>>,
dim: WKBDimension,
}
impl<'a> Polygon<'a> {
pub fn new(buf: &'a [u8], byte_order: Endianness, offset: u64, dim: WKBDimension) -> Self {
let mut reader = Cursor::new(buf);
reader.set_position(HEADER_BYTES + offset);
let num_rings = reader.read_u32(byte_order).unwrap().try_into().unwrap();
// - existing offset into buffer
// - 1: byteOrder
// - 4: wkbType
// - 4: numLineStrings
let mut ring_offset = offset + 1 + 4 + 4;
let mut wkb_linear_rings = Vec::with_capacity(num_rings);
for _ in 0..num_rings {
let polygon = WKBLinearRing::new(buf, byte_order, ring_offset, dim);
wkb_linear_rings.push(polygon);
ring_offset += polygon.size();
}
Self {
wkb_linear_rings,
dim,
}
}
/// 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
// - size of each linear ring
self.wkb_linear_rings
.iter()
.fold(1 + 4 + 4, |acc, ring| acc + ring.size())
}
pub fn dimension(&self) -> WKBDimension {
self.dim
}
}
impl<'a> PolygonTrait for Polygon<'a> {
type T = f64;
type RingType<'b> = WKBLinearRing<'a>where Self: 'b;
fn dim(&self) -> Dimensions {
self.dim.into()
}
fn num_interiors(&self) -> usize {
// Support an empty polygon with no rings
if self.wkb_linear_rings.is_empty() {
0
} else {
self.wkb_linear_rings.len() - 1
}
}
fn exterior(&self) -> Option<Self::RingType<'_>> {
if self.wkb_linear_rings.is_empty() {
None
} else {
Some(self.wkb_linear_rings[0])
}
}
unsafe fn interior_unchecked(&self, i: usize) -> Self::RingType<'_> {
*self.wkb_linear_rings.get_unchecked(i + 1)
}
}
impl<'a> PolygonTrait for &'a Polygon<'a> {
type T = f64;
type RingType<'b> = WKBLinearRing<'a> where Self: 'b;
fn dim(&self) -> Dimensions {
self.dim.into()
}
fn num_interiors(&self) -> usize {
// Support an empty polygon with no rings
if self.wkb_linear_rings.is_empty() {
0
} else {
self.wkb_linear_rings.len() - 1
}
}
fn exterior(&self) -> Option<Self::RingType<'_>> {
if self.wkb_linear_rings.is_empty() {
None
} else {
Some(self.wkb_linear_rings[0])
}
}
unsafe fn interior_unchecked(&self, i: usize) -> Self::RingType<'_> {
*self.wkb_linear_rings.get_unchecked(i + 1)
}
}