Comparing version 0.3.2 to 0.4.0
@@ -23,2 +23,31 @@ /* | ||
function toDeg(number) { | ||
return number * 180 / Math.PI; | ||
} | ||
/** | ||
* Inspired by: https://stackoverflow.com/questions/33624968/generate-coordinates-in-between-two-known-points | ||
* | ||
* Get bearing from 2 geo points. | ||
* @param {array} startPoint [lat, lon]. | ||
* @param {array} endPoint [lat, lon]. | ||
* | ||
* @returns {Distance} distance - Object with the response. | ||
*/ | ||
function getBearing(startPoint, endPoint) { | ||
const startLat = toRad(startPoint[0]); | ||
const startLong = toRad(startPoint[1]); | ||
const endLat = toRad(endPoint[0]); | ||
const endLong = toRad(endPoint[1]); | ||
let dLong = endLong - startLong; | ||
const dPhi = Math.log(Math.tan(endLat / 2.0 + Math.PI / 4.0) / | ||
Math.tan(startLat / 2.0 + Math.PI / 4.0)); | ||
if (Math.abs(dLong) > Math.PI) { | ||
dLong = (dLong > 0.0) ? -(2.0 * Math.PI - dLong) : (2.0 * Math.PI + dLong); | ||
} | ||
return (toDeg(Math.atan2(dLong, dPhi)) + 360.0) % 360.0; | ||
} | ||
/* | ||
@@ -111,2 +140,6 @@ The "Earth radius" R varies from 6356.752 km at the poles to 6378.137 km at the equator. More importantly, the radius | ||
} | ||
const startPoint = [opt.p1.lat, opt.p1.lon]; | ||
const endPoint = [opt.p2.lat, opt.p2.lon]; | ||
// -------------------------------------------------------------------------------- | ||
@@ -126,6 +159,10 @@ // Start the magic. | ||
} | ||
const bearing = getBearing(startPoint, endPoint); | ||
let result = { | ||
distance: parseFloat(distance), // 1447.35, | ||
unit: 'km', | ||
method: method | ||
method: method, | ||
bearing: bearing | ||
}; | ||
@@ -145,2 +182,60 @@ if (opt.timeUsed) { | ||
/** | ||
* Inspired by: https://stackoverflow.com/questions/33624968/generate-coordinates-in-between-two-known-points | ||
* @param {Array} coord Expected [lat, lon] | ||
* @param {number} bearing Bearing in degrees | ||
* @param {number} distance Distance in meters | ||
* @return {Array} Lat-lon coordinate. | ||
*/ | ||
GeoLib.prototype.createCoord = function createCoord(coord, bearing, distance) { | ||
/** | ||
* http://www.movable-type.co.uk/scripts/latlong.html | ||
* φ is latitude, λ is longitude, | ||
* θ is the bearing (clockwise from north), | ||
* δ is the angular distance d/R; | ||
* d being the distance travelled, R the earth’s radius* | ||
**/ | ||
const radius = 6371000; // earth radius in meters | ||
const δ = Number(distance) / radius; // angular distance in radians | ||
const θ = toRad(Number(bearing)); | ||
const φ1 = toRad(coord[0]); | ||
const λ1 = toRad(coord[1]); | ||
const φ2 = Math.asin(Math.sin(φ1) * Math.cos(δ) + | ||
Math.cos(φ1) * Math.sin(δ) * Math.cos(θ)); | ||
let λ2 = λ1 + Math.atan2(Math.sin(θ) * Math.sin(δ) * Math.cos(φ1), | ||
Math.cos(δ) - Math.sin(φ1) * Math.sin(φ2)); | ||
// normalise to -180..+180° | ||
λ2 = (λ2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI; | ||
return [toDeg(φ2), toDeg(λ2)]; | ||
}; | ||
/** | ||
* Inspired by: https://stackoverflow.com/questions/33624968/generate-coordinates-in-between-two-known-points | ||
* @param {Array} p1 Expected [lat, lon] | ||
* @param {Array} p2 Expected [lat, lon] | ||
* @param {number} distanceInterval of new points | ||
* @return {Array} lat-lon coordinates. | ||
*/ | ||
GeoLib.prototype.generatePoints = function generatePoints(p1, p2, distanceInterval = 100) { | ||
const result = GeoLib.prototype.distance([p1, p2]); | ||
const distanceInMeters = result.distance * 1000; | ||
const numberOfPoints = Math.floor(distanceInMeters / distanceInterval); | ||
const newPoints = []; | ||
for (let i = 1, l = numberOfPoints; i <= l; i += 1) { | ||
const newPoint = GeoLib.prototype.createCoord(p1, result.bearing, i * distanceInterval); | ||
newPoints.push(newPoint); | ||
} | ||
// { | ||
// distance: 39.53509158750874, | ||
// unit: 'km', | ||
// method: 'haversine', | ||
// bearing: 4.924503268005935 | ||
// } | ||
// console.log({ result, distanceInMeters, distanceInterval, numberOfPoints }); | ||
return newPoints; | ||
}; | ||
/* | ||
@@ -147,0 +242,0 @@ Inspired by: |
{ | ||
"name": "geo-lib", | ||
"description": "A Node.js module with useful geo functions. Made with performance in mind.", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/5orenso/geo-lib", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -33,6 +33,7 @@ # Geo-lib module for fast calculations of distance, speed and heading | ||
}); | ||
// result = { | ||
// distance: 1468.28, | ||
// { | ||
// distance: 1468.2753298955777, | ||
// unit: 'km', | ||
// method: 'haversine' | ||
// method: 'haversine', | ||
// bearing: 218.03212341269622 | ||
// } | ||
@@ -48,6 +49,7 @@ ``` | ||
]); | ||
// result = { | ||
// distance: 1468.28, | ||
// { | ||
// distance: 1468.2753298955777, | ||
// unit: 'km', | ||
// method: 'haversine' | ||
// method: 'haversine', | ||
// bearing: 218.03212341269622 | ||
// } | ||
@@ -64,13 +66,57 @@ ``` | ||
}); | ||
// result = { | ||
// distance: 1468.28, | ||
// { | ||
// distance: 1468.2753298955777, | ||
// unit: 'km', | ||
// method: 'haversine', | ||
// speedKph: 12.24, | ||
// speedMph: 7.61, | ||
// speedMpk: '4:54', | ||
// bearing: 218.03212341269622, | ||
// timeUsedInSeconds: 432000, | ||
// unit: 'km' | ||
// speedKph: 12.235627749129813, | ||
// speedMph: 7.602864250104541, | ||
// speedMpk: '4:54.22274637735927' | ||
// } | ||
``` | ||
Create new point in distance and bearing from a given point: | ||
```javascript | ||
let geoLib = require('geo-lib'); | ||
const result = geoLib.createCoord([70.000, 30.000], 360, 10000); | ||
console.log(result); | ||
// [ 70.08993216059186, 29.999999999999964 ] | ||
``` | ||
Create new points between 2 given points: | ||
```javascript | ||
let geoLib = require('geo-lib'); | ||
const result = geoLib.generatePoints([70.000, 30.000], [70.010, 30.010]); | ||
console.log(result); | ||
// [ | ||
// [ 70.00085094723158, 30.000850779931362 ], | ||
// [ 70.00170189040277, 30.001701629300936 ], | ||
// [ 70.002552829513, 30.00255254811713 ], | ||
// [ 70.00340376456182, 30.0034035363886 ], | ||
// [ 70.00425469554865, 30.004254594123793 ], | ||
// [ 70.00510562247304, 30.00510572133135 ], | ||
// [ 70.00595654533441, 30.005956918019837 ], | ||
// [ 70.00680746413225, 30.006808184197695 ], | ||
// [ 70.00765837886604, 30.00765951987357 ], | ||
// [ 70.00850928953531, 30.00851092505601 ], | ||
// [ 70.00936019613945, 30.009362399753574 ] | ||
// ] | ||
``` | ||
Create new points between 2 given points with fixed distance: | ||
```javascript | ||
let geoLib = require('geo-lib'); | ||
const result = geoLib.generatePoints([70.000, 30.000], [70.010, 30.010], 300); | ||
console.log(result); | ||
// [ | ||
// [ 70.002552829513, 30.00255254811713 ], | ||
// [ 70.00510562247304, 30.00510572133135 ], | ||
// [ 70.00765837886604, 30.00765951987357 ] | ||
// ] | ||
``` | ||
To check if a point is inside a polygon: | ||
@@ -94,15 +140,5 @@ ```javascript | ||
]); | ||
// result = true | ||
// true | ||
``` | ||
To check if 2 lines intersects: | ||
```javascript | ||
let geoLib = require('geo-lib'); | ||
let result = geoLib.linesIntersect( | ||
{lat: 59.75639, lon: 6.67968}, {lat: 61.15383, lon: 11.87622}, | ||
{lat: 61.51745, lon: 8.15185}, {lat: 59.75086, lon: 11.1621} | ||
); | ||
// result = true | ||
``` | ||
To check if 2 polygons overlaps: | ||
@@ -120,5 +156,14 @@ ```javascript | ||
]); | ||
// result = true | ||
// true | ||
``` | ||
To check if 2 lines intersects: | ||
```javascript | ||
let geoLib = require('geo-lib'); | ||
let result = geoLib.linesIntersect( | ||
{lat: 59.75639, lon: 6.67968}, {lat: 61.15383, lon: 11.87622}, | ||
{lat: 61.51745, lon: 8.15185}, {lat: 59.75086, lon: 11.1621} | ||
); | ||
// true | ||
``` | ||
@@ -165,6 +210,6 @@ ---------- | ||
# Install Node Security Platform CLI | ||
$ npm install nsp --global | ||
$ npm install nsp --global | ||
# From inside your project directory | ||
$ nsp check | ||
$ nsp check | ||
``` | ||
@@ -171,0 +216,0 @@ |
@@ -29,3 +29,4 @@ /* | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -35,4 +36,3 @@ method: 'haversine' | ||
} | ||
} | ||
}); |
@@ -28,3 +28,4 @@ /* | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -42,3 +43,4 @@ method: 'haversine' | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -56,3 +58,4 @@ method: 'haversine' | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -70,3 +73,4 @@ method: 'haversine' | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -84,3 +88,4 @@ method: 'haversine' | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
unit: 'km', | ||
@@ -99,3 +104,4 @@ method: 'haversine' | ||
assert.equals(result, { | ||
distance: 1472.86, | ||
bearing: 218.03212341269622, | ||
distance: 1472.85899, | ||
unit: 'km', | ||
@@ -114,7 +120,8 @@ method: 'vincenty' | ||
assert.equals(result, { | ||
distance: 1468.28, | ||
bearing: 218.03212341269622, | ||
distance: 1468.2753298955777, | ||
method: 'haversine', | ||
speedKph: 12.24, | ||
speedMph: 7.61, | ||
speedMpk: '4:54', | ||
speedKph: 12.235627749129813, | ||
speedMph: 7.602864250104541, | ||
speedMpk: '4:54.22274637735927', | ||
timeUsedInSeconds: 432000, | ||
@@ -151,2 +158,36 @@ unit: 'km' | ||
'Create coordinates between points': { | ||
'createCoord 1 point from p1 directly north for 10 km': () => { | ||
let myapp = require(appPath + 'lib/geo-lib'); | ||
let result = myapp.createCoord([70.000, 30.000], 360, 10000); | ||
assert.equals(result, [70.08993216059186, 29.999999999999964]); | ||
}, | ||
'generatePoints points between p1 and p2': () => { | ||
let myapp = require(appPath + 'lib/geo-lib'); | ||
let result = myapp.generatePoints([70.000, 30.000], [70.010, 30.010]); | ||
assert.equals(result, [ | ||
[70.00085094723158, 30.000850779931362], | ||
[70.00170189040277, 30.001701629300936], | ||
[70.002552829513, 30.00255254811713], | ||
[70.00340376456182, 30.0034035363886], | ||
[70.00425469554865, 30.004254594123793], | ||
[70.00510562247304, 30.00510572133135], | ||
[70.00595654533441, 30.005956918019837], | ||
[70.00680746413225, 30.006808184197695], | ||
[70.00765837886604, 30.00765951987357], | ||
[70.00850928953531, 30.00851092505601], | ||
[70.00936019613945, 30.009362399753574] | ||
]); | ||
}, | ||
'generatePoints points between p1 and p2 with distance 300 meters': () => { | ||
let myapp = require(appPath + 'lib/geo-lib'); | ||
let result = myapp.generatePoints([70.000, 30.000], [70.010, 30.010], 300); | ||
assert.equals(result, [ | ||
[70.002552829513, 30.00255254811713], | ||
[70.00510562247304, 30.00510572133135], | ||
[70.00765837886604, 30.00765951987357] | ||
]); | ||
} | ||
}, | ||
'Points and polygons': { | ||
@@ -153,0 +194,0 @@ 'isPointInPoly where point is inside poly': () => { |
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
102563
37
1885
245
65