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
30
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.0.0 to 5.0.4

4

index.d.ts

@@ -1,2 +0,2 @@

import { Feature, LineString, Point, Units, Position } from '@turf/helpers'
import { Feature, LineString, Units, Coord } from '@turf/helpers'

@@ -7,3 +7,3 @@ /**

export default function pointToLineDistance(
pt: Feature<Point> | Point | Position,
pt: Coord,
line: Feature<LineString> | LineString,

@@ -10,0 +10,0 @@ options?: {

@@ -8,2 +8,3 @@ // (logic of computation inspired by:

import rhumbDistance from '@turf/rhumb-distance';
import { toMercator, toWgs84 } from '@turf/projection';
import { featureOf } from '@turf/invariant';

@@ -15,5 +16,6 @@ import { segmentEach } from '@turf/meta';

lineString,
bearingToAngle,
degrees2radians,
convertDistance
bearingToAzimuth,
degreesToRadians,
convertLength,
isObject
} from '@turf/helpers';

@@ -26,4 +28,4 @@

* @name pointToLineDistance
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Feature<LineString>|Array<Array<number>>} line GeoJSON Feature or Geometry
* @param {Geometry|Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Geometry|Feature<LineString>|Array<Array<number>>} line GeoJSON Feature or Geometry
* @param {Object} [options={}] Optional parameters

@@ -37,13 +39,11 @@ * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers

*
* var distance = turf.pointToLineDistance(pt, line, 'miles');
* var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
* //=69.11854715938406
*/
function pointToLineDistance(pt, line, options) {
// Optional params
// Optional parameters
options = options || {};
// var units = options.units;
// var mercator = options.mercator;
if (!isObject(options)) throw new Error('options is invalid');
// validation
if (typeof options !== 'object') throw new Error('options must be an object');
if (!pt) throw new Error('pt is required');

@@ -78,3 +78,3 @@ if (Array.isArray(pt)) pt = point(pt);

* @param {Array<number>} b second segment point
* @param {Object} [options] Optional parameters
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers

@@ -85,9 +85,6 @@ * @param {boolean} [options.mercator=false] if distance should be on Mercator or WGS84 projection

function distanceToSegment(p, a, b, options) {
options = options || {};
var units = options.units;
var mercator = options.mercator;
var distanceAP = (mercator !== true) ? distance(a, p, units) : euclideanDistance(a, p, units);
var azimuthAP = bearingToAngle((mercator !== true) ? bearing(a, p) : rhumbBearing(a, p));
var azimuthAB = bearingToAngle((mercator !== true) ? bearing(a, b) : rhumbBearing(a, b));
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);

@@ -108,3 +105,3 @@ // if (angleA > 180) angleA = Math.abs(angleA - 360);

var azimuthBA = (azimuthAB + 180) % 360;
var azimuthBP = bearingToAngle((mercator !== true) ? bearing(b, p) : rhumbBearing(b, p));
var azimuthBP = bearingToAzimuth((mercator !== true) ? bearing(b, p) : rhumbBearing(b, p));
var angleB = Math.abs(azimuthBP - azimuthBA);

@@ -122,3 +119,3 @@ if (angleB > 180) angleB = Math.abs(angleB - 360);

*/
if (angleB > 90) return (mercator !== true) ? distance(p, b, units) : euclideanDistance(p, b, units);
if (angleB > 90) return (mercator !== true) ? distance(p, b, options) : euclideanDistance(p, b, options);
// finally if the projection falls inside the segment

@@ -135,4 +132,4 @@ // return the distance between P and the segment

*/
if (mercator !== true) return distanceAP * Math.sin(degrees2radians(angleA));
return mercatorPH(a, b, p, units);
if (mercator !== true) return distanceAP * Math.sin(degreesToRadians(angleA));
return mercatorPH(a, b, p, options);
}

@@ -147,6 +144,7 @@

