问题
为什么我要查询
box
在
rtree
返回空结果?
解释
拿着绳子
LINESTRING(1 1, 2 2)
两个多边形
POLYGON((0 0,0 1,1 1,1 0,0 0))
POLYGON((0 0,0 3,3 3,3 0,0 0))
我可以用
boost::geometry::witin()
查询linestring是否在任一多边形内(并且可以工作)
但是,如果我创建
树木
在多边形中,放置一个
箱
在
linestring
,查询
箱
是
within
这个
树木
返回空结果。
例子
这里有一个例子展示了
bg::within(line, polygon)
,以及不工作的
rtree.query(bgi::within( line_box1 ), ...)
void rtree_within() {
typedef bgm::point< double, 2, bg::cs::cartesian > point;
typedef bgm::box<point> box;
typedef bgm::linestring<point> line;
typedef bgm::polygon<point> polygon;
typedef std::pair<box, unsigned> value;
bgi::rtree<value, bgi::quadratic<16> > rtree;
std::vector<value> result_s;
polygon poly1;
polygon poly2;
line line1;
bg::read_wkt("POLYGON((0 0,0 1,1 1,1 0,0 0))", poly1);
bg::read_wkt("POLYGON((0 0,0 3,3 3,3 0,0 0))", poly2);
bg::read_wkt("LINESTRING(1 1, 2 2)", line1);
std::cout << "line2 in poly1: " << bg::within(line1, poly1) << std::endl;
std::cout << "line2 in poly3: " << bg::within(line1, poly2) << std::endl;
// boxes to insert into rtree
box poly_box1 = bg::return_envelope<box>( poly1 );
rtree.insert(std::make_pair(poly_box1, 0));
box poly_box2 = bg::return_envelope<box>( poly2 );
rtree.insert(std::make_pair(poly_box2, 2));
// box around the line
box line_box1 = bg::return_envelope<box>( line1 );
std::cout << "poly_box1: " << bg::wkt( poly_box1 ) << std::endl; // returns 0
std::cout << "poly_box2: " << bg::wkt( poly_box2 ) << std::endl; // returns 1
std::cout << "line_box1: " << bg::wkt( line_box1 ) << std::endl;
rtree.query(bgi::within( line_box1 ), std::back_inserter( result_s ));
std::cout << "line_box1 within rtree - size: " << result_s.size() << std::endl;
// result_s is empty (size == 0)
}