UNPKG

@yachteye/signalk-makkah-plugin

Version:
122 lines (121 loc) 5.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.newPositionGivenBearingAndDistance = exports.normalizeAngleDegrees = exports.angleDifferenceDegrees = exports.distanceBetweenMeters = exports.distanceBetweenNauticalMiles = exports.bearingTo = exports.NauticalMileInMeter = exports.radianToDegree = exports.degreeToRadian = void 0; /** * Convert degrees to radians. * @param degree * @returns */ const degreeToRadian = (degree) => { return (degree * Math.PI) / 180.0; }; exports.degreeToRadian = degreeToRadian; /** * * @param radian * @returns */ const radianToDegree = (radian) => { return (radian * 180.0) / Math.PI; }; exports.radianToDegree = radianToDegree; exports.NauticalMileInMeter = 1852.0; const EarthRadiusKM = 6371.01; const EarthRadiusNM = 3441.596; /** * Calculate the bearing from point-1 (from) to point-2 (to). * @param fromLatitude Latitude of point 1 (degrees). * @param fromLongitude Longitude of point 1 (degrees). * @param toLatitude Latitude of point 2 (degrees). * @param toLongitude Longitude of point 2 (degrees). * @returns the bearing in decimal degrees. */ const bearingTo = (fromLatitude, fromLongitude, toLatitude, toLongitude) => { const lat1 = (0, exports.degreeToRadian)(fromLatitude); const lat2 = (0, exports.degreeToRadian)(toLatitude); const dlon = (0, exports.degreeToRadian)(toLongitude - fromLongitude); const y = Math.sin(dlon) * Math.cos(lat2); const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dlon); const brng = ((0, exports.radianToDegree)(Math.atan2(y, x)) + 360) % 360; return brng; }; exports.bearingTo = bearingTo; /** * Calculate the distance from point-1 (from) to point-2 (to). * @param fromLatitude * @param fromLongitude * @param toLatitude * @param toLongitude * @returns the distance in Nautical miles. */ const distanceBetweenNauticalMiles = (fromLatitude, fromLongitude, toLatitude, toLongitude) => { let rv; const fromLatitudeRadians = (0, exports.degreeToRadian)(fromLatitude); const toLatitudeRadians = (0, exports.degreeToRadian)(toLatitude); rv = EarthRadiusNM * Math.acos(Math.sin(fromLatitudeRadians) * Math.sin(toLatitudeRadians) + Math.cos(fromLatitudeRadians) * Math.cos(toLatitudeRadians) * Math.cos((0, exports.degreeToRadian)(toLongitude) - (0, exports.degreeToRadian)(fromLongitude))); if (isNaN(rv)) { rv = 0; } return rv; }; exports.distanceBetweenNauticalMiles = distanceBetweenNauticalMiles; /** * Calculate the distance from point-1 (from) to point-2 (to). * @param fromLatitude * @param fromLongitude * @param toLatitude * @param toLongitude * @returns the distance in meters. */ const distanceBetweenMeters = (fromLatitude, fromLongitude, toLatitude, toLongitude) => { const miles = (0, exports.distanceBetweenNauticalMiles)(fromLatitude, fromLongitude, toLatitude, toLongitude); return miles * exports.NauticalMileInMeter; }; exports.distanceBetweenMeters = distanceBetweenMeters; /** * The (shortest) difference between two angles (degrees). The angles a1 and a2 must be in the [0, 360> range. * @param a1 Angle 1. * @param a2 Angle 2. * @returns Difference (can be negative). */ const angleDifferenceDegrees = (a1, a2) => { // Debug.Assert((a1 >= 0) && (a1 < 360), "AngleDifferenceDegrees() invalid a1 value: " + a1); // Debug.Assert((a2 >= 0) && (a2 < 360), "AngleDifferenceDegrees() invalid a2 value: " + a2); const da = (a2 - a1) % 360; return ((2 * da) % 360) - da; }; exports.angleDifferenceDegrees = angleDifferenceDegrees; /** * Normalize angle to a [0, 360> value. * @param angleDegrees Angle value (degrees). * @returns Normalized angle. */ const normalizeAngleDegrees = (angleDegrees) => { return angleDegrees - Math.floor(angleDegrees / 360) * 360; }; exports.normalizeAngleDegrees = normalizeAngleDegrees; /** * Given a start point, initial bearing, and distance, this will calculate the destina­tion point and final bearing travelling along a (shortest distance) great circle arc. * @see https://www.movable-type.co.uk/scripts/latlong.html . * @param fromLatitude Start point latitude (decimal degrees). * @param fromLongitude Start point longitude (decimal degrees). * @param bearing Bearing (clockwise from north). * @param distanceKM Distance in km. * @returns The latitude and longitude of the new point. */ const newPositionGivenBearingAndDistance = (fromLatitude, fromLongitude, bearing, distanceKM) => { const distRatio = distanceKM / EarthRadiusKM; const distRatioSine = Math.sin(distRatio); const distRatioCosine = Math.cos(distRatio); const startLatRad = (0, exports.degreeToRadian)(fromLatitude); const startLonRad = (0, exports.degreeToRadian)(fromLongitude); const startLatCos = Math.cos(startLatRad); const startLatSin = Math.sin(startLatRad); const endLatRads = Math.asin(startLatSin * distRatioCosine + startLatCos * distRatioSine * Math.cos((0, exports.degreeToRadian)(bearing))); const endLonRads = startLonRad + Math.atan2(Math.sin((0, exports.degreeToRadian)(bearing)) * distRatioSine * startLatCos, distRatioCosine - startLatSin * Math.sin(endLatRads)); return { latitude: (0, exports.radianToDegree)(endLatRads), longitude: (0, exports.radianToDegree)(endLonRads) }; }; exports.newPositionGivenBearingAndDistance = newPositionGivenBearingAndDistance;