Turfjs / Turfjs/turf

Issue with turf.difference behavior between Turf.js v5.16 and v6.5 (and recent versions)

Open
#2,836 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

polyclip-ts
Dominant language
TypeScript
Stars
10.5k
Forks
1k
Avg merge
1h 11m
Merged PRs (30d)
4

Description

I’ve encountered a discrepancy in the behavior of the turf.difference function between Turf.js versions 5.16 and 6.5.

In version 5.16, using turf.difference to subtract holes (multipolygon) from a world polygon works as expected, producing the correct result. However, when I update to version 6.5, the output is incorrect or differs from the expected result.

Steps to Reproduce:

Use the following world polygon as the outer boundary:

const worldPolygon = turf.polygon([[
    [-180.0, 89.0],
    [-180.0, -89.0],
    [180.0, -89.0],
    [180.0, 89.0],
    [-180.0, 89.0]
]]);

Use the multipolygon as the holes:

const multiPolygon =  turf.multiPolygon([
    [
        [
            [
                1.5848481682316446,
                48.31986594114687
            ],
            [
                1.8832104345803926,
                48.42342314863003
            ],
            [
                1.8734490264407953,
                48.31243967954646
            ],
            [
                2.0474541685456376,
                48.433892157002646
            ],
            [
                1.9407386741551667,
                48.44339036470831
            ],
            [
                2.139324407980982,
                48.51231658749331
            ],
            [
                2.140165825025228,
                48.54733551405033
            ],
            [
                2.005794737854104,
                48.62423252424017
            ],
            [
                2.14201346746852,
                48.62423252424017
            ],
            [
                2.1464195901569383,
                48.80761089514678
            ],
            [
                2.017641560842897,
                48.71825624766248
            ],
            [
                1.9156353862877893,
                48.79208242133859
            ],
            [
                1.9086020675566275,
                48.71211628317572
            ],
            [
                1.977029724641852,
                48.69007709065282
            ],
            [
                1.9380268126907942,
                48.663014310869556
            ],
            [
                1.9059004038856775,
                48.68139940276142
            ],
            [
                1.9020902267645763,
                48.638079149302555
            ],
            [
                1.8821344534680975,
                48.62423252424017
            ],
            [
                1.9008723646838,
                48.62423252424018
            ],
            [
                1.8853998352913024,
                48.448315795534654
            ],
            [
                1.7548343829094222,
                48.459936767662384
            ],
            [
                1.5848481682316446,
                48.31986594114687
            ]
        ]
    ],
    [
        [
            [
                1.7796498887017833,
                48.75364922492895
            ],
            [
                1.9059004038856775,
                48.68139940276142
            ],
            [
                1.9086020675566275,
                48.71211628317572
            ],
            [
                1.7796498887017833,
                48.75364922492895
            ]
        ]
    ],
    [
        [
            [
                2.0474541685456376,
                48.433892157002646
            ],
            [
                2.269137922503419,
                48.414161204603545
            ],
            [
                2.3139242542554825,
                48.62423252424017
            ],
            [
                2.14201346746852,
                48.62423252424017
            ],
            [
                2.140165825025228,
                48.54733551405033
            ],
            [
                2.1779387566694766,
                48.52571906686143
            ],
            [
                2.139324407980982,
                48.51231658749331
            ],
            [
                2.13897493841921,
                48.497772017709025
            ],
            [
                2.0474541685456376,
                48.433892157002646
            ]
        ]
    ],
    [
        [
            [
                2.268015568539596,
                48.408896777510364
            ],
            [
                2.2741686461205703,
                48.41371344527771
            ],
            [
                2.269137922503419,
                48.414161204603545
            ],
            [
                2.268015568539596,
                48.408896777510364
            ]
        ]
    ],
    [
        [
            [
                2.2741686461205703,
                48.41371344527771
            ],
            [
                2.4578541878508133,
                48.39736452202297
            ],
            [
                2.4938366712502784,
                48.58567095470934
            ],
            [
                2.2741686461205703,
                48.41371344527771
            ]
        ]
    ]
])

Apply turf.difference to subtract each hole from the world polygon;

let result = turf.difference(worldPolygon, multiPolygon);

The output from Turf.js v5.16 works as expected, but in Turf.js v6.5, the result is incorrect.

Expected Result:

The world polygon should have the specified holes subtracted correctly, and the resulting polygon should reflect these changes.

Image

https://jsfiddle.net/om39s8fh/1/

Actual Result:

In Turf.js v6.5 and recent versions, the output is incorrect, and the holes do not seem to be subtracted from the world polygon as expected.
https://jsfiddle.net/yg2dj108/2/

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the discrepancy at turf.difference with the supplied worldPolygon and multiPolygon, comparing the Turf.js v5.16, v6.5, and recent-version outputs using the linked JSFiddles. Done means the specified holes are subtracted correctly and the resulting polygon matches the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.