作者:UUUUUUUUUU8 | 来源:互联网 | 2023-10-12 09:46
文章目录1.空间几何关系2.最近邻查询3.函数查询4.作者寄语R树是一种多级平衡树,它是B树在多维空间上的扩展。在R树中存放的数据并不是原始数据,而是这些数据的最小边
文章目录
- 1.空间几何关系
- 2.最近邻查询
- 3.函数查询
- 4.作者寄语
R树是一种多级平衡树,它是B树在多维空间上的扩展。在R树中存放的数据并不是原始数据,而是这些数据的最小边界矩形(MBR),空间对象的MBR被包含于R树的叶结点中。在R树空间索引中,设计一些虚拟的矩形目标,将一些空间位置相近的目标,包含在这个矩形内,这些虚拟的矩形作为空间索引,它含有所包含的空间对象的指针。虚拟矩形还可以进一步细分,即可以再套虚拟矩形形成多级空间索引。
R+树,在R树的构造中,要求虚拟矩形一般尽可能少地重叠,并且一个空间对通常仅被一个虚拟矩形所包含。但空间对象千姿百态,它们的最小矩形范围经常重叠。 R+ 改进R树的空间索引,为了平衡,它允许虚拟矩形相互重叠,并允许一个空间目标被多个虚拟矩形所包含。
在Boost.Geometry中有R树的实现,它依赖Boost.Container, Boost.Core, Boost.Move, Boost.MPL, Boost.Range, Boost.Tuple.这些库。R树的元素都是box(矩形)和整数索引值。R树的实现在Geometry中被很好封装,如果使用它,最主要的需要掌握它的查询技巧。先介绍个简单的例子,希望读者能有个大概 的映像,源码如下: #include
#include
#include
#include
#include
#include
#include
#include
#include
#include namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::d2::point_xy<double, boost::geometry::cs::cartesian> DPoint;
typedef bg::model::box<DPoint> DBox;
typedef std::pair<DBox, unsigned> Value;int main()
{bgi::rtree<Value, bgi::quadratic<16>> rtree;for (unsigned i &#61; 0; i < 10; &#43;&#43;i){DBox b(DPoint(i &#43; 0.0f, i &#43; 0.0f), DPoint(i &#43; 0.5f, i &#43; 0.5f));rtree.insert(std::make_pair(b, i));}DBox query_box(DPoint(0, 0), DPoint(5, 5));std::vector<Value> result_s;rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));std::vector<Value> result_n;rtree.query(bgi::nearest(DPoint(0, 0), 5), std::back_inserter(result_n));std::cout << "spatial query box:" << std::endl;std::cout << bg::wkt<DBox>(query_box) << std::endl;std::cout << "spatial query result:" << std::endl;BOOST_FOREACH(Value const& v, result_s)std::cout << bg::wkt<DBox>(v.first) << " - " << v.second << std::endl;std::cout << "knn query point:" << std::endl;std::cout << bg::wkt<DPoint>(DPoint(0, 0)) << std::endl;std::cout << "knn query result:" << std::endl;BOOST_FOREACH(Value const& v, result_n)std::cout << bg::wkt<DBox>(v.first) << " - " << v.second << std::endl;return 0;
}
在源代码中&#xff0c;有详细的注释&#xff0c;请读者先阅读&#xff0c;从中可以看出能够非常的简单的构建一颗R树&#xff0c;然后非常方便的往R树里添加矩形索引。另外一方面Geometry中提供的R树功能非常多&#xff0c;主要包括3类方式找到目标对象。
1.空间几何关系
上图是官方提供的一张图&#xff0c;表示查询的几何关系。查询样式如下&#xff1a;
rt.query(index::contains(box), std::back_inserter(result));
rt.query(index::covered_by(box), std::back_inserter(result));
rt.query(index::covers(box), std::back_inserter(result));
rt.query(index::disjont(box), std::back_inserter(result));
rt.query(index::intersects(box), std::back_inserter(result));
rt.query(index::overlaps(box), std::back_inserter(result));
rt.query(index::within(box), std::back_inserter(result));
2.最近邻查询
std::vector<Value> returned_values;
Point pt();
rt.query(bgi::nearest(pt, k), std::back_inserter(returned_values));Segment seg();
rt.query(bgi::nearest(seg, k), std::back_inserter(returned_values));
3.函数查询
bool is_red(Value const& v)
{return v.is_red();
}struct is_red_o
{template <typename Value>bool operator()(Value const& v){return v.is_red();}
}
rt.query(index::intersects(box) && index::satisfies(is_red),std::back_inserter(result));
rt.query(index::intersects(box) && index::satisfies(is_red_o()),std::back_inserter(result));
#ifndef BOOST_NO_CXX11_LAMBDAS
rt.query(index::intersects(box) && index::satisfies([](Value const& v) { return v.is_red(); }),std::back_inserter(result));
#endif
R树在几何计算中&#xff0c;最常用的还是空间几何关系查询&#xff0c;同时查询的第一个条件参数还可以采用&&运算符来组合条件&#xff0c;单个可采用非(!)来表示相反的条件&#xff0c;形式如下所示&#xff1a;
rt.query(index::intersects(box1) && !index::within(box2),std::back_inserter(result));
rt.query(index::intersects(box1) && !index::within(box2) && index::overlaps(box3),std::back_inserter(result));index::query(rt, index::nearest(pt, k) && index::within(b), std::back_inserter(returned_values));
BOOST_FOREACH(Value & v, rt | index::adaptors::queried(index::nearest(pt, k) && index::covered_by(b)));
最后简单介绍下一个多边形构建R树的例子&#xff0c;源代码如下所示&#xff1a;
#include
#include
#include
#include
#include
#include
#include
#include
#include namespace bg &#61; boost::geometry;
namespace bgi &#61; boost::geometry::index;
typedef bg::model::point<double, 2, bg::cs::cartesian> DPoint;
typedef bg::model::box<DPoint> DBox;
typedef bg::model::polygon<DPoint, false, false> DPolygon;
typedef std::pair<DBox, unsigned> DValue;int main()
{std::vector<DPolygon> polygons;for (unsigned i &#61; 0; i < 10; &#43;&#43;i){DPolygon p;for (float a &#61; 0; a < 6.28316f; a &#43;&#61; 1.04720f){float x &#61; i &#43; int(10 * ::cos(a))*0.1f;float y &#61; i &#43; int(10 * ::sin(a))*0.1f;p.outer().push_back(DPoint(x, y));}polygons.push_back(p);}std::cout << "generated polygons:" << std::endl;BOOST_FOREACH(DPolygon const& p, polygons)std::cout << bg::wkt<DPolygon>(p) << std::endl;bgi::rtree< DValue, bgi::rstar<16, 4> > rtree; for (unsigned i &#61; 0; i < polygons.size(); &#43;&#43;i){DBox b &#61; bg::return_envelope<DBox>(polygons[i]);rtree.insert(std::make_pair(b, i));}DBox query_box(DPoint(0, 0), DPoint(5, 5));std::vector<DValue> result_s;rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));std::vector<DValue> result_n;rtree.query(bgi::nearest(DPoint(0, 0), 5), std::back_inserter(result_n));std::cout << "spatial query box:" << std::endl;std::cout << bg::wkt<DBox>(query_box) << std::endl;std::cout << "spatial query result:" << std::endl;BOOST_FOREACH(DValue const& v, result_s)std::cout << bg::wkt<DPolygon>(polygons[v.second]) << std::endl;std::cout << "knn query point:" << std::endl;std::cout << bg::wkt<DPoint>(DPoint(0, 0)) << std::endl;std::cout << "knn query result:" << std::endl;BOOST_FOREACH(DValue const& v, result_n)std::cout << bg::wkt<DPolygon>(polygons[v.second]) << std::endl;return 0;
}
4.作者寄语
合理的脚本代码可以有效的提高工作效率&#xff0c;减少重复劳动。