Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2342 - Handle when start and end are the same point in greatCircle #2343

Merged
merged 6 commits into from
Oct 15, 2024
Merged
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
3 changes: 2 additions & 1 deletion packages/turf-great-circle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

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

### Parameters

Expand Down
12 changes: 11 additions & 1 deletion packages/turf-great-circle/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { lineString } from "@turf/helpers";
import { getCoord } from "@turf/invariant";
import { GreatCircle } from "./lib/arc.js";

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

start = getCoord(start);
end = getCoord(end);

properties = properties || {};
npoints = npoints || 100;

if (start[0] === end[0] && start[1] === end[1]) {
const arr = Array(npoints);
arr.fill([start[0], start[1]]);
return lineString(arr, properties);
}

offset = offset || 10;

var generator = new GreatCircle(
Expand Down
22 changes: 21 additions & 1 deletion packages/turf-great-circle/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 { greatCircle } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -39,3 +39,23 @@ test("turf-great-circle", (t) => {
});
t.end();
});

test("turf-great-circle with same input and output", (t) => {
const start = point([0, 0]);
const end = point([0, 0]);
const line = greatCircle(start, end, {
npoints: 4,
});

t.deepEquals(
lineString([
[0, 0],
[0, 0],
[0, 0],
[0, 0],
]),
line
);

t.end();
});