index.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. // https://en.wikipedia.org/wiki/Rhumb_line
  4. var helpers_1 = require("@turf/helpers");
  5. var invariant_1 = require("@turf/invariant");
  6. /**
  7. * Calculates the distance along a rhumb line between two {@link Point|points} in degrees, radians,
  8. * miles, or kilometers.
  9. *
  10. * @name rhumbDistance
  11. * @param {Coord} from origin point
  12. * @param {Coord} to destination point
  13. * @param {Object} [options] Optional parameters
  14. * @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers
  15. * @returns {number} distance between the two points
  16. * @example
  17. * var from = turf.point([-75.343, 39.984]);
  18. * var to = turf.point([-75.534, 39.123]);
  19. * var options = {units: 'miles'};
  20. *
  21. * var distance = turf.rhumbDistance(from, to, options);
  22. *
  23. * //addToMap
  24. * var addToMap = [from, to];
  25. * from.properties.distance = distance;
  26. * to.properties.distance = distance;
  27. */
  28. function rhumbDistance(from, to, options) {
  29. if (options === void 0) { options = {}; }
  30. var origin = invariant_1.getCoord(from);
  31. var destination = invariant_1.getCoord(to);
  32. // compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
  33. // solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
  34. destination[0] +=
  35. destination[0] - origin[0] > 180
  36. ? -360
  37. : origin[0] - destination[0] > 180
  38. ? 360
  39. : 0;
  40. var distanceInMeters = calculateRhumbDistance(origin, destination);
  41. var distance = helpers_1.convertLength(distanceInMeters, "meters", options.units);
  42. return distance;
  43. }
  44. /**
  45. * Returns the distance travelling from ‘this’ point to destination point along a rhumb line.
  46. * Adapted from Geodesy: https://github.com/chrisveness/geodesy/blob/master/latlon-spherical.js
  47. *
  48. * @private
  49. * @param {Array<number>} origin point.
  50. * @param {Array<number>} destination point.
  51. * @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres).
  52. * @returns {number} Distance in km between this point and destination point (same units as radius).
  53. *
  54. * @example
  55. * var p1 = new LatLon(51.127, 1.338);
  56. * var p2 = new LatLon(50.964, 1.853);
  57. * var d = p1.distanceTo(p2); // 40.31 km
  58. */
  59. function calculateRhumbDistance(origin, destination, radius) {
  60. // φ => phi
  61. // λ => lambda
  62. // ψ => psi
  63. // Δ => Delta
  64. // δ => delta
  65. // θ => theta
  66. radius = radius === undefined ? helpers_1.earthRadius : Number(radius);
  67. // see www.edwilliams.org/avform.htm#Rhumb
  68. var R = radius;
  69. var phi1 = (origin[1] * Math.PI) / 180;
  70. var phi2 = (destination[1] * Math.PI) / 180;
  71. var DeltaPhi = phi2 - phi1;
  72. var DeltaLambda = (Math.abs(destination[0] - origin[0]) * Math.PI) / 180;
  73. // if dLon over 180° take shorter rhumb line across the anti-meridian:
  74. if (DeltaLambda > Math.PI) {
  75. DeltaLambda -= 2 * Math.PI;
  76. }
  77. // on Mercator projection, longitude distances shrink by latitude; q is the 'stretch factor'
  78. // q becomes ill-conditioned along E-W line (0/0); use empirical tolerance to avoid it
  79. var DeltaPsi = Math.log(Math.tan(phi2 / 2 + Math.PI / 4) / Math.tan(phi1 / 2 + Math.PI / 4));
  80. var q = Math.abs(DeltaPsi) > 10e-12 ? DeltaPhi / DeltaPsi : Math.cos(phi1);
  81. // distance is pythagoras on 'stretched' Mercator projection
  82. var delta = Math.sqrt(DeltaPhi * DeltaPhi + q * q * DeltaLambda * DeltaLambda); // angular distance in radians
  83. var dist = delta * R;
  84. return dist;
  85. }
  86. exports.default = rhumbDistance;