TopologyException error when finding difference from polygon and multipolygon

Viewed 274

I'm trying to use the turf.js 'difference' tool on a polygon and a multipolygon - the multipolygon areas are to be subtracted from the polygon area. The geometries look fine to me, but I'm receiving this error:

{
  "name": "TopologyException",
  "message": "Directed Edge visited twice during ring-building at (140.97187443743576, -36.980517793468664, undefined) [ undefined ]",
  "stack": "Error\n    at TopologyException.RuntimeException [as constructor] (https://npmcdn.com/@turf/turf@5.1.6/turf.js:24925:19)\n    at new TopologyException (https://npmcdn.com/@turf/turf@5.1.6/turf.js:30875:25)\n    at MaximalEdgeRing.computePoints (https://npmcdn.com/@turf/turf@5.1.6/turf.js:31381:46)\n    at MaximalEdgeRing.EdgeRing (https://npmcdn.com/@turf/turf@5.1.6/turf.js:31356:8)\n    at new MaximalEdgeRing (https://npmcdn.com/@turf/turf@5.1.6/turf.js:31536:17)\n    at PolygonBuilder.buildMaximalEdgeRings (https://npmcdn.com/@turf/turf@5.1.6/turf.js:32328:18)\n    at PolygonBuilder.add (https://npmcdn.com/@turf/turf@5.1.6/turf.js:32390:29)\n    at PolygonBuilder.add (https://npmcdn.com/@turf/turf@5.1.6/turf.js:32385:10)\n    at OverlayOp.computeOverlay (https://npmcdn.com/@turf/turf@5.1.6/turf.js:40843:17)\n    at OverlayOp.getResultGeometry (https://npmcdn.com/@turf/turf@5.1.6/turf.js:40812:10)",
  "pt": null
}

The operation works fine if I try it using the two polygons in the multipolygon individually (i.e. as separate polygons rather than as a multipolygon), but when both are combined into a multipolygon I get the error.

This is the offending line in my code:

diffResult = turf.difference(myPolygon,myMultiPolygon);

myPolygon looks like this:

{
  "type": "Polygon",
  "coordinates": [
    [
      [
        140.9716711384525,
        -36.97645228850101
      ],
      [
        140.9743975752003,
        -36.97682241916141
      ],
      [
        140.97751071844857,
        -36.97723786980259
      ],
      [
        140.97749308140382,
        -36.977304276099005
      ],
      [
        140.97715289421623,
        -36.97770848336402
      ],
      [
        140.97617893060388,
        -36.98131793226549
      ],
      [
        140.9714880598484,
        -36.9804459718428
      ],
      [
        140.9714500775476,
        -36.97642733756345
      ],
      [
        140.9716711384525,
        -36.97645228850101
      ]
    ]
  ]
}

myMultiPolygon looks like this:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          140.9741163253784,
          -36.97713531664132
        ],
        [
          140.9740304946899,
          -36.97903805606076
        ],
        [
          140.97437381744382,
          -36.98025509866784
        ],
        [
          140.97596168518066,
          -36.98128357020193
        ],
        [
          140.97641229629514,
          -36.98078647736292
        ],
        [
          140.9766697883606,
          -36.97970657483203
        ],
        [
          140.97628355026242,
          -36.97958658471583
        ],
        [
          140.97634792327878,
          -36.97900377288852
        ],
        [
          140.9764981269836,
          -36.97866094031662
        ],
        [
          140.97510337829587,
          -36.97727245260485
        ],
        [
          140.97422361373899,
          -36.97658677031591
        ],
        [
          140.9741163253784,
          -36.97713531664132
        ]
      ],
      [
        [
          140.9712839126587,
          -36.97727245260485
        ],
        [
          140.9725499153137,
          -36.97782099398754
        ],
        [
          140.97175598144528,
          -36.979398028436655
        ],
        [
          140.971884727478,
          -36.980615065286116
        ],
        [
          140.97134828567502,
          -36.98059792405719
        ],
        [
          140.9712839126587,
          -36.97727245260485
        ]
      ]
    ]
  ]
}

Note that both of the polygons in myMultiPolygon extend over at least one of the edges in myPolygon - i.e., neither of them fit completely within myPolygon. I'm not sure if this is the cause of the error - maybe I need to somehow 'trim' myMultiPolygon to myPolygon first, before trying 'difference'?

0 Answers
Related