bjornharrtell / jsts

JavaScript Topology Suite
http://bjornharrtell.github.io/jsts/
Other
1.49k stars 232 forks source link

UnaryUnionOp fails with gColl.buffer is not a function #495

Closed adrianmfi closed 1 year ago

adrianmfi commented 1 year ago

If OverlapUnion.unionFull fails with a topology exception, it falls back to using buffer, which is not a function on geometry

Stack trace

TypeError: gColl.buffer is not a function
    unionBuffer OverlapUnion.js:71
    unionFull OverlapUnion.js:134
    union OverlapUnion.js:113
    union OverlapUnion.js:34
    unionActual CascadedPolygonUnion.js:100
    unionSafe CascadedPolygonUnion.js:97
    binaryUnion CascadedPolygonUnion.js:81
    binaryUnion CascadedPolygonUnion.js:69
    unionTree CascadedPolygonUnion.js:106
    union CascadedPolygonUnion.js:63
    union CascadedPolygonUnion.js:36
    union UnaryUnionOp.js:89
    union UnaryUnionOp.js:39

I think this should fix the issue, but I'm not confident enough to open a PR

diff --git a/src/org/locationtech/jts/operation/union/OverlapUnion.js b/src/org/locationtech/jts/operation/union/OverlapUnion.js
index c62d28d0..29c173c1 100644
--- a/src/org/locationtech/jts/operation/union/OverlapUnion.js
+++ b/src/org/locationtech/jts/operation/union/OverlapUnion.js
@@ -1,4 +1,5 @@
 import HashSet from '../../../../../java/util/HashSet.js'
+import BufferOp from '../buffer/BufferOp.js'
 import UnionOp from './UnionOp.js'
 import TopologyException from '../../geom/TopologyException.js'
 import GeometryCombiner from '../../geom/util/GeometryCombiner.js'
@@ -68,7 +69,7 @@ export default class OverlapUnion {
   static unionBuffer(g0, g1) {
     const factory = g0.getFactory()
     const gColl = factory.createGeometryCollection([g0, g1])
-    const union = gColl.buffer(0.0)
+    const union = BufferOp.bufferOp(gColl, 0.0)
     return union
   }
   isBorderSegmentsSame(result, env) {

I am able to work around the issue by reducing the precision of my input polygons with GeometryPrecisionReducer.

bjornharrtell commented 1 year ago

Yep that looks like bug.

stevekirks commented 1 year ago

A similar call to the buffer function here throws a similar error if jsts is delivered as an es module. https://github.com/bjornharrtell/jsts/blob/f30d48ed7386889a19ea897b63fbd538d990851a/src/org/locationtech/jts/simplify/DouglasPeuckerSimplifier.js#L56

bjornharrtell commented 1 year ago

Fix upstream patch here https://github.com/bjornharrtell/jts/commit/f785d7e4b42b668092e4536e7d40131fb94bd930.

bjornharrtell commented 1 year ago

Additionally with https://github.com/bjornharrtell/jts/commit/979d414c42aeb30c045814bbcc035c5d92255248.