Socket
Socket
Sign inDemoInstall

turf-distance

Package Overview
Dependencies
Maintainers
9
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-distance - npm Package Compare versions

Comparing version 1.1.0 to 3.0.0-canary.2f5f7167

8

bench.js

@@ -6,8 +6,8 @@ var distance = require('./');

var pt1 = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.4, 39.4]}
type: "Feature",
geometry: {type: "Point", coordinates: [-75.4, 39.4]}
};
var pt2 = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.534, 39.123]}
type: "Feature",
geometry: {type: "Point", coordinates: [-75.534, 39.123]}
};

@@ -14,0 +14,0 @@

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

var invariant = require('turf-invariant');
var getCoord = require('turf-invariant').getCoord;
var radiansToDistance = require('turf-helpers').radiansToDistance;
//http://en.wikipedia.org/wiki/Haversine_formula

@@ -6,3 +7,3 @@ //http://www.movable-type.co.uk/scripts/latlong.html

/**
* Calculates the distance between two {@link Point|points} in degress, radians,
* Calculates the distance between two {@link Point|points} in degrees, radians,
* miles, or kilometers. This uses the

@@ -12,3 +13,3 @@ * [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula)

*
* @module turf/distance
* @name distance
* @category measurement

@@ -49,45 +50,15 @@ * @param {Feature<Point>} from origin point

*/
module.exports = function(point1, point2, units) {
invariant.featureOf(point1, 'Point', 'distance');
invariant.featureOf(point2, 'Point', 'distance');
var coordinates1 = point1.geometry.coordinates;
var coordinates2 = point2.geometry.coordinates;
module.exports = function (point1, point2, units) {
var degrees2radians = Math.PI / 180;
var coordinates1 = getCoord(point1);
var coordinates2 = getCoord(point2);
var dLat = degrees2radians * (coordinates2[1] - coordinates1[1]);
var dLon = degrees2radians * (coordinates2[0] - coordinates1[0]);
var lat1 = degrees2radians * coordinates1[1];
var lat2 = degrees2radians * coordinates2[1];
var dLat = toRad(coordinates2[1] - coordinates1[1]);
var dLon = toRad(coordinates2[0] - coordinates1[0]);
var lat1 = toRad(coordinates1[1]);
var lat2 = toRad(coordinates2[1]);
var a = Math.pow(Math.sin(dLat / 2), 2) +
Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
var a = Math.pow(Math.sin(dLat/2), 2) +
Math.pow(Math.sin(dLon/2), 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var R;
switch(units) {
case 'miles':
R = 3960;
break;
case 'kilometers':
case 'kilometres':
R = 6373;
break;
case 'degrees':
R = 57.2957795;
break;
case 'radians':
R = 1;
break;
case undefined:
R = 6373;
break;
default:
throw new Error('unknown option given to "units"');
}
var distance = R * c;
return distance;
return radiansToDistance(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), units);
};
function toRad(degree) {
return degree * Math.PI / 180;
}
{
"name": "turf-distance",
"version": "1.1.0",
"version": "3.0.0-canary.2f5f7167",
"description": "turf distance module",
"main": "index.js",
"scripts": {
"test": "tape test.js",
"doc": "dox -r < index.js | doxme --readme > README.md"
"test": "tape test.js"
},

@@ -28,9 +27,8 @@ "repository": {

"benchmark": "^1.0.0",
"tape": "^3.5.0",
"dox": "^0.6.1",
"doxme": "^1.4.3"
"tape": "^3.5.0"
},
"dependencies": {
"turf-invariant": "^1.0.2"
"turf-invariant": "^3.0.0-canary.2f5f7167",
"turf-helpers": "^3.0.0-canary.2f5f7167"
}
}

@@ -10,3 +10,3 @@ # turf-distance

Calculates the distance between two Point|points in degress, radians,
Calculates the distance between two Point|points in degrees, radians,
miles, or kilometers. This uses the

@@ -13,0 +13,0 @@ [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula)

@@ -6,11 +6,12 @@ var test = require('tape');

var pt1 = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}
type: "Feature",
geometry: {type: "Point", coordinates: [-75.343, 39.984]}
};
var pt2 = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.534, 39.123]}
type: "Feature",
geometry: {type: "Point", coordinates: [-75.534, 39.123]}
};
t.equal(distance(pt1, pt2, 'miles'), 60.37218405837491, 'miles');
t.equal(distance(pt1, pt2, 'nauticalmiles'), 52.461979624130436, 'miles');
t.equal(distance(pt1, pt2, 'kilometers'), 97.15957803131901, 'kilometers');

@@ -17,0 +18,0 @@ t.equal(distance(pt1, pt2, 'kilometres'), 97.15957803131901, 'kilometres');

Sorry, the diff of this file is not supported yet

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