geo_index/kdtree/
mod.rs

1//! An immutable, ABI-stable K-D Tree.
2//!
3//! ### Creation
4//!
5//! Use [`KDTreeBuilder`] to construct an [`KDTree`], which allows you to make queries.
6//!
7//! ### Search
8//!
9//! Use [`KDTreeIndex::range`] to search a KDTree given a bounding box query. Use
10//! [`KDTreeIndex::within`] to search a KDTree given a point and radius.
11//!
12//! ### Persisting
13//!
14//! You can use [`KDTree::into_inner`] to access the underlying `Vec<u8>` it contains.
15//!
16//! ### Recovering the index
17//!
18//! You can use [`KDTreeRef::try_new`] to construct a KDTree as a reference on an external byte
19//! slice. If you don't know the coordinate type used in the index, you can use
20//! [`CoordType::from_buffer`][crate::CoordType::from_buffer] to infer the coordinate type.
21//!
22//! ### Coordinate types
23//!
24//! Supported coordinate types implement [`IndexableNum`][crate::IndexableNum]. Note that float
25//! `NaN` is not supported and may panic.
26//!
27//! ## Example
28//!
29//! ```
30//! use geo_index::kdtree::{KDTreeBuilder, KDTreeIndex, KDTreeRef};
31//!
32//! // Create a KDTree
33//! let mut builder = KDTreeBuilder::<f64>::new(3);
34//! builder.add(0., 0.);
35//! builder.add(1., 1.);
36//! builder.add(2., 2.);
37//! let tree = builder.finish();
38//!
39//! // Perform a search
40//! assert_eq!(tree.range(0.5, 0.5, 1.5, 1.5), vec![1]);
41//!
42//! // Convert to underlying buffer
43//! let buffer = tree.into_inner();
44//!
45//! // Create tree as a reference onto this buffer
46//! let tree_ref = KDTreeRef::<f64>::try_new(&buffer).unwrap();
47//!
48//! // Perform search again
49//! assert_eq!(tree_ref.range(0.5, 0.5, 1.5, 1.5), vec![1]);
50//! ```
51
52mod builder;
53pub(crate) mod constants;
54mod index;
55mod r#trait;
56mod traversal;
57
58pub use builder::{KDTreeBuilder, DEFAULT_KDTREE_NODE_SIZE};
59pub use index::{KDTree, KDTreeMetadata, KDTreeRef};
60pub use r#trait::KDTreeIndex;
61pub use traversal::Node;
62
63#[cfg(test)]
64mod test;