使用c ++和boost :: geometry库存在所有可能无效几何的数据集?或至少我可以转换为boost :: geometry的无效几何的多边形坐标 示例:Selfintersection等 我想用最少的所有posibles无效几何测试我的应用程序。 像这样:
https://knowledge.safe.com/articles/21674/invalid-ogc-geometry-examples.html
但是有更多内部和外部多边形的测试用例。
答案 0 :(得分:0)
Boost Geometry库实现了OGC标准。来自intro
该库遵循现有惯例:
来自Boost 的
- 约定 来自std库的
- 约定
- 来自OGC几何标准之一的约定和名称,更具体地说,来自OGC Simple Feature Specification
因此,您使用的列表是相关的。
此外,您可以使用带有is_valid
参数的reason
函数来查询有关几何图形的库。我在这个网站上有几个例子说明了如何做到这一点。 (注意:并非所有约束都可以验证)
我们采用样本的外环方向(不是BG默认值):
namespace bg = boost::geometry;
using pt = bg::model::d2::point_xy<double>;
using poly = bg::model::polygon<pt, false>;
using multi = bg::model::multi_polygon<poly>;
让我们创建一个通用的检查器:
template <typename Geo = poly> void check(std::string wkt) {
Geo g;
bg::read_wkt(wkt, g);
std::string reason;
bool ok = bg::is_valid(g, reason);
std::cout << "Valid: " << std::boolalpha << ok << " (" << reason << ")\n";
bg::correct(g);
if (bg::is_valid(g, reason)) {
std::cout << "Autocorrected: " << bg::wkt(g) << "\n";
}
}
为所有测试用例运行它:
//Hole Outside Shell
check("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (15 15, 15 20, 20 20, 20 15, 15 15))");
//Nested Holes
check("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2), (3 3, 3 7, 7 7, 7 3, 3 3))");
//Disconnected Interior
check("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (5 0, 10 5, 5 10, 0 5, 5 0))");
//Self Intersection
check("POLYGON((0 0, 10 10, 0 10, 10 0, 0 0))");
//Ring Self Intersection
check("POLYGON((5 0, 10 0, 10 10, 0 10, 0 0, 5 0, 3 3, 5 6, 7 3, 5 0))");
//Nested Shells
check<multi>("MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),(( 2 2, 8 2, 8 8, 2 8, 2 2)))");
//Duplicated Rings
check<multi>("MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),((0 0, 10 0, 10 10, 0 10, 0 0)))");
//Too Few Points
check("POLYGON((2 2, 8 2))");
//Invalid Coordinate
check("POLYGON((NaN 3, 3 4, 4 4, 4 3, 3 3))");
//Ring Not Closed
check("POLYGON((0 0, 0 10, 10 10, 10 0))");
<强> Live On Coliru 强>
打印
Valid: false (Geometry has interior rings defined outside the outer boundary)
Valid: false (Geometry has nested interior rings)
Valid: false (Geometry has wrong orientation)
Valid: false (Geometry has wrong orientation)
Valid: false (Geometry has invalid self-intersections. A self-intersection point was found at (5, 0); method: t; operations: i/i; segment IDs {source, multi, ring, segment}: {0, -1, -1, 4}/{0, -1, -1, 8})
Valid: false (Multi-polygon has intersecting interiors)
Valid: false (Geometry has invalid self-intersections. A self-intersection point was found at (10, 0); method: e; operations: c/c; segment IDs {source, multi, ring, segment}: {0, 0, -1, 0}/{0, 1, -1, 0})
Valid: false (Geometry has too few points)
Valid: false (Geometry has point(s) with invalid coordinate(s))
Valid: false (Geometry is defined as closed but is open)
Autocorrected: POLYGON((0 0,10 0,10 10,0 10,0 0))
注意:
bg::correct
可能在某些情况下更正/部分问题,但是留下其他问题,而此check
函数无法报告此问题。
答案 1 :(得分:0)
我创建了一个库“boost_geometry_make_valid”,它允许纠正此数据集中描述的错误:
https://github.com/kleunen/boost_geometry_make_valid
我现在使用数据集进行测试,该库能够纠正所有提到的失败。最重要的是,从多边形中删除自相交。