我需要使用给定的数据点构建一个R树。我已经搜索了R树的实现。我找到的所有实现都是在给定矩形坐标作为输入时构建R树。但我需要在给定数据点本身(可以是一维的)时构建R树。代码应该负责创建包含这些数据点的矩形并构建R树。
我需要使用给定的数据点构建一个R树。我已经搜索了R树的实现。我找到的所有实现都是在给定矩形坐标作为输入时构建R树。但我需要在给定数据点本身(可以是一维的)时构建R树。代码应该负责创建包含这些数据点的矩形并构建R树。
在编程中,使用最小外接矩形(Minimum bounding rectangle,MBRs),其中min = max = 坐标。所有人都是这样做的。但好的实现将会在叶子节点中存储点数据的容量大约是目录节点的两倍。
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <vector>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
int main()
{
typedef bg::model::point<float, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
// a container of points
std::vector<point> points;
// create the rtree
bgi::rtree< point, bgi::linear<16> > rtree(points.begin(), points.end());
// insert some additional points
rtree.insert(point(/*...*/));
rtree.insert(point(/*...*/));
// find points intersecting a box
std::vector<point> query_result;
rtree.query(bgi::intersects(box(/*...*/)), std::back_inserter(query_result));
// do something with the result
}
我认为使用R树来存储点似乎是一种误用。尽管这种结构被指示用于存储空间数据,但经过一些研究,我发现它最适合存储非零面积区域(因为名称中的 R 代表 Region 或 Rectangle)。创建一个简单表格并建立良好索引应该能够提供更好的性能,无论是更新还是搜索数据。请考虑下面的例子:
CREATE TABLE locations (id, latitude, longitude);
CREATE INDEX idx_locations ON locations (latitude, longitude);
优于
CREATE VIRTUAL TABLE locations USING rtree( id, minLatitude, maxLatitude, minLongitude, maxLongitude);
如果您只是计划在每一行中重复minLatitude和maxLatitude以及minLongitude和maxLongitude,以表示点而不是矩形。尽管后一种方法将按预期工作,但R树适用于索引矩形区域,并且将它们用于存储点是最差性能的误用。建议使用上述复合索引。
min=max=coordinate
。在叶子级别存储点而不是MBR,我们会得到大约两倍数量的对象,并将磁盘空间减少近2倍。 - Has QUIT--Anony-Mousse