Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/turf-nearest-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/turf-nearest-point-on-line/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions packages/turf-nearest-point-on-line/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});