use h3o::error::LocalIjError;
use h3o::{CellIndex, CoordIJ, LatLng, LocalIJ, Resolution};
pub fn origin_cell(resolution: Resolution) -> CellIndex {
LatLng::new(0.0, 0.0)
.expect("(0.0, 0.0) is always a valid lat/lng")
.to_cell(resolution)
}
pub fn industry_cell(origin: CellIndex, hq: i32, hr: i32) -> Result<CellIndex, LocalIjError> {
CellIndex::try_from(LocalIJ::new(origin, CoordIJ::new(hq, hr)))
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Extensives {
pub c: f64,
pub v: f64,
pub s: f64,
pub t: f64,
pub n: u32,
pub hx: f64,
pub hz: f64,
}
impl Extensives {
fn merge(self, other: Self) -> Self {
Self {
c: self.c + other.c,
v: self.v + other.v,
s: self.s + other.s,
t: self.t + other.t,
n: self.n + other.n,
hx: self.hx + other.hx,
hz: self.hz + other.hz,
}
}
}
pub fn aggregate_by_parent(
cells: &[(CellIndex, Extensives)],
coarse_res: Resolution,
) -> Vec<(CellIndex, Extensives)> {
let mut order: Vec<CellIndex> = Vec::with_capacity(cells.len());
let mut by_parent: std::collections::HashMap<CellIndex, Extensives> =
std::collections::HashMap::with_capacity(cells.len());
for &(cell, ext) in cells {
let parent = cell
.parent(coarse_res)
.expect("coarse_res must be <= the fine cell's resolution");
match by_parent.get_mut(&parent) {
Some(acc) => *acc = acc.merge(ext),
None => {
by_parent.insert(parent, ext);
order.push(parent);
}
}
}
order
.into_iter()
.map(|parent| (parent, by_parent[&parent]))
.collect()
}