@turf/nearest-point-on-line 7.0.0 → 7.1.0-alpha.70
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 +10 -14
- package/dist/cjs/index.cjs +70 -62
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +70 -62
- package/dist/esm/index.js.map +1 -1
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -8,11 +8,11 @@ Takes a [Point][1] and a [LineString][2] and calculates the closest Point on the
|
|
|
8
8
|
|
|
9
9
|
### Parameters
|
|
10
10
|
|
|
11
|
-
* `lines` **([Geometry][3] | [Feature][4]<([LineString][
|
|
12
|
-
* `pt` **([Geometry][3] | [Feature][4]<[Point][
|
|
13
|
-
* `options` **[Object][
|
|
11
|
+
* `lines` **([Geometry][3] | [Feature][4]<([LineString][2] | [MultiLineString][5])>)** lines to snap to
|
|
12
|
+
* `pt` **([Geometry][3] | [Feature][4]<[Point][1]> | [Array][6]<[number][7]>)** point to snap from
|
|
13
|
+
* `options` **[Object][8]** Optional parameters (optional, default `{}`)
|
|
14
14
|
|
|
15
|
-
* `options.units` **[string][
|
|
15
|
+
* `options.units` **[string][9]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
|
|
16
16
|
|
|
17
17
|
### Examples
|
|
18
18
|
|
|
@@ -34,7 +34,7 @@ var addToMap = [line, pt, snapped];
|
|
|
34
34
|
snapped.properties['marker-color'] = '#00f';
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
Returns **[Feature][4]<[Point][
|
|
37
|
+
Returns **[Feature][4]<[Point][1]>** closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
|
|
38
38
|
|
|
39
39
|
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
40
40
|
|
|
@@ -44,19 +44,15 @@ Returns **[Feature][4]<[Point][7]>** closest point on the `line` to `point`. The
|
|
|
44
44
|
|
|
45
45
|
[4]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
46
46
|
|
|
47
|
-
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.
|
|
47
|
+
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.5
|
|
48
48
|
|
|
49
|
-
[6]: https://
|
|
49
|
+
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
50
50
|
|
|
51
|
-
[7]: https://
|
|
51
|
+
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
52
52
|
|
|
53
|
-
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/
|
|
53
|
+
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
54
54
|
|
|
55
|
-
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/
|
|
56
|
-
|
|
57
|
-
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
58
|
-
|
|
59
|
-
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
55
|
+
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
60
56
|
|
|
61
57
|
<!-- 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. -->
|
|
62
58
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -17,7 +17,6 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
21
20
|
|
|
22
21
|
// index.ts
|
|
23
22
|
var _bearing = require('@turf/bearing');
|
|
@@ -34,74 +33,83 @@ function nearestPointOnLine(lines, pt, options = {}) {
|
|
|
34
33
|
let closestPt = _helpers.point.call(void 0, [Infinity, Infinity], {
|
|
35
34
|
dist: Infinity,
|
|
36
35
|
index: -1,
|
|
36
|
+
multiFeatureIndex: -1,
|
|
37
37
|
location: -1
|
|
38
38
|
});
|
|
39
39
|
let length = 0;
|
|
40
|
-
_meta.flattenEach.call(void 0,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
intersectPt
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
40
|
+
_meta.flattenEach.call(void 0,
|
|
41
|
+
lines,
|
|
42
|
+
function(line, _featureIndex, multiFeatureIndex) {
|
|
43
|
+
const coords = _invariant.getCoords.call(void 0, line);
|
|
44
|
+
for (let i = 0; i < coords.length - 1; i++) {
|
|
45
|
+
const start = _helpers.point.call(void 0, coords[i]);
|
|
46
|
+
start.properties.dist = _distance.distance.call(void 0, pt, start, options);
|
|
47
|
+
const stop = _helpers.point.call(void 0, coords[i + 1]);
|
|
48
|
+
stop.properties.dist = _distance.distance.call(void 0, pt, stop, options);
|
|
49
|
+
const sectionLength = _distance.distance.call(void 0, start, stop, options);
|
|
50
|
+
const heightDistance = Math.max(
|
|
51
|
+
start.properties.dist,
|
|
52
|
+
stop.properties.dist
|
|
53
|
+
);
|
|
54
|
+
const direction = _bearing.bearing.call(void 0, start, stop);
|
|
55
|
+
const perpendicularPt1 = _destination.destination.call(void 0,
|
|
56
|
+
pt,
|
|
57
|
+
heightDistance,
|
|
58
|
+
direction + 90,
|
|
59
|
+
options
|
|
60
|
+
);
|
|
61
|
+
const perpendicularPt2 = _destination.destination.call(void 0,
|
|
62
|
+
pt,
|
|
63
|
+
heightDistance,
|
|
64
|
+
direction - 90,
|
|
65
|
+
options
|
|
66
|
+
);
|
|
67
|
+
const intersect = _lineintersect.lineIntersect.call(void 0,
|
|
68
|
+
_helpers.lineString.call(void 0, [
|
|
69
|
+
perpendicularPt1.geometry.coordinates,
|
|
70
|
+
perpendicularPt2.geometry.coordinates
|
|
71
|
+
]),
|
|
72
|
+
_helpers.lineString.call(void 0, [start.geometry.coordinates, stop.geometry.coordinates])
|
|
73
|
+
);
|
|
74
|
+
let intersectPt;
|
|
75
|
+
if (intersect.features.length > 0 && intersect.features[0]) {
|
|
76
|
+
intersectPt = __spreadProps(__spreadValues({}, intersect.features[0]), {
|
|
77
|
+
properties: {
|
|
78
|
+
dist: _distance.distance.call(void 0, pt, intersect.features[0], options),
|
|
79
|
+
multiFeatureIndex,
|
|
80
|
+
location: length + _distance.distance.call(void 0, start, intersect.features[0], options)
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (start.properties.dist < closestPt.properties.dist) {
|
|
85
|
+
closestPt = __spreadProps(__spreadValues({}, start), {
|
|
86
|
+
properties: __spreadProps(__spreadValues({}, start.properties), {
|
|
87
|
+
index: i,
|
|
88
|
+
multiFeatureIndex,
|
|
89
|
+
location: length
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (stop.properties.dist < closestPt.properties.dist) {
|
|
94
|
+
closestPt = __spreadProps(__spreadValues({}, stop), {
|
|
95
|
+
properties: __spreadProps(__spreadValues({}, stop.properties), {
|
|
96
|
+
index: i + 1,
|
|
97
|
+
multiFeatureIndex,
|
|
98
|
+
location: length + sectionLength
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (intersectPt && intersectPt.properties.dist < closestPt.properties.dist) {
|
|
103
|
+
closestPt = __spreadProps(__spreadValues({}, intersectPt), {
|
|
104
|
+
properties: __spreadProps(__spreadValues({}, intersectPt.properties), { index: i })
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
length += sectionLength;
|
|
80
108
|
}
|
|
81
|
-
if (start.properties.dist < closestPt.properties.dist) {
|
|
82
|
-
closestPt = __spreadProps(__spreadValues({}, start), {
|
|
83
|
-
properties: __spreadProps(__spreadValues({}, start.properties), { index: i, location: length })
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
if (stop.properties.dist < closestPt.properties.dist) {
|
|
87
|
-
closestPt = __spreadProps(__spreadValues({}, stop), {
|
|
88
|
-
properties: __spreadProps(__spreadValues({}, stop.properties), {
|
|
89
|
-
index: i + 1,
|
|
90
|
-
location: length + sectionLength
|
|
91
|
-
})
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
if (intersectPt && intersectPt.properties.dist < closestPt.properties.dist) {
|
|
95
|
-
closestPt = __spreadProps(__spreadValues({}, intersectPt), {
|
|
96
|
-
properties: __spreadProps(__spreadValues({}, intersectPt.properties), { index: i })
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
length += sectionLength;
|
|
100
109
|
}
|
|
101
|
-
|
|
110
|
+
);
|
|
102
111
|
return closestPt;
|
|
103
112
|
}
|
|
104
|
-
__name(nearestPointOnLine, "nearestPointOnLine");
|
|
105
113
|
var turf_nearest_point_on_line_default = nearestPointOnLine;
|
|
106
114
|
|
|
107
115
|
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;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,CAAC;AAAA,UACD,WAAW,CAAC,MAAM,SAAS,aAAa,KAAK,SAAS,WAAW,CAAC;AAAA,QACpE;AACA,YAAI;AAOJ,YAAI,UAAU,SAAS,SAAS,KAAK,UAAU,SAAS,CAAC,GAAG;AAC1D,wBAAc,iCACT,UAAU,SAAS,CAAC,IADX;AAAA,YAEZ,YAAY;AAAA,cACV,MAAM,SAAS,IAAI,UAAU,SAAS,CAAC,GAAG,OAAO;AAAA,cACjD;AAAA,cACA,UACE,SAAS,SAAS,OAAO,UAAU,SAAS,CAAC,GAAG,OAAO;AAAA,YAC3D;AAAA,UACF;AAAA,QACF;AAEA,YAAI,MAAM,WAAW,OAAO,UAAU,WAAW,MAAM;AACrD,sBAAY,iCACP,QADO;AAAA,YAEV,YAAY,iCACP,MAAM,aADC;AAAA,cAEV,OAAO;AAAA,cACP;AAAA,cACA,UAAU;AAAA,YACZ;AAAA,UACF;AAAA,QACF;AAEA,YAAI,KAAK,WAAW,OAAO,UAAU,WAAW,MAAM;AACpD,sBAAY,iCACP,OADO;AAAA,YAEV,YAAY,iCACP,KAAK,aADE;AAAA,cAEV,OAAO,IAAI;AAAA,cACX;AAAA,cACA,UAAU,SAAS;AAAA,YACrB;AAAA,UACF;AAAA,QACF;AAEA,YACE,eACA,YAAY,WAAW,OAAO,UAAU,WAAW,MACnD;AACA,sBAAY,iCACP,cADO;AAAA,YAEV,YAAY,iCAAK,YAAY,aAAjB,EAA6B,OAAO,EAAE;AAAA,UACpD;AAAA,QACF;AAEA,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAGA,IAAO,qCAAQ","sourcesContent":["import { Feature, Point, LineString, MultiLineString } from \"geojson\";\nimport { bearing } from \"@turf/bearing\";\nimport { distance } from \"@turf/distance\";\nimport { destination } from \"@turf/destination\";\nimport { lineIntersect as lineIntersects } from \"@turf/line-intersect\";\nimport { flattenEach } from \"@turf/meta\";\nimport { point, lineString, Coord, Units } from \"@turf/helpers\";\nimport { getCoords } from \"@turf/invariant\";\n\n/**\n * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.\n *\n * @name nearestPointOnLine\n * @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to\n * @param {Geometry|Feature<Point>|number[]} pt point to snap from\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers\n * @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.\n * @example\n * var line = turf.lineString([\n * [-77.031669, 38.878605],\n * [-77.029609, 38.881946],\n * [-77.020339, 38.884084],\n * [-77.025661, 38.885821],\n * [-77.021884, 38.889563],\n * [-77.019824, 38.892368]\n * ]);\n * var pt = turf.point([-77.037076, 38.884017]);\n *\n * var snapped = turf.nearestPointOnLine(line, pt, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [line, pt, snapped];\n * snapped.properties['marker-color'] = '#00f';\n */\nfunction nearestPointOnLine<G extends LineString | MultiLineString>(\n lines: Feature<G> | G,\n pt: Coord,\n options: { units?: Units } = {}\n): Feature<\n Point,\n {\n dist: number;\n index: number;\n multiFeatureIndex: number;\n location: number;\n [key: string]: any;\n }\n> {\n if (!lines || !pt) {\n throw new Error(\"lines and pt are required arguments\");\n }\n\n let closestPt: Feature<\n Point,\n { dist: number; index: number; multiFeatureIndex: number; location: number }\n > = point([Infinity, Infinity], {\n dist: Infinity,\n index: -1,\n multiFeatureIndex: -1,\n location: -1,\n });\n\n let length = 0.0;\n flattenEach(\n lines,\n function (line: any, _featureIndex: number, multiFeatureIndex: number) {\n const coords: any = getCoords(line);\n\n for (let i = 0; i < coords.length - 1; i++) {\n //start\n const start: Feature<Point, { dist: number }> = point(coords[i]);\n start.properties.dist = distance(pt, start, options);\n //stop\n const stop: Feature<Point, { dist: number }> = point(coords[i + 1]);\n stop.properties.dist = distance(pt, stop, options);\n // sectionLength\n const sectionLength = distance(start, stop, options);\n //perpendicular\n const heightDistance = Math.max(\n start.properties.dist,\n stop.properties.dist\n );\n const direction = bearing(start, stop);\n const perpendicularPt1 = destination(\n pt,\n heightDistance,\n direction + 90,\n options\n );\n const perpendicularPt2 = destination(\n pt,\n heightDistance,\n direction - 90,\n options\n );\n const intersect = lineIntersects(\n lineString([\n perpendicularPt1.geometry.coordinates,\n perpendicularPt2.geometry.coordinates,\n ]),\n lineString([start.geometry.coordinates, stop.geometry.coordinates])\n );\n let intersectPt:\n | Feature<\n Point,\n { dist: number; multiFeatureIndex: number; location: number }\n >\n | undefined;\n\n if (intersect.features.length > 0 && intersect.features[0]) {\n intersectPt = {\n ...intersect.features[0],\n properties: {\n dist: distance(pt, intersect.features[0], options),\n multiFeatureIndex: multiFeatureIndex,\n location:\n length + distance(start, intersect.features[0], options),\n },\n };\n }\n\n if (start.properties.dist < closestPt.properties.dist) {\n closestPt = {\n ...start,\n properties: {\n ...start.properties,\n index: i,\n multiFeatureIndex: multiFeatureIndex,\n location: length,\n },\n };\n }\n\n if (stop.properties.dist < closestPt.properties.dist) {\n closestPt = {\n ...stop,\n properties: {\n ...stop.properties,\n index: i + 1,\n multiFeatureIndex: multiFeatureIndex,\n location: length + sectionLength,\n },\n };\n }\n\n if (\n intersectPt &&\n intersectPt.properties.dist < closestPt.properties.dist\n ) {\n closestPt = {\n ...intersectPt,\n properties: { ...intersectPt.properties, index: i },\n };\n }\n // update length\n length += sectionLength;\n }\n }\n );\n\n return closestPt;\n}\n\nexport { nearestPointOnLine };\nexport default nearestPointOnLine;\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -9,7 +9,7 @@ import { Coord, Units } from '@turf/helpers';
|
|
|
9
9
|
* @param {Geometry|Feature<Point>|number[]} pt point to snap from
|
|
10
10
|
* @param {Object} [options={}] Optional parameters
|
|
11
11
|
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
|
|
12
|
-
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain
|
|
12
|
+
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
|
|
13
13
|
* @example
|
|
14
14
|
* var line = turf.lineString([
|
|
15
15
|
* [-77.031669, 38.878605],
|
|
@@ -32,6 +32,7 @@ declare function nearestPointOnLine<G extends LineString | MultiLineString>(line
|
|
|
32
32
|
}): Feature<Point, {
|
|
33
33
|
dist: number;
|
|
34
34
|
index: number;
|
|
35
|
+
multiFeatureIndex: number;
|
|
35
36
|
location: number;
|
|
36
37
|
[key: string]: any;
|
|
37
38
|
}>;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { Coord, Units } from '@turf/helpers';
|
|
|
9
9
|
* @param {Geometry|Feature<Point>|number[]} pt point to snap from
|
|
10
10
|
* @param {Object} [options={}] Optional parameters
|
|
11
11
|
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
|
|
12
|
-
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain
|
|
12
|
+
* @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
|
|
13
13
|
* @example
|
|
14
14
|
* var line = turf.lineString([
|
|
15
15
|
* [-77.031669, 38.878605],
|
|
@@ -32,6 +32,7 @@ declare function nearestPointOnLine<G extends LineString | MultiLineString>(line
|
|
|
32
32
|
}): Feature<Point, {
|
|
33
33
|
dist: number;
|
|
34
34
|
index: number;
|
|
35
|
+
multiFeatureIndex: number;
|
|
35
36
|
location: number;
|
|
36
37
|
[key: string]: any;
|
|
37
38
|
}>;
|
package/dist/esm/index.js
CHANGED
|
@@ -17,7 +17,6 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
21
20
|
|
|
22
21
|
// index.ts
|
|
23
22
|
import { bearing } from "@turf/bearing";
|
|
@@ -34,74 +33,83 @@ function nearestPointOnLine(lines, pt, options = {}) {
|
|
|
34
33
|
let closestPt = point([Infinity, Infinity], {
|
|
35
34
|
dist: Infinity,
|
|
36
35
|
index: -1,
|
|
36
|
+
multiFeatureIndex: -1,
|
|
37
37
|
location: -1
|
|
38
38
|
});
|
|
39
39
|
let length = 0;
|
|
40
|
-
flattenEach(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
start
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
intersectPt
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
40
|
+
flattenEach(
|
|
41
|
+
lines,
|
|
42
|
+
function(line, _featureIndex, multiFeatureIndex) {
|
|
43
|
+
const coords = getCoords(line);
|
|
44
|
+
for (let i = 0; i < coords.length - 1; i++) {
|
|
45
|
+
const start = point(coords[i]);
|
|
46
|
+
start.properties.dist = distance(pt, start, options);
|
|
47
|
+
const stop = point(coords[i + 1]);
|
|
48
|
+
stop.properties.dist = distance(pt, stop, options);
|
|
49
|
+
const sectionLength = distance(start, stop, options);
|
|
50
|
+
const heightDistance = Math.max(
|
|
51
|
+
start.properties.dist,
|
|
52
|
+
stop.properties.dist
|
|
53
|
+
);
|
|
54
|
+
const direction = bearing(start, stop);
|
|
55
|
+
const perpendicularPt1 = destination(
|
|
56
|
+
pt,
|
|
57
|
+
heightDistance,
|
|
58
|
+
direction + 90,
|
|
59
|
+
options
|
|
60
|
+
);
|
|
61
|
+
const perpendicularPt2 = destination(
|
|
62
|
+
pt,
|
|
63
|
+
heightDistance,
|
|
64
|
+
direction - 90,
|
|
65
|
+
options
|
|
66
|
+
);
|
|
67
|
+
const intersect = lineIntersects(
|
|
68
|
+
lineString([
|
|
69
|
+
perpendicularPt1.geometry.coordinates,
|
|
70
|
+
perpendicularPt2.geometry.coordinates
|
|
71
|
+
]),
|
|
72
|
+
lineString([start.geometry.coordinates, stop.geometry.coordinates])
|
|
73
|
+
);
|
|
74
|
+
let intersectPt;
|
|
75
|
+
if (intersect.features.length > 0 && intersect.features[0]) {
|
|
76
|
+
intersectPt = __spreadProps(__spreadValues({}, intersect.features[0]), {
|
|
77
|
+
properties: {
|
|
78
|
+
dist: distance(pt, intersect.features[0], options),
|
|
79
|
+
multiFeatureIndex,
|
|
80
|
+
location: length + distance(start, intersect.features[0], options)
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (start.properties.dist < closestPt.properties.dist) {
|
|
85
|
+
closestPt = __spreadProps(__spreadValues({}, start), {
|
|
86
|
+
properties: __spreadProps(__spreadValues({}, start.properties), {
|
|
87
|
+
index: i,
|
|
88
|
+
multiFeatureIndex,
|
|
89
|
+
location: length
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (stop.properties.dist < closestPt.properties.dist) {
|
|
94
|
+
closestPt = __spreadProps(__spreadValues({}, stop), {
|
|
95
|
+
properties: __spreadProps(__spreadValues({}, stop.properties), {
|
|
96
|
+
index: i + 1,
|
|
97
|
+
multiFeatureIndex,
|
|
98
|
+
location: length + sectionLength
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (intersectPt && intersectPt.properties.dist < closestPt.properties.dist) {
|
|
103
|
+
closestPt = __spreadProps(__spreadValues({}, intersectPt), {
|
|
104
|
+
properties: __spreadProps(__spreadValues({}, intersectPt.properties), { index: i })
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
length += sectionLength;
|
|
80
108
|
}
|
|
81
|
-
if (start.properties.dist < closestPt.properties.dist) {
|
|
82
|
-
closestPt = __spreadProps(__spreadValues({}, start), {
|
|
83
|
-
properties: __spreadProps(__spreadValues({}, start.properties), { index: i, location: length })
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
if (stop.properties.dist < closestPt.properties.dist) {
|
|
87
|
-
closestPt = __spreadProps(__spreadValues({}, stop), {
|
|
88
|
-
properties: __spreadProps(__spreadValues({}, stop.properties), {
|
|
89
|
-
index: i + 1,
|
|
90
|
-
location: length + sectionLength
|
|
91
|
-
})
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
if (intersectPt && intersectPt.properties.dist < closestPt.properties.dist) {
|
|
95
|
-
closestPt = __spreadProps(__spreadValues({}, intersectPt), {
|
|
96
|
-
properties: __spreadProps(__spreadValues({}, intersectPt.properties), { index: i })
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
length += sectionLength;
|
|
100
109
|
}
|
|
101
|
-
|
|
110
|
+
);
|
|
102
111
|
return closestPt;
|
|
103
112
|
}
|
|
104
|
-
__name(nearestPointOnLine, "nearestPointOnLine");
|
|
105
113
|
var turf_nearest_point_on_line_default = nearestPointOnLine;
|
|
106
114
|
export {
|
|
107
115
|
turf_nearest_point_on_line_default as default,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, Point, LineString, MultiLineString } from \"geojson\";\nimport { bearing } from \"@turf/bearing\";\nimport { distance } from \"@turf/distance\";\nimport { destination } from \"@turf/destination\";\nimport { lineIntersect as lineIntersects } from \"@turf/line-intersect\";\nimport { flattenEach } from \"@turf/meta\";\nimport { point, lineString, Coord, Units } from \"@turf/helpers\";\nimport { getCoords } from \"@turf/invariant\";\n\n/**\n * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.\n *\n * @name nearestPointOnLine\n * @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to\n * @param {Geometry|Feature<Point>|number[]} pt point to snap from\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers\n * @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, Point, LineString, MultiLineString } from \"geojson\";\nimport { bearing } from \"@turf/bearing\";\nimport { distance } from \"@turf/distance\";\nimport { destination } from \"@turf/destination\";\nimport { lineIntersect as lineIntersects } from \"@turf/line-intersect\";\nimport { flattenEach } from \"@turf/meta\";\nimport { point, lineString, Coord, Units } from \"@turf/helpers\";\nimport { getCoords } from \"@turf/invariant\";\n\n/**\n * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.\n *\n * @name nearestPointOnLine\n * @param {Geometry|Feature<LineString|MultiLineString>} lines lines to snap to\n * @param {Geometry|Feature<Point>|number[]} pt point to snap from\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers\n * @returns {Feature<Point>} closest point on the `line` to `point`. The properties object will contain four values: `index`: closest point was found on nth line part, `multiFeatureIndex`: closest point was found on the nth line of the `MultiLineString`, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.\n * @example\n * var line = turf.lineString([\n * [-77.031669, 38.878605],\n * [-77.029609, 38.881946],\n * [-77.020339, 38.884084],\n * [-77.025661, 38.885821],\n * [-77.021884, 38.889563],\n * [-77.019824, 38.892368]\n * ]);\n * var pt = turf.point([-77.037076, 38.884017]);\n *\n * var snapped = turf.nearestPointOnLine(line, pt, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [line, pt, snapped];\n * snapped.properties['marker-color'] = '#00f';\n */\nfunction nearestPointOnLine<G extends LineString | MultiLineString>(\n lines: Feature<G> | G,\n pt: Coord,\n options: { units?: Units } = {}\n): Feature<\n Point,\n {\n dist: number;\n index: number;\n multiFeatureIndex: number;\n location: number;\n [key: string]: any;\n }\n> {\n if (!lines || !pt) {\n throw new Error(\"lines and pt are required arguments\");\n }\n\n let closestPt: Feature<\n Point,\n { dist: number; index: number; multiFeatureIndex: number; location: number }\n > = point([Infinity, Infinity], {\n dist: Infinity,\n index: -1,\n multiFeatureIndex: -1,\n location: -1,\n });\n\n let length = 0.0;\n flattenEach(\n lines,\n function (line: any, _featureIndex: number, multiFeatureIndex: number) {\n const coords: any = getCoords(line);\n\n for (let i = 0; i < coords.length - 1; i++) {\n //start\n const start: Feature<Point, { dist: number }> = point(coords[i]);\n start.properties.dist = distance(pt, start, options);\n //stop\n const stop: Feature<Point, { dist: number }> = point(coords[i + 1]);\n stop.properties.dist = distance(pt, stop, options);\n // sectionLength\n const sectionLength = distance(start, stop, options);\n //perpendicular\n const heightDistance = Math.max(\n start.properties.dist,\n stop.properties.dist\n );\n const direction = bearing(start, stop);\n const perpendicularPt1 = destination(\n pt,\n heightDistance,\n direction + 90,\n options\n );\n const perpendicularPt2 = destination(\n pt,\n heightDistance,\n direction - 90,\n options\n );\n const intersect = lineIntersects(\n lineString([\n perpendicularPt1.geometry.coordinates,\n perpendicularPt2.geometry.coordinates,\n ]),\n lineString([start.geometry.coordinates, stop.geometry.coordinates])\n );\n let intersectPt:\n | Feature<\n Point,\n { dist: number; multiFeatureIndex: number; location: number }\n >\n | undefined;\n\n if (intersect.features.length > 0 && intersect.features[0]) {\n intersectPt = {\n ...intersect.features[0],\n properties: {\n dist: distance(pt, intersect.features[0], options),\n multiFeatureIndex: multiFeatureIndex,\n location:\n length + distance(start, intersect.features[0], options),\n },\n };\n }\n\n if (start.properties.dist < closestPt.properties.dist) {\n closestPt = {\n ...start,\n properties: {\n ...start.properties,\n index: i,\n multiFeatureIndex: multiFeatureIndex,\n location: length,\n },\n };\n }\n\n if (stop.properties.dist < closestPt.properties.dist) {\n closestPt = {\n ...stop,\n properties: {\n ...stop.properties,\n index: i + 1,\n multiFeatureIndex: multiFeatureIndex,\n location: length + sectionLength,\n },\n };\n }\n\n if (\n intersectPt &&\n intersectPt.properties.dist < closestPt.properties.dist\n ) {\n closestPt = {\n ...intersectPt,\n properties: { ...intersectPt.properties, index: i },\n };\n }\n // update length\n length += sectionLength;\n }\n }\n );\n\n return closestPt;\n}\n\nexport { nearestPointOnLine };\nexport default nearestPointOnLine;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB,sBAAsB;AAChD,SAAS,mBAAmB;AAC5B,SAAS,OAAO,kBAAgC;AAChD,SAAS,iBAAiB;AA4B1B,SAAS,mBACP,OACA,IACA,UAA6B,CAAC,GAU9B;AACA,MAAI,CAAC,SAAS,CAAC,IAAI;AACjB,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACvD;AAEA,MAAI,YAGA,MAAM,CAAC,UAAU,QAAQ,GAAG;AAAA,IAC9B,MAAM;AAAA,IACN,OAAO;AAAA,IACP,mBAAmB;AAAA,IACnB,UAAU;AAAA,EACZ,CAAC;AAED,MAAI,SAAS;AACb;AAAA,IACE;AAAA,IACA,SAAU,MAAW,eAAuB,mBAA2B;AACrE,YAAM,SAAc,UAAU,IAAI;AAElC,eAAS,IAAI,GAAG,IAAI,OAAO,SAAS,GAAG,KAAK;AAE1C,cAAM,QAA0C,MAAM,OAAO,CAAC,CAAC;AAC/D,cAAM,WAAW,OAAO,SAAS,IAAI,OAAO,OAAO;AAEnD,cAAM,OAAyC,MAAM,OAAO,I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U,SAAS;AAAA,YACrB;AAAA,UACF;AAAA,QACF;AAEA,YACE,eACA,YAAY,WAAW,OAAO,UAAU,WAAW,MACnD;AACA,sBAAY,iCACP,cADO;AAAA,YAEV,YAAY,iCAAK,YAAY,aAAjB,EAA6B,OAAO,EAAE;AAAA,UACpD;AAAA,QACF;AAEA,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAGA,IAAO,qCAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/nearest-point-on-line",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.1.0-alpha.70+948cdafaf",
|
|
4
4
|
"description": "turf nearest-point-on-line module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@turf/along": "^7.0.
|
|
50
|
-
"@turf/length": "^7.0.
|
|
51
|
-
"@turf/truncate": "^7.0.
|
|
49
|
+
"@turf/along": "^7.1.0-alpha.70+948cdafaf",
|
|
50
|
+
"@turf/length": "^7.1.0-alpha.70+948cdafaf",
|
|
51
|
+
"@turf/truncate": "^7.1.0-alpha.70+948cdafaf",
|
|
52
52
|
"@types/benchmark": "^2.1.5",
|
|
53
53
|
"@types/tape": "^4.2.32",
|
|
54
54
|
"benchmark": "^2.1.4",
|
|
@@ -61,14 +61,15 @@
|
|
|
61
61
|
"write-json-file": "^5.0.0"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@turf/bearing": "^7.0.
|
|
65
|
-
"@turf/destination": "^7.0.
|
|
66
|
-
"@turf/distance": "^7.0.
|
|
67
|
-
"@turf/helpers": "^7.0.
|
|
68
|
-
"@turf/invariant": "^7.0.
|
|
69
|
-
"@turf/line-intersect": "^7.0.
|
|
70
|
-
"@turf/meta": "^7.0.
|
|
64
|
+
"@turf/bearing": "^7.1.0-alpha.70+948cdafaf",
|
|
65
|
+
"@turf/destination": "^7.1.0-alpha.70+948cdafaf",
|
|
66
|
+
"@turf/distance": "^7.1.0-alpha.70+948cdafaf",
|
|
67
|
+
"@turf/helpers": "^7.1.0-alpha.70+948cdafaf",
|
|
68
|
+
"@turf/invariant": "^7.1.0-alpha.70+948cdafaf",
|
|
69
|
+
"@turf/line-intersect": "^7.1.0-alpha.70+948cdafaf",
|
|
70
|
+
"@turf/meta": "^7.1.0-alpha.70+948cdafaf",
|
|
71
|
+
"@types/geojson": "^7946.0.10",
|
|
71
72
|
"tslib": "^2.6.2"
|
|
72
73
|
},
|
|
73
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "948cdafaf70606d2e27fcc79973fa48ee1182067"
|
|
74
75
|
}
|