index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * Takes two {@link Point|points} and finds the bearing angle between them along a Rhumb line
  8. * i.e. the angle measured in degrees start the north line (0 degrees)
  9. *
  10. * @name rhumbBearing
  11. * @param {Coord} start starting Point
  12. * @param {Coord} end ending Point
  13. * @param {Object} [options] Optional parameters
  14. * @param {boolean} [options.final=false] calculates the final bearing if true
  15. * @returns {number} bearing from north in decimal degrees, between -180 and 180 degrees (positive clockwise)
  16. * @example
  17. * var point1 = turf.point([-75.343, 39.984], {"marker-color": "#F00"});
  18. * var point2 = turf.point([-75.534, 39.123], {"marker-color": "#00F"});
  19. *
  20. * var bearing = turf.rhumbBearing(point1, point2);
  21. *
  22. * //addToMap
  23. * var addToMap = [point1, point2];
  24. * point1.properties.bearing = bearing;
  25. * point2.properties.bearing = bearing;
  26. */
  27. function rhumbBearing(start, end, options) {
  28. if (options === void 0) { options = {}; }
  29. var bear360;
  30. if (options.final) {
  31. bear360 = calculateRhumbBearing(invariant_1.getCoord(end), invariant_1.getCoord(start));
  32. }
  33. else {
  34. bear360 = calculateRhumbBearing(invariant_1.getCoord(start), invariant_1.getCoord(end));
  35. }
  36. var bear180 = bear360 > 180 ? -(360 - bear360) : bear360;
  37. return bear180;
  38. }
  39. /**
  40. * Returns the bearing from ‘this’ point to destination point along a rhumb line.
  41. * Adapted from Geodesy: https://github.com/chrisveness/geodesy/blob/master/latlon-spherical.js
  42. *
  43. * @private
  44. * @param {Array<number>} from - origin point.
  45. * @param {Array<number>} to - destination point.
  46. * @returns {number} Bearing in degrees from north.
  47. * @example
  48. * var p1 = new LatLon(51.127, 1.338);
  49. * var p2 = new LatLon(50.964, 1.853);
  50. * var d = p1.rhumbBearingTo(p2); // 116.7 m
  51. */
  52. function calculateRhumbBearing(from, to) {
  53. // φ => phi
  54. // Δλ => deltaLambda
  55. // Δψ => deltaPsi
  56. // θ => theta
  57. var phi1 = helpers_1.degreesToRadians(from[1]);
  58. var phi2 = helpers_1.degreesToRadians(to[1]);
  59. var deltaLambda = helpers_1.degreesToRadians(to[0] - from[0]);
  60. // if deltaLambdaon over 180° take shorter rhumb line across the anti-meridian:
  61. if (deltaLambda > Math.PI) {
  62. deltaLambda -= 2 * Math.PI;
  63. }
  64. if (deltaLambda < -Math.PI) {
  65. deltaLambda += 2 * Math.PI;
  66. }
  67. var deltaPsi = Math.log(Math.tan(phi2 / 2 + Math.PI / 4) / Math.tan(phi1 / 2 + Math.PI / 4));
  68. var theta = Math.atan2(deltaLambda, deltaPsi);
  69. return (helpers_1.radiansToDegrees(theta) + 360) % 360;
  70. }
  71. exports.default = rhumbBearing;