mfogel / polygon-clipping

Apply boolean polygon clipping operations (union, intersection, difference, xor) to your Polygons & MultiPolygons.
MIT License
543 stars 63 forks source link

Unable to complete output ring starting at [...]. Last matching segment found ends at [...]. #140

Open oli-clive-griffin opened 2 years ago

oli-clive-griffin commented 2 years ago

I'm getting this error on trying to intersect these two polygons: (viewer link)

It isn't really clear why this wouldn't be working.

"polygon-clipping": "^0.15.3",
Error: Unable to complete output ring starting at [175.4412230000001, -37.929121]. Last matching segment found ends at [175.441261917386, -37.92871399662434].
    at Function.factory (node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1252:21)
    at Operation.run (node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1776:30)
    at Object.intersection (node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1800:20)
    at Object.intersect (node_modules/@turf/intersect/dist/js/index.js:49:51)

script to validate: this will throw at turf.intersect

const turf = require('@turf/turf');

const piece1 = {
    "type": "Polygon",
    "coordinates": [
        [
            [
                175.440248,
                -37.927138
            ],
            [
                175.441083,
                -37.927271
            ],
            [
                175.441362,
                -37.928504
            ],
            [
                175.44139,
                -37.928511
            ],
            [
                175.441542,
                -37.929194
            ],
            [
                175.441503,
                -37.929209
            ],
            [
                175.441476,
                -37.929182
            ],
            [
                175.441449,
                -37.929181
            ],
            [
                175.441223,
                -37.929121
            ],
            [
                175.441179,
                -37.928787
            ],
            [
                175.44065,
                -37.928795
            ],
            [
                175.440248,
                -37.927138
            ]
        ]
    ]
}

const piece2 = {
    "type": "Polygon",
    "coordinates": [
        [
            [
                175.4412230000001,
                -37.92912100000001
            ],
            [
                175.44144900000003,
                -37.929181000000035
            ],
            [
                175.441503,
                -37.929209
            ],
            [
                175.44154200000003,
                -37.92919400000002
            ],
            [
                175.44143567050224,
                -37.92871621679616
            ],
            [
                175.441261917386,
                -37.92871399662434
            ],
            [
                175.44117900000003,
                -37.928787000000014
            ],
            [
                175.4412230000001,
                -37.92912100000001
            ]
        ]
    ]
}

const piecesCollection = turf.featureCollection([turf.feature(piece1), turf.feature(piece2)])

// log link to view collection
console.log(`http://geojson.io/#data=data:application/json,${encodeURIComponent(JSON.stringify(piecesCollection))}`)

const intersection = turf.intersect(piece1, piece2).geometry

// log link to view intersection
console.log(`http://geojson.io/#data=data:application/json,${encodeURIComponent(JSON.stringify(intersection))}`)
oli-clive-griffin commented 2 years ago

the first thing that stands out is very different degrees of accuracy, could that be a factor?

markstos commented 1 year ago

This issue is fixed in the polygon-ts library: https://github.com/luizbarboza/polyclip-ts

I recommend using it instead.