@turf/nearest-point-on-line 6.5.0 → 7.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/dist/es/index.js +31 -32
- package/dist/js/index.d.ts +8 -10
- package/dist/js/index.js +38 -41
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -6,14 +6,15 @@
|
|
|
6
6
|
|
|
7
7
|
Takes a [Point][1] and a [LineString][2] and calculates the closest Point on the (Multi)LineString.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### Parameters
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- `options.units` **[string][11]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
|
|
11
|
+
* `lines` **([Geometry][3] | [Feature][4]<([LineString][5] | [MultiLineString][6])>)** lines to snap to
|
|
12
|
+
* `pt` **([Geometry][3] | [Feature][4]<[Point][7]> | [Array][8]<[number][9]>)** point to snap from
|
|
13
|
+
* `options` **[Object][10]** Optional parameters (optional, default `{}`)
|
|
15
14
|
|
|
16
|
-
**
|
|
15
|
+
* `options.units` **[string][11]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
|
|
16
|
+
|
|
17
|
+
### Examples
|
|
17
18
|
|
|
18
19
|
```javascript
|
|
19
20
|
var line = turf.lineString([
|
|
@@ -33,7 +34,7 @@ var addToMap = [line, pt, snapped];
|
|
|
33
34
|
snapped.properties['marker-color'] = '#00f';
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
Returns **[Feature][4]
|
|
37
|
+
Returns **[Feature][4]<[Point][7]>** closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
|
|
37
38
|
|
|
38
39
|
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
39
40
|
|
package/dist/es/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import distance from "@turf/distance";
|
|
|
3
3
|
import destination from "@turf/destination";
|
|
4
4
|
import lineIntersects from "@turf/line-intersect";
|
|
5
5
|
import { flattenEach } from "@turf/meta";
|
|
6
|
-
import { point, lineString
|
|
6
|
+
import { point, lineString } from "@turf/helpers";
|
|
7
7
|
import { getCoords } from "@turf/invariant";
|
|
8
8
|
/**
|
|
9
9
|
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
|
|
@@ -31,53 +31,52 @@ import { getCoords } from "@turf/invariant";
|
|
|
31
31
|
* var addToMap = [line, pt, snapped];
|
|
32
32
|
* snapped.properties['marker-color'] = '#00f';
|
|
33
33
|
*/
|
|
34
|
-
function nearestPointOnLine(lines, pt, options) {
|
|
35
|
-
if (
|
|
36
|
-
|
|
34
|
+
function nearestPointOnLine(lines, pt, options = {}) {
|
|
35
|
+
if (!lines || !pt) {
|
|
36
|
+
throw new Error("lines and pt are required arguments");
|
|
37
|
+
}
|
|
38
|
+
let closestPt = point([Infinity, Infinity], {
|
|
37
39
|
dist: Infinity,
|
|
40
|
+
index: -1,
|
|
41
|
+
location: -1,
|
|
38
42
|
});
|
|
39
|
-
|
|
43
|
+
let length = 0.0;
|
|
40
44
|
flattenEach(lines, function (line) {
|
|
41
|
-
|
|
42
|
-
for (
|
|
45
|
+
const coords = getCoords(line);
|
|
46
|
+
for (let i = 0; i < coords.length - 1; i++) {
|
|
43
47
|
//start
|
|
44
|
-
|
|
48
|
+
const start = point(coords[i]);
|
|
45
49
|
start.properties.dist = distance(pt, start, options);
|
|
46
50
|
//stop
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
const stop = point(coords[i + 1]);
|
|
52
|
+
stop.properties.dist = distance(pt, stop, options);
|
|
49
53
|
// sectionLength
|
|
50
|
-
|
|
54
|
+
const sectionLength = distance(start, stop, options);
|
|
51
55
|
//perpendicular
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
const heightDistance = Math.max(start.properties.dist, stop.properties.dist);
|
|
57
|
+
const direction = bearing(start, stop);
|
|
58
|
+
const perpendicularPt1 = destination(pt, heightDistance, direction + 90, options);
|
|
59
|
+
const perpendicularPt2 = destination(pt, heightDistance, direction - 90, options);
|
|
60
|
+
const intersect = lineIntersects(lineString([
|
|
57
61
|
perpendicularPt1.geometry.coordinates,
|
|
58
62
|
perpendicularPt2.geometry.coordinates,
|
|
59
|
-
]), lineString([start.geometry.coordinates,
|
|
60
|
-
|
|
61
|
-
if (intersect.features.length > 0) {
|
|
62
|
-
intersectPt = intersect.features[0]
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
]), lineString([start.geometry.coordinates, stop.geometry.coordinates]));
|
|
64
|
+
let intersectPt;
|
|
65
|
+
if (intersect.features.length > 0 && intersect.features[0]) {
|
|
66
|
+
intersectPt = Object.assign(Object.assign({}, intersect.features[0]), { properties: {
|
|
67
|
+
dist: distance(pt, intersect.features[0], options),
|
|
68
|
+
location: length + distance(start, intersect.features[0], options),
|
|
69
|
+
} });
|
|
66
70
|
}
|
|
67
71
|
if (start.properties.dist < closestPt.properties.dist) {
|
|
68
|
-
closestPt = start;
|
|
69
|
-
closestPt.properties.index = i;
|
|
70
|
-
closestPt.properties.location = length;
|
|
72
|
+
closestPt = Object.assign(Object.assign({}, start), { properties: Object.assign(Object.assign({}, start.properties), { index: i, location: length }) });
|
|
71
73
|
}
|
|
72
|
-
if (
|
|
73
|
-
closestPt =
|
|
74
|
-
closestPt.properties.index = i + 1;
|
|
75
|
-
closestPt.properties.location = length + sectionLength;
|
|
74
|
+
if (stop.properties.dist < closestPt.properties.dist) {
|
|
75
|
+
closestPt = Object.assign(Object.assign({}, stop), { properties: Object.assign(Object.assign({}, stop.properties), { index: i + 1, location: length + sectionLength }) });
|
|
76
76
|
}
|
|
77
77
|
if (intersectPt &&
|
|
78
78
|
intersectPt.properties.dist < closestPt.properties.dist) {
|
|
79
|
-
closestPt = intersectPt;
|
|
80
|
-
closestPt.properties.index = i;
|
|
79
|
+
closestPt = Object.assign(Object.assign({}, intersectPt), { properties: Object.assign(Object.assign({}, intersectPt.properties), { index: i }) });
|
|
81
80
|
}
|
|
82
81
|
// update length
|
|
83
82
|
length += sectionLength;
|
package/dist/js/index.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import { Feature, Point, LineString, MultiLineString
|
|
2
|
-
|
|
3
|
-
properties: {
|
|
4
|
-
index?: number;
|
|
5
|
-
dist?: number;
|
|
6
|
-
location?: number;
|
|
7
|
-
[key: string]: any;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
1
|
+
import { Feature, Point, LineString, MultiLineString } from "geojson";
|
|
2
|
+
import { Coord, Units } from "@turf/helpers";
|
|
10
3
|
/**
|
|
11
4
|
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
|
|
12
5
|
*
|
|
@@ -35,5 +28,10 @@ export interface NearestPointOnLine extends Feature<Point> {
|
|
|
35
28
|
*/
|
|
36
29
|
declare function nearestPointOnLine<G extends LineString | MultiLineString>(lines: Feature<G> | G, pt: Coord, options?: {
|
|
37
30
|
units?: Units;
|
|
38
|
-
}):
|
|
31
|
+
}): Feature<Point, {
|
|
32
|
+
dist: number;
|
|
33
|
+
index: number;
|
|
34
|
+
location: number;
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}>;
|
|
39
37
|
export default nearestPointOnLine;
|
package/dist/js/index.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const bearing_1 = tslib_1.__importDefault(require("@turf/bearing"));
|
|
5
|
+
const distance_1 = tslib_1.__importDefault(require("@turf/distance"));
|
|
6
|
+
const destination_1 = tslib_1.__importDefault(require("@turf/destination"));
|
|
7
|
+
const line_intersect_1 = tslib_1.__importDefault(require("@turf/line-intersect"));
|
|
8
|
+
const meta_1 = require("@turf/meta");
|
|
9
|
+
const helpers_1 = require("@turf/helpers");
|
|
10
|
+
const invariant_1 = require("@turf/invariant");
|
|
13
11
|
/**
|
|
14
12
|
* Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
|
|
15
13
|
*
|
|
@@ -36,53 +34,52 @@ var invariant_1 = require("@turf/invariant");
|
|
|
36
34
|
* var addToMap = [line, pt, snapped];
|
|
37
35
|
* snapped.properties['marker-color'] = '#00f';
|
|
38
36
|
*/
|
|
39
|
-
function nearestPointOnLine(lines, pt, options) {
|
|
40
|
-
if (
|
|
41
|
-
|
|
37
|
+
function nearestPointOnLine(lines, pt, options = {}) {
|
|
38
|
+
if (!lines || !pt) {
|
|
39
|
+
throw new Error("lines and pt are required arguments");
|
|
40
|
+
}
|
|
41
|
+
let closestPt = helpers_1.point([Infinity, Infinity], {
|
|
42
42
|
dist: Infinity,
|
|
43
|
+
index: -1,
|
|
44
|
+
location: -1,
|
|
43
45
|
});
|
|
44
|
-
|
|
46
|
+
let length = 0.0;
|
|
45
47
|
meta_1.flattenEach(lines, function (line) {
|
|
46
|
-
|
|
47
|
-
for (
|
|
48
|
+
const coords = invariant_1.getCoords(line);
|
|
49
|
+
for (let i = 0; i < coords.length - 1; i++) {
|
|
48
50
|
//start
|
|
49
|
-
|
|
51
|
+
const start = helpers_1.point(coords[i]);
|
|
50
52
|
start.properties.dist = distance_1.default(pt, start, options);
|
|
51
53
|
//stop
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
const stop = helpers_1.point(coords[i + 1]);
|
|
55
|
+
stop.properties.dist = distance_1.default(pt, stop, options);
|
|
54
56
|
// sectionLength
|
|
55
|
-
|
|
57
|
+
const sectionLength = distance_1.default(start, stop, options);
|
|
56
58
|
//perpendicular
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
const heightDistance = Math.max(start.properties.dist, stop.properties.dist);
|
|
60
|
+
const direction = bearing_1.default(start, stop);
|
|
61
|
+
const perpendicularPt1 = destination_1.default(pt, heightDistance, direction + 90, options);
|
|
62
|
+
const perpendicularPt2 = destination_1.default(pt, heightDistance, direction - 90, options);
|
|
63
|
+
const intersect = line_intersect_1.default(helpers_1.lineString([
|
|
62
64
|
perpendicularPt1.geometry.coordinates,
|
|
63
65
|
perpendicularPt2.geometry.coordinates,
|
|
64
|
-
]), helpers_1.lineString([start.geometry.coordinates,
|
|
65
|
-
|
|
66
|
-
if (intersect.features.length > 0) {
|
|
67
|
-
intersectPt = intersect.features[0]
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
]), helpers_1.lineString([start.geometry.coordinates, stop.geometry.coordinates]));
|
|
67
|
+
let intersectPt;
|
|
68
|
+
if (intersect.features.length > 0 && intersect.features[0]) {
|
|
69
|
+
intersectPt = Object.assign(Object.assign({}, intersect.features[0]), { properties: {
|
|
70
|
+
dist: distance_1.default(pt, intersect.features[0], options),
|
|
71
|
+
location: length + distance_1.default(start, intersect.features[0], options),
|
|
72
|
+
} });
|
|
71
73
|
}
|
|
72
74
|
if (start.properties.dist < closestPt.properties.dist) {
|
|
73
|
-
closestPt = start;
|
|
74
|
-
closestPt.properties.index = i;
|
|
75
|
-
closestPt.properties.location = length;
|
|
75
|
+
closestPt = Object.assign(Object.assign({}, start), { properties: Object.assign(Object.assign({}, start.properties), { index: i, location: length }) });
|
|
76
76
|
}
|
|
77
|
-
if (
|
|
78
|
-
closestPt =
|
|
79
|
-
closestPt.properties.index = i + 1;
|
|
80
|
-
closestPt.properties.location = length + sectionLength;
|
|
77
|
+
if (stop.properties.dist < closestPt.properties.dist) {
|
|
78
|
+
closestPt = Object.assign(Object.assign({}, stop), { properties: Object.assign(Object.assign({}, stop.properties), { index: i + 1, location: length + sectionLength }) });
|
|
81
79
|
}
|
|
82
80
|
if (intersectPt &&
|
|
83
81
|
intersectPt.properties.dist < closestPt.properties.dist) {
|
|
84
|
-
closestPt = intersectPt;
|
|
85
|
-
closestPt.properties.index = i;
|
|
82
|
+
closestPt = Object.assign(Object.assign({}, intersectPt), { properties: Object.assign(Object.assign({}, intersectPt.properties), { index: i }) });
|
|
86
83
|
}
|
|
87
84
|
// update length
|
|
88
85
|
length += sectionLength;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/nearest-point-on-line",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-alpha.0",
|
|
4
4
|
"description": "turf nearest-point-on-line module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"docs": "node ../../scripts/generate-readmes",
|
|
39
39
|
"test": "npm-run-all test:*",
|
|
40
40
|
"test:tape": "ts-node -r esm test.js",
|
|
41
|
-
"test:types": "tsc --esModuleInterop --noEmit types.ts"
|
|
41
|
+
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@turf/along": "^
|
|
45
|
-
"@turf/length": "^
|
|
46
|
-
"@turf/truncate": "^
|
|
44
|
+
"@turf/along": "^7.0.0-alpha.0",
|
|
45
|
+
"@turf/length": "^7.0.0-alpha.0",
|
|
46
|
+
"@turf/truncate": "^7.0.0-alpha.0",
|
|
47
47
|
"@types/tape": "*",
|
|
48
48
|
"benchmark": "*",
|
|
49
49
|
"load-json-file": "*",
|
|
@@ -55,13 +55,14 @@
|
|
|
55
55
|
"write-json-file": "*"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@turf/bearing": "^
|
|
59
|
-
"@turf/destination": "^
|
|
60
|
-
"@turf/distance": "^
|
|
61
|
-
"@turf/helpers": "^
|
|
62
|
-
"@turf/invariant": "^
|
|
63
|
-
"@turf/line-intersect": "^
|
|
64
|
-
"@turf/meta": "^
|
|
58
|
+
"@turf/bearing": "^7.0.0-alpha.0",
|
|
59
|
+
"@turf/destination": "^7.0.0-alpha.0",
|
|
60
|
+
"@turf/distance": "^7.0.0-alpha.0",
|
|
61
|
+
"@turf/helpers": "^7.0.0-alpha.0",
|
|
62
|
+
"@turf/invariant": "^7.0.0-alpha.0",
|
|
63
|
+
"@turf/line-intersect": "^7.0.0-alpha.0",
|
|
64
|
+
"@turf/meta": "^7.0.0-alpha.0",
|
|
65
|
+
"tslib": "^2.3.0"
|
|
65
66
|
},
|
|
66
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
|
|
67
68
|
}
|