Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 20 additions & 17 deletions packages/turf-line-slice-along/bench.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import Benchmark from "benchmark";
import { lineSliceAlong } from "./index.js";
import { loadJsonFileSync } from "load-json-file";
import { Feature, LineString } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

var line1 = JSON.parse(
fs.readFileSync(__dirname + "/test/fixtures/line1.geojson")
var line1: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "line1.geojson")
);
var route1 = JSON.parse(
fs.readFileSync(__dirname + "/test/fixtures/route1.geojson")
var route1: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "route1.geojson")
);
var route2 = JSON.parse(
fs.readFileSync(__dirname + "/test/fixtures/route2.geojson")
var route2: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "route2.geojson")
);

const options = { units: "miles" } as const;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this had been broken when we changed the options to an object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. So why wasn't the isObject call catching this? Or was bench just not being run by anyone?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it was just broken. There's no actual testing making sure bench works. I don't think we really want to just burn CI time benchmarking though.


var suite = new Benchmark.Suite("turf-line-slice-along");
suite
.add("turf-line-slice-along#line1 5-15 miles", function () {
lineSliceAlong(line1, 5, 15, "miles");
lineSliceAlong(line1, 5, 15, options);
})
.add("turf-line-slice-along#line1 50-250 miles", function () {
lineSliceAlong(line1, 50, 250, "miles");
lineSliceAlong(line1, 50, 250, options);
})
.add("turf-line-slice-along#line1 250-500 miles", function () {
lineSliceAlong(line1, 250, 500, "miles");
lineSliceAlong(line1, 250, 500, options);
})
.add("turf-line-slice-along#route1 5-15 miles", function () {
lineSliceAlong(route1, 5, 15, "miles");
lineSliceAlong(route1, 5, 15, options);
})
.add("turf-line-slice-along#route1 50-250 miles", function () {
lineSliceAlong(route1, 50, 250, "miles");
lineSliceAlong(route1, 50, 250, options);
})
.add("turf-line-slice-along#route1 250-500 miles", function () {
lineSliceAlong(route1, 250, 500, "miles");
lineSliceAlong(route1, 250, 500, options);
})
.add("turf-line-slice-along#route2 5-15 miles", function () {
lineSliceAlong(route2, 5, 15, "miles");
lineSliceAlong(route2, 5, 15, options);
})
.add("turf-line-slice-along#route2 15-25 miles", function () {
lineSliceAlong(route2, 15, 25, "miles");
lineSliceAlong(route2, 15, 25, options);
})
.add("turf-line-slice-along#route2 25-35 miles", function () {
lineSliceAlong(route2, 25, 35, "miles");
lineSliceAlong(route2, 25, 35, options);
})
.on("cycle", function (event) {
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.run();
17 changes: 0 additions & 17 deletions packages/turf-line-slice-along/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { bearing } from "@turf/bearing";
import { distance } from "@turf/distance";
import { destination } from "@turf/destination";
import { lineString, isObject } from "@turf/helpers";
import { lineString, isObject, Units } from "@turf/helpers";
import { Feature, LineString, Point, Position } from "geojson";

/**
* Takes a {@link LineString|line}, a specified distance along the line to a start {@link Point},
Expand All @@ -26,25 +27,29 @@ import { lineString, isObject } from "@turf/helpers";
* //addToMap
* var addToMap = [line, start, stop, sliced]
*/
function lineSliceAlong(line, startDist, stopDist, options) {
function lineSliceAlong(
line: Feature<LineString> | LineString,
startDist: number,
stopDist: number,
options: { units?: Units } = {}
): Feature<LineString> {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reckon we should set default units explicitly even if it effectively happens a lower level. Maybe we destructure similar to #2974?

const { units = "kilometers" } = options;

That'd also make sure we're not passing any rando options further down to distance or destination.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about accidentally sending extra options down.


var coords;
var slice = [];
var coords: Position[];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another couple of vars we could jettison.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets just apply eslint's no-var and prefer-const (in another PR)

var slice: Position[] = [];

// Validation
if (line.type === "Feature") coords = line.geometry.coordinates;
else if (line.type === "LineString") coords = line.coordinates;
else throw new Error("input must be a LineString Feature or Geometry");
var origCoordsLength = coords.length;
var travelled = 0;
var overshot, direction, interpolated;
for (var i = 0; i < coords.length; i++) {
const origCoordsLength = coords.length;
let travelled = 0;
let overshot: number, direction: number, interpolated: Feature<Point>;
for (let i = 0; i < coords.length; i++) {
if (startDist >= travelled && i === coords.length - 1) break;
else if (travelled > startDist && slice.length === 0) {
overshot = startDist - travelled;
let overshot = startDist - travelled;
if (!overshot) {
slice.push(coords[i]);
return lineString(slice);
Expand Down
6 changes: 4 additions & 2 deletions packages/turf-line-slice-along/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
"load-json-file": "^7.0.1",
"tape": "^5.9.0",
"tsup": "^8.4.0",
"tsx": "^4.19.4"
"tsx": "^4.19.4",
"typescript": "^5.8.3"
},
"dependencies": {
"@turf/bearing": "workspace:*",
"@turf/destination": "workspace:*",
"@turf/distance": "workspace:*",
"@turf/helpers": "workspace:*",
"@types/geojson": "^7946.0.10"
"@types/geojson": "^7946.0.10",
"tslib": "^2.8.1"
}
}
19 changes: 10 additions & 9 deletions packages/turf-line-slice-along/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import { loadJsonFileSync } from "load-json-file";
import { along } from "@turf/along";
import { length } from "@turf/length";
import { lineSliceAlong } from "./index.js";
import { Feature, LineString } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

var line1 = loadJsonFileSync(
var line1: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "line1.geojson")
);
var route1 = loadJsonFileSync(
var route1: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "route1.geojson")
);
var route2 = loadJsonFileSync(
var route2: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "route2.geojson")
);

test("turf-line-slice-along -- line1", function (t) {
var start = 500;
var stop = 750;
var options = { units: "miles" };
var options = { units: "miles" } as const;

var start_point = along(line1, start, options);
var end_point = along(line1, stop, options);
Expand All @@ -39,7 +40,7 @@ test("turf-line-slice-along -- line1", function (t) {
test("turf-line-slice-along -- line1 overshoot", function (t) {
var start = 500;
var stop = 1500;
var options = { units: "miles" };
var options = { units: "miles" } as const;

var start_point = along(line1, start, options);
var end_point = along(line1, stop, options);
Expand All @@ -57,7 +58,7 @@ test("turf-line-slice-along -- line1 overshoot", function (t) {
test("turf-line-slice-along -- route1", function (t) {
var start = 500;
var stop = 750;
var options = { units: "miles" };
var options = { units: "miles" } as const;

var start_point = along(route1, start, options);
var end_point = along(route1, stop, options);
Expand All @@ -75,7 +76,7 @@ test("turf-line-slice-along -- route1", function (t) {
test("turf-line-slice-along -- route2", function (t) {
var start = 25;
var stop = 50;
var options = { units: "miles" };
var options = { units: "miles" } as const;

var start_point = along(route2, start, options);
var end_point = along(route2, stop, options);
Expand All @@ -93,7 +94,7 @@ test("turf-line-slice-along -- route2", function (t) {
test("turf-line-slice-along -- start longer than line length", function (t) {
var start = 500000;
var stop = 800000;
var options = { units: "miles" };
var options = { units: "miles" } as const;

t.throws(
() => lineSliceAlong(line1, start, stop, options),
Expand All @@ -103,7 +104,7 @@ test("turf-line-slice-along -- start longer than line length", function (t) {
});

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

Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.