-
Notifications
You must be signed in to change notification settings - Fork 937
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
turf-clip for fast clipping to rectangular area #281
Comments
👍 x 💯 ! |
Gday @mourner I've had a first crack at this Basically I've just implemented the Sutherland-Hodgeman js code here.
which returns the original feature with properties just trimmed to the new shape
This seems to work ok for simple polygons but is failing for more complex polygons but Im not sure why at this stage... I think there is something wrong with the js from the site I referenced above as even their jsfiddle returns incorrect results depending on the inputs Anyway there is something off to a start... happy for any other contributors who know more about maths and algorithms :) |
@rowanwins actually I have a solid and fast Sutherland-Hodgeman implementation that was battle-tested for 5 years as a part of Leaflet. I've been meaning to extract, clean up and release it as a separate module for some time. Maybe I'll do that today to go alongside my lineclip module. |
No worries @mourner , let me know if I can assist otherwise I'll leave it with you |
Added Sutherland-Hodgeman polygon to rectangle clipping to https:/mapbox/lineclip. The implementation is extremely fast and 100% test-covered. So what's left is simply a wrapper Turf module that would handle GeoJSON input and output GeoJSON. |
Got some bugs fixed in lineclip with more test coverage so it should be all settled. BTW maybe |
💯 sounds great to me. I started pulling this together, will get it out today, probably. |
fyi @tcql I've deleted my repo just for clarity |
@mourner @rowanwins It's alive! Here's what I've got: https:/Turfjs/turf-bbox-clip It also occurred to me that this could easily be extended to iteratively clip collections. There are some quirks to results with polygons - notably S-H degeneracies, and issues where holes cross the bbox. See this which produces this when clipped. You would probably expect the hole to be dissolved into the polygon, but without adding more in depth clipping here, this is what we've got |
@tcql great! This is all fully expected with Sutherland-Hodgeman clipping, I don't think it's a problem that should be addressed. |
This would go outside the norm. I can definitely see this generating a lot issues from users who do not know or care about the algorithmic explanation for a degeneracy. Generally speaking, we shoot for the "just works as expected" level of use. Obviously, this is limiting in some ways, but it's a tradeoff we have made so far. I am open to tweaking this expectation, but if we do, we should keep in mind users with little to no level of computational geometry expertise (>95% of the users). Also, users who need speed and understand algorithm minutiae tend to be much more amenable to integrating external modules. |
I don't see it generating any issues if users are told what this module does exactly, e.g. using this illustration. Most users with little to no level of computational geometry expertise won't care for degenerate edges, it will just work. We're talking about hundreds to thousands of times faster here, not just a minor performance tweak, and it more that justifies any ambiguity of edge handling in my opinion. Performance is Turf's feature. All users need it even if they don't know it. Placing this module outside of Turf will lead to people never knowing about it and continuing to use turf.intersect on a rect polygon because they don't know how horribly slow it is, and take this performance for granted. |
@mourner @morganherlocker maybe an option long run (once we have |
@mourner I think once |
👍 Sounds like a good idea, once we finish |
@DenisCarriere it was already built by @tcql a few years ago (https:/Turfjs/turf-bbox-clip), just needs an update and integration into the monorepo. |
Yes for the The 👍 |
Closing in favor of PR #652 |
We need a new module for rectangular area clipping,
turf-clip
. Usingturf-intersect
for this purpose is extremely inefficient, since it both uses monstrous JSTS and is a generic clipping algorithm which is much slower than a simple axis-aligned rectangular clipping could have been.Here is a sample use case where features are clipped to a tile bbox using turf-intersect, making things much slower than they could have been: https:/mapbox/tile-reduce/blob/be8be60d1c575514a9d4bda74d2d6ca6ab5f6854/index.js#L136
For LineString and MultiLineString, https:/mapbox/lineclip should be great. One recent place where I replaced turf-intersect with lineclip got from 1000ms to 20ms.
For Polygon and MultiPolygon, I suggest writing a Sutherland-Hodgeman implementation (related: #139) in a separate module, similar to lineclip. It makes degenerate edges, yes, but it's not important for a lot of cases (tile-reduce is one).
@morganherlocker @tcql
The text was updated successfully, but these errors were encountered: