polygon-clipping icon indicating copy to clipboard operation
polygon-clipping copied to clipboard

Unable to find segment #13 [35.188333, 6.664167] -> [35.189483, 6.665317] in SweepLine tree.

Open mcwhittemore opened this issue 3 years ago • 2 comments

Version: 0.15.3 Function: polygonClipping.union Input:

[
	[
		[
			[35.183056, 6.658889],
			[35.182044, 6.658552],
			[35.182071, 6.658686],
			[35.182208, 6.658965],
			[35.182129, 6.658979],
			[35.181683, 6.659061],
			[35.181415, 6.659655],
			[35.181377, 6.660006],
			[35.181306, 6.660653],
			[35.181309, 6.661205],
			[35.181667, 6.66125],
			[35.186649, 6.664571],
			[35.189483, 6.665317],
			[35.188333, 6.664167],
			[35.188333, 6.661984],
			[35.184514, 6.660347],
			[35.183056, 6.658889]
		]
	],
	[
		[
			[35.193178, 6.670678],
			[35.1875, 6.657896],
			[35.1875, 6.659167],
			[35.188333, 6.66],
			[35.188333, 6.664167],
			[35.190833, 6.666667],
			[35.190833, 6.6675],
			[35.191667, 6.668333],
			[35.191667, 6.669167],
			[35.193178, 6.670678]
		]
	]
]

I'm poking around the lib a bit to see if I can figure out why this is happening. Maybe its the same cause as https://github.com/mfogel/polygon-clipping/issues/98?

mcwhittemore avatar May 06 '21 01:05 mcwhittemore

Run into this same issue on a different dataset, so I can provide additional information that can hopefully help move this issue forward.

Version [email protected]

The stacktrace:

Error: Unable to find segment #5789899 [142.64995174370168, -3.6504358187148824] -> [142.64995174370222, -3.650435818715287] in SweepLine tree. Please submit a bug report.
    at SweepLine.process (/home/totalpave/tp-shapefile/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1524:24)
    at Operation.run (/home/totalpave/tp-shapefile/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1761:35)
    at union (/home/totalpave/tp-shapefile/node_modules/polygon-clipping/dist/polygon-clipping.cjs.js:1792:20)
    at Function.dissolve [as default] (/home/totalpave/tp-shapefile/node_modules/@turf/dissolve/dist/js/index.js:49:51)
    at NetworkJob.$mergeFeatures (/home/totalpave/tp-shapefile/lib/NetworkJob.js:306:49)
    at NetworkJob.$osm (/home/totalpave/tp-shapefile/lib/NetworkJob.js:156:29)
    at NetworkJob.$populateFamilies (/home/totalpave/tp-shapefile/lib/NetworkJob.js:91:42)
    at NetworkJob.run (/home/totalpave/tp-shapefile/lib/NetworkJob.js:71:20)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
