index.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Returns the destination {@link Point} having travelled the given distance along a Rhumb line from the
  8. * origin Point with the (varant) given bearing.
  9. *
  10. * @name rhumbDestination
  11. * @param {Coord} origin starting point
  12. * @param {number} distance distance from the starting point
  13. * @param {number} bearing varant bearing angle ranging from -180 to 180 degrees from north
  14. * @param {Object} [options={}] Optional parameters
  15. * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
  16. * @param {Object} [options.properties={}] translate properties to destination point
  17. * @returns {Feature<Point>} Destination point.
  18. * @example
  19. * var pt = turf.point([-75.343, 39.984], {"marker-color": "F00"});
  20. * var distance = 50;
  21. * var bearing = 90;
  22. * var options = {units: 'miles'};
  23. *
  24. * var destination = turf.rhumbDestination(pt, distance, bearing, options);
  25. *
  26. * //addToMap
  27. * var addToMap = [pt, destination]
  28. * destination.properties['marker-color'] = '#00F';
  29. */
  30. function rhumbDestination(origin, distance, bearing, options) {
  31. if (options === void 0) { options = {}; }
  32. var wasNegativeDistance = distance < 0;
  33. var distanceInMeters = helpers_1.convertLength(Math.abs(distance), options.units, "meters");
  34. if (wasNegativeDistance)
  35. distanceInMeters = -Math.abs(distanceInMeters);
  36. var coords = invariant_1.getCoord(origin);
  37. var destination = calculateRhumbDestination(coords, distanceInMeters, bearing);
  38. // compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
  39. // solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
  40. destination[0] +=
  41. destination[0] - coords[0] > 180
  42. ? -360
  43. : coords[0] - destination[0] > 180
  44. ? 360
  45. : 0;
  46. return helpers_1.point(destination, options.properties);
  47. }
  48. /**
  49. * Returns the destination point having travelled along a rhumb line from origin point the given
  50. * distance on the given bearing.
  51. * Adapted from Geodesy: http://www.movable-type.co.uk/scripts/latlong.html#rhumblines
  52. *
  53. * @private
  54. * @param {Array<number>} origin - point
  55. * @param {number} distance - Distance travelled, in same units as earth radius (default: metres).
  56. * @param {number} bearing - Bearing in degrees from north.
  57. * @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres).
  58. * @returns {Array<number>} Destination point.
  59. */
  60. function calculateRhumbDestination(origin, distance, bearing, radius) {
  61. // φ => phi
  62. // λ => lambda
  63. // ψ => psi
  64. // Δ => Delta
  65. // δ => delta
  66. // θ => theta
  67. radius = radius === undefined ? helpers_1.earthRadius : Number(radius);
  68. var delta = distance / radius; // angular distance in radians
  69. var lambda1 = (origin[0] * Math.PI) / 180; // to radians, but without normalize to 𝜋
  70. var phi1 = helpers_1.degreesToRadians(origin[1]);
  71. var theta = helpers_1.degreesToRadians(bearing);
  72. var DeltaPhi = delta * Math.cos(theta);
  73. var phi2 = phi1 + DeltaPhi;
  74. // check for some daft bugger going past the pole, normalise latitude if so
  75. if (Math.abs(phi2) > Math.PI / 2) {
  76. phi2 = phi2 > 0 ? Math.PI - phi2 : -Math.PI - phi2;
  77. }
  78. var DeltaPsi = Math.log(Math.tan(phi2 / 2 + Math.PI / 4) / Math.tan(phi1 / 2 + Math.PI / 4));
  79. // E-W course becomes ill-conditioned with 0/0
  80. var q = Math.abs(DeltaPsi) > 10e-12 ? DeltaPhi / DeltaPsi : Math.cos(phi1);
  81. var DeltaLambda = (delta * Math.sin(theta)) / q;
  82. var lambda2 = lambda1 + DeltaLambda;
  83. return [
  84. (((lambda2 * 180) / Math.PI + 540) % 360) - 180,
  85. (phi2 * 180) / Math.PI,
  86. ]; // normalise to −180..+180°
  87. }
  88. exports.default = rhumbDestination;