Optimisations

This commit is contained in:
Andras Schmelczer 2026-02-01 21:00:59 +00:00
parent 66c2a25457
commit 9179acd4cd
21 changed files with 653 additions and 139 deletions

View file

@ -3,9 +3,9 @@
/// Divides the UK bounding box into cells of ~0.01 degrees (~1km),
/// each storing indices of rows whose lat/lon falls within that cell.
pub struct GridIndex {
min_lat: f64,
min_lon: f64,
cell_size: f64,
min_lat: f32,
min_lon: f32,
cell_size: f32,
cols: usize,
rows: usize,
/// cells[row * cols + col] = vec of row indices
@ -13,11 +13,11 @@ pub struct GridIndex {
}
impl GridIndex {
pub fn build(lat: &[f64], lon: &[f64], cell_size: f64) -> Self {
let mut min_lat = f64::INFINITY;
let mut max_lat = f64::NEG_INFINITY;
let mut min_lon = f64::INFINITY;
let mut max_lon = f64::NEG_INFINITY;
pub fn build(lat: &[f32], lon: &[f32], cell_size: f32) -> Self {
let mut min_lat = f32::INFINITY;
let mut max_lat = f32::NEG_INFINITY;
let mut min_lon = f32::INFINITY;
let mut max_lon = f32::NEG_INFINITY;
for index in 0..lat.len() {
if lat[index] < min_lat {
@ -71,6 +71,7 @@ impl GridIndex {
}
}
/// Query accepts f64 bounds (from HTTP parsing) and casts internally.
pub fn query(&self, south: f64, west: f64, north: f64, east: f64) -> Vec<u32> {
let Some((row_min, row_max, col_min, col_max)) =
self.clamp_bounds(south, west, north, east)
@ -121,10 +122,14 @@ impl GridIndex {
north: f64,
east: f64,
) -> Option<(usize, usize, usize, usize)> {
let row_min_raw = ((south - self.min_lat) / self.cell_size) as isize;
let row_max_raw = ((north - self.min_lat) / self.cell_size) as isize;
let col_min_raw = ((west - self.min_lon) / self.cell_size) as isize;
let col_max_raw = ((east - self.min_lon) / self.cell_size) as isize;
let min_lat = self.min_lat as f64;
let min_lon = self.min_lon as f64;
let cell_size = self.cell_size as f64;
let row_min_raw = ((south - min_lat) / cell_size) as isize;
let row_max_raw = ((north - min_lat) / cell_size) as isize;
let col_min_raw = ((west - min_lon) / cell_size) as isize;
let col_max_raw = ((east - min_lon) / cell_size) as isize;
let row_min = row_min_raw.max(0) as usize;
let row_max_clamped = row_max_raw.min(self.rows as isize - 1);