在多边形

时间:2018-05-25 22:48:43

标签: c++ boost boost-geometry

我想找到在rtree中索引的所有元素,这些元素与带孔的多边形的外环相交,但是使用Boost C ++库并不完全在任何孔内。

我知道如何让元素与外环相交:

// Constructing the exterior ring polygon
Boost2dRing p;
for (int i = 0; i < numPunts; i++)
{
    x = Punts.at(i).x;
    y = Punts.at(i).y;
    p.push_back(Boost2dPoint(x, y));
}

// Getting the intersecting elements with that polygon
m_RTree.query(bgi::intersects(p), std::back_inserter(res));
...
// Constructing the polygon for the inner ring (hole)
Boost2dRing p;
for (int i = 0; i < numPuntsHole; i++)
{
    x = PuntsHole.at(i).x;
    y = PuntsHole.at(i).y;
    pHole.push_back(Boost2dPoint(x, y));
}

// Now I try to get the elements inside completely this polygon but I get a compilation error
m_RTree.query(bgi::within(pHole), std::back_inserter(res));

错误讯息:

  

错误C2664:'int   提高:: MPL :: assertion_failed(升压:: MPL ::断言::类型)“:   无法从'boost :: mpl :: failed中转换参数1   ************(__ cdecl boost :: geometry :: strategy :: within :: services :: default_strategy :: NOT_IMPLEMENTED_FOR_THESE_TYPES :: *   ***********)(升压:: MPL ::断言_ ::类型)”   'boost :: mpl :: assert :: type'1&gt;用1> [1>   Geometry1 = Boost2dBox,1&gt; Geometry2 = Boost2dRing,1&gt;
  GeometryContained = Boost2dBox,1&gt;
  GeometryContaining = Boost2dRing 1&gt; ]注意:没有构造函数   可以采取源类型,或构造函数重载决议   暧昧

有任何提示可以达到这个目标吗?

1 个答案:

答案 0 :(得分:1)

对于您选择的几何操作数,未实现内部谓词。

但是,你可以通过很多减少工作来做你想做的事。让我们说你有戒指,例如:

Boost2dRing exterior, interior;
bg::read_wkt("POLYGON((0.1 0.1,0.1 0.5,0.5 0.5,0.5 0.1,0.1 0.1))", exterior);
bg::read_wkt("POLYGON((0.2 0.2,0.2 0.4,0.4 0.4,0.4 0.2,0.2 0.2))", interior);

现在,Boost Geometry具有Polygon的概念,它是一个外环和(多个)内环:

  

