BoxIndex

Struct BoxIndex 

Source
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>);

Fields§

§bbox: BBox

full bounding box of the index

Implementations§

Source§

impl<T: BoxIndexAccessor + Clone> BoxIndex<T>

Source

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 implement BoxIndexAccessor.
  • 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).
Source

pub fn index(&mut self)

Perform indexing of the added rectangles/points after all data has been added about the items.

Source

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.

Source

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.

Source

pub fn take(&mut self) -> Vec<T>

Get the items that have been added to the index. If taken, this index will be empty.

Trait Implementations§

Source§

impl<T: Clone + BoxIndexAccessor + Clone> Clone for BoxIndex<T>

Source§

fn clone(&self) -> BoxIndex<T>

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + BoxIndexAccessor + Clone> Debug for BoxIndex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq + BoxIndexAccessor + Clone> PartialEq for BoxIndex<T>

Source§

fn eq(&self, other: &BoxIndex<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V