The GeoJSON
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            142.61177817229367,
                            -3.5746898486258374
                        ],
                        [
                            142.61307179529587,
                            -3.5922722640644165
                        ],
                        [
                            142.61777758155156,
                            -3.609264944158095
                        ],
                        [
                            142.62571486112077,
                            -3.6250148205389205
                        ],
                        [
                            142.6365787290299,
                            -3.638916555913288
                        ],
                        [
                            142.63824783192996,
                            -3.6403542896943777
                        ],
                        [
                            142.63881795059655,
                            -3.6410482443928545
                        ],
                        [
                            142.6436937013806,
                            -3.6450452590026936
                        ],
                        [
                            142.64995174370222,
                            -3.650435818715287
                        ],
                        [
                            142.6446015411609,
                            -3.6463700865723054
                        ],
                        [
                            142.6506852371919,
                            -3.6510220895235763
                        ],
                        [
                            142.6508382434519,
                            -3.6511290981671003
                        ],
                        [
                            142.65182867087518,
                            -3.6517140969643473
                        ],
                        [
                            142.6524685048381,
                            -3.652238616327815
                        ],
                        [
                            142.65505545990433,
                            -3.6536200091560147
                        ],
                        [
                            142.6627450432497,
                            -3.6581618836059313
                        ],
                        [
                            142.66598331048056,
                            -3.6594553074225877
                        ],
                        [
                            142.66804463258438,
                            -3.660556020615491
                        ],
                        [
                            142.67092711622342,
                            -3.6614299548611466
                        ],
                        [
                            142.67559045313592,
                            -3.6632925778622014
                        ],
                        [
                            142.67560244404257,
                            -3.663296330153565
                        ],
                        [
                            142.67598998295313,
                            -3.6633770147164433
                        ],
                        [
                            142.6757374572013,
                            -3.663338579563343
                        ],
                        [
                            142.67904448822904,
                            -3.6640129545312266
                        ],
                        [
                            142.6803280198212,
                            -3.6642801823698634
                        ],
                        [
                            142.6849476427233,
                            -3.6656807409511045
                        ],
                        [
                            142.68895474930292,
                            -3.666076244335142
                        ],
                        [
                            142.6928959962701,
                            -3.666896801394282
                        ],
                        [
                            142.6932009814659,
                            -3.66689963359883
                        ],
                        [
                            142.6937689836239,
                            -3.6670154523496445
                        ],
                        [
                            142.6939299993193,
                            -3.667031453925333
                        ],
                        [
                            142.69845747443682,
                            -3.667014099297884
                        ],
                        [
                            142.7025277968348,
                            -3.6674157814504325
                        ],
                        [
                            142.6998530152113,
                            -3.667008745409473
                        ],
                        [
                            142.70053013363426,
                            -3.6670061424929945
                        ],
                        [
                            142.70384140745264,
                            -3.667316586083105
                        ],
                        [
                            142.70667884548178,
                            -3.6670247939944596
                        ],
                        [
                            142.71056115473937,
                            -3.667060846544186
                        ],
                        [
                            142.711065163006,
                            -3.6669656447002548
                        ],
                        [
                            142.71247791403306,
                            -3.6669602139324544
                        ],
                        [
                            142.71267393309418,
                            -3.6669392118664903
                        ],
                        [
                            142.71511716362687,
                            -3.6664989450232652
                        ],
                        [
                            142.7153313823274,
                            -3.666493726860022
                        ],
                        [
                            142.71609430933913,
                            -3.6663228646852253
                        ],
                        [
                            142.71953787901896,
                            -3.6657023380783116
                        ],
                        [
                            142.72141425427375,
                            -3.6655093519270534
                        ],
                        [
                            142.7225528346582,
                            -3.665159047142812
                        ],
                        [
                            142.7253770563111,
                            -3.664650126214127
                        ],
                        [
                            142.72552206929402,
                            -3.6646131226683236
                        ],
                        [
                            142.73806063076512,
                            -3.6604137875661604
                        ],
                        [
                            142.73829599767984,
                            -3.660315241352458
                        ],
                        [
                            142.74051042540975,
                            -3.6591210458085075
                        ],
                        [
                            142.74871950570235,
                            -3.6554906722933986
                        ],
                        [
                            142.75359376378393,
                            -3.652065469247807
                        ],
                        [
                            142.75383768497517,
                            -3.6519339275425997
                        ],
                        [
                            142.75415926781272,
                            -3.6516680823861836
                        ],
                        [
                            142.76316356849077,
                            -3.645340645966195
                        ],
                        [
                            142.7753455539016,
                            -3.632573293281461
                        ],
                        [
                            142.78479732711656,
                            -3.6176793553541082
                        ],
                        [
                            142.7911557489179,
                            -3.6012312866375926
                        ],
                        [
                            142.79417661706242,
                            -3.5838612422690113
                        ],
                        [
                            142.79374402771575,
                            -3.5662367749284263
                        ],
                        [
                            142.78987480089202,
                            -3.5490351766108987
                        ],
                        [
                            142.78271780429856,
                            -3.532917451947908
                        ],
                        [
                            142.77254820608704,
                            -3.5185029229743803
                        ],
                        [
                            142.75975688126084,
                            -3.5063454401505565
                        ],
                        [
                            142.74483538154934,
                            -3.4969121120959863
                        ],
                        [
                            142.72835704752632,
                            -3.4905653693513683
                        ],
                        [
                            142.7230085246727,
                            -3.4896383033948486
                        ],
                        [
                            142.72161925928964,
                            -3.4891798824320697
                        ],
                        [
                            142.71726656399048,
                            -3.488643042382072
                        ],
                        [
                            142.71095498835973,
                            -3.487549049319053
                        ],
                        [
                            142.71071850658774,
                            -3.48753160395097
                        ],
                        [
                            142.70832211692152,
                            -3.4875398782316633
                        ],
                        [
                            142.70409009519662,
                            -3.4870179214673516
                        ],
                        [
                            142.68647517228385,
                            -3.4883106047120056
                        ],
                        [
                            142.66945123114036,
                            -3.4930082690430764
                        ],
                        [
                            142.65367232609492,
                            -3.5009304350025228
                        ],
                        [
                            142.63974471515948,
                            -3.511772735255733
                        ],
                        [
                            142.62820357950022,
                            -3.5251185998204155
                        ],
                        [
                            142.61949246444541,
                            -3.5404552506510063
                        ],
                        [
                            142.6139462308434,
                            -3.5571933929497237
                        ],
                        [
                            142.61177817229367,
                            -3.5746898486258374
                        ]
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "id": 1293382
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            142.6496171516014,
                            -3.6497061473538697
                        ],
                        [
                            142.64938390021084,
                            -3.6500364627747666
                        ],
                        [
                            142.64995178861523,
                            -3.6504358503019123
                        ],
                        [
                            142.6542199538622,
                            -3.652850449588183
                        ],
                        [
                            142.65774094397116,
                            -3.6552557055738983
                        ],
                        [
                            142.6579259513733,
                            -3.65535706866927
                        ],
                        [
                            142.65809800059503,
                            -3.655044321495785
                        ],
                        [
                            142.66532001093265,
                            -3.659129854083032
                        ],
                        [
                            142.6820927407614,
                            -3.6646644156244332
                        ],
                        [
                            142.6996252520084,
                            -3.6668267851215384
                        ],
                        [
                            142.71724358666447,
                            -3.665533839504173
                        ],
                        [
                            142.73427048464313,
                            -3.6608352806357205
                        ],
                        [
                            142.75005144114124,
                            -3.652911722996782
                        ],
                        [
                            142.7639798852888,
                            -3.64206774471506
                        ],
                        [
                            142.77552050832855,
                            -3.628720170286175
                        ],
                        [
                            142.78422984206017,
                            -3.6133820372782948
                        ],
                        [
                            142.78977329577447,
                            -3.596642865541538
                        ],
                        [
                            142.791937997936,
                            -3.5791459895097435
                        ],
                        [
                            142.7906409518231,
                            -3.56156382664485
                        ],
                        [
                            142.78593219566395,
                            -3.5445720336908693
                        ],
                        [
                            142.77799285037315,
                            -3.5288235442790046
                        ],
                        [
                            142.76712813433184,
                            -3.5149234850924427
                        ],
                        [
                            142.75375561728544,
                            -3.5034059332553174
                        ],
                        [
                            142.75318777474385,
                            -3.503006609784677
                        ],
                        [
                            142.74464659027745,
                            -3.4976868881248953
                        ],
                        [
                            142.74446160510558,
                            -3.497585542463525
                        ],
                        [
                            142.72821965969268,
                            -3.4906582531988843
                        ],
                        [
                            142.71093599755548,
                            -3.4870265143822134
                        ],
                        [
                            142.69327464316765,
                            -3.4868298531432367
                        ],
                        [
                            142.67591411949738,
                            -3.4900758249250248
                        ],
                        [
                            142.65952140152817,
                            -3.496639723448043
                        ],
                        [
                            142.6447263117074,
                            -3.5062693679933905
                        ],
                        [
                            142.63209733736574,
                            -3.518594784757301
                        ],
                        [
                            142.62211979629487,
                            -3.533142411629478
                        ],
                        [
                            142.61517718767368,
                            -3.5493532824096374
                        ],
                        [
                            142.61153644466327,
                            -3.566604493788319
                        ],
                        [
                            142.61133765669072,
                            -3.5842331322022667
                        ],
                        [
                            142.614588659187,
                            -3.6015617427741207
                        ],
                        [
                            142.6211647026388,
                            -3.617924362644185
                        ],
                        [
                            142.63081321820763,
                            -3.6326921184826246
                        ],
                        [
                            142.64316350121288,
                            -3.6452974038188204
                        ],
                        [
                            142.6496171516014,
                            -3.6497061473538697
                        ]
                    ]
                ]
            }
        }
    ]
}

