You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Polygonizer to fix self-intersecting Polygons
#1262
Hi all,
I found on StackOverflow that it's possible to use Polygon.union(), to separate a self-intersecting Polygon into LineStrings, and then use a Polygonizer to put them back together into a valid Polygon or MultiPolygon.
The Polygonizer constructor Javadoc only says
public Polygonizer(boolean extractOnlyPolygonal)
Creates a polygonizer, specifying whether a valid polygonal geometry must be created. If the argument is true then areas may be discarded in order to ensure that the extracted geometry is a valid polygonal geometry.
Until today, I'd been using var p = new Polygonizer(false); to construct my Polygonizer, reasoning that I want to see all the pieces, even if they're still invalid. However, it seemed to always fill holes with Polygons. :( Today, in random thrashing about trying to solve this (because surely this shouldn't be difficult!) I stumbled across the fact that using var p = new Polygonizer(true); constructs a Polygonizer that appears to work the way I want!!!
Based on my experimentation, my understanding of the semantics is that, irrespective of the setting of extractOnlyPolygonal, the Polygonizer correctly generates Polygon(s) with holes in them. If extractOnlyPolygonal is set to false, it then takes the holes' "invalid" CCW rings and converts therm to valid CW rings and then Polygons, filling in the holes. When extractOnlyPolygonal is set to true, this doesn't happen.
However, there's no mention of hole processing in the Javadoc, and I'm unsure whether my understanding is always correct. Can someone who is knowledgeable about the inner workings of the Polygonizer please confirm that I have the right idea, or post a more thorough explanation of how it actually works?
Your observations are accurate. Here is how Polygonizer actually works under the hood in JTS, why extractOnlyPolygonal alters hole behavior, and a more robust modern alternative.
1. How Polygonizer Processes Rings and Holes
When you feed LineStrings into Polygonizer, it constructs a directed edge graph (planar arrangement) and extracts all minimal closed cycles as EdgeRing objects.
Every face in the arrangement forms a closed ring:
Outer boundaries (shells)
Enclosed voids (holes)
When extractOnlyPolygonal = false:
In this mode, Polygonizer disables strict OGC validity enforcement and does not filter or subordinate rings:
It converts every single closed EdgeRing found in the graph into a standalone, solid Polygon.
If you have an exterior shell enclosing a hole, the graph generates two minimal rings: the outer boundary and the hole boundary.
Because extractOnlyPolygonal = false, the hole ring is not assigned as an interior hole to the shell; instead, it is output as an independent solid Polygon alongside the outer polygon.
When rendered or collected into a GeometryCollection / MultiPolygon, the hole polygon physically covers the interior void, making the hole appear completely "filled in".
It analyzes spatial nesting to distinguish exterior shells from interior holes.
It assigns each hole ring to its enclosing shell as an interior ring (polygon.getInteriorRingN(i)).
If an extracted area or ring would violate OGC polygonal validity (e.g., overlapping faces, dangles, or duplicate coverage), it is discarded so the output strictly conforms to a valid Polygon or MultiPolygon.
2. Modern Standard: Use GeometryFixer Instead of Manual Polygonizing
The StackOverflow approach you referenced dates back to 2015 when buffer(0) or union() + Polygonizer were the only workarounds to repair self-intersecting geometries (like bowties or figure-eights).
In modern JTS releases, JTS includes org.locationtech.jts.geom.util.GeometryFixer, designed specifically to replace Polygonizer and buffer(0) hacks:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hi all,
I found on StackOverflow that it's possible to use Polygon.union(), to separate a self-intersecting Polygon into LineStrings, and then use a Polygonizer to put them back together into a valid Polygon or MultiPolygon.
The Polygonizer constructor Javadoc only says
Until today, I'd been using
var p = new Polygonizer(false);to construct my Polygonizer, reasoning that I want to see all the pieces, even if they're still invalid. However, it seemed to always fill holes with Polygons. :( Today, in random thrashing about trying to solve this (because surely this shouldn't be difficult!) I stumbled across the fact that usingvar p = new Polygonizer(true);constructs a Polygonizer that appears to work the way I want!!!Based on my experimentation, my understanding of the semantics is that, irrespective of the setting of
extractOnlyPolygonal, the Polygonizer correctly generates Polygon(s) with holes in them. IfextractOnlyPolygonalis set to false, it then takes the holes' "invalid" CCW rings and converts therm to valid CW rings and then Polygons, filling in the holes. WhenextractOnlyPolygonalis set to true, this doesn't happen.However, there's no mention of hole processing in the Javadoc, and I'm unsure whether my understanding is always correct. Can someone who is knowledgeable about the inner workings of the Polygonizer please confirm that I have the right idea, or post a more thorough explanation of how it actually works?
Thanks
Frank
All reactions