Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bearing and Reverse bearing #39

Open
eiredrake opened this issue Nov 26, 2018 · 3 comments
Open

Bearing and Reverse bearing #39

eiredrake opened this issue Nov 26, 2018 · 3 comments
Labels

Comments

@eiredrake
Copy link

@eiredrake eiredrake commented Nov 26, 2018

Is there a way to to calculate bearing and reverse bearing between points?

thanks!

@airbreather
Copy link
Member

@airbreather airbreather commented Nov 27, 2018

I don't think so. If so, it wouldn't really be anything that ProjNet4GeoAPI adds, since this library is just concerned with coordinate transformations.

You could, for example, use a conformal projection and compute angles there using NetTopologySuite, but that wouldn't really be different than just computing the angles on the unprojected coordinates.

Mike Gavaghan wrote a Geodesy library in both Java and C#... I copied the C# one onto GitHub (with his permission) at airbreather/Gavaghan.Geodesy, which seems a bit more promising.

GeodeticCalculator.CalculateGeodeticCurve can give you a GeodeticCurve which has the Azimuth / ReverseAzimuth, which you should be able to translate to bearing / reverse bearing, right?

@eiredrake
Copy link
Author

@eiredrake eiredrake commented Feb 26, 2019

Rolled back around to this and had a hard time remembering where I'd asked the question. but thank you I'll look into it. I appreciate the referral.

@FObermaier FObermaier added the question label Mar 11, 2019
@juliusfriedman
Copy link

@juliusfriedman juliusfriedman commented Nov 25, 2020

Something like this?

/// <summary>
		/// 
		/// </summary>
		public enum CompassDirection
        {
			N,
			NE,
			E,
			SE,
			S,
			SW,
			W,
			NW
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="bearing"></param>
		/// <returns></returns>
		[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
		public static CompassDirection GetCompassDirection(double bearing)
        {
			if (bearing >= 0 && bearing <= 22.5)
			{
				return CompassDirection.N;
			}
			else if (bearing >= 22.5 && bearing <= 67.5)
			{
				return CompassDirection.NE;
			}
			else if (bearing >= 67.5 && bearing <= 112.5)
			{
				return CompassDirection.E;
			}
			else if (bearing >= 112.5 && bearing <= 157.5)
			{
				return CompassDirection.SE;
			}
			else if (bearing >= 157.5 && bearing <= 205.5)
			{
				return CompassDirection.S;
			}
			else if (bearing >= 202.5 && bearing <= 247.5)
			{
				return CompassDirection.SW;
			}
			else if (bearing >= 247.5 && bearing <= 292.5)
			{
				return CompassDirection.W;
			}
			else if (bearing >= 292.5 && bearing <= 337.5)
			{
				return CompassDirection.NW;
			}
            else
            {
				return CompassDirection.N;
            }
		}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
		public static double DegreeBearing(double lon1, double lat1, double lon2, double lat2)
		{
			var dLon = ToRadians(lon2 - lon1);
			var dPhi = Math.Log(Math.Tan(ToRadians(lat2) / 2 + Math.PI / 4) / Math.Tan(ToRadians(lat1) / 2 + Math.PI / 4));
			if (Math.Abs(dLon) > Math.PI) dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
			return ToBearing(Math.Atan2(dLon, dPhi));
		}


[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
		public static double ToBearing(double radians)
		{
			// convert radians to degrees (as bearing: 0...360)
			return (ToDegrees(radians) + 360) % 360;
		}

See also: StackOverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.