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

Fix along with negative distance was undefined #2573

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 19 additions & 11 deletions packages/turf-along/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

Takes a [LineString][1] and returns a [Point][2] at a specified distance along the line.

If [distance][3] is longer than the length of [line][4], the last coordinate of the line is returned
If [distance][3] is negative, it will count distance along the line from end to start of the line. And if
a negative distance overshoots the length of the line, it will return the first coordinate of the line.

### Parameters

* `line` **[Feature][3]<[LineString][4]>** input line
* `distance` **[number][5]** distance along the line
* `options` **[Object][6]?** Optional parameters
* `line` **[Feature][5]<[LineString][6]>** input line
* `distance` **[number][7]** distance along the line
* `options` **[Object][8]?** Optional parameters

* `options.units` **[string][7]** can be degrees, radians, miles, or kilometers (optional, default `"kilometers"`)
* `options.units` **[string][9]** can be degrees, radians, miles, or kilometers (optional, default `"kilometers"`)

### Examples

Expand All @@ -26,23 +30,27 @@ var along = turf.along(line, 200, options);
var addToMap = [along, line]
```

Returns **[Feature][3]<[Point][8]>** Point `distance` `units` along the line
Returns **[Feature][5]<[Point][10]>** Point `distance` `units` along the line

[1]: https://tools.ietf.org/html/rfc7946#section-3.1.4

[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2

[3]: https://tools.ietf.org/html/rfc7946#section-3.2
[3]: distance

[4]: line

[5]: https://tools.ietf.org/html/rfc7946#section-3.2

[4]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[6]: https://tools.ietf.org/html/rfc7946#section-3.1.4

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[8]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[10]: https://tools.ietf.org/html/rfc7946#section-3.1.2

<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->

Expand Down
11 changes: 10 additions & 1 deletion packages/turf-along/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Feature, LineString, Point } from "geojson";
import { bearing } from "@turf/bearing";
import { destination } from "@turf/destination";
import { distance as measureDistance } from "@turf/distance";
import { point, Units } from "@turf/helpers";
import { feature, point, Units } from "@turf/helpers";
import { getGeom } from "@turf/invariant";
import { length } from "@turf/length";

/**
* Takes a {@link LineString} and returns a {@link Point} at a specified distance along the line.
*
* If {@link distance} is longer than the length of {@link line}, the last coordinate of the line is returned
* If {@link distance} is negative, it will count distance along the line from end to start of the line. And if
* a negative distance overshoots the length of the line, it will return the first coordinate of the line.
*
* @name along
* @param {Feature<LineString>} line input line
* @param {number} distance distance along the line
Expand All @@ -28,6 +33,10 @@ function along(
distance: number,
options: { units?: Units } = {}
): Feature<Point> {
if (distance < 0) {
const lineFeature = line.type === "Feature" ? line : feature(line);
distance = Math.max(0, length(lineFeature, options) + distance);
}
// Get Coords
const geom = getGeom(line);
const coords = geom.coordinates;
Expand Down
1 change: 1 addition & 0 deletions packages/turf-along/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@turf/distance": "workspace:^",
"@turf/helpers": "workspace:^",
"@turf/invariant": "workspace:^",
"@turf/length": "workspace:^",
"tslib": "^2.6.2"
}
}
17 changes: 15 additions & 2 deletions packages/turf-along/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ test("turf-along", (t) => {
const pt6 = along(line.geometry, 2, options);
const pt7 = along(line, 100, options);
const pt8 = along(line.geometry, 0, options);
const fc = featureCollection([pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8]);
const pt9 = along(line, -1, options);
const pt10 = along(line.geometry, -1, options);
const fc = featureCollection([
pt1,
pt2,
pt3,
pt4,
pt5,
pt6,
pt7,
pt8,
pt9,
pt10,
]);

fc.features.forEach((f) => {
t.ok(f);
t.equal(f.type, "Feature");
t.equal(f.geometry.type, "Point");
});
t.equal(fc.features.length, 8);
t.equal(fc.features.length, 10);
t.equal(fc.features[7].geometry.coordinates[0], pt8.geometry.coordinates[0]);
t.equal(fc.features[7].geometry.coordinates[1], pt8.geometry.coordinates[1]);

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

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