Skip to content
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
37 changes: 22 additions & 15 deletions packages/turf-line-slice/bench.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import Benchmark from "benchmark";
import { point } from "@turf/helpers";
import { lineSlice } from "./index.js";
import { loadJsonFileSync } from "load-json-file";
import { Feature, FeatureCollection, LineString } from "geojson";

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

var route1 = JSON.parse(fs.readFileSync(__dirname + "/test/in/route1.geojson"));
var route2 = JSON.parse(fs.readFileSync(__dirname + "/test/in/route2.geojson"));
var line1 = JSON.parse(fs.readFileSync(__dirname + "/test/in/line1.geojson"));
const route1: FeatureCollection = loadJsonFileSync(
path.join(__dirname, "test", "in", "route1.geojson")
);
const route2: FeatureCollection = loadJsonFileSync(
path.join(__dirname, "test", "in", "route2.geojson")
);
const line1: FeatureCollection = loadJsonFileSync(
path.join(__dirname, "test", "in", "line1.geojson")
);

var start1 = point([-97.79617309570313, 22.254624939561698]);
var stop1 = point([-97.72750854492188, 22.057641623615734]);
var start2 = point([-79.0850830078125, 37.60117623656667]);
var stop2 = point([-77.7667236328125, 38.65119833229951]);
var start3 = point([-112.60660171508789, 45.96021963947196]);
var stop3 = point([-111.97265625, 48.84302835299516]);
const start1 = point([-97.79617309570313, 22.254624939561698]);
const stop1 = point([-97.72750854492188, 22.057641623615734]);
const start2 = point([-79.0850830078125, 37.60117623656667]);
const stop2 = point([-77.7667236328125, 38.65119833229951]);
const start3 = point([-112.60660171508789, 45.96021963947196]);
const stop3 = point([-111.97265625, 48.84302835299516]);

var suite = new Benchmark.Suite("turf-line-slice");
const suite = new Benchmark.Suite("turf-line-slice");
suite
.add("turf-line-slice#simple", function () {
lineSlice(start1, stop1, line1);
lineSlice(start1, stop1, line1.features[0] as Feature<LineString>);
})
.add("turf-line-slice#route1", function () {
lineSlice(start2, stop2, route1);
lineSlice(start2, stop2, route1.features[0] as Feature<LineString>);
})
.add("turf-line-slice#route2", function () {
lineSlice(start3, stop3, route2);
lineSlice(start3, stop3, route2.features[0] as Feature<LineString>);

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.

Pretty sure this was all just broken when the test fixtures changed to FeatureCollections?

})
.on("cycle", function (event) {
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.run();
14 changes: 0 additions & 14 deletions packages/turf-line-slice/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getCoords, getType } from "@turf/invariant";
import { lineString as linestring } from "@turf/helpers";
import { Coord, lineString as linestring } from "@turf/helpers";
import { nearestPointOnLine } from "@turf/nearest-point-on-line";
import { Feature, LineString } from "geojson";

/**
* Takes a {@link LineString|line}, a start {@link Point}, and a stop point
Expand Down Expand Up @@ -31,30 +32,32 @@ import { nearestPointOnLine } from "@turf/nearest-point-on-line";
* //addToMap
* var addToMap = [start, stop, line]
*/
function lineSlice(startPt, stopPt, line) {
function lineSlice(
startPt: Coord,
stopPt: Coord,
line: Feature<LineString> | LineString
): Feature<LineString> {
// Validation
var coords = getCoords(line);
const coords = getCoords(line);
if (getType(line) !== "LineString")
throw new Error("line must be a LineString");

var startVertex = nearestPointOnLine(line, startPt);
var stopVertex = nearestPointOnLine(line, stopPt);
var ends;
if (startVertex.properties.index <= stopVertex.properties.index) {
ends = [startVertex, stopVertex];
} else {
ends = [stopVertex, startVertex];
}
var clipCoords = [ends[0].geometry.coordinates];
const startVertex = nearestPointOnLine(line, startPt);
const stopVertex = nearestPointOnLine(line, stopPt);
const ends =
startVertex.properties.index <= stopVertex.properties.index
? [startVertex, stopVertex]
: [stopVertex, startVertex];
const clipCoords = [ends[0].geometry.coordinates];
for (
var i = ends[0].properties.index + 1;
let i = ends[0].properties.index + 1;
i < ends[1].properties.index + 1;
i++
) {
clipCoords.push(coords[i]);
}
clipCoords.push(ends[1].geometry.coordinates);
return linestring(clipCoords, line.properties);
return linestring(clipCoords, line.type === "Feature" ? line.properties : {});
}

export { lineSlice };
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-line-slice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
"tape": "^5.9.0",
"tsup": "^8.4.0",
"tsx": "^4.19.4",
"typescript": "^5.8.3",
"write-json-file": "^6.0.0"
},
"dependencies": {
"@turf/helpers": "workspace:*",
"@turf/invariant": "workspace:*",
"@turf/nearest-point-on-line": "workspace:*",
"@types/geojson": "^7946.0.10"
"@types/geojson": "^7946.0.10",
"tslib": "^2.8.1"
}
}
13 changes: 9 additions & 4 deletions packages/turf-line-slice/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { writeJsonFileSync } from "write-json-file";
import { truncate } from "@turf/truncate";
import { featureCollection, point, lineString } from "@turf/helpers";
import { lineSlice } from "./index.js";
import { Feature, FeatureCollection, LineString, Point } from "geojson";

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

Expand All @@ -19,16 +20,20 @@ const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directories.in + filename),
geojson: loadJsonFileSync(directories.in + filename) as FeatureCollection,
};
});

test("turf-line-slice", (t) => {
for (const { filename, geojson, name } of fixtures) {
const [linestring, start, stop] = geojson.features;
const [linestring, start, stop] = geojson.features as [
Feature<LineString>,
Feature<Point>,
Feature<Point>,
];
const sliced = truncate(lineSlice(start, stop, linestring));
sliced.properties["stroke"] = "#f0f";
sliced.properties["stroke-width"] = 6;
sliced.properties!["stroke"] = "#f0f";
sliced.properties!["stroke-width"] = 6;
const results = featureCollection(geojson.features);
results.features.push(sliced);

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.