[GIS] Union on polygons with holes using JSTS

merge

JSTS union

I'm trying to union two polygons (see above) with JSTS. My first polygon contains an hole, and my second polygon fill partially this hole.

The expected result is a unique polygon with two hole (or one hole with two points at the same location.

I've tried with PostGIS and the result is two holes). The actual result is what you can see on C: my first polygon has no hole any more.

Is it a defect of the JSTS tool or is it just some configuration to add somewhere?

I'm using the JSTS union function like that:
firstGeometry.union(secondGeometry);


I don't think I have topological errors. I'm easily able to reproduce this case by drawing polygons on OpenLayers, and my polygons seems to be correct. I'm using JSTS and not JTS. I've tried with the buffer(0,0) function, but I have unfortunately the same result.

Here is an example of a union that fails:

POLYGON((335179.0532258 328356.69669355,335178.1782258 328437.63419355,335285.3657258 328437.19669355,335284.9282258 328355.82169355,335179.0532258 328356.69669355),(335213.6157258 328373.32169355,335198.3032258 328400.88419355,335225.4282258 328426.69669355,335267.4282258 328415.75919355,335262.1782258 328371.13419355,335213.6157258 328373.32169355))

POLYGON((335213.6157258 328373.32169355,335225.4282258 328426.69669355,335267.4282258 328415.75919355,335213.6157258 328373.32169355))

And the result of a JSTS union() between these two geometries or a buffer(0,0) on a new geometry collection containing these geometries is the following polygon :

POLYGON((335179.0532258 328356.69669355,335178.1782258 328437.63419355,335285.3657258 328437.19669355,335284.9282258 328355.82169355,335179.0532258 328356.69669355))


It happens in each case where I have

  • 1 to many hole(s)
  • 1 to many polygon(s) within this/these hole(s), creating "sub-holes"
  • Sub-holes are sharing a vertex

Doesn't seems to happen in other cases.
Geometries are topologically correct.


I've found a defect about that on GitHub: https://github.com/bjornharrtell/jsts/issues/135
I'm still looking for a way to fix it!

Best Answer

Fixed by Björn Harrtell: https://github.com/bjornharrtell/jsts/commit/e356be0aa03d8e88b59b3c7b1b1ac7938904f450

PolygonBuilder.js, line 105:

Replace freeHoleList = freeHoleList.concat(minEdgeRings);

to minEdgeRings.forEach(function(ring) { freeHoleList.push(ring); });