-
Notifications
You must be signed in to change notification settings - Fork 1k
@turf/line-slice-along to TypeScript #2978
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| 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(); | ||
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}, | ||
|
|
@@ -26,31 +27,36 @@ 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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
That'd also make sure we're not passing any rando options further down to distance or destination.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point about accidentally sending extra options down. |
||
| const { units = "kilometers" } = options; | ||
|
|
||
| var coords; | ||
| var slice = []; | ||
| var coords: Position[]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another couple of vars we could jettison.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| direction = bearing(coords[i], coords[i - 1]) - 180; | ||
| interpolated = destination(coords[i], overshot, direction, options); | ||
| interpolated = destination(coords[i], overshot, direction, { units }); | ||
| slice.push(interpolated.geometry.coordinates); | ||
| } | ||
|
|
||
|
|
@@ -61,7 +67,7 @@ function lineSliceAlong(line, startDist, stopDist, options) { | |
| return lineString(slice); | ||
| } | ||
| direction = bearing(coords[i], coords[i - 1]) - 180; | ||
| interpolated = destination(coords[i], overshot, direction, options); | ||
| interpolated = destination(coords[i], overshot, direction, { units }); | ||
| slice.push(interpolated.geometry.coordinates); | ||
| return lineString(slice); | ||
| } | ||
|
|
@@ -74,7 +80,7 @@ function lineSliceAlong(line, startDist, stopDist, options) { | |
| return lineString(slice); | ||
| } | ||
|
|
||
| travelled += distance(coords[i], coords[i + 1], options); | ||
| travelled += distance(coords[i], coords[i + 1], { units }); | ||
| } | ||
|
|
||
| if (travelled < startDist && coords.length === origCoordsLength) | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.