123456789101112131415161718192021222324252627282930313233343536373839 |
- import { getCoord } from "@turf/invariant";
- import { radiansToLength, degreesToRadians, } from "@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 = getCoord(from);
- var coordinates2 = getCoord(to);
- var dLat = degreesToRadians(coordinates2[1] - coordinates1[1]);
- var dLon = degreesToRadians(coordinates2[0] - coordinates1[0]);
- var lat1 = degreesToRadians(coordinates1[1]);
- var lat2 = 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 radiansToLength(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), options.units);
- }
- export default distance;
|