Rewrite server in rust
This commit is contained in:
parent
0cea9b873c
commit
bf2d5de156
13 changed files with 3875 additions and 547 deletions
130
server-rs/src/index.rs
Normal file
130
server-rs/src/index.rs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/// Grid-based spatial index for fast rectangle queries over property rows.
|
||||
///
|
||||
/// 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,
|
||||
cols: usize,
|
||||
rows: usize,
|
||||
/// cells[row * cols + col] = vec of row indices
|
||||
cells: Vec<Vec<u32>>,
|
||||
}
|
||||
|
||||
impl GridIndex {
|
||||
/// Build the grid index from lat/lon arrays.
|
||||
pub fn build(lat: &[f64], lon: &[f64], cell_size: f64) -> Self {
|
||||
// Compute bounding box with a small margin
|
||||
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;
|
||||
|
||||
for i in 0..lat.len() {
|
||||
let la = lat[i];
|
||||
let lo = lon[i];
|
||||
if la < min_lat {
|
||||
min_lat = la;
|
||||
}
|
||||
if la > max_lat {
|
||||
max_lat = la;
|
||||
}
|
||||
if lo < min_lon {
|
||||
min_lon = lo;
|
||||
}
|
||||
if lo > max_lon {
|
||||
max_lon = lo;
|
||||
}
|
||||
}
|
||||
|
||||
// Add margin
|
||||
min_lat -= cell_size;
|
||||
min_lon -= cell_size;
|
||||
max_lat += cell_size;
|
||||
max_lon += cell_size;
|
||||
|
||||
let rows = ((max_lat - min_lat) / cell_size).ceil() as usize + 1;
|
||||
let cols = ((max_lon - min_lon) / cell_size).ceil() as usize + 1;
|
||||
|
||||
eprintln!(
|
||||
"Building grid index: {}x{} cells ({} total), cell_size={}",
|
||||
rows,
|
||||
cols,
|
||||
rows * cols,
|
||||
cell_size
|
||||
);
|
||||
|
||||
let mut cells: Vec<Vec<u32>> = vec![Vec::new(); rows * cols];
|
||||
|
||||
for i in 0..lat.len() {
|
||||
let r = ((lat[i] - min_lat) / cell_size) as usize;
|
||||
let c = ((lon[i] - min_lon) / cell_size) as usize;
|
||||
let idx = r * cols + c;
|
||||
cells[idx].push(i as u32);
|
||||
}
|
||||
|
||||
eprintln!("Grid index built.");
|
||||
|
||||
GridIndex {
|
||||
min_lat,
|
||||
min_lon,
|
||||
cell_size,
|
||||
cols,
|
||||
rows,
|
||||
cells,
|
||||
}
|
||||
}
|
||||
|
||||
/// Query all row indices within the given bounding box.
|
||||
pub fn query(&self, south: f64, west: f64, north: f64, east: f64) -> Vec<u32> {
|
||||
let (r_min, r_max, c_min, c_max) = self.clamp_bounds(south, west, north, east);
|
||||
|
||||
let mut result = Vec::new();
|
||||
for r in r_min..=r_max {
|
||||
let row_start = r * self.cols;
|
||||
for c in c_min..=c_max {
|
||||
result.extend_from_slice(&self.cells[row_start + c]);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Iterate all row indices in bounds without allocating a Vec.
|
||||
#[inline]
|
||||
pub fn for_each_in_bounds(
|
||||
&self,
|
||||
south: f64,
|
||||
west: f64,
|
||||
north: f64,
|
||||
east: f64,
|
||||
mut f: impl FnMut(u32),
|
||||
) {
|
||||
let (r_min, r_max, c_min, c_max) = self.clamp_bounds(south, west, north, east);
|
||||
|
||||
for r in r_min..=r_max {
|
||||
let row_start = r * self.cols;
|
||||
for c in c_min..=c_max {
|
||||
for &row_idx in &self.cells[row_start + c] {
|
||||
f(row_idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clamp_bounds(&self, south: f64, west: f64, north: f64, east: f64) -> (usize, usize, usize, usize) {
|
||||
let r_min = ((south - self.min_lat) / self.cell_size) as isize;
|
||||
let r_max = ((north - self.min_lat) / self.cell_size) as isize;
|
||||
let c_min = ((west - self.min_lon) / self.cell_size) as isize;
|
||||
let c_max = ((east - self.min_lon) / self.cell_size) as isize;
|
||||
|
||||
let r_min = r_min.max(0) as usize;
|
||||
let r_max = (r_max.min(self.rows as isize - 1)).max(0) as usize;
|
||||
let c_min = c_min.max(0) as usize;
|
||||
let c_max = (c_max.min(self.cols as isize - 1)).max(0) as usize;
|
||||
|
||||
(r_min, r_max, c_min, c_max)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue