Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function convertDistance @turf/helpers; #732

Merged
merged 3 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/turf-helpers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
# radiansToDistance

Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet

**Parameters**

Expand All @@ -260,6 +261,7 @@ Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
# distanceToRadians

Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet

**Parameters**

Expand All @@ -271,6 +273,7 @@ Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
# distanceToDegrees

Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet

**Parameters**

Expand Down Expand Up @@ -310,6 +313,19 @@ Converts an angle in degrees to radians

Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** angle in radians

# convertDistance

Converts a distance to the requested unit.
Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet

**Parameters**

- `distance` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** to be converted
- `originalUnit` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** of the distance
- `finalUnit` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** returned unit (optional, default `kilometers`)

Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the converted distance

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->
Expand Down
27 changes: 27 additions & 0 deletions packages/turf-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ var factors = {
yards: 6969600,
meters: 6373000,
metres: 6373000,
centimeters: 6.373e+8,
centimetres: 6.373e+8,
kilometers: 6373,
kilometres: 6373,
feet: 20908792.65
Expand All @@ -283,12 +285,15 @@ var factors = {
* //=120.43
*/
function round(num, precision) {
if (num === undefined || num === null || isNaN(num)) throw new Error('num is required');
if (precision && !(precision >= 0)) throw new Error('precision must be a positive number');
var multiplier = Math.pow(10, precision || 0);
return Math.round(num * multiplier) / multiplier;
}

/**
* Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
*
* @name radiansToDistance
* @param {number} radians in radians across the sphere
Expand All @@ -305,6 +310,7 @@ function radiansToDistance(radians, units) {

/**
* Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
*
* @name distanceToRadians
* @param {number} distance in real units
Expand All @@ -321,6 +327,7 @@ function distanceToRadians(distance, units) {

/**
* Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet
*
* @name distanceToDegrees
* @param {number} distance in real units
Expand Down Expand Up @@ -375,6 +382,25 @@ function degrees2radians(degrees) {
return radians * Math.PI / 180;
}


/**
* Converts a distance to the requested unit.
* Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
*
* @param {number} distance to be converted
* @param {string} originalUnit of the distance
* @param {string} [finalUnit=kilometers] returned unit
* @returns {number} the converted distance
*/
function convertDistance(distance, originalUnit, finalUnit) {
if (distance === null || distance === undefined) throw new Error('distance is required');
if (!(distance >= 0)) throw new Error('distance must be a positive number');

var convertedDistance = radiansToDistance(distanceToRadians(distance, originalUnit), finalUnit || 'kilometers');
return convertedDistance;
}


module.exports = {
feature: feature,
featureCollection: featureCollection,
Expand All @@ -391,5 +417,6 @@ module.exports = {
radians2degrees: radians2degrees,
degrees2radians: degrees2radians,
bearingToAngle: bearingToAngle,
convertDistance: convertDistance,
round: round
};
5 changes: 5 additions & 0 deletions packages/turf-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"geojson"
],
"author": "Turf Authors",
"contributors": [
"Tom MacWright <@tmcw>",
"Stefano Borghi <@stebogit>",
"Denis Carriere <@DenisCarriere>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
Expand Down
23 changes: 20 additions & 3 deletions packages/turf-helpers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
radians2degrees,
degrees2radians,
bearingToAngle,
convertDistance,
round
} = require('./');

Expand Down Expand Up @@ -317,19 +318,25 @@ test('distanceToRadians', t => {
t.end();
});

test('distanceToDegrees', t => {
t.equal(distanceToDegrees(1, 'radians'), 57.29577951308232);
t.equal(distanceToDegrees(100, 'kilometers'), 0.8990393772647469);
t.equal(distanceToDegrees(10, 'miles'), 0.14468631190172304);
t.throws(() => distanceToRadians(1, 'foo'), 'invalid units');
t.end();
});

test('radians2degrees', t => {
t.equal(round(radians2degrees(Math.PI / 3), 6), 60, 'radiance conversion PI/3');
t.equal(radians2degrees(3.5 * Math.PI), 270, 'radiance conversion 3.5PI');
t.equal(radians2degrees(-Math.PI), -180, 'radiance conversion -PI');

t.end();
});

test('radians2degrees', t => {
t.equal(degrees2radians(60), Math.PI / 3, 'degrees conversion 60');
t.equal(degrees2radians(270), 1.5 * Math.PI, 'degrees conversion 270');
t.equal(degrees2radians(-180), -Math.PI, 'degrees conversion -180');

t.end();
});

Expand All @@ -339,14 +346,24 @@ test('bearingToAngle', t => {
t.equal(bearingToAngle(410), 50);
t.equal(bearingToAngle(-200), 160);
t.equal(bearingToAngle(-395), 325);

t.end();
});

test('round', t => {
t.equal(round(125.123), 125);
t.equal(round(123.123, 1), 123.1);
t.equal(round(123.5), 124);
t.throws(() => round(34.5, 'precision'), 'invalid precision');
t.throws(() => round(34.5, -5), 'invalid precision');
t.end();
});

test('convertDistance', t => {
t.equal(convertDistance(1000, 'meters'), 1);
t.equal(convertDistance(1, 'kilometers', 'miles'), 0.6213714106386318);
t.equal(convertDistance(1, 'miles', 'kilometers'), 1.6093434343434343);
t.equal(convertDistance(1, 'nauticalmiles'), 1.851999843075488);
t.equal(convertDistance(1, 'meters', 'centimeters'), 100);
t.throws(() => convertDistance(1, 'foo'), 'invalid units');
t.end();
});