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
use std::convert::TryFrom;
use std::ffi::CString;
use std::fmt::{self, Debug, Formatter};
use std::os::raw::c_int;
use std::str::FromStr;
use geo::LineString;
use serde::{Deserialize, Serialize};
use h3ron_h3_sys::H3Index;
use crate::index::Index;
use crate::to_geo::ToLineString;
use crate::{Error, FromH3Index, H3Cell, ToCoordinate};
#[derive(PartialOrd, PartialEq, Clone, Serialize, Deserialize, Hash, Eq, Ord, Copy)]
#[repr(transparent)]
pub struct H3Edge(H3Index);
impl Debug for H3Edge {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "H3Edge({})", self.to_string())
}
}
impl TryFrom<u64> for H3Edge {
type Error = Error;
fn try_from(h3index: H3Index) -> Result<Self, Self::Error> {
let index = Self::new(h3index);
index.validate()?;
Ok(index)
}
}
impl H3Edge {
pub fn is_edge_valid(&self) -> bool {
unsafe { h3ron_h3_sys::h3UnidirectionalEdgeIsValid(self.h3index()) != 0 }
}
pub fn edge_length_km(resolution: u8) -> f64 {
unsafe { h3ron_h3_sys::edgeLengthKm(resolution as c_int) }
}
pub fn edge_length_m(resolution: u8) -> f64 {
unsafe { h3ron_h3_sys::edgeLengthM(resolution as c_int) }
}
pub fn exact_length_km(&self) -> f64 {
unsafe { h3ron_h3_sys::exactEdgeLengthKm(self.h3index()) }
}
pub fn exact_length_m(&self) -> f64 {
unsafe { h3ron_h3_sys::exactEdgeLengthM(self.h3index()) }
}
pub fn exact_length_rads(&self) -> f64 {
unsafe { h3ron_h3_sys::exactEdgeLengthRads(self.h3index()) }
}
pub fn destination_index_unchecked(&self) -> H3Cell {
let index =
unsafe { h3ron_h3_sys::getDestinationH3IndexFromUnidirectionalEdge(self.h3index()) };
H3Cell::new(index)
}
pub fn destination_index(&self) -> Result<H3Cell, Error> {
let res = self.destination_index_unchecked();
res.validate()?;
Ok(res)
}
pub fn origin_index_unchecked(&self) -> H3Cell {
let index = unsafe { h3ron_h3_sys::getOriginH3IndexFromUnidirectionalEdge(self.h3index()) };
H3Cell::new(index)
}
pub fn origin_index(&self) -> Result<H3Cell, Error> {
let res = self.origin_index_unchecked();
res.validate()?;
Ok(res)
}
}
impl FromH3Index for H3Edge {
fn from_h3index(h3index: H3Index) -> Self {
H3Edge::new(h3index)
}
}
impl Index for H3Edge {
fn h3index(&self) -> H3Index {
self.0
}
fn new(h3index: H3Index) -> Self {
Self(h3index)
}
fn validate(&self) -> Result<(), Error> {
if !self.is_edge_valid() {
Err(Error::InvalidH3Edge(self.h3index()))
} else {
Ok(())
}
}
}
impl ToString for H3Edge {
fn to_string(&self) -> String {
format!("{:x}", self.0)
}
}
impl FromStr for H3Edge {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let h3index: H3Index = CString::new(s)
.map(|cs| unsafe { h3ron_h3_sys::stringToH3(cs.as_ptr()) })
.map_err(|_| Error::InvalidInput)?;
Self::try_from(h3index)
}
}
impl ToLineString for H3Edge {
fn to_linestring(&self) -> Result<LineString<f64>, Error> {
Ok(LineString::from(vec![
self.origin_index()?.to_coordinate(),
self.destination_index()?.to_coordinate(),
]))
}
fn to_linestring_unchecked(&self) -> LineString<f64> {
LineString::from(vec![
self.origin_index_unchecked().to_coordinate(),
self.destination_index_unchecked().to_coordinate(),
])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[should_panic(expected = "InvalidH3Edge")]
#[test]
fn checks_both_validity() {
let edge = H3Edge::new(0x149283080ddbffff);
assert!(edge.validate().is_ok());
let edge = H3Edge::new(0x89283080ddbffff_u64);
edge.validate().unwrap();
}
#[test]
fn can_find_parent() {
let edge = H3Edge::new(0x149283080ddbffff);
assert_eq!(edge.resolution(), 9);
let parent_8 = edge.get_parent(8).unwrap();
assert_eq!(parent_8.resolution(), 8);
assert!(edge.is_child_of(&parent_8));
let parent_1 = edge.get_parent(1).unwrap();
assert_eq!(parent_1.resolution(), 1);
assert!(edge.is_child_of(&parent_1));
}
#[test]
fn debug_hexadecimal() {
let edge = H3Edge::new(0x149283080ddbffff);
assert_eq!(
format!("{:?}", edge),
"H3Edge(149283080ddbffff)".to_string()
)
}
#[test]
fn to_linestring() {
let edge = H3Edge::new(0x149283080ddbffff);
let ls = edge.to_linestring().unwrap();
assert_eq!(ls.0.len(), 2);
assert_eq!(ls.0[0], edge.origin_index_unchecked().to_coordinate());
assert_eq!(ls.0[1], edge.destination_index_unchecked().to_coordinate());
}
}