Skip to content

Commit

Permalink
Handle edge case when a multypolygon is created after GEOSSimplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Ylannl committed Jan 20, 2025
1 parent cf78de1 commit cfce345
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/roofer-app/roofer-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ int main(int argc, const char* argv[]) {
}
}
sorting_running.store(false);
sorted_pending.notify_one();
logger.debug("[sorter] Finished sorter");
});

Expand Down
27 changes: 23 additions & 4 deletions src/extra/misc/Vector2DOpsGEOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <geos_c.h>
#include <roofer/logger/logger.h>

#include <__config>
#include <roofer/misc/Vector2DOps.hpp>
#include <vector>

Expand All @@ -48,6 +49,8 @@ namespace roofer::misc {
}
bool orient_polygon(GEOSGeometry*& g_polygon, ORIENTATION orientation) {
const GEOSGeometry* g_ring = GEOSGetExteriorRing_r(gc, g_polygon);
if (g_ring == nullptr)
throw std::runtime_error("orient_polygon: unable to get exterior ring");
GEOSGeometry* g_ring_ = orient_ring(g_ring, orientation);
bool reversed = false;
if (!g_ring_) {
Expand Down Expand Up @@ -212,19 +215,35 @@ namespace roofer::misc {
to_geos_polygon(lr, g_polygon);

if (GEOSisValid_r(gc, g_polygon) != 1) {
logger.info("feature not valid");
logger.error("Encouterend feature that is not valid. WKT: {}",
GEOSGeomToWKT_r(gc, g_polygon));
GEOSGeom_destroy_r(gc, g_polygon);
// if (output_failures) polygons_out.push_back(lr);
continue;
throw std::runtime_error("feature not valid");
}

GEOSGeometry* simplified_geom =
GEOSSimplify_r(gc, g_polygon, double(tolerance));
if (GEOSisValid_r(gc, simplified_geom) != 1) {
logger.info("feature not valid after simplify");
logger.error(
"Encouterend feature that is not valid after simplify. WKT: {}",
GEOSGeomToWKT_r(gc, simplified_geom));
GEOSGeom_destroy_r(gc, g_polygon);
GEOSGeom_destroy_r(gc, simplified_geom);
// if (output_failures) polygons_out.push_back(lr);
continue;
throw std::runtime_error("feature not valid after simplify");
}
// dump simplified geometry to wkt
if (GEOSGetNumGeometries_r(gc, simplified_geom) != 1) {
logger.error(
"Encouterend feature that is a multi-geometry after simplify. "
"WKT: {}",
GEOSGeomToWKT_r(gc, simplified_geom));
GEOSGeom_destroy_r(gc, g_polygon);
GEOSGeom_destroy_r(gc, simplified_geom);
// if (output_failures) polygons_out.push_back(lr);
throw std::runtime_error(
"feature has multiple geometries after simplify");
}
if (orient_after_simplify) orient_polygon(simplified_geom, CCW);

Expand Down

0 comments on commit cfce345

Please sign in to comment.