diff --git a/packages/turf-great-circle/README.md b/packages/turf-great-circle/README.md index 8703e2577..30eae73ae 100644 --- a/packages/turf-great-circle/README.md +++ b/packages/turf-great-circle/README.md @@ -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 diff --git a/packages/turf-great-circle/index.js b/packages/turf-great-circle/index.js index bd4ee79d8..c7d36243e 100644 --- a/packages/turf-great-circle/index.js +++ b/packages/turf-great-circle/index.js @@ -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 @@ -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( diff --git a/packages/turf-great-circle/test.ts b/packages/turf-great-circle/test.ts index f5a1a3c3a..61613aab1 100644 --- a/packages/turf-great-circle/test.ts +++ b/packages/turf-great-circle/test.ts @@ -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)); @@ -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(); +});