Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/point-to-line-distance

Package Overview
Dependencies
Maintainers
4
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/point-to-line-distance - npm Package Compare versions

Comparing version 5.1.5 to 5.1.6

227

index.js

@@ -1,21 +0,9 @@

// (logic of computation inspired by:
// https://stackoverflow.com/questions/32771458/distance-from-lat-lng-point-to-minor-arc-segment)
import bearing from '@turf/bearing';
import distance from '@turf/distance';
import rhumbBearing from '@turf/rhumb-bearing';
import rhumbDistance from '@turf/rhumb-distance';
import { toMercator, toWgs84 } from '@turf/projection';
import { featureOf } from '@turf/invariant';
import { segmentEach } from '@turf/meta';
import {
point,
feature,
lineString,
bearingToAzimuth,
degreesToRadians,
convertLength,
isObject
} from '@turf/helpers';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Taken from http://geomalgorithms.com/a02-_lines.html
var invariant_1 = require("@turf/invariant");
var meta_1 = require("@turf/meta");
var distance_1 = require("@turf/distance");
var rhumb_distance_1 = require("@turf/rhumb-distance");
var helpers_1 = require("@turf/helpers");
/**

@@ -26,7 +14,7 @@ * Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the

* @name pointToLineDistance
* @param {Coord} pt Feature or Geometry
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Feature<LineString>} line GeoJSON Feature or Geometry
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @param {boolean} [options.mercator=false] if distance should be on Mercator or WGS84 projection
* @param {string} [options.units='kilometers'] can be anything supported by turf/convertLength, eg degrees, radians, miles, or kilometers
* @param {string} [options.method='geodesic'] wehther to calculate the distance based on geodesic (spheroid) or planar (flat) method. Valid options are 'geodesic' or 'planar'.
* @returns {number} distance between point and line

@@ -41,29 +29,36 @@ * @example

function pointToLineDistance(pt, line, options) {
if (options === void 0) { options = {}; }
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
if (!options.method)
options.method = 'geodesic';
if (!options.units)
options.units = 'kilometers';
// validation
if (!pt) throw new Error('pt is required');
if (Array.isArray(pt)) pt = point(pt);
else if (pt.type === 'Point') pt = feature(pt);
else featureOf(pt, 'Point', 'point');
if (!line) throw new Error('line is required');
if (Array.isArray(line)) line = lineString(line);
else if (line.type === 'LineString') line = feature(line);
else featureOf(line, 'LineString', 'line');
if (!pt)
throw new Error('pt is required');
if (Array.isArray(pt))
pt = helpers_1.point(pt);
else if (pt.type === 'Point')
pt = helpers_1.feature(pt);
else
invariant_1.featureOf(pt, 'Point', 'point');
if (!line)
throw new Error('line is required');
if (Array.isArray(line))
line = helpers_1.lineString(line);
else if (line.type === 'LineString')
line = helpers_1.feature(line);
else
invariant_1.featureOf(line, 'LineString', 'line');
var distance = Infinity;
var p = pt.geometry.coordinates;
segmentEach(line, function (segment) {
meta_1.segmentEach(line, function (segment) {
var a = segment.geometry.coordinates[0];
var b = segment.geometry.coordinates[1];
var d = distanceToSegment(p, a, b, options);
if (distance > d) distance = d;
if (d < distance)
distance = d;
});
return distance;
return helpers_1.convertLength(distance, 'degrees', options.units);
}
/**

@@ -76,140 +71,24 @@ * Returns the distance between a point P on a segment AB.

* @param {Array<number>} b second segment point
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @param {boolean} [options.mercator=false] if distance should be on Mercator or WGS84 projection
* @param {Object} [options={}] Optional parameters
* @returns {number} distance
*/
function distanceToSegment(p, a, b, options) {
var mercator = options.mercator;
var distanceAP = (mercator !== true) ? distance(a, p, options) : euclideanDistance(a, p, options);
var azimuthAP = bearingToAzimuth((mercator !== true) ? bearing(a, p) : rhumbBearing(a, p));
var azimuthAB = bearingToAzimuth((mercator !== true) ? bearing(a, b) : rhumbBearing(a, b));
var angleA = Math.abs(azimuthAP - azimuthAB);
// if (angleA > 180) angleA = Math.abs(angleA - 360);
// if the angle PAB is obtuse its projection on the line extending the segment falls outside the segment
// thus return distance between P and the start point A
/*
P__
|\ \____
| \ \____
| \ \____
| \_____________\
H A B
*/
if (angleA > 90) return distanceAP;
var azimuthBA = (azimuthAB + 180) % 360;
var azimuthBP = bearingToAzimuth((mercator !== true) ? bearing(b, p) : rhumbBearing(b, p));
var angleB = Math.abs(azimuthBP - azimuthBA);
if (angleB > 180) angleB = Math.abs(angleB - 360);
// also if the angle ABP is acute the projection of P falls outside the segment, on the other side
// so return the distance between P and the end point B
/*
____P
____/ /|
____/ / |
____/ / |
/______________/ |
A B H
*/
if (angleB > 90) return (mercator !== true) ? distance(p, b, options) : euclideanDistance(p, b, options);
// finally if the projection falls inside the segment
// return the distance between P and the segment
/*
P
__/|\
__/ | \
__/ | \
__/ | \
/____________|____\
A H B
*/
if (mercator !== true) return distanceAP * Math.sin(degreesToRadians(angleA));
return mercatorPH(a, b, p, options);
var v = [b[0] - a[0], b[1] - a[1]];
var w = [p[0] - a[0], p[1] - a[1]];
var c1 = dot(w, v);
if (c1 <= 0)
return calcDistance(p, a, { method: options.method, units: 'degrees' });
var c2 = dot(v, v);
if (c2 <= c1)
return calcDistance(p, b, { method: options.method, units: 'degrees' });
var b2 = c1 / c2;
var Pb = [a[0] + (b2 * v[0]), a[1] + (b2 * v[1])];
return calcDistance(p, Pb, { method: options.method, units: 'degrees' });
}
/**
* Returns the distance between a point P on a segment AB, on Mercator projection
*
* @private
* @param {Array<number>} a first segment point
* @param {Array<number>} b second segment point
* @param {Array<number>} p external point
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @returns {number} distance
*/
function mercatorPH(a, b, p, options) {
var delta = 0;
// translate points if any is crossing the 180th meridian
if (Math.abs(a[0]) >= 180 || Math.abs(b[0]) >= 180 || Math.abs(p[0]) >= 180) {
delta = (a[0] > 0 || b[0] > 0 || p[0] > 0) ? -180 : 180;
}
var origin = point(p);
var A = toMercator([a[0] + delta, a[1]]);
var B = toMercator([b[0] + delta, b[1]]);
var P = toMercator([p[0] + delta, p[1]]);
var h = toWgs84(euclideanIntersection(A, B, P));
if (delta !== 0) h[0] -= delta; // translate back to original position
var distancePH = rhumbDistance(origin, h, options);
return distancePH;
function dot(u, v) {
return (u[0] * v[0] + u[1] * v[1]);
}
/**
* Returns the point H projection of a point P on a segment AB, on the euclidean plain
* from https://stackoverflow.com/questions/10301001/perpendicular-on-a-line-segment-from-a-given-point#answer-12499474
* P
* |
* |
* _________|____
* A H B
*
* @private
* @param {Array<number>} a first segment point
* @param {Array<number>} b second segment point
* @param {Array<number>} p external point
* @returns {Array<number>} projected point
*/
function euclideanIntersection(a, b, p) {
var x1 = a[0], y1 = a[1],
x2 = b[0], y2 = b[1],
x3 = p[0], y3 = p[1];
var px = x2 - x1, py = y2 - y1;
var dab = px * px + py * py;
var u = ((x3 - x1) * px + (y3 - y1) * py) / dab;
var x = x1 + u * px, y = y1 + u * py;
return [x, y]; // H
function calcDistance(a, b, options) {
return options.method === 'planar' ? rhumb_distance_1.default(a, b, options) : distance_1.default(a, b, options);
}
/**
* Returns euclidean distance between two points
*
* @private
* @param {Object} from first point
* @param {Object} to second point
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
* @returns {number} squared distance
*/
function euclideanDistance(from, to, options) {
var units = options.units;
// translate points if any is crossing the 180th meridian
var delta = 0;
if (Math.abs(from[0]) >= 180) {
delta = (from[0] > 0) ? -180 : 180;
}
if (Math.abs(to[0]) >= 180) {
delta = (to[0] > 0) ? -180 : 180;
}
var p1 = toMercator([from[0] + delta, from[1]]);
var p2 = toMercator([to[0] + delta, to[1]]);
var sqr = function (n) { return n * n; };
var squareD = sqr(p1[0] - p2[0]) + sqr(p1[1] - p2[1]);
var d = Math.sqrt(squareD);
return convertLength(d, 'meters', units);
}
export default pointToLineDistance;
exports.default = pointToLineDistance;
{
"name": "@turf/point-to-line-distance",
"version": "5.1.5",
"version": "5.1.6",
"description": "turf point-to-line-distance module",
"main": "main.js",
"module": "main.es.js",
"main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"main.js",
"main.es.js"
"index.d.ts"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js",
"bench": "node -r @std/esm bench.js",
"prepare": "tsc",
"pretest": "tsc",
"test": "node test.js",
"bench": "node bench.js",
"docs": "node ../../scripts/generate-readmes"

@@ -40,24 +37,21 @@ },

"devDependencies": {
"@std/esm": "*",
"@turf/circle": "^5.1.5",
"@turf/circle": "*",
"benchmark": "*",
"load-json-file": "*",
"rollup": "*",
"typescript": "*",
"tape": "*",
"write-json-file": "*"
"write-json-file": "*",
"tslint": "*",
"@types/tape": "*"
},
"dependencies": {
"@turf/bearing": "^5.1.5",
"@turf/distance": "^5.1.5",
"@turf/helpers": "^5.1.5",
"@turf/invariant": "^5.1.5",
"@turf/meta": "^5.1.5",
"@turf/projection": "^5.1.5",
"@turf/rhumb-bearing": "^5.1.5",
"@turf/rhumb-distance": "^5.1.5"
},
"@std/esm": {
"esm": "js",
"cjs": true
"@turf/bearing": "6.x",
"@turf/distance": "6.x",
"@turf/helpers": "6.x",
"@turf/invariant": "6.x",
"@turf/meta": "6.x",
"@turf/projection": "6.x",
"@turf/rhumb-bearing": "6.x",
"@turf/rhumb-distance": "6.x"
}
}

@@ -7,3 +7,3 @@ # @turf/point-to-line-distance

Returns the minimum distance between a [Point](https://tools.ietf.org/html/rfc7946#section-3.1.2) and a [LineString](https://tools.ietf.org/html/rfc7946#section-3.1.4), being the distance from a line the
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`.

@@ -13,7 +13,7 @@

- `pt` **[Coord](https://tools.ietf.org/html/rfc7946#section-3.1.1)** Feature or Geometry
- `line` **[Feature](https://tools.ietf.org/html/rfc7946#section-3.2)&lt;[LineString](https://tools.ietf.org/html/rfc7946#section-3.1.4)>** GeoJSON Feature or Geometry
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`)
- `options.units` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
- `options.mercator` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if distance should be on Mercator or WGS84 projection (optional, default `false`)
- `pt` **([Feature][3]&lt;[Point][4]> | [Array][5]&lt;[number][6]>)** Feature or Geometry
- `line` **[Feature][3]&lt;[LineString][7]>** GeoJSON Feature or Geometry
- `options` **[Object][8]** Optional parameters (optional, default `{}`)
- `options.units` **[string][9]** can be anything supported by turf/convertLength, eg degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
- `options.method` **[string][9]** wehther to calculate the distance based on geodesic (spheroid) or planar (flat) method. Valid options are 'geodesic' or 'planar'. (optional, default `'geodesic'`)

@@ -30,4 +30,22 @@ **Examples**

Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance between point and line
Returns **[number][6]** distance between point and line
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[3]: https://tools.ietf.org/html/rfc7946#section-3.2
[4]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[7]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
<!-- This file is automatically generated. Please don't edit it directly:

@@ -34,0 +52,0 @@ if you find an error, edit the source file (likely index.js), and re-run

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc