`union` produces zero-area holes
I have these two valid polygons and if I combine them with union the resulting polygon has these weird, minuscule holes in its coordinates.
const poly1 = turf.polygon([
[
[23.528533550247662, 61.71874065973247], [23.52853355025006, 61.71873665738031], [23.528537215415554, 61.71873305138608],
[23.52854381981232, 61.71873055596089], [23.528552055358887, 61.71872966535399], [23.528560290905453, 61.71873055596089],
[23.528565223251558, 61.71873241961342], [23.5285654769612, 61.71873229940589], [23.52865130766738, 61.718772965978395],
[23.52865105395742, 61.71877308618591], [23.528652726010275, 61.71877371795857], [23.528656391180597, 61.71877732395281],
[23.528656391181798, 61.718779325128814], [23.528656867075814, 61.718779325128814], [23.52865686710085, 61.71881999164763],
[23.528656867123598, 61.71885693300448], [23.528737138408967, 61.71889496537389], [23.528736884697977, 61.71889508558142],
[23.52873855675748, 61.71889571735409], [23.528742221942313, 61.718899323348325], [23.528742221944714, 61.71890332570046],
[23.52873855676288, 61.71890693169559], [23.52873195233019, 61.71890942712188], [23.52872371673584, 61.718910317729275],
[23.528715481141486, 61.71890942712188], [23.528710548768352, 61.718907563468456], [23.528710295057174, 61.71890768367608],
[23.5286244643864, 61.71886701726455], [23.528624718097227, 61.71886689705694], [23.528623046039897, 61.71886626528408],
[23.5286193808629, 61.718862659288945], [23.5286193808641, 61.71886065811281], [23.528618904968834, 61.71886065811281],
[23.528618904993877, 61.71881999164763], [23.528618905016618, 61.71878305023432], [23.528538633751033, 61.71874501770806],
[23.528538887460847, 61.71874489750045], [23.528537215410154, 61.7187442657276], [23.528533550247662, 61.71874065973247],
],
]);
const poly2 = turf.polygon([
[
[23.528710295057174, 61.71890768367608], [23.528737138408967, 61.71889496537389], [23.52882296911515, 61.718935631731775],
[23.528822715403848, 61.71893575193929], [23.528824387465537, 61.718936383711956], [23.528828052655197, 61.7189399897062],
[23.528828052657605, 61.71894399205835], [23.528824387470934, 61.71894759805346], [23.528817783029535, 61.718950093479755],
[23.528809547424316, 61.71895098408716], [23.528801311819098, 61.718950093479755], [23.528796379439445, 61.718948229826324],
[23.52879612572794, 61.71894835003394], [23.528710295057174, 61.71890768367608],
],
]);
const unionPoly = turf.union(turf.featureCollection([poly1, poly2]));This is what we get as the unionPoly's coordinates:
[
// Outer ring.
[
[23.528533550247662, 61.71874065973247],
[23.52853355025006, 61.71873665738031],
[23.528537215415554, 61.71873305138608],
[23.52854381981232, 61.71873055596089],
[23.528552055358887, 61.71872966535399],
[23.528560290905453, 61.71873055596089],
[23.528565223251558, 61.71873241961342],
[23.5285654769612, 61.71873229940589],
[23.52865130766738, 61.718772965978395],
[23.52865105395742, 61.71877308618591],
[23.528652726010275, 61.71877371795857],
[23.528656391180597, 61.71877732395281],
[23.528656391181798, 61.718779325128814],
[23.528656867075814, 61.718779325128814],
[23.52865686710085, 61.71881999164763],
[23.528656867123598, 61.71885693300448],
[23.528737138408967, 61.71889496537389],
[23.52882296911515, 61.718935631731775],
[23.528822715403848, 61.71893575193929],
[23.528824387465537, 61.718936383711956],
[23.528828052655197, 61.7189399897062],
[23.528828052657605, 61.71894399205835],
[23.528824387470934, 61.71894759805346],
[23.528817783029535, 61.718950093479755],
[23.528809547424316, 61.71895098408716],
[23.528801311819098, 61.718950093479755],
[23.528796379439445, 61.718948229826324],
[23.52879612572794, 61.71894835003394],
[23.528710295057174, 61.71890768367608],
[23.5286244643864, 61.71886701726455],
[23.528624718097227, 61.71886689705694],
[23.528623046039897, 61.71886626528408],
[23.5286193808629, 61.718862659288945],
[23.5286193808641, 61.71886065811281],
[23.528618904968834, 61.71886065811281],
[23.528618904993877, 61.71881999164763],
[23.528618905016618, 61.71878305023432],
[23.528538633751033, 61.71874501770806],
[23.528538887460847, 61.71874489750045],
[23.528537215410154, 61.7187442657276],
[23.528533550247662, 61.71874065973247],
],
// Hole 1.
[
[23.528737138408967, 61.71889496537389],
[23.528736884697977, 61.71889508558142],
[23.528736884697977, 61.71889508558142],
[23.528737138408967, 61.71889496537389],
],
// Hole 2.
[
[23.528710295057174, 61.71890768367608],
[23.528710548768355, 61.718907563468456],
[23.528710548768352, 61.718907563468456],
[23.528710295057174, 61.71890768367608],
],
]The outer ring looks fine, but what's interesting are these holes. The first hole contains just two unique coordinate points, effectively making it a polygon whose area is zero. The second hole is almost the same issue, though there the second and third coordinate differ by about 3e-15, making it a polygon with an area of about 15 square micrometers.
I run into issues with this when passing this unionPoly to turf's booleanEqual function (or more accurately it errors out in cleanCoords which it calls internally).
ERROR Error: invalid polygon, fewer than 4 points
at cleanLine (index.js:90:13)
at index.js:18:24
at Array.forEach (<anonymous>)
at cleanCoords (index.js:17:26)
at booleanEqual (index.js:14:26)I can work around this by filtering out invalid holes in the polygon (I currently do this by checking their area and filtering them out if they are smaller than 0.001m²), but it would be nice if those weren't there to begin with.
These existing issues might be related but not sure: https://github.com/Turfjs/turf/issues/2887 https://github.com/Turfjs/turf/issues/2890 https://github.com/Turfjs/turf/issues/3033
Using turf 7.4.0.
Source: Turfjs/turf