pub struct BoxIndex<T: BoxIndexAccessor + Clone> {
pub bbox: BBox,
/* private fields */
}Expand description
§BoxIndex
§Description
An Index for points and rectangles
A really fast static spatial index for 2D points and rectangles in JavaScript. Uses either a fast simple Hilbert curve algorithm or a more complex Hilbert curve (S2) algorithm.
This is a partial port/rust port of the flatbush codebase. The port is designed to work with this library and also S2 cells.
The default hilbert curve ported is simpler and originally designed for simpler datasets. This library also provides the S2 hilbert curve for cm precision of the Earth using S2 cells.
§Usage
use gistools::data_structures::{BoxIndex, BoxIndexAccessor};
use gistools::proj::Coords;
use s2json::BBox;
// Test item
#[derive(Debug, Clone, PartialEq)]
struct Item {
pub id: usize,
pub min_x: f64,
pub min_y: f64,
pub max_x: f64,
pub max_y: f64,
}
// define how to access the min_x, min_y, max_x, and max_y properties
impl BoxIndexAccessor for Item {
fn bbox(&self) -> BBox {
BBox::new(self.min_x, self.min_y, self.max_x, self.max_y)
}
}
// create the index
let mut index = BoxIndex::<Item>::new(vec![], None);
// make a bounding box query
let items = index.search(&BBox::new(40.0, 40.0, 60.0, 60.0), None::<fn(&Item) -> bool>);
// make a k-nearest-neighbors query
let items =
index.neighbors(Coords::new_xy(50., 50.), Some(3), None, None::<fn(&Item) -> bool>);§Links
Fields§
§bbox: BBoxfull bounding box of the index
Implementations§
Source§impl<T: BoxIndexAccessor + Clone> BoxIndex<T>
impl<T: BoxIndexAccessor + Clone> BoxIndex<T>
Sourcepub fn new(items: Vec<T>, node_size: Option<usize>) -> Self
pub fn new(items: Vec<T>, node_size: Option<usize>) -> Self
Create a BoxIndex index that will hold a given number of items.
§Parameters
items: The items to index. Requires the item type to implementBoxIndexAccessor.accessor: A function for accessing the min_x, min_y, max_x, and max_y properties of the items.[node_size]: Size of the tree node (16 by default).
Sourcepub fn index(&mut self)
pub fn index(&mut self)
Perform indexing of the added rectangles/points after all data has been added about the items.
Sourcepub fn search(
&self,
bbox: &BBox,
filter_fn: Option<impl Fn(&T) -> bool>,
) -> Vec<T>
pub fn search( &self, bbox: &BBox, filter_fn: Option<impl Fn(&T) -> bool>, ) -> Vec<T>
Search the index by a bounding box.
§Parameters
min_x: The minimum x coordinate of the query point.min_y: The minimum y coordinate of the query point.max_x: The maximum x coordinate of the query point.max_y: The maximum y coordinate of the query point.[filter_fn]: An optional function that is called on every found item; if supplied, only items for which this function returns true will be included in the results array.
§Returns
An array of indices of items intersecting or touching the given bounding box.
Sourcepub fn neighbors<P: GetXY>(
&self,
p: P,
max_results: Option<usize>,
max_distance: Option<f64>,
filter_fn: Option<impl Fn(&T) -> bool>,
) -> Vec<T>
pub fn neighbors<P: GetXY>( &self, p: P, max_results: Option<usize>, max_distance: Option<f64>, filter_fn: Option<impl Fn(&T) -> bool>, ) -> Vec<T>
Search items in order of distance from the given point.
§Parameters
x: The x coordinate of the query point.y: The y coordinate of the query point.[max_results]: The maximum number of results to return.[max_distance]: The maximum distance to search.[filter_fn]: An optional function for filtering the results.
§Returns
An array of indices of items found.
Trait Implementations§
impl<T: BoxIndexAccessor + Clone> StructuralPartialEq for BoxIndex<T>
Auto Trait Implementations§
impl<T> Freeze for BoxIndex<T>
impl<T> RefUnwindSafe for BoxIndex<T>where
T: RefUnwindSafe,
impl<T> Send for BoxIndex<T>where
T: Send,
impl<T> Sync for BoxIndex<T>where
T: Sync,
impl<T> Unpin for BoxIndex<T>where
T: Unpin,
impl<T> UnwindSafe for BoxIndex<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)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 more