Take the invalid polygon POLYGON((0 100, 100 100, 0 0, 100 0, 0 100))
- an egg timer shape with an undeclared point of intersection
Many instructions say that JTS can create a valid version of this using the buffer
method:
Geometry input = new WKTReader().read("POLYGON((0 100, 100 100, 0 0, 100 0, 0 100))");
Geometry output = geom.buffer(0);
return output;
However, this produces the output POLYGON ((0 100, 100 100, 50 50, 0 100))
where part of the polygon is lost:
Is there a way to get JTS to validate polygons such that it will produce the output MULTIPOLYGON(((0 100, 100 100, 50 50, 0 100)), ((0 0, 100 0, 50 50, 0 0)))
for the input given?
This seems like something that should be built in to the API (maybe this behaviour is a bug) - have I missed something?
Thank you.
JTS seems to offer the behaviour I require, though I had to do a little legwork in my own code. The
validate
function I wrote breaks down a polygon/multipolygon into a collection of non self intersecting linestrings, and then uses thePolygonizer
class to build polygons from the result. I have tested it on the following (limited) set of inputs, and it seems to behave the way I require:Code: