turf
turf copied to clipboard
Wrong distance calculated from pointToLineDistance
Turf version:
6.5.0
Description of issue:
There seems to be a bug in the calculation of the minimum distance from a point to a line in turf. When calculating the length of a line showing the minimum distance between a point and a line I get a different value than when using the pointToLineDistance function.
Example illustrating the bug:
https://jsfiddle.net/h8c9pvfa/36/
Hi @mathildor
So I think this is a quirk of visualising data in EPSG:3857 (Web Mercator) and the data being in EPSG:4326 (WGS84).
Pulling your data into QGIS and visualising it in WGS84 shows their different trajectory. I also used QGIS to calculate the nearest point and then checked their respective lengths and they are pretty similar to what Turf is generating.
Hopefully that helps, if so feel free to close this issue :)
Hi again,
I looked into this a bit further and I'm still confused.
If I use turf. nearestPointOnLine this function also gives 3,4m while turf. pointToLineDistance says 4,3m. As far as I can see in the documentation, these functions should give me the same results?
I updated the jsfiddle to show the two calculations:
https://jsfiddle.net/n5tsq0uy/4/
Also, won't looking at the data in QGIS in epsg:4326 be misleading when finding the nearest point, since the coordinate-system has different scaling in the x- and y-axis? I think the point you're showing as the nearest point in QGIS is only the point that looks like the closes point in that projection, due to the scaling.
@rowanwins have you had time to look further into this?
Faced with the same issue:
const distanceToLine = turf.pointToLineDistance(point, line, distanceOptions);
const distanceToLinePoint = turf.distance(point, turf.nearestPointOnLine(line, point), distanceOptions);
I expected that these distances should be the same. turf.pointToLineDistance - returns the wrong value