@turf/point-to-line-distance 7.1.0 → 7.3.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 +15 -14
- package/dist/cjs/index.cjs +23 -21
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +4 -5
- package/dist/esm/index.d.ts +4 -5
- package/dist/esm/index.js +24 -22
- package/dist/esm/index.js.map +1 -1
- package/package.json +20 -19
package/README.md
CHANGED
|
@@ -4,18 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
## pointToLineDistance
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
Calculates the distance between a given point and the nearest point on a
|
|
8
|
+
line. Sometimes referred to as the cross track distance.
|
|
9
9
|
|
|
10
10
|
### Parameters
|
|
11
11
|
|
|
12
|
-
* `pt` **([Feature][
|
|
13
|
-
* `line` **[Feature][
|
|
12
|
+
* `pt` **([Feature][1]<[Point][2]> | [Array][3]<[number][4]>)** Feature or Geometry
|
|
13
|
+
* `line` **[Feature][1]<[LineString][5]>** GeoJSON Feature or Geometry
|
|
14
14
|
* `options` **[Object][6]** Optional parameters (optional, default `{}`)
|
|
15
15
|
|
|
16
|
-
* `options.units` **[
|
|
17
|
-
|
|
18
|
-
* `options.method` **[string][7]** whether to calculate the distance based on geodesic (spheroid) or
|
|
16
|
+
* `options.units` **Units** Supports all valid Turf [Units][7] (optional, default `"kilometers"`)
|
|
17
|
+
* `options.method` **[string][8]** whether to calculate the distance based on geodesic (spheroid) or
|
|
19
18
|
planar (flat) method. Valid options are 'geodesic' or 'planar'. (optional, default `"geodesic"`)
|
|
20
19
|
|
|
21
20
|
### Examples
|
|
@@ -28,21 +27,23 @@ var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
|
|
|
28
27
|
//=69.11854715938406
|
|
29
28
|
```
|
|
30
29
|
|
|
31
|
-
Returns **[number][
|
|
30
|
+
Returns **[number][4]** distance between point and line
|
|
32
31
|
|
|
33
|
-
[1]: https://tools.ietf.org/html/rfc7946#section-3.
|
|
32
|
+
[1]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
34
33
|
|
|
35
|
-
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.
|
|
34
|
+
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
36
35
|
|
|
37
|
-
[3]: https://
|
|
36
|
+
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
38
37
|
|
|
39
|
-
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/
|
|
38
|
+
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
40
39
|
|
|
41
|
-
[5]: https://
|
|
40
|
+
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.4
|
|
42
41
|
|
|
43
42
|
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
44
43
|
|
|
45
|
-
[7]: https://
|
|
44
|
+
[7]: https://turfjs.org/docs/api/types/Units
|
|
45
|
+
|
|
46
|
+
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
46
47
|
|
|
47
48
|
<!-- 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. -->
|
|
48
49
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
|
-
var _distance = require('@turf/distance');
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
var _helpers = require('@turf/helpers');
|
|
8
|
+
var _nearestpointonline = require('@turf/nearest-point-on-line');
|
|
9
9
|
var _invariant = require('@turf/invariant');
|
|
10
10
|
var _meta = require('@turf/meta');
|
|
11
11
|
var _rhumbdistance = require('@turf/rhumb-distance');
|
|
12
12
|
function pointToLineDistance(pt, line, options = {}) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (!options.units) {
|
|
17
|
-
options.units = "kilometers";
|
|
18
|
-
}
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const method = (_a = options.method) != null ? _a : "geodesic";
|
|
15
|
+
const units = (_b = options.units) != null ? _b : "kilometers";
|
|
19
16
|
if (!pt) {
|
|
20
17
|
throw new Error("pt is required");
|
|
21
18
|
}
|
|
@@ -39,39 +36,44 @@ function pointToLineDistance(pt, line, options = {}) {
|
|
|
39
36
|
let distance = Infinity;
|
|
40
37
|
const p = pt.geometry.coordinates;
|
|
41
38
|
_meta.segmentEach.call(void 0, line, (segment) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
distance
|
|
39
|
+
if (segment) {
|
|
40
|
+
const a = segment.geometry.coordinates[0];
|
|
41
|
+
const b = segment.geometry.coordinates[1];
|
|
42
|
+
const d = distanceToSegment(p, a, b, { method });
|
|
43
|
+
if (d < distance) {
|
|
44
|
+
distance = d;
|
|
45
|
+
}
|
|
47
46
|
}
|
|
48
47
|
});
|
|
49
|
-
return _helpers.convertLength.call(void 0, distance, "degrees",
|
|
48
|
+
return _helpers.convertLength.call(void 0, distance, "degrees", units);
|
|
50
49
|
}
|
|
51
50
|
function distanceToSegment(p, a, b, options) {
|
|
51
|
+
if (options.method === "geodesic") {
|
|
52
|
+
const nearest = _nearestpointonline.nearestPointOnLine.call(void 0, _helpers.lineString.call(void 0, [a, b]).geometry, p, {
|
|
53
|
+
units: "degrees"
|
|
54
|
+
});
|
|
55
|
+
return nearest.properties.dist;
|
|
56
|
+
}
|
|
52
57
|
const v = [b[0] - a[0], b[1] - a[1]];
|
|
53
58
|
const w = [p[0] - a[0], p[1] - a[1]];
|
|
54
59
|
const c1 = dot(w, v);
|
|
55
60
|
if (c1 <= 0) {
|
|
56
|
-
return
|
|
61
|
+
return _rhumbdistance.rhumbDistance.call(void 0, p, a, { units: "degrees" });
|
|
57
62
|
}
|
|
58
63
|
const c2 = dot(v, v);
|
|
59
64
|
if (c2 <= c1) {
|
|
60
|
-
return
|
|
65
|
+
return _rhumbdistance.rhumbDistance.call(void 0, p, b, { units: "degrees" });
|
|
61
66
|
}
|
|
62
67
|
const b2 = c1 / c2;
|
|
63
68
|
const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];
|
|
64
|
-
return
|
|
69
|
+
return _rhumbdistance.rhumbDistance.call(void 0, p, Pb, { units: "degrees" });
|
|
65
70
|
}
|
|
66
71
|
function dot(u, v) {
|
|
67
72
|
return u[0] * v[0] + u[1] * v[1];
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
return options.method === "planar" ? _rhumbdistance.rhumbDistance.call(void 0, a, b, options) : _distance.distance.call(void 0, a, b, options);
|
|
71
|
-
}
|
|
72
|
-
var turf_point_to_line_distance_default = pointToLineDistance;
|
|
74
|
+
var index_default = pointToLineDistance;
|
|
73
75
|
|
|
74
76
|
|
|
75
77
|
|
|
76
|
-
exports.default =
|
|
78
|
+
exports.default = index_default; exports.pointToLineDistance = pointToLineDistance;
|
|
77
79
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AAEA
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-point-to-line-distance/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACEA;AACE;AAEA;AACA;AACA;AAAA,wCAEK;AACP,iEAAmC;AACnC,4CAA0B;AAC1B,kCAA4B;AAC5B,qDAA8B;AAqB9B,SAAS,mBAAA,CACP,EAAA,EACA,IAAA,EACA,QAAA,EAGI,CAAC,CAAA,EACG;AAzCV,EAAA,IAAA,EAAA,EAAA,EAAA;AA2CE,EAAA,MAAM,OAAA,EAAA,CAAS,GAAA,EAAA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,EAAG,CAAA,EAAG,EAAE,KAAA,EAAO,UAAU,CAAC,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,EAAK,GAAA,CAAI,CAAA,EAAG,CAAC,CAAA;AACnB,EAAA,GAAA,CAAI,GAAA,GAAM,EAAA,EAAI;AACZ,IAAA,OAAO,0CAAA,CAAc,EAAG,CAAA,EAAG,EAAE,KAAA,EAAO,UAAU,CAAC,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,EAAK,GAAA,EAAK,EAAA;AAChB,EAAA,MAAM,GAAA,EAAK,CAAC,CAAA,CAAE,CAAC,EAAA,EAAI,GAAA,EAAK,CAAA,CAAE,CAAC,CAAA,EAAG,CAAA,CAAE,CAAC,EAAA,EAAI,GAAA,EAAK,CAAA,CAAE,CAAC,CAAC,CAAA;AAE9C,EAAA,OAAO,0CAAA,CAAc,EAAG,EAAA,EAAI,EAAE,KAAA,EAAO,UAAU,CAAC,CAAA;AAClD;AAEA,SAAS,GAAA,CAAI,CAAA,EAAa,CAAA,EAAa;AACrC,EAAA,OAAO,CAAA,CAAE,CAAC,EAAA,EAAI,CAAA,CAAE,CAAC,EAAA,EAAI,CAAA,CAAE,CAAC,EAAA,EAAI,CAAA,CAAE,CAAC,CAAA;AACjC;AAGA,IAAO,cAAA,EAAQ,mBAAA;AD9Df;AACE;AACA;AACF,mFAAC","file":"/home/runner/work/turf/turf/packages/turf-point-to-line-distance/dist/cjs/index.cjs","sourcesContent":[null,"// Taken from http://geomalgorithms.com/a02-_lines.html\nimport { Feature, LineString } from \"geojson\";\nimport {\n convertLength,\n Coord,\n feature,\n lineString,\n point,\n Units,\n} from \"@turf/helpers\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { featureOf } from \"@turf/invariant\";\nimport { segmentEach } from \"@turf/meta\";\nimport { rhumbDistance } from \"@turf/rhumb-distance\";\n\n/**\n * Calculates the distance between a given point and the nearest point on a\n * line. Sometimes referred to as the cross track distance.\n *\n * @function\n * @param {Feature<Point>|Array<number>} pt Feature or Geometry\n * @param {Feature<LineString>} line GeoJSON Feature or Geometry\n * @param {Object} [options={}] Optional parameters\n * @param {Units} [options.units=\"kilometers\"] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}\n * @param {string} [options.method=\"geodesic\"] whether to calculate the distance based on geodesic (spheroid) or\n * planar (flat) method. Valid options are 'geodesic' or 'planar'.\n * @returns {number} distance between point and line\n * @example\n * var pt = turf.point([0, 0]);\n * var line = turf.lineString([[1, 1],[-1, 1]]);\n *\n * var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});\n * //=69.11854715938406\n */\nfunction pointToLineDistance(\n pt: Coord,\n line: Feature<LineString> | LineString,\n options: {\n units?: Units;\n method?: \"geodesic\" | \"planar\";\n } = {}\n): number {\n // Optional parameters\n const method = options.method ?? \"geodesic\";\n const units = options.units ?? \"kilometers\";\n\n // validation\n if (!pt) {\n throw new Error(\"pt is required\");\n }\n if (Array.isArray(pt)) {\n pt = point(pt);\n } else if (pt.type === \"Point\") {\n pt = feature(pt);\n } else {\n featureOf(pt, \"Point\", \"point\");\n }\n\n if (!line) {\n throw new Error(\"line is required\");\n }\n if (Array.isArray(line)) {\n line = lineString(line);\n } else if (line.type === \"LineString\") {\n line = feature(line);\n } else {\n featureOf(line, \"LineString\", \"line\");\n }\n\n let distance = Infinity;\n const p = pt.geometry.coordinates;\n segmentEach(line, (segment) => {\n if (segment) {\n const a = segment.geometry.coordinates[0];\n const b = segment.geometry.coordinates[1];\n const d = distanceToSegment(p, a, b, { method });\n if (d < distance) {\n distance = d;\n }\n }\n });\n return convertLength(distance, \"degrees\", units);\n}\n\n/**\n * Returns the distance between a point P on a segment AB.\n *\n * @private\n * @param {Array<number>} p external point\n * @param {Array<number>} a first segment point\n * @param {Array<number>} b second segment point\n * @param {Object} [options={}] Optional parameters\n * @returns {number} distance\n */\nfunction distanceToSegment(\n p: number[], // point to measure from\n a: number[], // start point of the segment to measure to\n b: number[], // end point of the segment to measure to\n options: {\n method: \"geodesic\" | \"planar\";\n }\n) {\n // Internally just use degrees, and then convert to the user's requested units\n // in the calling function.\n if (options.method === \"geodesic\") {\n // Use nearestPointOnLine to properly calculate distances on a spherical\n // Earth.\n const nearest = nearestPointOnLine(lineString([a, b]).geometry, p, {\n units: \"degrees\",\n });\n return nearest.properties.dist;\n }\n\n // Perform scalar calculations instead using rhumb lines.\n const v = [b[0] - a[0], b[1] - a[1]];\n const w = [p[0] - a[0], p[1] - a[1]];\n\n const c1 = dot(w, v);\n if (c1 <= 0) {\n return rhumbDistance(p, a, { units: \"degrees\" });\n }\n const c2 = dot(v, v);\n if (c2 <= c1) {\n return rhumbDistance(p, b, { units: \"degrees\" });\n }\n const b2 = c1 / c2;\n const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];\n\n return rhumbDistance(p, Pb, { units: \"degrees\" });\n}\n\nfunction dot(u: number[], v: number[]) {\n return u[0] * v[0] + u[1] * v[1];\n}\n\nexport { pointToLineDistance };\nexport default pointToLineDistance;\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -2,15 +2,14 @@ import { Feature, LineString } from 'geojson';
|
|
|
2
2
|
import { Coord, Units } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Calculates the distance between a given point and the nearest point on a
|
|
6
|
+
* line. Sometimes referred to as the cross track distance.
|
|
7
7
|
*
|
|
8
|
-
* @
|
|
8
|
+
* @function
|
|
9
9
|
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
|
|
10
10
|
* @param {Feature<LineString>} line GeoJSON Feature or Geometry
|
|
11
11
|
* @param {Object} [options={}] Optional parameters
|
|
12
|
-
* @param {
|
|
13
|
-
* (ex: degrees, radians, miles, or kilometers)
|
|
12
|
+
* @param {Units} [options.units="kilometers"] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}
|
|
14
13
|
* @param {string} [options.method="geodesic"] whether to calculate the distance based on geodesic (spheroid) or
|
|
15
14
|
* planar (flat) method. Valid options are 'geodesic' or 'planar'.
|
|
16
15
|
* @returns {number} distance between point and line
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,15 +2,14 @@ import { Feature, LineString } from 'geojson';
|
|
|
2
2
|
import { Coord, Units } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Calculates the distance between a given point and the nearest point on a
|
|
6
|
+
* line. Sometimes referred to as the cross track distance.
|
|
7
7
|
*
|
|
8
|
-
* @
|
|
8
|
+
* @function
|
|
9
9
|
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
|
|
10
10
|
* @param {Feature<LineString>} line GeoJSON Feature or Geometry
|
|
11
11
|
* @param {Object} [options={}] Optional parameters
|
|
12
|
-
* @param {
|
|
13
|
-
* (ex: degrees, radians, miles, or kilometers)
|
|
12
|
+
* @param {Units} [options.units="kilometers"] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}
|
|
14
13
|
* @param {string} [options.method="geodesic"] whether to calculate the distance based on geodesic (spheroid) or
|
|
15
14
|
* planar (flat) method. Valid options are 'geodesic' or 'planar'.
|
|
16
15
|
* @returns {number} distance between point and line
|
package/dist/esm/index.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
// index.ts
|
|
2
|
-
import { distance as getDistance } from "@turf/distance";
|
|
3
2
|
import {
|
|
4
3
|
convertLength,
|
|
5
4
|
feature,
|
|
6
5
|
lineString,
|
|
7
6
|
point
|
|
8
7
|
} from "@turf/helpers";
|
|
8
|
+
import { nearestPointOnLine } from "@turf/nearest-point-on-line";
|
|
9
9
|
import { featureOf } from "@turf/invariant";
|
|
10
10
|
import { segmentEach } from "@turf/meta";
|
|
11
|
-
import { rhumbDistance
|
|
11
|
+
import { rhumbDistance } from "@turf/rhumb-distance";
|
|
12
12
|
function pointToLineDistance(pt, line, options = {}) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (!options.units) {
|
|
17
|
-
options.units = "kilometers";
|
|
18
|
-
}
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const method = (_a = options.method) != null ? _a : "geodesic";
|
|
15
|
+
const units = (_b = options.units) != null ? _b : "kilometers";
|
|
19
16
|
if (!pt) {
|
|
20
17
|
throw new Error("pt is required");
|
|
21
18
|
}
|
|
@@ -39,39 +36,44 @@ function pointToLineDistance(pt, line, options = {}) {
|
|
|
39
36
|
let distance = Infinity;
|
|
40
37
|
const p = pt.geometry.coordinates;
|
|
41
38
|
segmentEach(line, (segment) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
distance
|
|
39
|
+
if (segment) {
|
|
40
|
+
const a = segment.geometry.coordinates[0];
|
|
41
|
+
const b = segment.geometry.coordinates[1];
|
|
42
|
+
const d = distanceToSegment(p, a, b, { method });
|
|
43
|
+
if (d < distance) {
|
|
44
|
+
distance = d;
|
|
45
|
+
}
|
|
47
46
|
}
|
|
48
47
|
});
|
|
49
|
-
return convertLength(distance, "degrees",
|
|
48
|
+
return convertLength(distance, "degrees", units);
|
|
50
49
|
}
|
|
51
50
|
function distanceToSegment(p, a, b, options) {
|
|
51
|
+
if (options.method === "geodesic") {
|
|
52
|
+
const nearest = nearestPointOnLine(lineString([a, b]).geometry, p, {
|
|
53
|
+
units: "degrees"
|
|
54
|
+
});
|
|
55
|
+
return nearest.properties.dist;
|
|
56
|
+
}
|
|
52
57
|
const v = [b[0] - a[0], b[1] - a[1]];
|
|
53
58
|
const w = [p[0] - a[0], p[1] - a[1]];
|
|
54
59
|
const c1 = dot(w, v);
|
|
55
60
|
if (c1 <= 0) {
|
|
56
|
-
return
|
|
61
|
+
return rhumbDistance(p, a, { units: "degrees" });
|
|
57
62
|
}
|
|
58
63
|
const c2 = dot(v, v);
|
|
59
64
|
if (c2 <= c1) {
|
|
60
|
-
return
|
|
65
|
+
return rhumbDistance(p, b, { units: "degrees" });
|
|
61
66
|
}
|
|
62
67
|
const b2 = c1 / c2;
|
|
63
68
|
const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];
|
|
64
|
-
return
|
|
69
|
+
return rhumbDistance(p, Pb, { units: "degrees" });
|
|
65
70
|
}
|
|
66
71
|
function dot(u, v) {
|
|
67
72
|
return u[0] * v[0] + u[1] * v[1];
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
return options.method === "planar" ? getPlanarDistance(a, b, options) : getDistance(a, b, options);
|
|
71
|
-
}
|
|
72
|
-
var turf_point_to_line_distance_default = pointToLineDistance;
|
|
74
|
+
var index_default = pointToLineDistance;
|
|
73
75
|
export {
|
|
74
|
-
|
|
76
|
+
index_default as default,
|
|
75
77
|
pointToLineDistance
|
|
76
78
|
};
|
|
77
79
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"sourcesContent":["// Taken from http://geomalgorithms.com/a02-_lines.html\nimport { Feature, LineString } from \"geojson\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["// Taken from http://geomalgorithms.com/a02-_lines.html\nimport { Feature, LineString } from \"geojson\";\nimport {\n convertLength,\n Coord,\n feature,\n lineString,\n point,\n Units,\n} from \"@turf/helpers\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { featureOf } from \"@turf/invariant\";\nimport { segmentEach } from \"@turf/meta\";\nimport { rhumbDistance } from \"@turf/rhumb-distance\";\n\n/**\n * Calculates the distance between a given point and the nearest point on a\n * line. Sometimes referred to as the cross track distance.\n *\n * @function\n * @param {Feature<Point>|Array<number>} pt Feature or Geometry\n * @param {Feature<LineString>} line GeoJSON Feature or Geometry\n * @param {Object} [options={}] Optional parameters\n * @param {Units} [options.units=\"kilometers\"] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}\n * @param {string} [options.method=\"geodesic\"] whether to calculate the distance based on geodesic (spheroid) or\n * planar (flat) method. Valid options are 'geodesic' or 'planar'.\n * @returns {number} distance between point and line\n * @example\n * var pt = turf.point([0, 0]);\n * var line = turf.lineString([[1, 1],[-1, 1]]);\n *\n * var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});\n * //=69.11854715938406\n */\nfunction pointToLineDistance(\n pt: Coord,\n line: Feature<LineString> | LineString,\n options: {\n units?: Units;\n method?: \"geodesic\" | \"planar\";\n } = {}\n): number {\n // Optional parameters\n const method = options.method ?? \"geodesic\";\n const units = options.units ?? \"kilometers\";\n\n // validation\n if (!pt) {\n throw new Error(\"pt is required\");\n }\n if (Array.isArray(pt)) {\n pt = point(pt);\n } else if (pt.type === \"Point\") {\n pt = feature(pt);\n } else {\n featureOf(pt, \"Point\", \"point\");\n }\n\n if (!line) {\n throw new Error(\"line is required\");\n }\n if (Array.isArray(line)) {\n line = lineString(line);\n } else if (line.type === \"LineString\") {\n line = feature(line);\n } else {\n featureOf(line, \"LineString\", \"line\");\n }\n\n let distance = Infinity;\n const p = pt.geometry.coordinates;\n segmentEach(line, (segment) => {\n if (segment) {\n const a = segment.geometry.coordinates[0];\n const b = segment.geometry.coordinates[1];\n const d = distanceToSegment(p, a, b, { method });\n if (d < distance) {\n distance = d;\n }\n }\n });\n return convertLength(distance, \"degrees\", units);\n}\n\n/**\n * Returns the distance between a point P on a segment AB.\n *\n * @private\n * @param {Array<number>} p external point\n * @param {Array<number>} a first segment point\n * @param {Array<number>} b second segment point\n * @param {Object} [options={}] Optional parameters\n * @returns {number} distance\n */\nfunction distanceToSegment(\n p: number[], // point to measure from\n a: number[], // start point of the segment to measure to\n b: number[], // end point of the segment to measure to\n options: {\n method: \"geodesic\" | \"planar\";\n }\n) {\n // Internally just use degrees, and then convert to the user's requested units\n // in the calling function.\n if (options.method === \"geodesic\") {\n // Use nearestPointOnLine to properly calculate distances on a spherical\n // Earth.\n const nearest = nearestPointOnLine(lineString([a, b]).geometry, p, {\n units: \"degrees\",\n });\n return nearest.properties.dist;\n }\n\n // Perform scalar calculations instead using rhumb lines.\n const v = [b[0] - a[0], b[1] - a[1]];\n const w = [p[0] - a[0], p[1] - a[1]];\n\n const c1 = dot(w, v);\n if (c1 <= 0) {\n return rhumbDistance(p, a, { units: \"degrees\" });\n }\n const c2 = dot(v, v);\n if (c2 <= c1) {\n return rhumbDistance(p, b, { units: \"degrees\" });\n }\n const b2 = c1 / c2;\n const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];\n\n return rhumbDistance(p, Pb, { units: \"degrees\" });\n}\n\nfunction dot(u: number[], v: number[]) {\n return u[0] * v[0] + u[1] * v[1];\n}\n\nexport { pointToLineDistance };\nexport default pointToLineDistance;\n"],"mappings":";AAEA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,0BAA0B;AACnC,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB;AAC5B,SAAS,qBAAqB;AAqB9B,SAAS,oBACP,IACA,MACA,UAGI,CAAC,GACG;AAzCV;AA2CE,QAAM,UAAS,aAAQ,WAAR,YAAkB;AACjC,QAAM,SAAQ,aAAQ,UAAR,YAAiB;AAG/B,MAAI,CAAC,IAAI;AACP,UAAM,IAAI,MAAM,gBAAgB;AAAA,EAClC;AACA,MAAI,MAAM,QAAQ,EAAE,GAAG;AACrB,SAAK,MAAM,EAAE;AAAA,EACf,WAAW,GAAG,SAAS,SAAS;AAC9B,SAAK,QAAQ,EAAE;AAAA,EACjB,OAAO;AACL,cAAU,IAAI,SAAS,OAAO;AAAA,EAChC;AAEA,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,MAAM,kBAAkB;AAAA,EACpC;AACA,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,WAAO,WAAW,IAAI;AAAA,EACxB,WAAW,KAAK,SAAS,cAAc;AACrC,WAAO,QAAQ,IAAI;AAAA,EACrB,OAAO;AACL,cAAU,MAAM,cAAc,MAAM;AAAA,EACtC;AAEA,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a,GAAa;AACrC,SAAO,EAAE,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;AACjC;AAGA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/point-to-line-distance",
|
|
3
|
-
"version": "7.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.3.0",
|
|
4
|
+
"description": "Calculates the distance between a given point and the nearest point on a line.",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Stefano Borghi <@stebogit>"
|
|
@@ -54,29 +54,30 @@
|
|
|
54
54
|
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@turf/circle": "
|
|
57
|
+
"@turf/circle": "7.3.0",
|
|
58
58
|
"@types/benchmark": "^2.1.5",
|
|
59
|
-
"@types/tape": "^
|
|
59
|
+
"@types/tape": "^5.8.1",
|
|
60
60
|
"benchmark": "^2.1.4",
|
|
61
61
|
"load-json-file": "^7.0.1",
|
|
62
62
|
"npm-run-all": "^4.1.5",
|
|
63
|
-
"tape": "^5.
|
|
64
|
-
"tsup": "^8.0
|
|
65
|
-
"tsx": "^4.
|
|
66
|
-
"typescript": "^5.
|
|
67
|
-
"write-json-file": "^
|
|
63
|
+
"tape": "^5.9.0",
|
|
64
|
+
"tsup": "^8.4.0",
|
|
65
|
+
"tsx": "^4.19.4",
|
|
66
|
+
"typescript": "^5.8.3",
|
|
67
|
+
"write-json-file": "^6.0.0"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@turf/bearing": "
|
|
71
|
-
"@turf/distance": "
|
|
72
|
-
"@turf/helpers": "
|
|
73
|
-
"@turf/invariant": "
|
|
74
|
-
"@turf/meta": "
|
|
75
|
-
"@turf/
|
|
76
|
-
"@turf/
|
|
77
|
-
"@turf/rhumb-
|
|
70
|
+
"@turf/bearing": "7.3.0",
|
|
71
|
+
"@turf/distance": "7.3.0",
|
|
72
|
+
"@turf/helpers": "7.3.0",
|
|
73
|
+
"@turf/invariant": "7.3.0",
|
|
74
|
+
"@turf/meta": "7.3.0",
|
|
75
|
+
"@turf/nearest-point-on-line": "7.3.0",
|
|
76
|
+
"@turf/projection": "7.3.0",
|
|
77
|
+
"@turf/rhumb-bearing": "7.3.0",
|
|
78
|
+
"@turf/rhumb-distance": "7.3.0",
|
|
78
79
|
"@types/geojson": "^7946.0.10",
|
|
79
|
-
"tslib": "^2.
|
|
80
|
+
"tslib": "^2.8.1"
|
|
80
81
|
},
|
|
81
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "9f58a103e8f9a587ab640307ed03ba5233913ddd"
|
|
82
83
|
}
|