Skip to content

Commit

Permalink
Fix invalid values from CoverageUnionNG (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Jan 15, 2025
1 parent db50ec0 commit 8f7a7df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/operation/overlayng/CoverageUnion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace overlayng { // geos.operation.overlayng
std::unique_ptr<Geometry>
CoverageUnion::geomunion(const Geometry* coverage)
{
double area_in = coverage->getArea();
std::unique_ptr<Geometry> result;

// a precision model is not needed since no noding is done
Expand All @@ -47,9 +46,10 @@ CoverageUnion::geomunion(const Geometry* coverage)
result = OverlayNG::geomunion(coverage, nullptr, &bcn);
}

double area_out = result->getArea();
double area_in = coverage->getArea();

if (std::abs((area_out - area_in)/area_in) > AREA_PCT_DIFF_TOL) {
if ( (area_in != 0.0) &&
(std::abs((result->getArea() - area_in)/area_in) > AREA_PCT_DIFF_TOL)) {
throw geos::util::TopologyException("CoverageUnion cannot process overlapping inputs.");
}

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/operation/overlayng/CoverageUnionNGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <geos/operation/overlayng/CoverageUnion.h>

// std
#include <cfenv>
#include <memory>

using namespace geos::geom;
Expand All @@ -31,7 +32,9 @@ struct test_coverageunionng_data {
{
std::unique_ptr<Geometry> geom = r.read(wkt);
std::unique_ptr<Geometry> expected = r.read(wktExpected);
std::feclearexcept(FE_ALL_EXCEPT);
std::unique_ptr<Geometry> result = CoverageUnion::geomunion(geom.get());
ensure("FE_INVALID raised", !std::fetestexcept(FE_INVALID));

try {
ensure_equals_geometry_xyzm(result.get(), expected.get());
Expand Down Expand Up @@ -220,4 +223,31 @@ void object::test<17>()
"POLYGON M ((0 0 0, 1 0 1, 1 1 2, 0 1 3, 0 0 0))");
}

// Check empty polygon
template<>
template<>
void object::test<18>()
{
checkUnion("POLYGON EMPTY",
"POLYGON EMPTY");
}

// Check empty GeometryCollection, with M dimension
template<>
template<>
void object::test<19>()
{
checkUnion("GEOMETRYCOLLECTION M EMPTY",
"GEOMETRYCOLLECTION M EMPTY");
}

// Check GeometryCollection of empty polygon
template<>
template<>
void object::test<20>()
{
checkUnion("GEOMETRYCOLLECTION( POLYGON EMPTY )",
"POLYGON EMPTY");
}

} // namespace tut

0 comments on commit 8f7a7df

Please sign in to comment.