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
use geo::{
GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Rect,
Triangle,
};
use geo_types::{Coordinate, Geometry, Line, Polygon};
use crate::error::check_valid_h3_resolution;
use crate::{line, polyfill, Error, Index};
pub trait ToH3Indexes {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error>;
}
impl ToH3Indexes for Polygon<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
check_valid_h3_resolution(h3_resolution)?;
let mut indexes = polyfill(&self, h3_resolution);
Ok(indexes.drain(..).map(Index::new).collect())
}
}
impl ToH3Indexes for MultiPolygon<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
let mut outvec = vec![];
for poly in self.0.iter() {
let mut thisvec = poly.to_h3_indexes(h3_resolution)?;
outvec.append(&mut thisvec);
}
Ok(outvec)
}
}
impl ToH3Indexes for Point<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
check_valid_h3_resolution(h3_resolution)?;
Ok(vec![Index::from_coordinate(&self.0, h3_resolution)?])
}
}
impl ToH3Indexes for MultiPoint<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
let mut outvec = vec![];
for pt in self.0.iter() {
outvec.push(Index::from_coordinate(&pt.0, h3_resolution)?);
}
Ok(outvec)
}
}
impl ToH3Indexes for Coordinate<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
check_valid_h3_resolution(h3_resolution)?;
Ok(vec![Index::from_coordinate(&self, h3_resolution)?])
}
}
impl ToH3Indexes for LineString<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
check_valid_h3_resolution(h3_resolution)?;
let mut indexes = line(&self, h3_resolution)?;
Ok(indexes.drain(..).map(Index::new).collect())
}
}
impl ToH3Indexes for MultiLineString<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
let mut outvec = vec![];
for ls in self.0.iter() {
let mut thisvec = ls.to_h3_indexes(h3_resolution)?;
outvec.append(&mut thisvec);
}
Ok(outvec)
}
}
impl ToH3Indexes for Rect<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
self.to_polygon().to_h3_indexes(h3_resolution)
}
}
impl ToH3Indexes for Triangle<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
self.to_polygon().to_h3_indexes(h3_resolution)
}
}
impl ToH3Indexes for Line<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
LineString::from(vec![self.start, self.end]).to_h3_indexes(h3_resolution)
}
}
impl ToH3Indexes for GeometryCollection<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
let mut outvec = vec![];
for geom in self.0.iter() {
let mut thisvec = geom.to_h3_indexes(h3_resolution)?;
outvec.append(&mut thisvec);
}
Ok(outvec)
}
}
impl ToH3Indexes for Geometry<f64> {
fn to_h3_indexes(&self, h3_resolution: u8) -> Result<Vec<Index>, Error> {
match self {
Geometry::Point(pt) => pt.to_h3_indexes(h3_resolution),
Geometry::Line(l) => l.to_h3_indexes(h3_resolution),
Geometry::LineString(ls) => ls.to_h3_indexes(h3_resolution),
Geometry::Polygon(poly) => poly.to_h3_indexes(h3_resolution),
Geometry::MultiPoint(mp) => mp.to_h3_indexes(h3_resolution),
Geometry::MultiLineString(mls) => mls.to_h3_indexes(h3_resolution),
Geometry::MultiPolygon(mpoly) => mpoly.to_h3_indexes(h3_resolution),
Geometry::GeometryCollection(gc) => gc.to_h3_indexes(h3_resolution),
Geometry::Rect(r) => r.to_h3_indexes(h3_resolution),
Geometry::Triangle(tr) => tr.to_h3_indexes(h3_resolution),
}
}
}