Skip to content

Commit

Permalink
Issue 2342 - Handle when start and end are the same point in greatCir…
Browse files Browse the repository at this point in the history
…cle (#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 <james@smallsaucepan.com>
  • Loading branch information
rowanwins and smallsaucepan authored Oct 15, 2024
1 parent e7227f5 commit 3b5f94a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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();
});

0 comments on commit 3b5f94a

Please sign in to comment.