turf icon indicating copy to clipboard operation
turf copied to clipboard

difference throws "Each LinearRing of a Polygon must have 4 or more Positions" with valid polygons

Open bhalderman-bv opened this issue 4 years ago • 3 comments

Running the below on turfjs.org/docs throws "Each LinearRing of a Polygon must have 4 or more Positions" The polygons are valid on their own

turf.difference( 
    turf.polygon([[[-75.36578580737115,40.08918086136594],[-75.36576837301256,40.08918086136594],[-75.36576703190805,40.08918188736182],[-75.36576434969903,40.08918188736182],[-75.36576434969903,40.0891829133577],[-75.36576300859453,40.08918393935358],[-75.36576300859453,40.08918496534946],[-75.36576434969903,40.08918599134534],[-75.36576434969903,40.0891880433371],[-75.36576300859453,40.08918906933298],[-75.36576300859453,40.08919419931238],[-75.36576569080354,40.08919625130414],[-75.36576703190805,40.08919625130414],[-75.36576837301256,40.08919522530826],[-75.36577910184862,40.08919522530826],[-75.36578044295312,40.08919419931238],[-75.36578312516214,40.08919419931238],[-75.36578446626665,40.0891931733165],[-75.36578580737115,40.0891931733165],[-75.36578580737115,40.08918086136594]]]),
    turf.polygon([[[-75.3657858073712,40.0891808613659],[-75.3657683730126,40.0891808613659],[-75.365767031908,40.0891818873618],[-75.365764349699,40.0891818873618],[-75.365764349699,40.0891829133577],[-75.3657630085945,40.0891839393536],[-75.3657630085945,40.0891849653495],[-75.365764349699,40.0891859913453],[-75.365764349699,40.0891880433371],[-75.3657630085945,40.089189069333],[-75.3657630085945,40.0891941993124],[-75.3657656908035,40.0891962513041],[-75.365767031908,40.0891962513041],[-75.3657683730126,40.0891952253083],[-75.3657791018486,40.0891952253083],[-75.3657804429531,40.0891941993124],[-75.3657831251621,40.0891941993124],[-75.3657844662666,40.0891931733165],[-75.3657858073712,40.0891931733165],[-75.3657858073712,40.0891808613659]]])
)

bhalderman-bv avatar Jul 29 '21 22:07 bhalderman-bv

I just ran into this as well. Here is my test case.

a = {
    "type": "Feature",
    "properties": 
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    116.0006122594772,
                    -31.87714864096409
                ],
                [
                    116.0007966067714,
                    -31.87760903651429
                ],
                [
                    116.0030036060436,
                    -31.87762697801566
                ],
                [
                    116.0030445430547,
                    -31.877594724613
                ],
                [
                    116.0030440283621,
                    -31.87712824790725
                ],
                [
                    116.0029992706849,
                    -31.87707811528838
                ],
                [
                    116.0006643598726,
                    -31.87706374056182
                ],
                [
                    116.0006122594772,
                    -31.87714864096409
                ]
            ]
        ]
    }
}

b={
    "type": "Feature",
    "properties": {
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    116.0006122594771,
                    -31.87714864096408
                ],
                [
                    116.0007966067713,
                    -31.87760903651428
                ],
                [
                    116.0030036060435,
                    -31.87762697801564
                ],
                [
                    116.0030445430546,
                    -31.87759472461299
                ],
                [
                    116.003044028362,
                    -31.87712824790724
                ],
                [
                    116.0029992706848,
                    -31.87707811528837
                ],
                [
                    116.0006643598725,
                    -31.87706374056181
                ],
                [
                    116.0006122594771,
                    -31.87714864096408
                ]
            ]
        ]
    }
}

turf.difference(a,b) // ok
turf.difference(b,a) // throws error

stevage avatar Jul 13 '23 06:07 stevage

Same here, it looks like the function difference creates invalid polygons in some cases, and then this function call helpers.polygon(differenced[0], properties); throws the error in the file helpers/index.js

tnlogy avatar Sep 08 '23 10:09 tnlogy

Seems like this is fixed in 7.0.0-alpha-0! Thank you for your effort in developing this!

Since the api is changed you have to rewrite code that uses geojson-objects from difference(geom1, geom2) to something like difference(featureCollection([geom1.type === 'Feature' ? geom1 : feature(geom1), geom2.type === 'Feature' ? geom2 : feature(geom2)])) to handle geojson objects with type Feature or type Polygon, etc.

tnlogy avatar Sep 08 '23 11:09 tnlogy