42 lines
1.8 KiB
JavaScript
Executable File
42 lines
1.8 KiB
JavaScript
Executable File
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var invariant_1 = require("@turf/invariant");
|
|
var helpers_1 = require("@turf/helpers");
|
|
//http://en.wikipedia.org/wiki/Haversine_formula
|
|
//http://www.movable-type.co.uk/scripts/latlong.html
|
|
/**
|
|
* Calculates the distance between two {@link Point|points} in degrees, radians, miles, or kilometers.
|
|
* This uses the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) to account for global curvature.
|
|
*
|
|
* @name distance
|
|
* @param {Coord | Point} from origin point or coordinate
|
|
* @param {Coord | Point} to destination point or coordinate
|
|
* @param {Object} [options={}] Optional parameters
|
|
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
|
|
* @returns {number} distance between the two points
|
|
* @example
|
|
* var from = turf.point([-75.343, 39.984]);
|
|
* var to = turf.point([-75.534, 39.123]);
|
|
* var options = {units: 'miles'};
|
|
*
|
|
* var distance = turf.distance(from, to, options);
|
|
*
|
|
* //addToMap
|
|
* var addToMap = [from, to];
|
|
* from.properties.distance = distance;
|
|
* to.properties.distance = distance;
|
|
*/
|
|
function distance(from, to, options) {
|
|
if (options === void 0) { options = {}; }
|
|
var coordinates1 = invariant_1.getCoord(from);
|
|
var coordinates2 = invariant_1.getCoord(to);
|
|
var dLat = helpers_1.degreesToRadians(coordinates2[1] - coordinates1[1]);
|
|
var dLon = helpers_1.degreesToRadians(coordinates2[0] - coordinates1[0]);
|
|
var lat1 = helpers_1.degreesToRadians(coordinates1[1]);
|
|
var lat2 = helpers_1.degreesToRadians(coordinates2[1]);
|
|
var a = Math.pow(Math.sin(dLat / 2), 2) +
|
|
Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
return helpers_1.radiansToLength(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), options.units);
|
|
}
|
|
exports.default = distance;
|