boost :: geometry :: union没有结果

时间:2014-03-07 19:15:16

标签: c++ boost boost-geometry

我正在尝试使用boost :: geometry :: union_合并一些多边形,但某些条目的输出向量为空。

这是一个例子:

#include <iostream>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

#include <boost/foreach.hpp>

int main()
{
    typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;

polygon green, blue;

boost::geometry::read_wkt(
    "POLYGON((440820.110024126 4047009.80267429, 440806.545727707 4046942.39533656, 440797.171880196 4046895.8425726, 440804.202135392 4046888.73092639, 440813.573458131 4046935.27114371, 440827.140279322 4047002.69102807))", green);

boost::geometry::read_wkt(
    "POLYGON((440855.857887967 4046932.1248641, 440813.740724389 4046942.21109839, 440806.376538684 4046935.44583646, 440848.493702262 4046925.35960217))", blue);


if(boost::geometry::area(green) < 0) boost::geometry::reverse(green);
if(boost::geometry::area(blue) < 0 ) boost::geometry::reverse(blue);


std::vector<polygon> output;
boost::geometry::union_(green, blue, output);

std::cout << " output size is : " << output.size() << std::endl;

int i = 0;
std::cout << "green || blue:" << std::endl;

BOOST_FOREACH(polygon const& p, output)
{
    std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
}

system("pause");
return 0;
}

当我画出我获得的两个多边形时:enter image description here

但上面代码的结果是:

output size is : 0
green || blue:
Press any key to continue . . .

这是这个函数中的错误还是我的代码中有错误?

1 个答案:

答案 0 :(得分:3)

为提高效率,提升Geomwetry算法假设输入是“标准化”或“规范”。

所以你必须确保你的输入多边形是:

correct(green);
correct(blue);

boost::geometry::union_(green, blue, output);

int i = 0;
for(polygon const& p: output)
{
    std::cout << i++ << ": " << boost::geometry::area(p) << "\n";
    std::cout << "Shape: " << dsv(p) << "\n";
}

打印:

  

0: 1289.08
  Shape: (((440813, 4.04693e+06), (440804, 4.04689e+06), (440797, 4.0469e+06), (440807, 4.04694e+06), (440820, 4.04701e+06), (440827, 4.047e+06), (440815, 4.04694e+06), (440856, 4.04693e+06), (440848, 4.04693e+06), (440813, 4.04693e+06)))

查看 Live on Coliru

完整示例

#include <iostream>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

#include <boost/foreach.hpp>

int main()
{
    typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
    using boost::geometry::correct;
    using boost::geometry::dsv;

    polygon green, blue;

    boost::geometry::read_wkt(
            "POLYGON((440820.110024126 4047009.80267429, 440806.545727707 4046942.39533656, 440797.171880196 4046895.8425726, 440804.202135392 4046888.73092639, 440813.573458131 4046935.27114371, 440827.140279322 4047002.69102807))", 
            green);
    boost::geometry::read_wkt(
            "POLYGON((440855.857887967 4046932.1248641, 440813.740724389 4046942.21109839, 440806.376538684 4046935.44583646, 440848.493702262 4046925.35960217))", 
            blue);

    correct(green);
    correct(blue);

    std::vector<polygon> output;
    boost::geometry::union_(green, blue, output);

    int i = 0;
    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << "\n";
        std::cout << "Shape: " << dsv(p) << "\n";
    }
}