k-nearest neighbors search for RBush. Implements a simple depth-first kNN search algorithm using a priority queue.
var rbush = require('rbush');
var knn = require('rbush-knn');
var tree = rbush().load(data); // create an RBush index
var 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:
var neighbors = knn(tree, 40, 40, 10, function (item) {
return item.foo === 'bar';
});
knn(tree, x, y, [k, filterFn, maxDistance])
tree
: an RBush treex
,y
: query coordinatesk
: number of neighbors to search for (Infinity
by default)filterFn
: optional filter function;k
nearest items wherefilterFn(item) === true
will be returned.maxDistance
(optional): maximum distance between neighbors and the query coordinates (Infinity
by default)
- Breaking: updated to be compatible with RBush 2.0.
- Breaking: signature changed from
tree, [x, y], k, filterFn
totree, x, y, k, filterFn
- Improved performance by ~20%.
- Add an optional filter function argument.
- 2.5x performance improvement!
- Fixed an error when requesting more items than the tree has. #1
- Initial release.