turf icon indicating copy to clipboard operation
turf copied to clipboard

@turf/kinks and @turf/unkink-polygon

Open polizz opened this issue 6 years ago • 9 comments

The kinks package detects self-intersection in the following shape: https://gist.github.com/polizz/53bfb5d8cd0e7cac09ae6543e1c89528. When I run the shape through unkink-polygon, I get the same shape back out. Not sure what is wrong here. The unkink-polygon function does work on other self-intersecting polygons. Let me know if I can provide more information.

export function removeDuplicatesAndFormatGeoJson(wkt) {
  try {
    let cleanGeom = clean(parse(wkt));
    const hasKinks = get(kinks(cleanGeom), 'features[0].geometry', false);

    if (hasKinks) {
      const unkinked = unkink(cleanGeom);
...

polizz avatar Nov 15 '17 20:11 polizz

Hi @polizz

Thanks for that test case. Our unkinkPolygon is based on the simplepolygon lib so we'll raise the issue upstream and hopefully we can work out what's going on.

Cheers

rowanwins avatar Nov 15 '17 23:11 rowanwins

@rowanwins I had to refactor simplepolygon and I internalized the library into Turf to get the build to pass. Maybe it was something in the refactoring?

https://github.com/Turfjs/turf/blob/master/packages/turf-unkink-polygon/lib/simplepolygon.js https://github.com/Turfjs/turf/blob/master/packages/turf-unkink-polygon/lib/geojson-polygon-self-intersections.js

Might be worth re-including simplepolygon into Turf to see if it changed anything.

DenisCarriere avatar Nov 17 '17 08:11 DenisCarriere

👍 Just confirmed that this is a bug 🐛

I'll send a PR with the test case included, we can investigate this more on the PR.

DenisCarriere avatar Nov 17 '17 08:11 DenisCarriere

@rowanwins Darn... 🤕 I tried replacing simplepolygon with the upstream npm version and it still didn't fix the issue... at least it's not the refactored code the issue.

DenisCarriere avatar Nov 17 '17 08:11 DenisCarriere

I don't know if it helps, but I was researching simplepolygon's issue list. There was an old issue where it was stated that a very vertical line tripped up it's self-intersection detection. The test shape I provided has just such a line.

polizz avatar Nov 17 '17 16:11 polizz

🤔 ... Don't know what could solve that, maybe you can submit your test case to simplepolygon to have them fix it upstream and we can use it in Turf.

DenisCarriere avatar Nov 17 '17 19:11 DenisCarriere

Hi @polizz

So I've done some more digging into this.

simplepolygon uses it's own self-intersection library rather than @turf/kinks. The geojson-polygon-self-intersections library is a bit more advanced. This explains why there are some anomalies between your results. For example the geojson-polygon-self-intersections lib provides some options such as reportVertexOnEdge, which is what is happening with your poly.

One option for turf would be to adopt the geojson-polygon-self-intersections as our kinks module. Currently our kinks algorithm is a basic brute force algorithm with no options.

It probably makes sense in my mind for the @turf/kinks & @turf/unkinkPolygon modules to be aligned. Thoughts @DenisCarriere

rowanwins avatar Jan 03 '18 23:01 rowanwins

👍 @rowanwins Agreed, having both modules align would be the best approach using geojson-polygon-self-intersections, unfortunately that's not a quick fix and would require some serious effort in making that modification.

DenisCarriere avatar Jan 09 '18 15:01 DenisCarriere

Hi @DenisCarriere. I had to re-implement kink and unkinkPolygon functions in my own project because of this issue. It's ES6 and a little brute-force but it does the job. Cheers!

https://gist.github.com/barnabas-avalara/2a9d9be5edd84fe2da8e769a9a9c509e

barnabas-avalara avatar Oct 03 '18 23:10 barnabas-avalara