@turf/point-to-line-distance
Advanced tools
Comparing version 7.1.0 to 7.2.0
@@ -5,6 +5,6 @@ import { Feature, LineString } from 'geojson'; | ||
/** | ||
* Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the | ||
* minimum distance between the point and any segment of the `LineString`. | ||
* Calculates the distance between a given point and the nearest point on a | ||
* line. Sometimes referred to as the cross track distance. | ||
* | ||
* @name pointToLineDistance | ||
* @function | ||
* @param {Feature<Point>|Array<number>} pt Feature or Geometry | ||
@@ -11,0 +11,0 @@ * @param {Feature<LineString>} line GeoJSON Feature or Geometry |
// index.ts | ||
import { distance as getDistance } from "@turf/distance"; | ||
import { | ||
@@ -9,12 +8,10 @@ convertLength, | ||
} from "@turf/helpers"; | ||
import { nearestPointOnLine } from "@turf/nearest-point-on-line"; | ||
import { featureOf } from "@turf/invariant"; | ||
import { segmentEach } from "@turf/meta"; | ||
import { rhumbDistance as getPlanarDistance } from "@turf/rhumb-distance"; | ||
import { rhumbDistance } from "@turf/rhumb-distance"; | ||
function pointToLineDistance(pt, line, options = {}) { | ||
if (!options.method) { | ||
options.method = "geodesic"; | ||
} | ||
if (!options.units) { | ||
options.units = "kilometers"; | ||
} | ||
var _a, _b; | ||
const method = (_a = options.method) != null ? _a : "geodesic"; | ||
const units = (_b = options.units) != null ? _b : "kilometers"; | ||
if (!pt) { | ||
@@ -43,12 +40,20 @@ throw new Error("pt is required"); | ||
segmentEach(line, (segment) => { | ||
const a = segment.geometry.coordinates[0]; | ||
const b = segment.geometry.coordinates[1]; | ||
const d = distanceToSegment(p, a, b, options); | ||
if (d < distance) { | ||
distance = d; | ||
if (segment) { | ||
const a = segment.geometry.coordinates[0]; | ||
const b = segment.geometry.coordinates[1]; | ||
const d = distanceToSegment(p, a, b, { method }); | ||
if (d < distance) { | ||
distance = d; | ||
} | ||
} | ||
}); | ||
return convertLength(distance, "degrees", options.units); | ||
return convertLength(distance, "degrees", units); | ||
} | ||
function distanceToSegment(p, a, b, options) { | ||
if (options.method === "geodesic") { | ||
const nearest = nearestPointOnLine(lineString([a, b]).geometry, p, { | ||
units: "degrees" | ||
}); | ||
return nearest.properties.dist; | ||
} | ||
const v = [b[0] - a[0], b[1] - a[1]]; | ||
@@ -58,11 +63,11 @@ const w = [p[0] - a[0], p[1] - a[1]]; | ||
if (c1 <= 0) { | ||
return calcDistance(p, a, { method: options.method, units: "degrees" }); | ||
return rhumbDistance(p, a, { units: "degrees" }); | ||
} | ||
const c2 = dot(v, v); | ||
if (c2 <= c1) { | ||
return calcDistance(p, b, { method: options.method, units: "degrees" }); | ||
return rhumbDistance(p, b, { units: "degrees" }); | ||
} | ||
const b2 = c1 / c2; | ||
const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]]; | ||
return calcDistance(p, Pb, { method: options.method, units: "degrees" }); | ||
return rhumbDistance(p, Pb, { units: "degrees" }); | ||
} | ||
@@ -72,5 +77,2 @@ function dot(u, v) { | ||
} | ||
function calcDistance(a, b, options) { | ||
return options.method === "planar" ? getPlanarDistance(a, b, options) : getDistance(a, b, options); | ||
} | ||
var turf_point_to_line_distance_default = pointToLineDistance; | ||
@@ -77,0 +79,0 @@ export { |
{ | ||
"name": "@turf/point-to-line-distance", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"description": "turf point-to-line-distance module", | ||
@@ -57,27 +57,28 @@ "author": "Turf Authors", | ||
"devDependencies": { | ||
"@turf/circle": "^7.1.0", | ||
"@turf/circle": "^7.2.0", | ||
"@types/benchmark": "^2.1.5", | ||
"@types/tape": "^4.2.32", | ||
"@types/tape": "^4.13.4", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^7.0.1", | ||
"npm-run-all": "^4.1.5", | ||
"tape": "^5.7.2", | ||
"tsup": "^8.0.1", | ||
"tsx": "^4.6.2", | ||
"typescript": "^5.2.2", | ||
"tape": "^5.9.0", | ||
"tsup": "^8.3.5", | ||
"tsx": "^4.19.2", | ||
"typescript": "^5.5.4", | ||
"write-json-file": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"@turf/bearing": "^7.1.0", | ||
"@turf/distance": "^7.1.0", | ||
"@turf/helpers": "^7.1.0", | ||
"@turf/invariant": "^7.1.0", | ||
"@turf/meta": "^7.1.0", | ||
"@turf/projection": "^7.1.0", | ||
"@turf/rhumb-bearing": "^7.1.0", | ||
"@turf/rhumb-distance": "^7.1.0", | ||
"@turf/bearing": "^7.2.0", | ||
"@turf/distance": "^7.2.0", | ||
"@turf/helpers": "^7.2.0", | ||
"@turf/invariant": "^7.2.0", | ||
"@turf/meta": "^7.2.0", | ||
"@turf/nearest-point-on-line": "^7.2.0", | ||
"@turf/projection": "^7.2.0", | ||
"@turf/rhumb-bearing": "^7.2.0", | ||
"@turf/rhumb-distance": "^7.2.0", | ||
"@types/geojson": "^7946.0.10", | ||
"tslib": "^2.6.2" | ||
"tslib": "^2.8.1" | ||
}, | ||
"gitHead": "68915eeebc9278bb40dec3f1034499698a0561ef" | ||
"gitHead": "7b0f0374c4668cd569f8904c71e2ae7d941be867" | ||
} |
@@ -7,9 +7,9 @@ # @turf/point-to-line-distance | ||
Returns the minimum distance between a [Point][1] and a [LineString][2], being the distance from a line the | ||
minimum distance between the point and any segment of the `LineString`. | ||
Calculates the distance between a given point and the nearest point on a | ||
line. Sometimes referred to as the cross track distance. | ||
### Parameters | ||
* `pt` **([Feature][3]<[Point][1]> | [Array][4]<[number][5]>)** Feature or Geometry | ||
* `line` **[Feature][3]<[LineString][2]>** GeoJSON Feature or Geometry | ||
* `pt` **([Feature][1]<[Point][2]> | [Array][3]<[number][4]>)** Feature or Geometry | ||
* `line` **[Feature][1]<[LineString][5]>** GeoJSON Feature or Geometry | ||
* `options` **[Object][6]** Optional parameters (optional, default `{}`) | ||
@@ -32,13 +32,13 @@ | ||
Returns **[number][5]** distance between point and line | ||
Returns **[number][4]** distance between point and line | ||
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2 | ||
[1]: https://tools.ietf.org/html/rfc7946#section-3.2 | ||
[2]: 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]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number | ||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number | ||
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.4 | ||
@@ -45,0 +45,0 @@ [6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26679
175
11
+ Added@turf/nearest-point-on-line@7.2.0(transitive)
Updated@turf/bearing@^7.2.0
Updated@turf/distance@^7.2.0
Updated@turf/helpers@^7.2.0
Updated@turf/invariant@^7.2.0
Updated@turf/meta@^7.2.0
Updated@turf/projection@^7.2.0
Updated@turf/rhumb-bearing@^7.2.0
Updated@turf/rhumb-distance@^7.2.0
Updatedtslib@^2.8.1