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

[@turf/line-slice-along] Fix offset distance equal to line length #2030

Merged
merged 2 commits into from
Feb 14, 2021
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
4 changes: 3 additions & 1 deletion packages/turf-line-slice-along/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ function lineSliceAlong(line, startDist, stopDist, options) {

if (travelled < startDist && coords.length === origCoordsLength)
throw new Error("Start position is beyond line");
return lineString(coords[coords.length - 1]);

var last = coords[coords.length - 1];
return lineString([last, last]);
}

export default lineSliceAlong;
26 changes: 23 additions & 3 deletions packages/turf-line-slice-along/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from "tape";
import path from "path";
import load from "load-json-file";
import along from "@turf/along";
import length from "@turf/length";
import lineSliceAlong from "./index";

var line1 = load.sync(
Expand All @@ -14,7 +15,7 @@ var route2 = load.sync(
path.join(__dirname, "test", "fixtures", "route2.geojson")
);

test("turf-line-slice -- line1", function (t) {
test("turf-line-slice-along -- line1", function (t) {
var start = 500;
var stop = 750;
var options = { units: "miles" };
Expand All @@ -32,7 +33,7 @@ test("turf-line-slice -- line1", function (t) {
t.end();
});

test("turf-line-slice -- line1 overshoot", function (t) {
test("turf-line-slice-along -- line1 overshoot", function (t) {
var start = 500;
var stop = 1500;
var options = { units: "miles" };
Expand Down Expand Up @@ -86,7 +87,7 @@ test("turf-line-slice-along -- route2", function (t) {
t.end();
});

test("turf-line-slice -- start longer than line length", function (t) {
test("turf-line-slice-along -- start longer than line length", function (t) {
var start = 500000;
var stop = 800000;
var options = { units: "miles" };
Expand All @@ -97,3 +98,22 @@ test("turf-line-slice -- start longer than line length", function (t) {
);
t.end();
});

test("turf-line-slice-along -- start equal to line length", function (t) {
var options = { units: "miles" };
var start = length(line1, options);
var stop = start + 100;

var start_point = along(line1, start, options);
var end_point = along(line1, stop, options);
var sliced = lineSliceAlong(line1, start, stop, options);

t.equal(sliced.type, "Feature");
t.equal(sliced.geometry.type, "LineString");
t.deepEqual(sliced.geometry.coordinates[0], start_point.geometry.coordinates);
t.deepEqual(
sliced.geometry.coordinates[sliced.geometry.coordinates.length - 1],
end_point.geometry.coordinates
);
t.end();
});