From 0349931f0f4a31b7552b7a0c4165fcbb912b3598 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Tue, 25 Oct 2022 14:32:18 +0200 Subject: [PATCH] add a function to project coordinates --- src/math/wgs84.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/math/wgs84.ts b/src/math/wgs84.ts index 505c8e1..99d9fd5 100644 --- a/src/math/wgs84.ts +++ b/src/math/wgs84.ts @@ -23,4 +23,31 @@ export class WGS84 { return 2 * EarthRadius * Math.asin(Math.sqrt(a)); } + + /// @brief Projects coordinate based on the distance and bearing + /// @param coordinate The root coordinate + /// @param distance The distance in metres + /// @param bearing The bearing in degree + static project( + coordinate: { lat: number; lon: number }, + distance: number, + bearing: number, + ): { lat: number; lon: number } { + const lat0 = Angle.deg2rad(coordinate.lat); + const lon0 = Angle.deg2rad(coordinate.lon); + const radians = Angle.deg2rad(bearing); + + const lat1 = Math.asin( + Math.sin(lat0) * Math.cos(distance / EarthRadius) + + Math.cos(lat0) * Math.sin(distance / EarthRadius) * Math.cos(radians), + ); + const lon1 = + lon0 + + Math.atan2( + Math.sin(radians) * Math.sin(distance / EarthRadius) * Math.cos(lat0), + Math.cos(distance / EarthRadius) - Math.sin(lat0) * Math.sin(lat1), + ); + + return { lat: Angle.rad2deg(lat1), lon: Angle.rad2deg(lon1) }; + } }