美文网首页
空间查询算法RBush

空间查询算法RBush

作者: 不决书 | 来源:发表于2023-06-11 08:31 被阅读0次

方案

https://github.com/mourner/rbush

Usage

Importing RBush

// as a ES module
import RBush from 'rbush';

// as a CommonJS module
const RBush = require('rbush');

Creating a Tree

const tree = new RBush();

An optional argument to RBush defines the maximum number of entries in a tree node. 9 (used by default) is a reasonable choice for most applications. Higher value means faster insertion and slower search, and vice versa.

const tree = new RBush(16);

Adding Data

Insert an item:

const item = {
    minX: 20,
    minY: 40,
    maxX: 30,
    maxY: 50,
    foo: 'bar'
};
tree.insert(item);

Removing Data

Remove a previously inserted item:

tree.remove(item);

By default, RBush removes objects by reference. However, you can pass a custom equals function to compare by value for removal, which is useful when you only have a copy of the object you need removed (e.g. loaded from server):

tree.remove(itemCopy, (a, b) => {
    return a.id === b.id;
});

Remove all items:

tree.clear();

Data Format

By default, RBush assumes the format of data points to be an object with minX, minY, maxX and maxY properties. You can customize this by overriding toBBox, compareMinX and compareMinY methods like this:

class MyRBush extends RBush {
    toBBox([x, y]) { return {minX: x, minY: y, maxX: x, maxY: y}; }
    compareMinX(a, b) { return a.x - b.x; }
    compareMinY(a, b) { return a.y - b.y; }
}
const tree = new MyRBush();
tree.insert([20, 50]); // accepts [x, y] points

If you're indexing a static list of points (you don't need to add/remove points after indexing), you should use kdbush which performs point indexing 5-8x faster than RBush.

Bulk-Inserting Data

Bulk-insert the given data into the tree:

tree.load([item1, item2, ...]);

Bulk insertion is usually ~2-3 times faster than inserting items one by one. After bulk loading (bulk insertion into an empty tree), subsequent query performance is also ~20-30% better.

Note that when you do bulk insertion into an existing tree, it bulk-loads the given data into a separate tree and inserts the smaller tree into the larger tree. This means that bulk insertion works very well for clustered data (where items in one update are close to each other), but makes query performance worse if the data is scattered.

Search

const result = tree.search({
    minX: 40,
    minY: 20,
    maxX: 80,
    maxY: 70
});

Returns an array of data items (points or rectangles) that the given bounding box intersects.

Note that the search method accepts a bounding box in {minX, minY, maxX, maxY} format regardless of the data format.

const allItems = tree.all();

Returns all items of the tree.

Collisions

const result = tree.collides({minX: 40, minY: 20, maxX: 80, maxY: 70});

Returns true if there are any items intersecting the given bounding box, otherwise false.

Export and Import

// export data as JSON object
const treeData = tree.toJSON();

// import previously exported data

相关文章

  • 图片连接成线

    需要rbush依赖

  • PostGIS函数应用(一)

    先展示查询的效果: (1)几何图形空间查询: (2)Buffer空间查询: (3)查询函数 PostGIS空间查询...

  • 创建表空间及用户

    临时表空间 数据表空间 创建用户 赋予权限 查询所有用户 查询所有临时表空间 查询所有表空间 删除用户 删除表空间...

  • 字典树

    功能 字典树是用数组存储大量字符串的一种算法 字典树算法开辟空间非常大,但是对于字符串插入和查询有很快的速度 用法...

  • 快速排序和堆排序算法

    快速排序算法能够快速排序列表或查询。它基于分割交换排序的原则,这种类型的算法占用空间较小,它将待排序列表分为三个主...

  • oracle 常用指令

    oracle常用指令 表空间查询 查询表空间中对象的详细信息 重建索引 创建表空间 查询表文件是否自动扩展 优化表...

  • arcgis js 4.14中针对sceneLayerView查

    最近针对scenelayer进行空间查询,SceneLayer不支持空间查询,用到sceneLayerView,这...

  • 查询算法

    1、二分查找 二分查找的时间复杂度是O(logn),查找数据的效率非常高。不过,并不是什么情况下都可以用二分查找,...

  • Kubernetes 常用命令

    查询组件状态 查询名字空间 查询节点 查看kubedns状态 查看kubedns日志

  • mongoose 空间查询

      工作中我用了Koa2做了后台,选用了mongo数据库。因为要用到空间查询显示当前地图视图的空间查询结果,经过一...

网友评论

      本文标题:空间查询算法RBush

      本文链接:https://www.haomeiwen.com/subject/tfuxydtx.html