How Flatbush works: Static Spatial Indexing
mainFlatbush is a static spatial index for 2D points and rectangles using the packed Hilbert R-tree algorithm.
Key Characteristics:
- Static: You cannot add or remove items once the index is built. You must define the number of items upfront, add them, and then call
.finish(). - Performance: Optimized for fast indexing and searching with a low memory footprint.
- Binary Storage: The index is stored in a single
ArrayBuffer(accessible viaindex.data), making it easy to transfer between threads (e.g., viapostMessage) or save to a file.
Typical Workflow:
- Initialize with the total number of items.
- Add items using
.add(). - Finalize the index with
.finish(). - Perform queries using
.search()or.neighbors().
const index = new Flatbush(1000);
for (const p of items) {
index.add(p.minX, p.minY, p.maxX, p.maxY);
}
index.finish();
const found = index.search(minX, minY, maxX, maxY).map((i) => items[i]);