* @param {Array<number>} p external point
* @param {string} [units='kilometers'] can be degrees, radians, miles, or kilometers
* @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, units) {
function mercatorPH(a, b, p, options) {
var delta = 0;

@@ -162,6 +160,6 @@ // translate points if any is crossing the 180th meridian

var P = toMercator([p[0] + delta, p[1]]);
var h = toWGS84(euclideanIntersection(A, B, P));
var h = toWgs84(euclideanIntersection(A, B, P));
if (delta !== 0) h[0] -= delta; // translate back to original position
var distancePH = rhumbDistance(origin, h, units);
var distancePH = rhumbDistance(origin, h, options);
return distancePH;

@@ -202,6 +200,8 @@ }

* @param {Object} to second point
* @param {string} [units='kilometers'] can be degrees, radians, miles, or kilometers
* @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, units) {
function euclideanDistance(from, to, options) {
var units = options.units;
// translate points if any is crossing the 180th meridian

@@ -221,50 +221,5 @@ var delta = 0;

var d = Math.sqrt(squareD);
return convertDistance(d, 'meters', units);
return convertLength(d, 'meters', units);
}
/**
* Convert lon/lat values to 900913 x/y.
* from https://github.com/mapbox/sphericalmercator
*
* @private
* @param {Array<number>} lonLat WGS84 point
* @returns {Array<number>} Mercator [x, y] point
*/
function toMercator(lonLat) {
var D2R = Math.PI / 180,
// 900913 properties.
A = 6378137.0,
MAXEXTENT = 20037508.342789244;
var xy = [
A * lonLat[0] * D2R,
A * Math.log(Math.tan((Math.PI * 0.25) + (0.5 * lonLat[1] * D2R)))
];
// if xy value is beyond maxextent (e.g. poles), return maxextent.
if (xy[0] > MAXEXTENT) xy[0] = MAXEXTENT;
if (xy[0] < -MAXEXTENT) xy[0] = -MAXEXTENT;
if (xy[1] > MAXEXTENT) xy[1] = MAXEXTENT;
if (xy[1] < -MAXEXTENT) xy[1] = -MAXEXTENT;
return xy;
}
/**
* Convert 900913 x/y values to lon/lat.
* from https://github.com/mapbox/sphericalmercator
*
* @private
* @param {Array<number>} xy Mercator [x, y] point
* @returns {Array<number>} WGS84 [lon, lat] point
*/
function toWGS84(xy) {
// 900913 properties.
var R2D = 180 / Math.PI,
A = 6378137.0;
return [
(xy[0] * R2D / A),
((Math.PI * 0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D
];
}
export default pointToLineDistance;

@@ -9,2 +9,3 @@ 'use strict';

var rhumbDistance = _interopDefault(require('@turf/rhumb-distance'));
var projection = require('@turf/projection');
var invariant = require('@turf/invariant');

@@ -22,4 +23,4 @@ var meta = require('@turf/meta');

* @name pointToLineDistance
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Feature<LineString>|Array<Array<number>>} line GeoJSON Feature or Geometry
* @param {Geometry|Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Geometry|Feature<LineString>|Array<Array<number>>} line GeoJSON Feature or Geometry
* @param {Object} [options={}] Optional parameters

@@ -33,13 +34,11 @@ * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers

*
* var distance = turf.pointToLineDistance(pt, line, 'miles');
* var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
* //=69.11854715938406
*/
function pointToLineDistance(pt, line, options) {
// Optional params
// Optional parameters
options = options || {};
// var units = options.units;
// var mercator = options.mercator;
if (!helpers.isObject(options)) throw new Error('options is invalid');
// validation
if (typeof options !== 'object') throw new Error('options must be an object');
if (!pt) throw new Error('pt is required');

@@ -74,3 +73,3 @@ if (Array.isArray(pt)) pt = helpers.point(pt);

* @param {Array<number>} b second segment point
* @param {Object} [options] Optional parameters
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers

@@ -81,9 +80,6 @@ * @param {boolean} [options.mercator=false] if distance should be on Mercator or WGS84 projection

function distanceToSegment(p, a, b, options) {
options = options || {};
var units = options.units;
var mercator = options.mercator;
var distanceAP = (mercator !== true) ? distance(a, p, units) : euclideanDistance(a, p, units);
var azimuthAP = helpers.bearingToAngle((mercator !== true) ? bearing(a, p) : rhumbBearing(a, p));
var azimuthAB = helpers.bearingToAngle((mercator !== true) ? bearing(a, b) : rhumbBearing(a, b));
var distanceAP = (mercator !== true) ? distance(a, p, options) : euclideanDistance(a, p, options);
var azimuthAP = helpers.bearingToAzimuth((mercator !== true) ? bearing(a, p) : rhumbBearing(a, p));
var azimuthAB = helpers.bearingToAzimuth((mercator !== true) ? bearing(a, b) : rhumbBearing(a, b));
var angleA = Math.abs(azimuthAP - azimuthAB);

@@ -104,3 +100,3 @@ // if (angleA > 180) angleA = Math.abs(angleA - 360);

var azimuthBA = (azimuthAB + 180) % 360;
var azimuthBP = helpers.bearingToAngle((mercator !== true) ? bearing(b, p) : rhumbBearing(b, p));
var azimuthBP = helpers.bearingToAzimuth((mercator !== true) ? bearing(b, p) : rhumbBearing(b, p));
var angleB = Math.abs(azimuthBP - azimuthBA);

@@ -118,3 +114,3 @@ if (angleB > 180) angleB = Math.abs(angleB - 360);

*/
if (angleB > 90) return (mercator !== true) ? distance(p, b, units) : euclideanDistance(p, b, units);
if (angleB > 90) return (mercator !== true) ? distance(p, b, options) : euclideanDistance(p, b, options);
// finally if the projection falls inside the segment

@@ -131,4 +127,4 @@ // return the distance between P and the segment

*/
if (mercator !== true) return distanceAP * Math.sin(helpers.degrees2radians(angleA));
return mercatorPH(a, b, p, units);
if (mercator !== true) return distanceAP * Math.sin(helpers.degreesToRadians(angleA));
return mercatorPH(a, b, p, options);
}

@@ -143,6 +139,7 @@

* @param {Array<number>} p external point
* @param {string} [units='kilometers'] can be degrees, radians, miles, or kilometers
* @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, units) {
function mercatorPH(a, b, p, options) {
var delta = 0;

@@ -155,9 +152,9 @@ // translate points if any is crossing the 180th meridian

var origin = helpers.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));
var A = projection.toMercator([a[0] + delta, a[1]]);
var B = projection.toMercator([b[0] + delta, b[1]]);
var P = projection.toMercator([p[0] + delta, p[1]]);
var h = projection.toWgs84(euclideanIntersection(A, B, P));
if (delta !== 0) h[0] -= delta; // translate back to original position
var distancePH = rhumbDistance(origin, h, units);
var distancePH = rhumbDistance(origin, h, options);
return distancePH;

@@ -198,6 +195,8 @@ }

* @param {Object} to second point
* @param {string} [units='kilometers'] can be degrees, radians, miles, or kilometers
* @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, units) {
function euclideanDistance(from, to, options) {
var units = options.units;
// translate points if any is crossing the 180th meridian

@@ -211,4 +210,4 @@ var delta = 0;

}
var p1 = toMercator([from[0] + delta, from[1]]);
var p2 = toMercator([to[0] + delta, to[1]]);
var p1 = projection.toMercator([from[0] + delta, from[1]]);
var p2 = projection.toMercator([to[0] + delta, to[1]]);

@@ -218,51 +217,6 @@ var sqr = function (n) { return n * n; };

var d = Math.sqrt(squareD);
return helpers.convertDistance(d, 'meters', units);
return helpers.convertLength(d, 'meters', units);
}
/**
* Convert lon/lat values to 900913 x/y.
* from https://github.com/mapbox/sphericalmercator
*
* @private
* @param {Array<number>} lonLat WGS84 point
* @returns {Array<number>} Mercator [x, y] point
*/
function toMercator(lonLat) {
var D2R = Math.PI / 180,
// 900913 properties.
A = 6378137.0,
MAXEXTENT = 20037508.342789244;
var xy = [
A * lonLat[0] * D2R,
A * Math.log(Math.tan((Math.PI * 0.25) + (0.5 * lonLat[1] * D2R)))
];
// if xy value is beyond maxextent (e.g. poles), return maxextent.
if (xy[0] > MAXEXTENT) xy[0] = MAXEXTENT;
if (xy[0] < -MAXEXTENT) xy[0] = -MAXEXTENT;
if (xy[1] > MAXEXTENT) xy[1] = MAXEXTENT;
if (xy[1] < -MAXEXTENT) xy[1] = -MAXEXTENT;
return xy;
}
/**
* Convert 900913 x/y values to lon/lat.
* from https://github.com/mapbox/sphericalmercator
*
* @private
* @param {Array<number>} xy Mercator [x, y] point
* @returns {Array<number>} WGS84 [lon, lat] point
*/
function toWGS84(xy) {
// 900913 properties.
var R2D = 180 / Math.PI,
A = 6378137.0;
return [
(xy[0] * R2D / A),
((Math.PI * 0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D
];
}
module.exports = pointToLineDistance;
module.exports.default = pointToLineDistance;
{
"name": "@turf/point-to-line-distance",
"version": "5.0.0",
"version": "5.0.4",
"description": "turf point-to-line-distance module",

@@ -17,3 +17,2 @@ "main": "main",

"test": "node -r @std/esm test.js",
"posttest": "uglifyjs main.js -o main.min.js",
"bench": "node -r @std/esm bench.js"

@@ -40,19 +39,19 @@ },

"devDependencies": {
"@turf/circle": "*",
"@std/esm": "*",
"@turf/circle": "^5.0.4",
"benchmark": "*",
"load-json-file": "*",
"rollup": "*",
"tape": "*",
"write-json-file": "*",
"rollup": "*",
"@std/esm": "*",
"uglify-js": "*"
"write-json-file": "*"
},
"dependencies": {
"@turf/bearing": "5.0.0",
"@turf/distance": "5.0.0",
"@turf/helpers": "5.0.0",
"@turf/invariant": "5.0.0",
"@turf/meta": "5.0.0",
"@turf/rhumb-bearing": "5.0.0",
"@turf/rhumb-distance": "5.0.0"
"@turf/bearing": "^5.0.4",
"@turf/distance": "^5.0.4",
"@turf/helpers": "^5.0.4",
"@turf/invariant": "^5.0.4",
"@turf/meta": "^5.0.4",
"@turf/projection": "^5.0.4",
"@turf/rhumb-bearing": "^5.0.4",
"@turf/rhumb-distance": "^5.0.4"
},

@@ -59,0 +58,0 @@ "@std/esm": {

@@ -12,5 +12,5 @@ # @turf/point-to-line-distance

- `pt` **([Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;[Point](http://geojson.org/geojson-spec.html#point)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>)** Feature or Geometry
- `line` **([Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;[LineString](http://geojson.org/geojson-spec.html#linestring)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>)** GeoJSON Feature or Geometry
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Optional parameters
- `pt` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;[Point](http://geojson.org/geojson-spec.html#point)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>)** Feature or Geometry
- `line` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;[LineString](http://geojson.org/geojson-spec.html#linestring)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>)** 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'`)

@@ -25,3 +25,3 @@ - `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`)

var distance = turf.pointToLineDistance(pt, line, 'miles');
var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
//=69.11854715938406

@@ -28,0 +28,0 @@ ```

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