diff --git a/packages/turf-nearest-point-on-line/index.ts b/packages/turf-nearest-point-on-line/index.ts index c3f60b6b7..f80bbd773 100644 --- a/packages/turf-nearest-point-on-line/index.ts +++ b/packages/turf-nearest-point-on-line/index.ts @@ -250,13 +250,10 @@ function nearestPointOnSegment( // If angle AI or BI is greater than angleAB, I lies on the circle *beyond* A // and B so use the closest of A or B as the intersection if (angle(A, I) > angleAB || angle(B, I) > angleAB) { - if ( - distance(vectorToLngLat(I), vectorToLngLat(A)) <= - distance(vectorToLngLat(I), vectorToLngLat(B)) - ) { - return [vectorToLngLat(A), true, false]; + if (distance(posC, posA) <= distance(posC, posB)) { + return [posA, true, false]; } else { - return [vectorToLngLat(B), false, true]; + return [posB, false, true]; } } diff --git a/packages/turf-nearest-point-on-line/package.json b/packages/turf-nearest-point-on-line/package.json index 779c78e6b..35a24568c 100644 --- a/packages/turf-nearest-point-on-line/package.json +++ b/packages/turf-nearest-point-on-line/package.json @@ -3,6 +3,9 @@ "version": "7.2.0", "description": "Finds the nearest point on a line to a given point", "author": "Turf Authors", + "contributors": [ + "mike castleman <@mlc>" + ], "license": "MIT", "bugs": { "url": "https://github.com/Turfjs/turf/issues" diff --git a/packages/turf-nearest-point-on-line/test.ts b/packages/turf-nearest-point-on-line/test.ts index 34e2e73c6..47e837022 100644 --- a/packages/turf-nearest-point-on-line/test.ts +++ b/packages/turf-nearest-point-on-line/test.ts @@ -517,3 +517,20 @@ test("turf-nearest-point-on-line -- issue 2808 redundant point support", (t) => t.end(); }); + +test("turf-nearest-point-on-line -- issue 2870 faraway point", (t) => { + const line1 = lineString([ + [-96.80974, -58.32062], + [-50.2464, 38.36643], + ]); + const thePoint = point([13.6023, 46.50646]); + + const nearest = nearestPointOnLine(line1, thePoint); + t.deepEqual( + truncate(nearest, { precision: 8 }).geometry.coordinates, + [-50.2464, 38.36643], + "nearest point should be [-50.2464, 38.36643]" + ); + + t.end(); +});