Some background context:

I'm not using this library directly, I'm using @turf/dissolve which I guess uses the union function on this library. The task that this process is doing is taking imported line strings representing roads from a shapefile and buffering each line string creating a polygon box around covering each line, I then take each polygon and any overlapping polygons are then dissolve into a single polygon. The end result is that I may have 1-to-many polygons as a FeatureCollection.

This works on most datasets however there's been a few edge cases including errors being generated from this library such as Unable to complete output ring or Maximum call stack size exceeded. I was able to work-around these errors by incorporating this hack:

private $fixUnableToCompleteOutputRing(col: any): any {
        getInstance().getLogger().warn(TAG, 'Fixing output ring geometry...');
        try {
            col = buffer(col, 0.0001);
            col = dissolve(col);
        }
        catch (ex) {
            // This happens if we have two polygons that interweaves together too closely
            // Applying a small buffer usually corrects it
            if (/Unable to complete output ring|Maximum call stack size exceeded/g.test(ex.message)) {
                this.$fixUnableToCompleteOutputRing(col);
            }
        }

        getInstance().getLogger().info(TAG, 'Fixed output ring geometry');

        return col;
    }

The hack expands the polygon by buffering it by a small amount, which may not be ideal for everyone's use case, which has resolved most issues.

breautek avatar Dec 10 '21 12:12 breautek

Another set of polygons that reproduces this issue: https://gist.github.com/russellporter/6ef55dc00ce48dabfa7038bb750e2989

russellporter avatar Mar 15 '22 20:03 russellporter