1

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!

  • 1
    Chances are it's just a topological error with the file or with the way the file has been converted into a JTS geometry. I'd suggest cleaning your geometry using the GeometrySnapper class before performing the union. Otherwise, the JTS docs also recommend performing a buffer operation with a distance of 0.0 to correct topological errors. I find these operations usually solve the topo errors that I've run into. – WhiteboxDev Aug 8 '14 at 17:13
  • 1
    Polygon B looks invalid - there probably shouldn't be a line between the "fill" of the hole and the rest of the polygon. – Ian Turton Aug 10 '14 at 11:06
  • Thanks you both for your answers. I've edited my question with the result of the @WhiteboxDev 's suggestion: using of buffer(0,0). I'll try to use the GeometrySnapper asap. – Sébastien BATEZAT Aug 10 '14 at 23:12
  • 1
    I once used the offsetcurve tool of JSTS and found that the result from it is not always reliable (possibly some bug). It creates wired results for me. I have used the exact input values for the same function of GEOS and it returns the correct result. – mfdev Aug 11 '14 at 13:13
  • 1
    The defect has been identified and fixed. – Björn Harrtell Jan 21 '15 at 14:46
1

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); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.