mirror of
https://github.com/Turfjs/turf.git
synced 2026-02-01 16:57:21 +00:00
@turf/line-slice to TypeScript (#2979)
This commit is contained in:
parent
7aaae1795b
commit
eb9de6486c
@ -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>);
|
||||
})
|
||||
.on("cycle", function (event) {
|
||||
.on("cycle", function (event: any) {
|
||||
console.log(String(event.target));
|
||||
})
|
||||
.run();
|
||||
|
||||
14
packages/turf-line-slice/index.d.ts
vendored
14
packages/turf-line-slice/index.d.ts
vendored
@ -1,14 +0,0 @@
|
||||
import { Feature, LineString } from "geojson";
|
||||
import { Coord } from "@turf/helpers";
|
||||
|
||||
/**
|
||||
* http://turfjs.org/docs/#lineslice
|
||||
*/
|
||||
declare function lineSlice(
|
||||
startPt: Coord,
|
||||
stopPt: Coord,
|
||||
line: Feature<LineString> | LineString
|
||||
): Feature<LineString>;
|
||||
|
||||
export { lineSlice };
|
||||
export default lineSlice;
|
||||
@ -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
|
||||
@ -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 };
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@ -3876,6 +3876,9 @@ importers:
|
||||
'@types/geojson':
|
||||
specifier: ^7946.0.10
|
||||
version: 7946.0.14
|
||||
tslib:
|
||||
specifier: ^2.8.1
|
||||
version: 2.8.1
|
||||
devDependencies:
|
||||
'@turf/truncate':
|
||||
specifier: workspace:*
|
||||
@ -3901,6 +3904,9 @@ importers:
|
||||
tsx:
|
||||
specifier: ^4.19.4
|
||||
version: 4.19.4
|
||||
typescript:
|
||||
specifier: ^5.8.3
|
||||
version: 5.8.3
|
||||
write-json-file:
|
||||
specifier: ^6.0.0
|
||||
version: 6.0.0
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user