34 lines
1.2 KiB
Rust
34 lines
1.2 KiB
Rust
//! H3 spatial cell precomputation for property rows.
|
|
|
|
use anyhow::Context;
|
|
use rayon::prelude::*;
|
|
|
|
use crate::consts::H3_PRECOMPUTE_MAX;
|
|
|
|
/// Precompute H3 cell IDs for all rows at the maximum resolution only.
|
|
/// Parent cells for lower resolutions are derived on the fly via `CellIndex::parent()`.
|
|
pub fn precompute_h3(lat: &[f32], lon: &[f32]) -> anyhow::Result<Vec<u64>> {
|
|
let res = H3_PRECOMPUTE_MAX;
|
|
tracing::info!("Precomputing H3 cells at resolution {}", res);
|
|
|
|
let h3_res =
|
|
h3o::Resolution::try_from(res).with_context(|| format!("Invalid H3 resolution: {res}"))?;
|
|
|
|
let cells: Vec<u64> = lat
|
|
.par_iter()
|
|
.zip(lon.par_iter())
|
|
.enumerate()
|
|
.map(|(i, (&latitude, &longitude))| {
|
|
let coord = h3o::LatLng::new(latitude as f64, longitude as f64).unwrap_or_else(|err| {
|
|
panic!(
|
|
"Invalid coordinates at row {}: lat={}, lon={}: {}",
|
|
i, latitude, longitude, err
|
|
)
|
|
});
|
|
u64::from(coord.to_cell(h3_res))
|
|
})
|
|
.collect();
|
|
|
|
tracing::info!("H3 precomputation complete ({} cells)", cells.len());
|
|
Ok(cells)
|
|
}
|