多边形是多边形是由一个外部边界和零个或多个内部边界(OGC Simple Feature Specification

定义的平面

所以,让我们改用:

bg::reverse(interior);
Boost2dPolygon polygon;
polygon.outer() = exterior;
polygon.inners().push_back(interior);
  

请注意,内圈的方向是反转的。

或者,实际上,直接使用构造函数:

Boost2dPolygon polygon({exterior, interior});

或者,甚至立即从WKT阅读:

bg::read_wkt("POLYGON((0.1 0.1,0.1 0.5,0.5 0.5,0.5 0.1,0.1 0.1) (0.2 0.2,0.4 0.2,0.4 0.4,0.2 0.4,0.2 0.2))", polygon);

现在你可以一次查询:

std::vector<RTree::value_type> res;
m_RTree.query(bgi::intersects(polygon), std::back_inserter(res));

完整演示!

给出以下sample()树内容:

using RTree = bgi::rtree<std::pair<Boost2dBox, std::string>, bgi::rstar<16> >;
RTree sample() {
    RTree tree;

    std::pair<std::string, std::string> items[] = {
        { "BOX(0.2 0.2,0.2 0.4,0.4 0.4,0.4 0.2,0.2 0.2)",           "ok" },
        { "BOX(0.28 0.28,0.28 0.32,0.32 0.32,0.32 0.28,0.28 0.28)", "within gap" },
        { "BOX(0.28 0.28,0.28 0.32,0.36 0.32,0.36 0.28,0.28 0.28)", "small overlap" },
        { "BOX(2 2,2 4,4 4,4 2,2 2)",                               "outside exterior" },
    };
    for (auto& item : items) {
        Boost2dBox box;
        bg::read_wkt(item.first, box);
        checks("box", box);
        tree.insert({box, item.second});
    }

    return tree;
}

我们可以手动测试:

RTree const m_RTree = sample();

std::cout << "Sample tree:\n";
for (auto& value : m_RTree) {
    std::cout << " - " << std::quoted(value.second) << ": " << bg::wkt(value.first) << "\n";

    Boost2dMultiPolygon mp;
    if (bg::intersection(polygon, value.first, mp))
        std::cout << "      (intersection is " << bg::wkt(mp) << ")\n";
}

打印

Sample tree:
 - "ok": POLYGON((0.2 0.2,0.2 0.4,0.4 0.4,0.4 0.2,0.2 0.2))
      (intersection is MULTIPOLYGON(((0.2 0.2,0.2 0.4,0.4 0.4,0.4 0.2,0.2 0.2),(0.25 0.25,0.35 0.25,0.35 0.35,0.25 0.35,0.25 0.25))))
 - "within gap": POLYGON((0.28 0.28,0.28 0.32,0.32 0.32,0.32 0.28,0.28 0.28))
      (intersection is MULTIPOLYGON())
 - "small overlap": POLYGON((0.28 0.28,0.28 0.32,0.36 0.32,0.36 0.28,0.28 0.28))
      (intersection is MULTIPOLYGON(((0.35 0.32,0.36 0.32,0.36 0.28,0.35 0.28,0.35 0.32))))
 - "outside exterior": POLYGON((2 2,2 4,4 4,4 2,2 2))
      (intersection is MULTIPOLYGON())

并验证与树查询比较的结果:

m_RTree.query(bgi::intersects(polygon), std::back_inserter(matches));

std::cout << "Intersecting with: ";
for (auto& match : matches) std::cout << " " << std::quoted(match.second) << " ";

打印哪些:

Intersecting with:  "ok"  "small overlap" 

全部查看 Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/arithmetic/arithmetic.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/geometry/algorithms/intersects.hpp>
#include <boost/geometry/algorithms/envelope.hpp>
#include <boost/geometry/algorithms/intersection.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/geometry/index/predicates.hpp>
#include <boost/geometry/index/adaptors/query.hpp>
#include <boost/geometry/io/io.hpp>
#include <iostream>
#include <fstream>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

using Boost2dPoint        = bg::model::d2::point_xy<double>;
using Boost2dRing         = bg::model::ring<Boost2dPoint>;
using Boost2dPolygon      = bg::model::polygon<Boost2dPoint>;
using Boost2dMultiPolygon = bg::model::multi_polygon<Boost2dPolygon>;
using Boost2dBox          = bg::model::box<Boost2dPoint>;

template <typename G> void checks(std::string name, G& geom) {
    std::cout << name << ": " << bg::wkt(geom) << "\n";

    std::string reason;
    if (!bg::is_valid(geom, reason)) {
        std::cout << name << ": " << reason << "\n";

        bg::correct(geom);

        std::cout << bg::wkt(geom) << "\n";
        if (!bg::is_valid(geom, reason)) {
            std::cout << name << " corrected: " << reason << "\n";
        }
    }
}

using RTree = bgi::rtree<std::pair<Boost2dBox, std::string>, bgi::rstar<16> >;
RTree sample() {
    RTree tree;

    std::pair<std::string, std::string> items[] = {
        { "BOX(0.2 0.2,0.2 0.4,0.4 0.4,0.4 0.2,0.2 0.2)",           "ok" },
        { "BOX(0.28 0.28,0.28 0.32,0.32 0.32,0.32 0.28,0.28 0.28)", "within gap" },
        { "BOX(0.28 0.28,0.28 0.32,0.36 0.32,0.36 0.28,0.28 0.28)", "small overlap" },
        { "BOX(2 2,2 4,4 4,4 2,2 2)",                               "outside exterior" },
    };
    for (auto& item : items) {
        Boost2dBox box;
        bg::read_wkt(item.first, box);
        checks("box", box);
        tree.insert({box, item.second});
    }

    return tree;
}

int main() {
    Boost2dPolygon polygon;
    bg::read_wkt("POLYGON((0.1 0.1,0.1 0.5,0.5 0.5,0.5 0.1,0.1 0.1) (0.25 0.25,0.35 0.25,0.35 0.35,0.25 0.35,0.25 0.25))", polygon);
    checks("polygon", polygon);

    RTree const m_RTree = sample();

    std::cout << "Sample tree:\n";
    for (auto& value : m_RTree) {
        std::cout << " - " << std::quoted(value.second) << ": " << bg::wkt(value.first) << "\n";

        Boost2dMultiPolygon mp;
        if (bg::intersection(polygon, value.first, mp))
            std::cout << "      (intersection is " << bg::wkt(mp) << ")\n";
    }
    std::cout << "\n";

    std::vector<RTree::value_type> matches;
    m_RTree.query(bgi::intersects(polygon), std::back_inserter(matches));

    std::cout << "Intersecting with: ";
    for (auto& match : matches) std::cout << " " << std::quoted(match.second) << " ";
    std::cout << "\n";
}