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
use std::borrow::Borrow;

use geo::algorithm::simplify::Simplify;
use geo_types::{MultiPolygon, Polygon};

use h3ron::collections::H3CellSet;
use h3ron::iter::change_resolution;
use h3ron::{H3Cell, ToLinkedPolygons};

use crate::error::Error;

/// calculates a [`MultiPolygon`] of the area covered by a graph
pub trait CoveredArea {
    type Error;

    /// calculates a [`MultiPolygon`] of the area covered by a graph
    ///
    /// As the resulting geometry will be quite complex, it is recommended
    /// to reduce the h3 resolution using `reduce_resolution_by`. A value of 3
    /// will make the calculation based on resolution 7 for a graph of resolution 10.
    /// Reducing the resolution leads to a overestimation of the area.
    ///
    /// A slight simplification will be applied to the output geometry and
    /// eventual holes will be removed.
    fn covered_area(&self, reduce_resolution_by: u8) -> Result<MultiPolygon<f64>, Self::Error>;
}

/// calculates a [`MultiPolygon`] of the area covered by a [`H3Cell`] iterator.
pub fn cells_covered_area<I>(
    cell_iter: I,
    cell_iter_resolution: u8,
    reduce_resolution_by: u8,
) -> Result<MultiPolygon<f64>, Error>
where
    I: IntoIterator,
    I::Item: Borrow<H3Cell>,
{
    let t_res = cell_iter_resolution.saturating_sub(reduce_resolution_by);
    let mut cells =
        change_resolution(cell_iter.into_iter(), t_res).collect::<Result<H3CellSet, _>>()?;
    let cell_vec: Vec<_> = cells.drain().collect();
    let mp = MultiPolygon::from(
        cell_vec
            // remove the number of vertices by smoothing
            .to_linked_polygons(true)?
            .drain(..)
            // reduce the number of vertices again and discard all holes
            .map(|p| Polygon::new(p.exterior().simplify(&0.000001), vec![]))
            .collect::<Vec<_>>(),
    );
    Ok(mp)
}