提升几何相交会产生奇怪的结果

Posted

技术标签:

【中文标题】提升几何相交会产生奇怪的结果【英文标题】:boost geometry intersection give strange results 【发布时间】:2015-06-04 09:56:08 【问题描述】:

我想将 boost 几何中的交集函数与一条线和一个多边形一起使用。我希望交点是位于多边形内的线的一部分。

不幸的是,boost geometry 返回位于多边形外部的线部分。这是 boost 几何中的错误还是我的代码有问题?

#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>

namespace bg = boost::geometry;

using value_type = double ;
using cs_type = bg::cs::cartesian;
using point_type = bg::model::point< value_type , 2 , cs_type >;
using polygon_type = bg::model::ring< point_type > ;
using line_string_type = bg::model::linestring< point_type >;
using multi_line_type = bg::model::multi_linestring< line_string_type >;

int main( int argc , char *argv[] )

    line_string_type line;
    line.push_back( point_type  13.37688020921095 , 53.66231710654281  );
    line.push_back( point_type  13.3857295713429 , 53.6636835518369  );
    line.push_back( point_type  13.39213495232734 , 53.66501934623722  );
    line.push_back( point_type  13.39719615524716 , 53.66546436809296  );
    line.push_back( point_type  13.40724694386097 , 53.66240690770171  );

    polygon_type polygon;
    polygon.push_back( point_type  13.35 , 53.64  );
    polygon.push_back( point_type  13.39 , 53.64  );
    polygon.push_back( point_type  13.39 , 53.68  );
    polygon.push_back( point_type  13.35 , 53.68  );
    polygon.push_back( point_type  13.35 , 53.64  );

    multi_line_type intersection;
    bg::intersection( line , polygon , intersection );

    return 0;

【问题讨论】:

【参考方案1】:

确保您的输入几何满足记录的先决条件。

您可以使用bg::correct 解决大多数问题(例如多边形中点的正确逆时针顺序、关闭未闭合的多边形等):

Live On Coliru

#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>

namespace bg = boost::geometry;

using value_type       = double;
using cs_type          = bg::cs::cartesian;
using point_type       = bg::model::point<value_type, 2, cs_type>;
using polygon_type     = bg::model::ring<point_type>;
using line_string_type = bg::model::linestring<point_type>;
using multi_line_type  = bg::model::multi_linestring<line_string_type>;

int main()

    line_string_type line;
    line.push_back(point_type13.37688020921095, 53.66231710654281);
    line.push_back(point_type13.3857295713429,  53.6636835518369);
    line.push_back(point_type13.39213495232734, 53.66501934623722);
    line.push_back(point_type13.39719615524716, 53.66546436809296);
    line.push_back(point_type13.40724694386097, 53.66240690770171);
    bg::correct(line);

    polygon_type polygon;
    polygon.push_back(point_type13.35, 53.64);
    polygon.push_back(point_type13.39, 53.64);
    polygon.push_back(point_type13.39, 53.68);
    polygon.push_back(point_type13.35, 53.68);
    polygon.push_back(point_type13.35, 53.64);
    bg::correct(polygon);

    multi_line_type intersection;
    bg::intersection(line, polygon, intersection);

    std::cout << bg::wkt(intersection);

打印出来

MULTILINESTRING((13.3769 53.6623,13.3857 53.6637,13.39 53.6646))

如果输入 un-corrected,它会改为打印:

MULTILINESTRING((13.39 53.6646,13.3921 53.665,13.3972 53.6655,13.4072 53.6624))

【讨论】:

添加了可视化。有关如何制作精美 SVG 图形的信息:***.com/questions/29078165/boost-intersection/… 很好的答案!现在按预期工作。谢谢@sehe 干杯!我忘了提到is_valid(这需要提升 1.57+ AFAIR)。它们是输入验证的重要工具。处罚可能是 UB,所以请注意安全!

以上是关于提升几何相交会产生奇怪的结果的主要内容,如果未能解决你的问题,请参考以下文章

在我的查询中使用 Point() 会产生奇怪的结果,是我用错了还是意料之中?

简单的字数 MapReduce 示例产生奇怪的结果

SQL IN 查询产生奇怪的结果

为啥减去这两次(在 1927 年)会产生奇怪的结果?

猴子修补对象会产生奇怪的结果

`std::find()` 用字符串产生奇怪的结果