-
Notifications
You must be signed in to change notification settings - Fork 992
Description
Hi,
I found an unexpected behavior when using turf.polygonize.
I have two sets of LineStrings:
"Wrong" set – four lines that form a nearly square shape, but turf.polygonize fails to produce a polygon.
"Correct" set – four lines that form a similar square (slightly rotated), and turf.polygonize works as expected.
Example code:
`import * as turf from "@turf/turf";
// WRONG
const wrongLineA = turf.lineString([[-38.487131111537174, -12.969499981792461], [-38.48702388505574, -12.961422253524237]]);
const wrongLineB = turf.lineString([[-38.48916841468447, -12.963048521826027], [-38.47908912542943, -12.962708971301478]]);
const wrongLineC = turf.lineString([[-38.48103707317553, -12.961743932968547], [-38.48071539373122, -12.969928887718208]]);
const wrongLineD = turf.lineString([[-38.488310602832975, -12.968534943459533], [-38.47887467246656, -12.96830261941642]]);
const wrongLines = turf.featureCollection([wrongLineA, wrongLineB, wrongLineC, wrongLineD]);
const wrongPolygons = turf.polygonize(wrongLines);
console.log("Polygons wrong:", wrongPolygons);
// CORRECT
const correctLineE = turf.lineString([[-38.474339679399634, -12.961745441517174], [-38.47405825002598, -12.968280856971925]]);
const correctLineF = turf.lineString([[-38.474339679399634, -12.961745441517174], [-38.46767918422326, -12.961213852700281]]);
const correctLineG = turf.lineString([[-38.46767918422326, -12.961213852700281], [-38.46724140519758, -12.967499108711786]]);
const correctLineH = turf.lineString([[-38.47405825002598, -12.968280856971925], [-38.46724140519758, -12.967499108711786]]);
const correctLines = turf.featureCollection([correctLineE, correctLineF, correctLineG, correctLineH]);
const correctPolygons = turf.polygonize(correctLines);
console.log("Polygons correct:", correctPolygons);
`
Observed behavior:
The "wrong" set does not produce a polygon.
The "correct" set produces the polygon as expected.
It seems turf.polygonize requires exact matches between line endpoints to form polygons. In the "wrong" set, the lines are close but endpoints do not coincide exactly, causing the function to fail. Precision issues or slight misalignments prevent proper polygonization.
Question / Request:
Is this expected behavior?
Any suggestions on implementations I can do to make the wrong example work?
