Skip to content

Commit 3b5f94a

Browse files
Issue 2342 - Handle when start and end are the same point in greatCircle (#2343)
* Handle when start and end are the same point * Start populating CHANGELOG.md * See if husky is running yet... * prettier * Update handling to match npoints --------- Co-authored-by: James Beard <[email protected]>
1 parent e7227f5 commit 3b5f94a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/turf-great-circle/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
Calculate great circles routes as [LineString][1] or [MultiLineString][2].
88
If the `start` and `end` points span the antimeridian, the resulting feature will
9-
be split into a `MultiLineString`.
9+
be split into a `MultiLineString`. If the `start` and `end` positions are the same
10+
then a `LineString` will be returned with duplicate coordinates the length of the `npoints` option.
1011

1112
### Parameters
1213

packages/turf-great-circle/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { lineString } from "@turf/helpers";
12
import { getCoord } from "@turf/invariant";
23
import { GreatCircle } from "./lib/arc.js";
34

45
/**
56
* Calculate great circles routes as {@link LineString} or {@link MultiLineString}.
67
* If the `start` and `end` points span the antimeridian, the resulting feature will
7-
* be split into a `MultiLineString`.
8+
* be split into a `MultiLineString`. If the `start` and `end` positions are the same
9+
* then a `LineString` will be returned with duplicate coordinates the length of the `npoints` option.
810
*
911
* @function
1012
* @param {Coord} start source point feature
@@ -34,8 +36,16 @@ function greatCircle(start, end, options) {
3436

3537
start = getCoord(start);
3638
end = getCoord(end);
39+
3740
properties = properties || {};
3841
npoints = npoints || 100;
42+
43+
if (start[0] === end[0] && start[1] === end[1]) {
44+
const arr = Array(npoints);
45+
arr.fill([start[0], start[1]]);
46+
return lineString(arr, properties);
47+
}
48+
3949
offset = offset || 10;
4050

4151
var generator = new GreatCircle(

packages/turf-great-circle/test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath } from "url";
55
import { loadJsonFileSync } from "load-json-file";
66
import { writeJsonFileSync } from "write-json-file";
77
import { truncate } from "@turf/truncate";
8-
import { featureCollection } from "@turf/helpers";
8+
import { featureCollection, point, lineString } from "@turf/helpers";
99
import { greatCircle } from "./index.js";
1010

1111
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -39,3 +39,23 @@ test("turf-great-circle", (t) => {
3939
});
4040
t.end();
4141
});
42+
43+
test("turf-great-circle with same input and output", (t) => {
44+
const start = point([0, 0]);
45+
const end = point([0, 0]);
46+
const line = greatCircle(start, end, {
47+
npoints: 4,
48+
});
49+
50+
t.deepEquals(
51+
lineString([
52+
[0, 0],
53+
[0, 0],
54+
[0, 0],
55+
[0, 0],
56+
]),
57+
line
58+
);
59+
60+
t.end();
61+
});

0 commit comments

Comments
 (0)