turf icon indicating copy to clipboard operation
turf copied to clipboard

Wrong distance calculated from pointToLineDistance

Open mathildor opened this issue 3 years ago • 6 comments

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.

image

Example illustrating the bug:

https://jsfiddle.net/h8c9pvfa/36/

mathildor avatar Mar 15 '22 08:03 mathildor

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.

Screen Shot 2022-03-17 at 8 52 24 pm

Hopefully that helps, if so feel free to close this issue :)

rowanwins avatar Mar 17 '22 10:03 rowanwins

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/

mathildor avatar Mar 21 '22 11:03 mathildor

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.

mathildor avatar Mar 21 '22 12:03 mathildor

@rowanwins have you had time to look further into this?

mathildor avatar Mar 30 '22 09:03 mathildor

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

zo0m avatar Jun 19 '23 12:06 zo0m