1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var invariant_1 = require("@turf/invariant");
|
4 | var helpers_1 = require("@turf/helpers");
|
5 | //http://en.wikipedia.org/wiki/Haversine_formula
|
6 | //http://www.movable-type.co.uk/scripts/latlong.html
|
7 | /**
|
8 | * Calculates the distance between two {@link Point|points} in degrees, radians, miles, or kilometers.
|
9 | * This uses the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) to account for global curvature.
|
10 | *
|
11 | * @name distance
|
12 | * @param {Coord | Point} from origin point or coordinate
|
13 | * @param {Coord | Point} to destination point or coordinate
|
14 | * @param {Object} [options={}] Optional parameters
|
15 | * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
|
16 | * @returns {number} distance between the two points
|
17 | * @example
|
18 | * var from = turf.point([-75.343, 39.984]);
|
19 | * var to = turf.point([-75.534, 39.123]);
|
20 | * var options = {units: 'miles'};
|
21 | *
|
22 | * var distance = turf.distance(from, to, options);
|
23 | *
|
24 | * //addToMap
|
25 | * var addToMap = [from, to];
|
26 | * from.properties.distance = distance;
|
27 | * to.properties.distance = distance;
|
28 | */
|
29 | function distance(from, to, options) {
|
30 | if (options === void 0) { options = {}; }
|
31 | var coordinates1 = invariant_1.getCoord(from);
|
32 | var coordinates2 = invariant_1.getCoord(to);
|
33 | var dLat = helpers_1.degreesToRadians(coordinates2[1] - coordinates1[1]);
|
34 | var dLon = helpers_1.degreesToRadians(coordinates2[0] - coordinates1[0]);
|
35 | var lat1 = helpers_1.degreesToRadians(coordinates1[1]);
|
36 | var lat2 = helpers_1.degreesToRadians(coordinates2[1]);
|
37 | var a = Math.pow(Math.sin(dLat / 2), 2) +
|
38 | Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
|
39 | return helpers_1.radiansToLength(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), options.units);
|
40 | }
|
41 | exports.default = distance;
|