index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // index.ts
  2. import {
  3. convertLength,
  4. degreesToRadians,
  5. earthRadius,
  6. point
  7. } from "@turf/helpers";
  8. import { getCoord } from "@turf/invariant";
  9. function rhumbDestination(origin, distance, bearing, options = {}) {
  10. const wasNegativeDistance = distance < 0;
  11. let distanceInMeters = convertLength(
  12. Math.abs(distance),
  13. options.units,
  14. "meters"
  15. );
  16. if (wasNegativeDistance) distanceInMeters = -Math.abs(distanceInMeters);
  17. const coords = getCoord(origin);
  18. const destination = calculateRhumbDestination(
  19. coords,
  20. distanceInMeters,
  21. bearing
  22. );
  23. destination[0] += destination[0] - coords[0] > 180 ? -360 : coords[0] - destination[0] > 180 ? 360 : 0;
  24. return point(destination, options.properties);
  25. }
  26. function calculateRhumbDestination(origin, distance, bearing, radius) {
  27. radius = radius === void 0 ? earthRadius : Number(radius);
  28. const delta = distance / radius;
  29. const lambda1 = origin[0] * Math.PI / 180;
  30. const phi1 = degreesToRadians(origin[1]);
  31. const theta = degreesToRadians(bearing);
  32. const DeltaPhi = delta * Math.cos(theta);
  33. let phi2 = phi1 + DeltaPhi;
  34. if (Math.abs(phi2) > Math.PI / 2) {
  35. phi2 = phi2 > 0 ? Math.PI - phi2 : -Math.PI - phi2;
  36. }
  37. const DeltaPsi = Math.log(
  38. Math.tan(phi2 / 2 + Math.PI / 4) / Math.tan(phi1 / 2 + Math.PI / 4)
  39. );
  40. const q = Math.abs(DeltaPsi) > 1e-11 ? DeltaPhi / DeltaPsi : Math.cos(phi1);
  41. const DeltaLambda = delta * Math.sin(theta) / q;
  42. const lambda2 = lambda1 + DeltaLambda;
  43. return [
  44. (lambda2 * 180 / Math.PI + 540) % 360 - 180,
  45. phi2 * 180 / Math.PI
  46. ];
  47. }
  48. var turf_rhumb_destination_default = rhumbDestination;
  49. export {
  50. turf_rhumb_destination_default as default,
  51. rhumbDestination
  52. };
  53. //# sourceMappingURL=index.js.map