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

nearestPointOnLine uses wrong calculation #1878

Open
manly opened this issue Mar 30, 2020 · 2 comments
Open

nearestPointOnLine uses wrong calculation #1878

manly opened this issue Mar 30, 2020 · 2 comments

Comments

@manly
Copy link

manly commented Mar 30, 2020

https://github.com/Turfjs/turf/blob/master/packages/turf-nearest-point-on-line/index.ts

This code is wrong. It is an approximation.

You calculate the azimuth (bearing/direction) between p1 and p2, and from that azimuth, you try to make a perpendicular great circle by changing that azimuth by +180 (or -180, whatever). The problem with this is that, as you move along the path from p1 to p2, your azimuth constantly changes, assuming you are taking the shortest great-circle distance (ie: Haversine/Vincenty). Unless you use a line that assumes the azimuth does not change (ie: occasionally used for moving by sea), your azimuth only applies for p1. p2 has a different azimuth, and so does every single point along that great-circle.

It is a decent approximation of an answer for short distances, but know that this calculation will be way* off if distance(p1, p2) is either long, or if you're close to true north/south pole.

If you do have the proper code for this, I would welcome you let me know.

Thanks

-Phil

@rowanwins
Copy link
Member

Thank you for the description of the issue @manly

This module is in need of a bit of an overhaul - there are number of reported issues with it.

THere are perhaps some interesting ideas here which could be adapted
https://stackoverflow.com/questions/16429562/find-a-point-in-a-polyline-which-is-closest-to-a-latlng

@smallsaucepan
Copy link
Member

Hi @JamesLMilner @rowanwins @twelch . Will take a look at this if you'd like to assign my way.

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

No branches or pull requests

4 participants