turf icon indicating copy to clipboard operation
turf copied to clipboard

pointOnLine is not giving true closest point

Open bulutkartal opened this issue 6 years ago • 18 comments

Technically closest point should be where the point has right angle with line. Following example shows snapped point is off and actually it is not the closest point on the line.

Jsfiddle example is here.

bulutkartal avatar Sep 25 '17 16:09 bulutkartal

Can this be bc of projection difference as well?

bulutkartal avatar Sep 25 '17 19:09 bulutkartal

I'm thinking it might be a Google Maps projection (Leaflet would be the same).

🤔 Not too sure.

DenisCarriere avatar Sep 25 '17 19:09 DenisCarriere

When I use pointToLineDistance , and set mercator as true, it gives me the right distance. but on snapped distance it is different than pointToLineDistance. and there is no option for mercator in pointOnLine.

bulutkartal avatar Sep 25 '17 20:09 bulutkartal

@bulutkartal Feel free to send a PR with an option for mercator. 👍

DenisCarriere avatar Sep 25 '17 20:09 DenisCarriere

I wish I could but I am not that talented when it comes to js :) Is there a way to use turf projection with js? turf.min.js seems like it doesn't include turf projection.

bulutkartal avatar Sep 25 '17 20:09 bulutkartal

I think it is not a projection problem, it seems calculation problem to me. Google maps uses WGS84 and I shouldn't set mercator true to be able to find the right distance.

here is the google maps coordinate information.

Since turf and google maps uses WGS84 I can't figure out what is causing this.

bulutkartal avatar Sep 25 '17 20:09 bulutkartal

@bulutkartal send a PR with your test case to packages/turf-point-on-line/test.js

DenisCarriere avatar Sep 25 '17 21:09 DenisCarriere

@DenisCarriere , I am really not that advanced to be able to do it :( I am sorry. I prepared another fiddle showing snapped position is kinda off on mapbox as well. So it is not google maps, it is turf :(

bulutkartal avatar Sep 26 '17 06:09 bulutkartal

@DenisCarriere I checked but I couldn't do the pull request. I ve never done this before.

bulutkartal avatar Sep 26 '17 15:09 bulutkartal

I've never done this before.

Better late then never right? 😄

GitHub has tons of documentation to get you started, here's one that might help you start: https://help.github.com/articles/fork-a-repo/

DenisCarriere avatar Sep 26 '17 16:09 DenisCarriere

@DenisCarriere thank you for the information. But I don't know anything abt testling or tape. So I won't be able to make changes on packages/turf-point-on-line/test.js in correct way. All I will do is just copy and paste the coordinates from my fiddles. I am a great user but not a developer :) Fiddles show there is an issue with pointOnLine, but I am really far from doing what did you ask me to do :) Sorry for that. I love the functions you guys provided with turf.js but this is all I can do for now.

bulutkartal avatar Sep 26 '17 16:09 bulutkartal

@DenisCarriere , @rowanwins , @stebogit do you guys plan to fix this issue? I will either use pointToLineDistance distance on my project or wait until next version if this will be fixed :)

bulutkartal avatar Sep 27 '17 21:09 bulutkartal

@bulutkartal I wanted to work on this issue, but we recently decided to go for the next major release (v5.0) which will take few weeks. I'll probably spend all my "contributor time" on that, but later I'll definitely get back to this (unless somebody else fixed it in the meanwhile 😄 ).

stebogit avatar Sep 27 '17 21:09 stebogit

Hi all,

I looked a bit into this issue and think I have found the problem: the turf-nearest-point-on-line packages is using turf-line-intersect to calculate LineString intersection between the line segments and the perpendicular of the point. However, the turf-line-intersect package does this in Cartesian coordinates rather than spherical:

https://github.com/Turfjs/turf/blob/4024ab49ce082a6a6420f53f005266b448259972/packages/turf-line-intersect/index.js

See for example correct implementation at (Intersection of two paths given start points and bearings) http://www.movable-type.co.uk/scripts/latlong.html The link here also gives immediately a good test case :)

woutervh- avatar Jan 03 '18 10:01 woutervh-

I'm looking for a function that given a point and a line will give me the closest point on that line using "projection" - i.e. the perpendicular place in that line. Given the results of this function (index) I'm guessing that this will return the closest coordinate that is part of this line. But then I need to calculate if this is the next segment, previous segment and the perpendicular location. I didn't find anything in turf to do it, does any of you know how to solve this without writing a ton of code?

HarelM avatar Jun 04 '21 06:06 HarelM

You may look at

https://github.com/Turfjs/turf/tree/master/packages/turf-point-to-line-distance

In the scenario you describe, where there exists a point on a segment that is closer than the segment's ends, it finds that point

https://github.com/Turfjs/turf/blob/84110709afda447a686ccdf55724af6ca755c1f8/packages/turf-point-to-line-distance/index.ts#L117

and uses it to measure the distance.

zstadler avatar Jun 04 '21 10:06 zstadler

Yup, this is my definition of writing a ton of code...

HarelM avatar Jun 04 '21 10:06 HarelM

For anyone interested here's my implementation, based on this too: https://jsfiddle.net/soulwire/UA6H5/ I wanted to be able to split a line into two parts according to a point close to that route: https://github.com/IsraelHikingMap/Site/blob/af75bbe46cca025f8a3bb74c8958a8f4295589a9/IsraelHiking.Web/src/application/services/spatial.service.ts#L63

HarelM avatar Jun 04 '21 18:06 HarelM