Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look into clipper2-java for polygon union/buffer/unbuffer ops #439

Open
msbarry opened this issue Jan 11, 2023 · 2 comments
Open

Look into clipper2-java for polygon union/buffer/unbuffer ops #439

msbarry opened this issue Jan 11, 2023 · 2 comments

Comments

@msbarry
Copy link
Contributor

msbarry commented Jan 11, 2023

@bdon pointed out this project https://github.com/micycle1/Clipper2-java which seems like it might have faster implementations of expensive JTS polygon union and buffer/unbuffer operations (including ones that work in integer coordinates). Together these JTS operations are responsible for about 45% of overall CPU time (20% for snap/fix, and 25% for buffer/unbuffer)

@msbarry
Copy link
Contributor Author

msbarry commented Jan 11, 2023

I did a quick test trying to use it for snapAndFixPolygon and bufferUnionUnbuffer in FeatureMerge and it looks like it does what we want most of the time, but it doesn't appear to fix single-point self-intersections, has inconsistent order for output winding, and visible artifacts when rendered with maplibre gl:

image

I might not have been using the library correctly. Here's what I tried for snapAndFixPolygon 2081893 and for bufferUnionUnbuffer:

   private static Geometry bufferUnionUnbuffer(double buffer, List<Geometry> polygonGroup) {
+    double amount = buffer * 4096d / 256d;
+    var merged = new Paths64();
     for (int i = 0; i < polygonGroup.size(); i++) {
-      polygonGroup.set(i, buffer(buffer, polygonGroup.get(i)));
+      var clipper = GeoUtils.toClipper2(polygonGroup.get(i));
+      merged.addAll(clipper);
     }
-    Geometry merged = GeoUtils.createGeometryCollection(polygonGroup);
-    merged = union(merged);
-    merged = unbuffer(buffer, merged);
-    return merged;
+    merged = Clipper.InflatePaths(merged, amount, JoinType.Miter, EndType.Polygon);
+    merged = Clipper.InflatePaths(merged, -amount, JoinType.Miter, EndType.Polygon);
+    return GeoUtils.fromClipper2(merged);
    }

Let me know if anyone has any suggestions!

@bdon
Copy link
Contributor

bdon commented Jan 11, 2023

It's worth checking if https://github.com/lightbringer/clipper-java fails in the same way; supposedly Clipper2 is strictly better than Clipper1, but possible failures are:

  • differences (bugs?) in Clipper2 java port vs reference implementations in C++, Delphi and C#
  • bug or unhandled case in Clipper2 algorithm itself
  • Fundamental difference between Clipper2 validity model and OGC Simple + Valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants