You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在基于C++与boost::geometry的全量无效几何数据集?

Great question—testing edge cases with invalid geometries is critical for building robust geometry processing apps, especially when working with boost::geometry. While there’s no official full-fledged dataset of invalid geometries tailored specifically for boost::geometry, we can work with convertible resources and build our own comprehensive test cases, including those with inner and outer rings. Let’s dive in:

1. Ready-to-Use Invalid Geometry Examples (Compatible with boost::geometry)

Below are concrete examples of invalid polygons (including those with inner rings) that you can directly use with boost::geometry. All examples use WKT format, which is easily parsed into boost's polygon types.

First, define the basic types you’ll need:

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

using Point = boost::geometry::model::d2::point_xy<double>;
using Polygon = boost::geometry::model::polygon<Point>;

Self-Intersecting Outer Ring

A classic "bowtie" polygon where the outer ring crosses itself:

Polygon self_intersecting_outer() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 10, 10 0, 10 10, 0 0))", poly);
    return poly;
}

Why invalid: The outer ring has a self-intersection. boost::geometry::is_valid() will flag this as a failure.

Self-Intersecting Outer Ring with Valid Inner Ring

The outer ring is self-intersecting, but the inner ring is valid:

Polygon self_intersecting_outer_with_inner() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 20, 20 0, 20 20, 0 0), (5 5, 5 15, 15 15, 15 5, 5 5))", poly);
    return poly;
}

Why invalid: The outer ring's self-intersection makes the entire polygon invalid, even though the inner ring is well-formed.

Inner Ring Intersects Outer Ring

An inner ring that crosses the boundary of the outer ring:

Polygon inner_intersects_outer() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 20, 20 20, 20 0, 0 0), (5 5, 5 25, 15 25, 15 5, 5 5))", poly);
    return poly;
}

Why invalid: The inner ring extends outside the outer ring, causing a boundary intersection.

Self-Intersecting Inner Ring

A valid outer ring with a self-intersecting inner ring:

Polygon self_intersecting_inner() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 20, 20 20, 20 0, 0 0), (5 5, 5 15, 15 5, 15 15, 5 5))", poly);
    return poly;
}

Why invalid: The inner ring forms a bowtie shape, which violates OGC geometry rules.

Overlapping Inner Rings

Two inner rings that overlap each other:

Polygon overlapping_inner_rings() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 20, 20 20, 20 0, 0 0), (2 2, 2 10, 10 10, 10 2, 2 2), (6 6, 6 14, 14 14, 14 6, 6 6))", poly);
    return poly;
}

Why invalid: Inner rings must be disjoint (no overlapping areas) per OGC standards.

Inner Ring Located Outside Outer Ring

An inner ring that’s completely outside the bounds of the outer ring:

Polygon inner_outside_outer() {
    Polygon poly;
    boost::geometry::read_wkt("POLYGON((0 0, 0 20, 20 20, 20 0, 0 0), (25 25, 25 35, 35 35, 35 25, 25 25))", poly);
    return poly;
}

Why invalid: Inner rings must be entirely contained within the outer ring.

Incorrect Ring Orientation

A polygon where the outer ring uses clockwise orientation (boost::geometry defaults to counter-clockwise for outer rings, clockwise for inner rings):

Polygon incorrect_ring_orientation() {
    Polygon poly;
    // Clockwise outer ring (invalid by default)
    boost::geometry::read_wkt("POLYGON((0 0, 20 0, 20 20, 0 20, 0 0))", poly);
    return poly;
}

Why invalid: Violates the default winding order rules. You can adjust the validity strategy if needed, but this counts as invalid under boost's default settings.

2. Generating Additional Test Cases

If you need even more invalid geometries, you can easily generate them:

  • Random Perturbation: Take a valid polygon and randomly adjust point coordinates to create self-intersections or ring overlaps.
  • Manual Edits: Modify WKT strings to swap points, extend rings, or create overlaps.
  • boost::geometry Tools: Use functions like buffer() with extreme parameters (e.g., negative buffer sizes that cause self-intersections) to generate invalid shapes.
3. Validating Geometries with boost::geometry

To confirm these are invalid, use boost's validity check function:

#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/algorithms/is_valid.hpp>
#include <iostream>

void check_geometry_validity(const Polygon& poly) {
    std::string failure_reason;
    bool is_valid = boost::geometry::is_valid(poly, failure_reason);
    
    if (!is_valid) {
        std::cout << "Invalid geometry: " << failure_reason << std::endl;
    } else {
        std::cout << "Valid geometry" << std::endl;
    }
}

// Example usage:
// check_geometry_validity(self_intersecting_outer());

内容的提问来源于stack exchange,提问作者Ruben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:45:08