use crate::SplitCriterion;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::{Random, thread_rng};
use sklears_core::{
error::{Result, SklearsError},
traits::Untrained,
};
use std::collections::{HashMap, VecDeque};
use std::marker::PhantomData;
use crate::decision_tree::{DecisionTreeConfig, SplitCriterion};
#[derive(Debug, Clone)]
pub enum CoordinateSystem {
Geographic,
Projected { epsg_code: u32 },
UTM { zone: u8, hemisphere: Hemisphere },
WebMercator,
Custom { name: String },
}
#[derive(Debug, Clone, Copy)]
pub enum Hemisphere {
North,
South,
}
#[derive(Debug, Clone, Copy)]
pub struct SpatialPoint {
pub x: f64,
pub y: f64,
pub z: Option<f64>,
}
impl SpatialPoint {
pub fn new(x: f64, y: f64) -> Self {
Self { x, y, z: None }
}
pub fn new_3d(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z: Some(z) }
}
pub fn distance(&self, other: &SpatialPoint) -> f64 {
let dx = self.x - other.x;
let dy = self.y - other.y;
match (self.z, other.z) {
(Some(z1), Some(z2)) => {
let dz = z1 - z2;
(dx * dx + dy * dy + dz * dz).sqrt()
}
_ => (dx * dx + dy * dy).sqrt(),
}
}
pub fn haversine_distance(&self, other: &SpatialPoint) -> f64 {
const EARTH_RADIUS_KM: f64 = 6371.0;
let lat1 = self.y.to_radians();
let lat2 = other.y.to_radians();
let delta_lat = (other.y - self.y).to_radians();
let delta_lon = (other.x - self.x).to_radians();
let a = (delta_lat / 2.0).sin().powi(2)
+ lat1.cos() * lat2.cos() * (delta_lon / 2.0).sin().powi(2);
let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
EARTH_RADIUS_KM * c
}
}
#[derive(Debug, Clone, Copy)]
pub struct BoundingBox {
pub min_x: f64,
pub max_x: f64,
pub min_y: f64,
pub max_y: f64,
}
impl BoundingBox {
pub fn new(min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Self {
Self {
min_x,
max_x,
min_y,
max_y,
}
}
pub fn from_points(points: &[SpatialPoint]) -> Self {
if points.is_empty() {
return Self::new(0.0, 0.0, 0.0, 0.0);
}
let mut min_x = points[0].x;
let mut max_x = points[0].x;
let mut min_y = points[0].y;
let mut max_y = points[0].y;
for point in points.iter().skip(1) {
min_x = min_x.min(point.x);
max_x = max_x.max(point.x);
min_y = min_y.min(point.y);
max_y = max_y.max(point.y);
}
Self::new(min_x, max_x, min_y, max_y)
}
pub fn contains(&self, point: &SpatialPoint) -> bool {
point.x >= self.min_x
&& point.x <= self.max_x
&& point.y >= self.min_y
&& point.y <= self.max_y
}
pub fn area(&self) -> f64 {
(self.max_x - self.min_x) * (self.max_y - self.min_y)
}
pub fn intersection(&self, other: &BoundingBox) -> Option<BoundingBox> {
let min_x = self.min_x.max(other.min_x);
let max_x = self.max_x.min(other.max_x);
let min_y = self.min_y.max(other.min_y);
let max_y = self.max_y.min(other.max_y);
if min_x <= max_x && min_y <= max_y {
Some(BoundingBox::new(min_x, max_x, min_y, max_y))
} else {
None
}
}
}
#[derive(Debug, Clone)]
pub enum SpatialSplitCriterion {
Standard(SplitCriterion),
GeographicDistance {
distance_metric: DistanceMetric,
distance_weight: f64,
},
SpatialAutocorrelation {
lag_distance: f64,
autocorr_weight: f64,
},
GeographicallyWeighted {
bandwidth: f64,
kernel: SpatialKernel,
},
Quadtree {
max_depth: usize,
min_points_per_cell: usize,
},
Voronoi {
n_sites: usize,
site_selection: VoronoiSiteSelection,
},
ElevationAware {
elevation_weight: f64,
slope_threshold: f64,
},
}
#[derive(Debug, Clone, Copy)]
pub enum DistanceMetric {
Euclidean,
Manhattan,
Haversine,
GreatCircle,
Geodesic,
}
#[derive(Debug, Clone, Copy)]
pub enum SpatialKernel {
Gaussian,
Exponential,
Linear,
Uniform,
Tricube,
}
#[derive(Debug, Clone, Copy)]
pub enum VoronoiSiteSelection {
Random,
KMeans,
FarthestPoint,
Grid,
}
#[derive(Debug, Clone)]
pub struct SpatialDataStructure {
pub points: Vec<SpatialPoint>,
pub features: Array2<f64>,
pub targets: Array1<f64>,
pub coordinate_system: CoordinateSystem,
pub spatial_index: Option<SpatialIndex>,
pub metadata: SpatialMetadata,
}
#[derive(Debug, Clone)]
pub struct SpatialMetadata {
pub bounding_box: BoundingBox,
pub resolution: Option<f64>,
pub clusters: Vec<SpatialCluster>,
pub elevation_stats: Option<ElevationStats>,
pub autocorrelation_stats: Option<AutocorrelationStats>,
}
#[derive(Debug, Clone)]
pub struct SpatialCluster {
pub id: usize,
pub center: SpatialPoint,
pub point_indices: Vec<usize>,
pub radius: f64,
}
#[derive(Debug, Clone)]
pub struct ElevationStats {
pub mean: f64,
pub std: f64,
pub min: f64,
pub max: f64,
pub slope_distribution: Vec<f64>,
}
#[derive(Debug, Clone)]
pub struct AutocorrelationStats {
pub morans_i: f64,
pub gearys_c: f64,
pub lisa: Vec<f64>,
pub spatial_lag: Vec<f64>,
}
#[derive(Debug, Clone)]
pub enum SpatialIndex {
Quadtree(QuadTree),
RTree(RTree),
Grid(GridIndex),
KDTree(KDTree),
}
#[derive(Debug, Clone)]
pub struct QuadTree {
pub root: QuadTreeNode,
pub max_depth: usize,
pub max_points_per_leaf: usize,
}
#[derive(Debug, Clone)]
pub struct QuadTreeNode {
pub bounds: BoundingBox,
pub points: Vec<usize>,
pub children: Option<[Box<QuadTreeNode>; 4]>,
pub is_leaf: bool,
pub depth: usize,
}
#[derive(Debug, Clone)]
pub struct RTree {
pub root: RTreeNode,
pub max_entries: usize,
}
#[derive(Debug, Clone)]
pub struct RTreeNode {
pub bounds: BoundingBox,
pub entries: Vec<RTreeEntry>,
pub is_leaf: bool,
}
#[derive(Debug, Clone)]
pub struct RTreeEntry {
pub bounds: BoundingBox,
pub data: RTreeData,
}
#[derive(Debug, Clone)]
pub enum RTreeData {
Point(usize),
Node(Box<RTreeNode>),
}
#[derive(Debug, Clone)]
pub struct GridIndex {
pub cells: Vec<Vec<GridCell>>,
pub cell_size: f64,
pub bounds: BoundingBox,
pub grid_width: usize,
pub grid_height: usize,
}
#[derive(Debug, Clone)]
pub struct GridCell {
pub bounds: BoundingBox,
pub point_indices: Vec<usize>,
}
#[derive(Debug, Clone)]
pub struct KDTree {
pub root: Option<KDTreeNode>,
pub dimensions: usize,
}
#[derive(Debug, Clone)]
pub struct KDTreeNode {
pub point_index: usize,
pub split_dim: usize,
pub left: Option<Box<KDTreeNode>>,
pub right: Option<Box<KDTreeNode>>,
}
impl SpatialDataStructure {
pub fn new(
points: Vec<SpatialPoint>,
features: Array2<f64>,
targets: Array1<f64>,
coordinate_system: CoordinateSystem,
) -> Self {
let bounding_box = BoundingBox::from_points(&points);
Self {
points,
features,
targets,
coordinate_system,
spatial_index: None,
metadata: SpatialMetadata {
bounding_box,
resolution: None,
clusters: Vec::new(),
elevation_stats: None,
autocorrelation_stats: None,
},
}
}
pub fn build_spatial_index(&mut self, index_type: SpatialIndexType) -> Result<()> {
match index_type {
SpatialIndexType::Quadtree {
max_depth,
max_points_per_leaf,
} => {
let quadtree = self.build_quadtree(max_depth, max_points_per_leaf)?;
self.spatial_index = Some(SpatialIndex::Quadtree(quadtree));
}
SpatialIndexType::Grid { cell_size } => {
let grid = self.build_grid_index(cell_size)?;
self.spatial_index = Some(SpatialIndex::Grid(grid));
}
SpatialIndexType::KDTree => {
let kdtree = self.build_kdtree()?;
self.spatial_index = Some(SpatialIndex::KDTree(kdtree));
}
}
Ok(())
}
fn build_quadtree(&self, max_depth: usize, max_points_per_leaf: usize) -> Result<QuadTree> {
let point_indices: Vec<usize> = (0..self.points.len()).collect();
let root = self.build_quadtree_node(
&point_indices,
self.metadata.bounding_box,
0,
max_depth,
max_points_per_leaf,
)?;
Ok(QuadTree {
root,
max_depth,
max_points_per_leaf,
})
}
fn build_quadtree_node(
&self,
point_indices: &[usize],
bounds: BoundingBox,
depth: usize,
max_depth: usize,
max_points_per_leaf: usize,
) -> Result<QuadTreeNode> {
if depth >= max_depth || point_indices.len() <= max_points_per_leaf {
return Ok(QuadTreeNode {
bounds,
points: point_indices.to_vec(),
children: None,
is_leaf: true,
depth,
});
}
let mid_x = (bounds.min_x + bounds.max_x) / 2.0;
let mid_y = (bounds.min_y + bounds.max_y) / 2.0;
let quadrants = [
BoundingBox::new(bounds.min_x, mid_x, bounds.min_y, mid_y), BoundingBox::new(mid_x, bounds.max_x, bounds.min_y, mid_y), BoundingBox::new(bounds.min_x, mid_x, mid_y, bounds.max_y), BoundingBox::new(mid_x, bounds.max_x, mid_y, bounds.max_y), ];
let mut children = Vec::new();
for quadrant in &quadrants {
let quadrant_points: Vec<usize> = point_indices
.iter()
.copied()
.filter(|&idx| quadrant.contains(&self.points[idx]))
.collect();
let child = self.build_quadtree_node(
&quadrant_points,
*quadrant,
depth + 1,
max_depth,
max_points_per_leaf,
)?;
children.push(Box::new(child));
}
if children.len() != 4 {
return Err(SklearsError::InvalidInput(
"Failed to create quadtree children".to_string(),
));
}
let children_array: [Box<QuadTreeNode>; 4] = [
children.remove(0),
children.remove(0),
children.remove(0),
children.remove(0),
];
Ok(QuadTreeNode {
bounds,
points: Vec::new(),
children: Some(children_array),
is_leaf: false,
depth,
})
}
fn build_grid_index(&self, cell_size: f64) -> Result<GridIndex> {
let bounds = self.metadata.bounding_box;
let grid_width = ((bounds.max_x - bounds.min_x) / cell_size).ceil() as usize;
let grid_height = ((bounds.max_y - bounds.min_y) / cell_size).ceil() as usize;
let mut cells = vec![
vec![
GridCell {
bounds: BoundingBox::new(0.0, 0.0, 0.0, 0.0),
point_indices: Vec::new(),
};
grid_height
];
grid_width
];
for i in 0..grid_width {
for j in 0..grid_height {
let min_x = bounds.min_x + i as f64 * cell_size;
let max_x = min_x + cell_size;
let min_y = bounds.min_y + j as f64 * cell_size;
let max_y = min_y + cell_size;
cells[i][j].bounds = BoundingBox::new(min_x, max_x, min_y, max_y);
}
}
for (point_idx, point) in self.points.iter().enumerate() {
let grid_x = ((point.x - bounds.min_x) / cell_size).floor() as usize;
let grid_y = ((point.y - bounds.min_y) / cell_size).floor() as usize;
if grid_x < grid_width && grid_y < grid_height {
cells[grid_x][grid_y].point_indices.push(point_idx);
}
}
Ok(GridIndex {
cells,
cell_size,
bounds,
grid_width,
grid_height,
})
}
fn build_kdtree(&self) -> Result<KDTree> {
let mut point_indices: Vec<usize> = (0..self.points.len()).collect();
let root = self.build_kdtree_node(&mut point_indices, 0, 2)?;
Ok(KDTree {
root,
dimensions: 2,
})
}
fn build_kdtree_node(
&self,
point_indices: &mut [usize],
depth: usize,
dimensions: usize,
) -> Result<Option<KDTreeNode>> {
if point_indices.is_empty() {
return Ok(None);
}
if point_indices.len() == 1 {
return Ok(Some(KDTreeNode {
point_index: point_indices[0],
split_dim: depth % dimensions,
left: None,
right: None,
}));
}
let split_dim = depth % dimensions;
point_indices.sort_by(|&a, &b| {
let coord_a = if split_dim == 0 {
self.points[a].x
} else {
self.points[a].y
};
let coord_b = if split_dim == 0 {
self.points[b].x
} else {
self.points[b].y
};
coord_a.partial_cmp(&coord_b).expect("operation should succeed")
});
let median = point_indices.len() / 2;
let median_index = point_indices[median];
let left_child =
self.build_kdtree_node(&mut point_indices[..median], depth + 1, dimensions)?;
let right_child =
self.build_kdtree_node(&mut point_indices[median + 1..], depth + 1, dimensions)?;
Ok(Some(KDTreeNode {
point_index: median_index,
split_dim,
left: left_child.map(Box::new),
right: right_child.map(Box::new),
}))
}
pub fn calculate_spatial_autocorrelation(&mut self, lag_distance: f64) -> Result<()> {
let n = self.points.len();
let mut weights = Array2::zeros((n, n));
for i in 0..n {
for j in 0..n {
if i != j {
let distance = match self.coordinate_system {
CoordinateSystem::Geographic => {
self.points[i].haversine_distance(&self.points[j])
}
_ => self.points[i].distance(&self.points[j]),
};
if distance <= lag_distance {
weights[[i, j]] = 1.0 / distance.max(1e-8);
}
}
}
}
for i in 0..n {
let row_sum: f64 = weights.row(i).sum();
if row_sum > 0.0 {
for j in 0..n {
weights[[i, j]] /= row_sum;
}
}
}
let y_mean = self.targets.mean().unwrap_or(0.0);
let mut numerator = 0.0;
let mut denominator = 0.0;
let mut w_sum = 0.0;
for i in 0..n {
for j in 0..n {
let yi = self.targets[i] - y_mean;
let yj = self.targets[j] - y_mean;
numerator += weights[[i, j]] * yi * yj;
w_sum += weights[[i, j]];
}
denominator += (self.targets[i] - y_mean).powi(2);
}
let morans_i = if denominator > 0.0 && w_sum > 0.0 {
(n as f64 / w_sum) * (numerator / denominator)
} else {
0.0
};
let mut geary_numerator = 0.0;
for i in 0..n {
for j in 0..n {
geary_numerator += weights[[i, j]] * (self.targets[i] - self.targets[j]).powi(2);
}
}
let gearys_c = if denominator > 0.0 && w_sum > 0.0 {
((n - 1) as f64 / (2.0 * w_sum)) * (geary_numerator / denominator)
} else {
1.0
};
let mut spatial_lag = Vec::with_capacity(n);
for i in 0..n {
let mut lag_value = 0.0;
for j in 0..n {
lag_value += weights[[i, j]] * self.targets[j];
}
spatial_lag.push(lag_value);
}
let lisa: Vec<f64> = (0..n)
.map(|i| {
let yi = self.targets[i] - y_mean;
let mut lisa_value = 0.0;
for j in 0..n {
let yj = self.targets[j] - y_mean;
lisa_value += weights[[i, j]] * yj;
}
yi * lisa_value
})
.collect();
self.metadata.autocorrelation_stats = Some(AutocorrelationStats {
morans_i,
gearys_c,
lisa,
spatial_lag,
});
Ok(())
}
pub fn spatial_clustering(
&mut self,
n_clusters: usize,
method: SpatialClusteringMethod,
) -> Result<()> {
match method {
SpatialClusteringMethod::KMeans => self.kmeans_clustering(n_clusters),
SpatialClusteringMethod::DBSCAN { eps, min_points } => {
self.dbscan_clustering(eps, min_points)
}
SpatialClusteringMethod::Hierarchical => self.hierarchical_clustering(n_clusters),
}
}
fn kmeans_clustering(&mut self, n_clusters: usize) -> Result<()> {
let mut rng = scirs2_core::random::thread_rng();
let mut centroids: Vec<SpatialPoint> = Vec::new();
for _ in 0..n_clusters {
if !self.points.is_empty() {
let idx = rng.gen_range(0..self.points.len());
centroids.push(self.points[idx].clone());
}
}
let mut clusters = vec![
SpatialCluster {
id: 0,
center: SpatialPoint::new(0.0, 0.0),
point_indices: Vec::new(),
radius: 0.0,
};
n_clusters
];
for iteration in 0..100 {
for cluster in &mut clusters {
cluster.point_indices.clear();
}
for (point_idx, point) in self.points.iter().enumerate() {
let mut min_distance = f64::INFINITY;
let mut closest_cluster = 0;
for (cluster_idx, centroid) in centroids.iter().enumerate() {
let distance = point.distance(centroid);
if distance < min_distance {
min_distance = distance;
closest_cluster = cluster_idx;
}
}
clusters[closest_cluster].point_indices.push(point_idx);
}
let mut converged = true;
for (cluster_idx, cluster) in clusters.iter_mut().enumerate() {
if cluster.point_indices.is_empty() {
continue;
}
let mut sum_x = 0.0;
let mut sum_y = 0.0;
for &point_idx in &cluster.point_indices {
sum_x += self.points[point_idx].x;
sum_y += self.points[point_idx].y;
}
let new_centroid = SpatialPoint::new(
sum_x / cluster.point_indices.len() as f64,
sum_y / cluster.point_indices.len() as f64,
);
if centroids[cluster_idx].distance(&new_centroid) > 1e-6 {
converged = false;
}
centroids[cluster_idx] = new_centroid;
cluster.center = new_centroid;
cluster.id = cluster_idx;
let mut max_distance: f64 = 0.0;
for &point_idx in &cluster.point_indices {
let distance = self.points[point_idx].distance(&cluster.center);
max_distance = max_distance.max(distance);
}
cluster.radius = max_distance;
}
if converged {
break;
}
}
self.metadata.clusters = clusters;
Ok(())
}
fn dbscan_clustering(&mut self, eps: f64, min_points: usize) -> Result<()> {
let n = self.points.len();
let mut cluster_labels = vec![-1i32; n]; let mut cluster_id = 0i32;
for i in 0..n {
if cluster_labels[i] != -1 {
continue; }
let neighbors = self.find_neighbors(i, eps);
if neighbors.len() < min_points {
cluster_labels[i] = -2; continue;
}
cluster_labels[i] = cluster_id;
let mut seed_set = VecDeque::from(neighbors);
while let Some(neighbor) = seed_set.pop_front() {
if cluster_labels[neighbor] == -2 {
cluster_labels[neighbor] = cluster_id; } else if cluster_labels[neighbor] == -1 {
cluster_labels[neighbor] = cluster_id;
let neighbor_neighbors = self.find_neighbors(neighbor, eps);
if neighbor_neighbors.len() >= min_points {
seed_set.extend(neighbor_neighbors);
}
}
}
cluster_id += 1;
}
let mut clusters = HashMap::new();
for (point_idx, &label) in cluster_labels.iter().enumerate() {
if label >= 0 {
clusters
.entry(label as usize)
.or_insert_with(Vec::new)
.push(point_idx);
}
}
self.metadata.clusters = clusters
.into_iter()
.map(|(id, point_indices)| {
let mut sum_x = 0.0;
let mut sum_y = 0.0;
for &point_idx in &point_indices {
sum_x += self.points[point_idx].x;
sum_y += self.points[point_idx].y;
}
let center = SpatialPoint::new(
sum_x / point_indices.len() as f64,
sum_y / point_indices.len() as f64,
);
let mut max_distance: f64 = 0.0;
for &point_idx in &point_indices {
let distance = self.points[point_idx].distance(¢er);
max_distance = max_distance.max(distance);
}
SpatialCluster {
id,
center,
point_indices,
radius: max_distance,
}
})
.collect();
Ok(())
}
fn find_neighbors(&self, point_idx: usize, eps: f64) -> Vec<usize> {
let mut neighbors = Vec::new();
let query_point = &self.points[point_idx];
for (i, point) in self.points.iter().enumerate() {
if i != point_idx && query_point.distance(point) <= eps {
neighbors.push(i);
}
}
neighbors
}
fn hierarchical_clustering(&mut self, n_clusters: usize) -> Result<()> {
self.kmeans_clustering(n_clusters)
}
}
#[derive(Debug, Clone)]
pub enum SpatialIndexType {
Quadtree {
max_depth: usize,
max_points_per_leaf: usize,
},
Grid { cell_size: f64 },
KDTree,
}
#[derive(Debug, Clone)]
pub enum SpatialClusteringMethod {
KMeans,
DBSCAN { eps: f64, min_points: usize },
Hierarchical,
}
pub struct SpatialDecisionTree<State = Untrained> {
config: DecisionTreeConfig,
spatial_config: SpatialTreeConfig,
spatial_data: Option<SpatialDataStructure>,
model: Option<Box<dyn SpatialTreeModel>>,
state: PhantomData<State>,
}
#[derive(Debug, Clone)]
pub struct SpatialTreeConfig {
pub split_criterion: SpatialSplitCriterion,
pub coordinate_system: CoordinateSystem,
pub consider_autocorrelation: bool,
pub spatial_regularization: f64,
pub min_spatial_cluster_size: usize,
}
impl Default for SpatialTreeConfig {
fn default() -> Self {
Self {
split_criterion: SpatialSplitCriterion::Standard(SplitCriterion::MSE),
coordinate_system: CoordinateSystem::Geographic,
consider_autocorrelation: false,
spatial_regularization: 0.01,
min_spatial_cluster_size: 5,
}
}
}
pub trait SpatialTreeModel: Send + Sync {
fn predict(&self, points: &[SpatialPoint], features: &Array2<f64>) -> Result<Array1<f64>>;
fn update(&mut self, spatial_data: &SpatialDataStructure) -> Result<()>;
fn get_spatial_importance(&self) -> Result<HashMap<String, f64>>;
}