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
214
use std::collections::HashMap;
use std::os::raw::c_int;

use geo::algorithm::euclidean_distance::EuclideanDistance;
use geo_types::{Coordinate, LineString, Point, Polygon};

use h3ron_h3_sys::{destroyLinkedPolygon, h3SetToLinkedGeo, radsToDegs, H3Index, LinkedGeoPolygon};

use crate::algorithm::smoothen_h3_linked_polygon;
use crate::collections::H3CompactedVec;
use crate::{HasH3Index, Index};

pub trait ToPolygon {
    fn to_polygon(&self) -> Polygon<f64>;
}

pub trait ToCoordinate {
    fn to_coordinate(&self) -> Coordinate<f64>;
}

/// join hexagon polygons to larger polygons where hexagons are touching each other
pub trait ToLinkedPolygons {
    fn to_linked_polygons(&self, smoothen: bool) -> Vec<Polygon<f64>>;
}

impl ToLinkedPolygons for Vec<Index> {
    fn to_linked_polygons(&self, smoothen: bool) -> Vec<Polygon<f64>> {
        let mut h3indexes: Vec<_> = self.iter().map(|i| i.h3index()).collect();
        h3indexes.sort_unstable();
        h3indexes.dedup();
        to_linked_polygons(&h3indexes, smoothen)
    }
}

impl ToLinkedPolygons for H3CompactedVec {
    fn to_linked_polygons(&self, smoothen: bool) -> Vec<Polygon<f64>> {
        if let Some(res) = self.finest_resolution_contained() {
            let mut h3indexes: Vec<_> = self.iter_uncompacted_indexes(res).collect();
            h3indexes.sort_unstable();
            h3indexes.dedup();
            to_linked_polygons(&h3indexes, smoothen)
        } else {
            vec![]
        }
    }
}

/// join hexagon polygons to larger polygons where hexagons are touching each other
///
/// The indexes will be grouped by the `align_to_h3_resolution`, so this will generate polygons
/// not exceeding the area of that parent resolution.
///
/// Corners will be aligned to the corners of the parent resolution when they are less than an
/// edge length away from them. This is to avoid gaps when `smoothen` is set to true.
///
/// This algorithm still needs some optimization to improve the runtime.
pub trait ToAlignedLinkedPolygons {
    fn to_aligned_linked_polygons(
        &self,
        align_to_h3_resolution: u8,
        smoothen: bool,
    ) -> Vec<Polygon<f64>>;
}

impl ToAlignedLinkedPolygons for Vec<Index> {
    fn to_aligned_linked_polygons(
        &self,
        align_to_h3_resolution: u8,
        smoothen: bool,
    ) -> Vec<Polygon<f64>> {
        let mut h3indexes_grouped = HashMap::new();
        for i in self.iter() {
            let parent = i.get_parent_unchecked(align_to_h3_resolution);
            h3indexes_grouped
                .entry(parent)
                .or_insert_with(Vec::new)
                .push(i.h3index())
        }

        let mut polygons = Vec::new();
        for (parent_index, h3indexes) in h3indexes_grouped.drain() {
            if smoothen {
                //
                // align to the corners of the parent index
                //

                let parent_poly_vertices: Vec<_> = parent_index
                    .to_polygon()
                    .exterior()
                    .0
                    .iter()
                    .map(|c| Point::from(*c))
                    .collect();

                // edge length of the child indexes
                let edge_length = {
                    let ring = Index::new(h3indexes[0]).to_polygon();
                    let p1 = Point::from(ring.exterior().0[0]);
                    let p2 = Point::from(ring.exterior().0[1]);
                    p1.euclidean_distance(&p2)
                };

                for poly in to_linked_polygons(&h3indexes, true).drain(..) {
                    let points_new: Vec<_> = poly
                        .exterior()
                        .0
                        .iter()
                        .map(|c| {
                            let p = Point::from(*c);
                            if let Some(pv) = parent_poly_vertices
                                .iter()
                                .find(|pv| p.euclidean_distance(*pv) < edge_length)
                            {
                                pv.0
                            } else {
                                *c
                            }
                        })
                        .collect();
                    polygons.push(Polygon::new(
                        LineString::from(points_new),
                        poly.interiors().to_vec(),
                    ));
                }
            } else {
                polygons.append(&mut to_linked_polygons(&h3indexes, false));
            }
        }
        polygons
    }
}

/// convert raw h3indexes to linked polygons
///
/// With `smoothen` an optional smoothing can be applied to the polygons to remove
/// H3 artifacts.
///
/// for this case, the slice must already be deduplicated, and all h3 indexes must be the same resolutions
pub fn to_linked_polygons(h3indexes: &[H3Index], smoothen: bool) -> Vec<Polygon<f64>> {
    if h3indexes.is_empty() {
        return vec![];
    }
    unsafe {
        let mut lgp = LinkedGeoPolygon {
            first: std::ptr::null_mut(),
            last: std::ptr::null_mut(),
            next: std::ptr::null_mut(),
        };
        h3SetToLinkedGeo(h3indexes.as_ptr(), h3indexes.len() as c_int, &mut lgp);

        let mut polygons = vec![];
        let mut cur_linked_geo_polygon = Some(&lgp);
        while let Some(poly) = cur_linked_geo_polygon.as_ref() {
            let mut exterior = None;
            let mut interiors = vec![];
            let mut linked_loop_i = 0;
            let mut cur_linked_geo_loop = poly.first.as_ref();
            while let Some(linked_loop) = cur_linked_geo_loop {
                let mut coordinates = vec![];
                let mut cur_linked_geo_coord = linked_loop.first.as_ref();
                while let Some(linked_coord) = cur_linked_geo_coord {
                    coordinates.push((
                        radsToDegs(linked_coord.vertex.lon),
                        radsToDegs(linked_coord.vertex.lat),
                    ));
                    cur_linked_geo_coord = linked_coord.next.as_ref();
                }

                if coordinates.len() >= 3 {
                    let linestring = LineString::from(coordinates);
                    if linked_loop_i == 0 {
                        exterior = Some(linestring)
                    } else {
                        interiors.push(linestring)
                    }
                }

                linked_loop_i += 1;
                cur_linked_geo_loop = linked_loop.next.as_ref();
            }
            if let Some(ext) = exterior {
                let poly = Polygon::new(ext, interiors);
                if smoothen {
                    polygons.push(smoothen_h3_linked_polygon(&poly));
                } else {
                    polygons.push(poly);
                }
            }
            cur_linked_geo_polygon = poly.next.as_ref();
        }
        destroyLinkedPolygon(&mut lgp);
        polygons
    }
}

#[cfg(test)]
mod tests {
    use geo_types::Coordinate;

    use crate::{Index, ToLinkedPolygons};

    #[test]
    fn donut_linked_polygon() {
        let ring = Index::from_coordinate(&Coordinate::from((23.3, 12.3)), 6)
            .unwrap()
            .hex_ring(1)
            .unwrap();
        let polygons = ring.to_linked_polygons(false);
        assert_eq!(polygons.len(), 1);
        assert_eq!(polygons[0].exterior().0.len(), 19);
        assert_eq!(polygons[0].interiors().len(), 1);
        assert_eq!(polygons[0].interiors()[0].0.len(), 7);
    }
}