hextree/hex_tree_set.rs
1use crate::{compaction::SetCompactor, Cell, HexTreeMap};
2use std::iter::FromIterator;
3
4/// A HexTreeSet is a structure for representing geographical regions
5/// and efficiently testing performing hit-tests on that region. Or,
6/// in other words: I have a region defined; does it contain this
7/// point on earth?
8///
9///
10/// # Usage
11///
12/// <iframe src="https://kepler.gl/demo?mapUrl=https://gist.githubusercontent.com/JayKickliter/8f91a8437b7dd89321b22cde50e71c3a/raw/a60c83cb15e75aba660fb6535d8e0061fa504205/monaco.kepler.json" width="100%" height="600"></iframe>
13///
14/// ----
15///
16/// Let's create a HexTreeSet for Monaco as visualized in the map
17///
18/// ```
19/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
20/// use geo::coord;
21/// use hextree::{Cell, HexTreeSet};
22/// #
23/// # use byteorder::{LittleEndian as LE, ReadBytesExt};
24/// # let idx_bytes = include_bytes!("../assets//monaco.res12.h3idx");
25/// # let rdr = &mut idx_bytes.as_slice();
26/// # let mut cells = Vec::new();
27/// # while let Ok(idx) = rdr.read_u64::<LE>() {
28/// # cells.push(Cell::from_raw(idx)?);
29/// # }
30///
31/// // `cells` is a slice of `Index`s
32/// let monaco: HexTreeSet = cells.iter().collect();
33///
34/// // You can see in the map above that our set covers Point 1 (green
35/// // check) but not Point 2 (red x), let's test that.
36/// // Lat/lon 43.73631, 7.42418 @ res 12
37/// let point_1 = Cell::from_raw(0x8c3969a41da15ff)?;
38/// // Lat/lon 43.73008, 7.42855 @ res 12
39/// let point_2 = Cell::from_raw(0x8c3969a415065ff)?;
40///
41/// assert!(monaco.contains(point_1));
42/// assert!(!monaco.contains(point_2));
43///
44/// # Ok(())
45/// # }
46/// ```
47pub type HexTreeSet = HexTreeMap<(), SetCompactor>;
48
49impl FromIterator<Cell> for HexTreeSet {
50 fn from_iter<I>(iter: I) -> Self
51 where
52 I: IntoIterator<Item = Cell>,
53 {
54 let mut set = HexTreeMap::with_compactor(SetCompactor);
55 for cell in iter {
56 set.insert(cell, ());
57 }
58 set
59 }
60}
61
62impl<'a> FromIterator<&'a Cell> for HexTreeSet {
63 fn from_iter<I>(iter: I) -> Self
64 where
65 I: IntoIterator<Item = &'a Cell>,
66 {
67 let mut set = HexTreeMap::with_compactor(SetCompactor);
68 for cell in iter {
69 set.insert(*cell, ());
70 }
71 set
72 }
73}