-
Notifications
You must be signed in to change notification settings - Fork 943
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-ellipse] fixes the wrong shape when drawing "big ellipses" near the poles #2739
base: master
Are you sure you want to change the base?
Conversation
-Fixes the wrong behavior of turf-ellipse when drawing "big ellipses" near the poles -Fixes the wrong behavior of turf-ellipse when rotating an ellipse. The rotation is now fully taken account during the calculation of the geometry. -Adds two test to directly check both previous points -Fixes one of the existing test `turf-ellipse` which was not launched when launching `pnpm test` -Changes the test `turf-ellipse` so that it fits the fixes made in the calculation of the geometry
Thanks @hadbn. What a fantastic contribution. Doing some comparisons between the old and new test fixtures, have the "*-degrees" tests been broken all along? This current test output suggests the ellipses have never been the right size longitudinally, let alone the correct shape? Also, and I need to apologise for not mentioning it sooner, there is another ellipse issue open - #1767 It has a solution suggested, and I'm wondering if that could be incorporated into your PR without much extra effort? It would mean (I think) adding an optional Again, sorry for not mentioning this sooner. Didn't expect you to surge ahead with a PR right away! If that's not possible for you let me know and I will have a look myself. |
To be honest, I didn't spend much time studying the old tests so I couldn't say what was functional and what wasn't. However, I did notice a lot of errors like the ones you point out, which motivated me to change how the tests are run. It might also be worth thinking about more "robust" tests. The “in”/“out” comparison tests only allow you to check that the geometry doesn't “change” according to the implementation, but they don't verify the accuracy of the geometry. It was with this in mind that I introduced the two tests of comparison with a circle and invariance by rotation. If you have any other idea of comparison feel free to tell me and I will add them. I actually saw the other issue you are mentionning. As it's my first PR I wasn't sure if I could resolve two issues in a single PR, so I did not. |
In this case closing two issues in the same module will be fine. And happy to stick with the tests the way you're doing them. |
…t they are equidistant from one other
@smallsaucepan here are the modifications. Moreover, this method gives prettier ellipses but I have to admit that it's quite expensive. Are you happy with that ? |
Thanks @hadbn. Discussing with the other maintainers about the performance impact. |
…ence is now optional. The default value for accuracy (0) leads to the same result as previously. Using custom values for accuracy will distribute points along the circumference (which is more precise for "thin" ellipses, but more expensive).
@smallsaucepan as I was concerned about the cost of the new method, I updated the function. The parameter "accuracy" is now optionnal. When not used (or set to 0) the ellipse will be calculated using the historical method. When the parameter is used, the new method will be used. I am -of course- opened to every comment you (or other maintainers) could make. |
Thanks @hadbn. Let's talk through our options. Firstly, the other maintainers weren't too concerned if the new method is slower, as It's probably ok for us to admit the old algorithm doesn't provide satisfactory results for most cases, and we probably shouldn't use it any more or keep the code. Your point about the new algorithm being of limited value the closer we get to a circle makes me wonder if we can base the default accuracy on the ratio between major and minor axis? Did some tests below: L-R is old method (red), then accuracy 1, 2 and 3 in shades of green. Major:minor ratio 2:1Hard to distinguish much of a difference. Major:minor ratio 5:1Starting to see slight differences - third row of accuracy 1 points are noticeably lower than same points for accuracy 2. Major:minor ratio 10:1Differences for accuracy 1 are very noticeable. 2 and 3 still seem pretty similar. What would you think about defaulting accuracy to: ratio < 5 accuracy 1 and letting the user override it if they want to? No need to implement that yet. Let's discuss and agree on an approach first 👍 |
Thanks. Your examples are very relevant. I'm happy with the thresholds you suggest for the ratio being >= 5, and for letting the user override the accuracy. ratio < 2 : accuracy 0 (which is ~half the cost of 1 accuracy) |
Of course! Silly of me. I forgot that would be for more circular ellipses. Apologies. I was playing around with a couple of other ideas. Notably, found this solution: https://stackoverflow.com/a/20257593/3606644 It seems to work better with those high aspect ratio ellipses, without having to do the expensive perimeter calculation first. Only problem is steps would have to be a multiple of four (it calculates a single quadrant). Which led me to wonder what happens if a user puts in a steps not divisible by four? Turf has probably done this for forever, so I suspect if anyone was doing that they would have raised it as a bug. Would you mind if I investigate that other approach a bit, and if it performs better than the perimeter method maybe we can look at merging that with your initial commits? Appreciate your patience working though this. |
This other approach looks interesting. I'm looking forward to see how it will perform. Would you mind keeping me in touch ? I'm not sure to understand what you meant by saying "Turf has probably done this for forever, so I suspect if anyone was doing that they would have raised it as a bug.". Is there an historical limitation to ensure that steps is a multible of 4 ? |
@hadbn of course! Performance is about 5x slower than the old version, which is about 2x faster than accuracy 1. So, it seems promising. I'm having trouble with the results I'm getting though. Would you mind taking a look if I share the implementation? That way we can both troubleshoot. For some reason my segment lengths aren't as uniform as the example, and the last segment is oversized. My results are the red graph.
By that I mean Turf has always given the user exactly the number of steps they ask for - even it it's a weird number like 11 that leads to the blunted shape above. I'm presuming that if anyone was actually doing that they'd have probably raised it as a Turf bug, even though the function was giving them exactly what they were asking for. |
I assume accuracy 0 to be 2x faster than accuracy 1, so accuracy 0 and your method should be equivalent (performance speaking). Yes of course, please share the implementation ! I see your point when you assume that 11 steps are required. But what if users are curently asking for 101 steps for instance ? The distance between points would be smaller so it won't lead to the blunted shape (or less), and users would not have raised an issue. However, it's still not a multiple of 4. |
@smallsaucepan do you still need me to take a look at your implementation ? In such case, could you share it ? |
Sorry @hadbn, I had to prioritise some other stuff. Yes, would be great if you could take a look! I had the bright idea of pushing my work in progress to a branch of hadbn:fix-ellipse-harversine. Then if we get it to work you could just merge that work in progress branch into your fix-ellipse-harversine branch and the PR could proceed from there. I don't have permission to do that though i.e. create a branch in your repo. Would you be comfortable to add me as a collaborator to https://github.com/hadbn/turf ? |
Just sent you an invite. Thx. |
…can review and help debug. Approach shows promise however results aren't as expected (see Turfjs#2739 (comment)) for details.
Done! Thanks @hadbn. Have pushed a branch Appreciate your help. Let me know when you've had a chance to take a look and / or if you can see what the problem is! |
@smallsaucepan, I just took a look at it, sorry for the delay. You can find the corrections on the branch you pushed on. The method you took from stackoverflow is usefull to calculate the values of the parameter Given a point corresponding to the parameter This angle is the one that can be used to calculate the local radius, and the destination. |
It was not obvious at all, though ! Took me quite a while to figure it out :) |
That's great you were able to track it down @hadbn. Thank you. I've updated from the riemann-wip branch, and am getting the result below though? 7.1.0 is red, riemann-wip is green. Is there still something I need to do? |
Sorry I ran some tests and forgot to revert before pushing. I just fixed it (on Riemann-wip). Sorry about that. |
Amazing work @hadbn! They look great 👍 Really pleased we spent the time getting this right, and appreciate your help. Are you happy to merge riemann-wip back into your fix-ellipse-haversine branch, and then we can ping one of the other maintainers to review the PR? |
Yes, seems good to me, I will do so. Do you confirm that I can delete the other implementations and only keep riemann for the pr ? |
Definitely. Only had the others in there for ease of comparing performance, which I'll paste below for whoever reviews:
old - quick but very poor aesthetically |
Btw, with this code // Divide steps by 4 for one quadrant
steps = Math.floor(steps / 4); let's use Math.ceil instead to make sure the quality errs on the high side. |
-Fixes the wrong behavior of
turf-ellipse
when drawing "big ellipses" near the poles-Fixes the wrong behavior of
turf-ellipse
when rotating an ellipse. The rotation is now fully taken account during the calculation of the geometry.-Adds two test to directly check both previous points
-Fixes one of the existing test
turf-ellipse
which was not launched when launchingpnpm test
-Changes the test
turf-ellipse
so that it fits the fixes made in the calculation of the geometryResolves #1767
Resolves #2736