4.0.0 • Published 1 year ago
rbush-knn v4.0.0
rbush-knn
k-nearest neighbors search for RBush. Implements a simple depth-first kNN search algorithm using a priority queue.
import RBush from 'rbush';
import knn from 'rbush-knn';
const tree = new RBush(); // create RBush tree
tree.load(data); // bulk insert
const neighbors = knn(tree, 40, 40, 10); // return 10 nearest items around point [40, 40]You can optionally pass a filter function to find k neighbors that satisfy a certain condition:
const neighbors = knn(tree, 40, 40, 10, function (item) {
    return item.foo === 'bar';
});API
knn(tree, x, y, k, filterFn, maxDistance)
- tree: an RBush tree
- x,- y: query coordinates
- k: number of neighbors to search for (- Infinityby default)
- filterFn: optional filter function;- knearest items where- filterFn(item) === truewill be returned.
- maxDistance(optional): maximum distance between neighbors and the query coordinates (- Infinityby default)