h3ron_polars/algorithm/chunkedarray/
compact.rs1use crate::{Error, FromIndexIterator, IndexChunked};
2use h3ron::collections::CompactedCellVec;
3use h3ron::H3Cell;
4use polars_core::prelude::UInt64Chunked;
5
6pub trait H3CompactCells {
8 fn h3_compact_cells(&self) -> Result<UInt64Chunked, Error>;
12}
13
14impl<'a> H3CompactCells for IndexChunked<'a, H3Cell> {
15 fn h3_compact_cells(&self) -> Result<UInt64Chunked, Error> {
16 let mut ccv = CompactedCellVec::new();
17 ccv.add_cells(self.iter_indexes_nonvalidated().flatten(), true)?;
18
19 Ok(UInt64Chunked::from_index_iter(ccv.iter_compacted_cells()))
20 }
21}
22
23#[cfg(test)]
24mod tests {
25 use crate::algorithm::H3CompactCells;
26 use crate::{AsH3CellChunked, FromIndexIterator};
27 use h3ron::H3Cell;
28 use polars_core::prelude::{TakeRandom, UInt64Chunked};
29
30 #[test]
31 fn cell_compact() {
32 let cell = H3Cell::from_coordinate((4.5, 1.3).into(), 6).unwrap();
33
34 let ca = UInt64Chunked::from_index_iter(&cell.get_children(7).unwrap());
35 assert_eq!(ca.len(), 7);
36
37 let changed = ca.h3cell().h3_compact_cells().unwrap();
38 assert_eq!(changed.len(), 1);
39 assert_eq!(changed.h3cell().get(0), Some(cell));
40 }
41}