pub struct RTree<T>where
T: Clone,{ /* private fields */ }Expand description
R-tree for spatial indexing and queries
§Examples
use scirs2_spatial::rtree::RTree;
use scirs2_core::ndarray::array;
// Create points
let points = array![
[0.0, 0.0],
[1.0, 1.0],
[2.0, 2.0],
[3.0, 3.0],
[4.0, 4.0],
];
// Build R-tree with points
let mut rtree = RTree::new(2, 4, 8).unwrap();
for (i, point) in points.rows().into_iter().enumerate() {
rtree.insert(point.to_owned(), i).unwrap();
}
// Search for points within a range
let query_min = array![0.5, 0.5];
let query_max = array![2.5, 2.5];
// The views need to be passed explicitly in the actual code
let query_min_view = query_min.view();
let query_max_view = query_max.view();
let results = rtree.search_range(&query_min_view, &query_max_view).unwrap();
println!("Found {} points in range", results.len());
// Find the nearest neighbors to a point
let query_point = array![2.5, 2.5];
let query_point_view = query_point.view();
let nearest = rtree.nearest(&query_point_view, 2).unwrap();
println!("Nearest points: {:?}", nearest);Implementations§
Source§impl<T: Clone> RTree<T>
impl<T: Clone> RTree<T>
Sourcepub fn delete<F>(
&mut self,
point: &ArrayView1<'_, f64>,
data_predicate: Option<F>,
) -> SpatialResult<bool>
pub fn delete<F>( &mut self, point: &ArrayView1<'_, f64>, data_predicate: Option<F>, ) -> SpatialResult<bool>
Delete a data point from the R-tree
§Arguments
point- The point coordinates to deletedata_predicate- An optional function that takes a reference to the data and returns true if it should be deleted. This is useful when multiple data points share the same coordinates.
§Returns
A SpatialResult containing true if a point was deleted, false otherwise
Source§impl<T: Clone> RTree<T>
impl<T: Clone> RTree<T>
Sourcepub fn insert_rectangle(
&mut self,
min: Array1<f64>,
max: Array1<f64>,
data: T,
) -> SpatialResult<()>
pub fn insert_rectangle( &mut self, min: Array1<f64>, max: Array1<f64>, data: T, ) -> SpatialResult<()>
Source§impl<T: Clone> RTree<T>
impl<T: Clone> RTree<T>
Source§impl<T: Clone> RTree<T>
impl<T: Clone> RTree<T>
Sourcepub fn optimize(&mut self) -> SpatialResult<()>
pub fn optimize(&mut self) -> SpatialResult<()>
Optimize the R-tree by rebuilding it with the current data
This can significantly improve query performance by reducing overlap and creating a more balanced tree.
§Returns
A SpatialResult containing nothing if successful
Sourcepub fn bulk_load(
ndim: usize,
min_entries: usize,
max_entries: usize,
points: Vec<(Array1<f64>, T)>,
) -> SpatialResult<Self>
pub fn bulk_load( ndim: usize, min_entries: usize, max_entries: usize, points: Vec<(Array1<f64>, T)>, ) -> SpatialResult<Self>
Sourcepub fn calculate_total_overlap(&self) -> SpatialResult<f64>
pub fn calculate_total_overlap(&self) -> SpatialResult<f64>
Calculate the total overlap in the R-tree
This is a quality metric for the tree. Lower overlap generally means better query performance.
§Returns
The total overlap area between all pairs of nodes at each level
Source§impl<T: Clone> RTree<T>
impl<T: Clone> RTree<T>
Sourcepub fn search_range(
&self,
min: &ArrayView1<'_, f64>,
max: &ArrayView1<'_, f64>,
) -> SpatialResult<Vec<(usize, T)>>
pub fn search_range( &self, min: &ArrayView1<'_, f64>, max: &ArrayView1<'_, f64>, ) -> SpatialResult<Vec<(usize, T)>>
Sourcepub fn nearest(
&self,
point: &ArrayView1<'_, f64>,
k: usize,
) -> SpatialResult<Vec<(usize, T, f64)>>
pub fn nearest( &self, point: &ArrayView1<'_, f64>, k: usize, ) -> SpatialResult<Vec<(usize, T, f64)>>
Find the k nearest neighbors to a query point
§Arguments
point- The query pointk- The number of nearest neighbors to find
§Returns
A SpatialResult containing a vector of (index, data, distance) tuples for the k nearest data points,
sorted by distance (closest first), or an error if the point has invalid dimensions
Sourcepub fn spatial_join<U, P>(
&self,
other: &RTree<U>,
predicate: P,
) -> SpatialResult<Vec<(T, U)>>
pub fn spatial_join<U, P>( &self, other: &RTree<U>, predicate: P, ) -> SpatialResult<Vec<(T, U)>>
Perform a spatial join between this R-tree and another
§Arguments
other- The other R-tree to join withpredicate- A function that takes MBRs from both trees and returns true if they should be joined, e.g., for an intersection join:|mbr1, mbr2| mbr1.intersects(mbr2)
§Returns
A SpatialResult containing a vector of pairs of data from both trees that satisfy the predicate,
or an error if the R-trees have different dimensions
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for RTree<T>
impl<T> RefUnwindSafe for RTree<T>where
T: RefUnwindSafe,
impl<T> Send for RTree<T>where
T: Send,
impl<T> Sync for RTree<T>where
T: Sync,
impl<T> Unpin for RTree<T>where
T: Unpin,
impl<T> UnwindSafe for RTree<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.