Situs User Guide
Situs is a Rust crate of spatial indexes over f64 boxes, with no
false negatives.
Overview
Situs answers two questions of a set of stored boxes: which of them might meet a query box, and which might lie nearest to a query point. The index operates as a candidate filter rather than a decision procedure. It returns every entry that could be an answer, and the caller decides exactly among them, so situs itself decides nothing geometric.
The index never omits an answer. Every comparison is made on stored f64
values, or on certified bounds whose error can only add candidates, and no distance is presented as exact.
A box query in fact returns exactly the entries whose boxes meet the window: arithmetic determines
only the shape of the tree, never its contents.
The crate provides two index types over (Rect, u32) entries:
Indexis static and bulk-loaded by Sort-Tile-Recursive packing. It is compact and the fastest to query, suited to a set that is built once and queried many times.DynamicIndexis an R*-tree with insertion and exact removal, whose bounds shrink as entries leave. It suits a set that changes.
The guide has four parts: the guarantee and the argument behind it, boxes and the static index, the dynamic index, and nearest neighbors.
The crate is #![no_std] with alloc, has no dependencies by default, forbids
unsafe code, and does not panic on any input it accepts.
Conventions
A few conventions hold throughout the crate.
- Boxes are closed. A
Rectis an axis-aligned box with closed intervals on both axes, so boxes that touch along an edge or at a corner meet. A contact is an intersection. - A
Rectthat exists is valid.Rect::newrefuses a NaN or infinite bound and an inverted interval, and nothing else. A box of zero width, zero height or both is a legitimate entry and a legitimate query. - An id is a key rather than a payload. Each entry carries a
u32chosen by the caller, which need not be unique, so the index remains independent of what it indexes. The caller maps each id back to its own data. - Distances are bounds. A nearest-neighbor result carries certified bounds,
lower_sq ≤ d² ≤ upper_sq, on the exact squared distance, never a distance presented as exact. - Results are deterministic. The same entries in the same order give the same tree and the same results. Visit orders are deterministic rather than meaningful.
- Examples are short Rust programs whose results are shown with
assert_eq!. Every example in this guide is compiled and run against the published crate.
Citation
If you use situs in academic work, please cite the software. The DOI below always resolves to the latest version. The methods it implements are listed under References.
Erlick, J. (2026). situs (Version 0.1.0) [Computer software]. https://doi.org/10.5281/zenodo.24064034
Installation
Requirements
- Rust 1.85 or later, edition 2024
- No dependencies by default. The crate works in
#![no_std]crates with an allocator.
From crates.io
cargo add situs
or add it to Cargo.toml directly:
[dependencies]
situs = "0.1"
Cargo features
| Feature | Default | Effect |
|---|---|---|
exact-nearest |
off | Adds nearest_exact to both index types, which ranks the nearest-neighbor candidates
exactly with the exact distance comparison of
arithmetica (see
Exact ranking). It adds arithmetica, also
no_std, as the only dependency.
|
cargo add situs --features exact-nearest
From source
git clone https://github.com/JustinGeoSci/situs.git
cd situs
cargo build
cargo test
API reference documentation for every release is published on docs.rs.
Quick start
Bulk-load a static index and query it with a box, then grow and shrink a dynamic index and ask it for the nearest entry:
use situs::{DynamicIndex, Index, Rect};
let a = Rect::new(0.0, 0.0, 1.0, 1.0).unwrap();
let b = Rect::new(5.0, 5.0, 6.0, 6.0).unwrap();
// A static index, built once.
let index = Index::bulk_load(vec![(a, 0), (b, 1)]);
let mut hits = Vec::new();
index.query(&Rect::new(1.0, 1.0, 2.0, 2.0).unwrap(), |id| hits.push(id));
assert_eq!(hits, [0]); // touching at a corner counts
// A dynamic index, changed in place.
let mut dynamic = DynamicIndex::new();
dynamic.insert(a, 0);
dynamic.insert(b, 1);
assert!(dynamic.remove(&a, 0));
assert_eq!(dynamic.bounds(), Some(b)); // the bounds shrink as entries leave
// The nearest entry to (4, 4), with certified bounds on d² = 2.
let near = dynamic.nearest_candidates(4.0, 4.0, 1).unwrap();
assert_eq!(near[0].id, 1);
assert!(near[0].lower_sq <= 2.0 && 2.0 <= near[0].upper_sq);
Boxes and the static index covers queries and joins, and The guarantee explains why no answer is ever lost.