代码之家  ›  专栏  ›  技术社区  ›  SymbolixAU Adam Erickson

如何查询框是否在rtree中

  •  0
  • SymbolixAU Adam Erickson  · 技术社区  · 8 年前

    问题

    为什么我要查询 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)
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   rafix07    8 年前

    当你调用自由函数时 within(geom1,geom2) 如果返回true geom1 在里面 geom2 是的。但是当你读到关于使用 within 作为谓词

    生成定义值和几何关系的谓词。如果bg::within(indexable,geometry)返回true,则查询将返回值。

    所以你想看看 indexable 在里面 geometry ,结果为false[可转位为rect,几何为line]。你应该用 contains 谓词代替 在内部 使用rtree。 包含 谓词等于 bg::within(Geometry, Indexable) 然后你的代码按预期工作。

    推荐文章