Skip to content

Commit

Permalink
Fixed multiple modules that were performing scalar instead of spheric…
Browse files Browse the repository at this point in the history
…al calculations (#2717)

Fixes to multiple modules that were performing scalar (2D) calculations when they should have been doing spherical (3D). Most changes were to nearestPointOnLine. pointToLineDistance now uses nearestPointOnLine for spherical calculations. Flow on corrections impacted lineSlice and nearestPointToLine as well.

Tidied up some tests - fixed module name in test definitions, added a benchmark overall time in a few places for easier comparisons.
  • Loading branch information
smallsaucepan authored Sep 25, 2024
1 parent 68a5bb2 commit 5692841
Show file tree
Hide file tree
Showing 40 changed files with 3,468 additions and 3,161 deletions.
23 changes: 22 additions & 1 deletion packages/turf-line-slice/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { truncate } from "@turf/truncate";
import { featureCollection } from "@turf/helpers";
import { featureCollection, point, lineString } from "@turf/helpers";
import { lineSlice } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -38,3 +38,24 @@ test("turf-line-slice", (t) => {
}
t.end();
});

test("turf-nearest-point-on-line -- issue 2023", (t) => {
const ptStart = point([3.69140625, 51.72702815704774]);
const ptEnd = point([0.31936718356317106, 47.93913163509963]);
const line = lineString([
[3.69140625, 51.72702815704774],
[-5.3173828125, 41.60722821271717],
]);

const slice = lineSlice(ptStart, ptEnd, line);

t.deepEqual(
truncate(slice, { precision: 8 }).geometry.coordinates,
[
[3.69140625, 51.72702816],
[-0.03079923, 48.08596086],
],
"slice should be [[3.69140625, 51.72702816], [-0.03079923, 48.08596086]]"
);
t.end();
});
4 changes: 2 additions & 2 deletions packages/turf-line-slice/test/out/line1.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"geometry": {
"type": "LineString",
"coordinates": [
[-97.835729, 22.247393],
[-97.835747, 22.247595],
[-97.820892, 22.17596],
[-97.738467, 22.051207]
[-97.738477, 22.051413]
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-line-slice/test/out/line2.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"geometry": {
"type": "LineString",
"coordinates": [
[0.049987, 0.049987],
[0.849858, 0.849858]
[0.05, 0.050008],
[0.849981, 0.850017]
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-line-slice/test/out/route2.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3796,7 +3796,7 @@
"geometry": {
"type": "LineString",
"coordinates": [
[-111.895792, 48.877416],
[-111.895843, 48.877468],
[-111.878176, 48.860393],
[-111.867242, 48.849753],
[-111.866486, 48.849013],
Expand Down
18 changes: 9 additions & 9 deletions packages/turf-nearest-point-on-line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

## nearestPointOnLine

Takes a [Point][1] and a [LineString][2] and calculates the closest Point on the (Multi)LineString.
Returns the nearest point on a line to a given point.

### Parameters

* `lines` **([Geometry][3] | [Feature][4]<([LineString][2] | [MultiLineString][5])>)** lines to snap to
* `pt` **([Geometry][3] | [Feature][4]<[Point][1]> | [Array][6]<[number][7]>)** point to snap from
* `lines` **([Geometry][1] | [Feature][2]<([LineString][3] | [MultiLineString][4])>)** lines to snap to
* `pt` **([Geometry][1] | [Feature][2]<[Point][5]> | [Array][6]<[number][7]>)** point to snap from
* `options` **[Object][8]** Optional parameters (optional, default `{}`)

* `options.units` **[string][9]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
Expand All @@ -34,17 +34,17 @@ var addToMap = [line, pt, snapped];
snapped.properties['marker-color'] = '#00f';
```

Returns **[Feature][4]<[Point][1]>** closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
Returns **[Feature][2]<[Point][5]>** closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.

[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[1]: https://tools.ietf.org/html/rfc7946#section-3.1

[2]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[2]: https://tools.ietf.org/html/rfc7946#section-3.2

[3]: https://tools.ietf.org/html/rfc7946#section-3.1
[3]: https://tools.ietf.org/html/rfc7946#section-3.1.4

[4]: https://tools.ietf.org/html/rfc7946#section-3.2
[4]: https://tools.ietf.org/html/rfc7946#section-3.1.5

[5]: https://tools.ietf.org/html/rfc7946#section-3.1.5
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.2

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

Expand Down
15 changes: 12 additions & 3 deletions packages/turf-nearest-point-on-line/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { nearestPointOnLine } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -29,10 +29,19 @@ const fixtures = fs.readdirSync(directory).map((filename) => {
* route1 x 195 ops/sec ±2.23% (80 runs sampled)
* route2 x 218 ops/sec ±2.42% (78 runs sampled)
*/
let totalTime = 0.0;
const suite = new Benchmark.Suite("turf-nearest-point-on-line");
for (const { name, geojson } of fixtures) {
const [line, point] = geojson.features;
suite.add(name, () => nearestPointOnLine(line, point));
suite.add(name, () => nearestPointOnLine(line, point), {
onComplete: (e: Event) =>
(totalTime = totalTime += e.target.times?.elapsed),
});
}

suite.on("cycle", (e) => console.log(String(e.target))).run();
suite
.on("cycle", (e: Event) => console.log(String(e.target)))
.on("complete", () =>
console.log(`completed in ${totalTime.toFixed(2)} seconds`)
)
.run();
Loading

0 comments on commit 5692841

Please sign in to comment.