Which one of these azimuth values is the right one? And why?

First of all, let's make clear that I want the Azimuth on the surface of the Earth, i.e. the angle between two locations, for example New York and Moscow.

I am testing some azimuth calculations with my JS functions (shown below). For the points A(-170, -89) to B(10, 89), I get ~90º.

JS function for Azimuth on sphere (from Wikipedia)

var dLon = lon2 - lon1;
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
var angle = Math.atan2(y, x) * 180 / Math.PI;

JS function for Azimuth on oblate spheroid (from Wikipedia)

var dLon = lon2 - lon1;
var f = 1 / 298.257223563;    /* Flattening for WGS 84 */
var b = (1 - f) * (1 - f);
var tanLat2 = Math.tan(lat2);
var y = Math.sin(dLon);
var x;
if (lat1 === 0) {
var x = b * tanLat2;
} else {
var a = f * (2 - f);
var tanLat1 = Math.tan(lat1);
var c = 1 + b * tanLat2 * tanLat2;
var d = 1 + b * tanLat1 * tanLat1;
var t = b * Math.tan(lat2) / Math.tan(lat1) + a * Math.sqrt(c / d);
var x = (t - Math.cos(dLon)) * Math.sin(lat1);
}
var angle = Math.atan2(y, x) * 180 / Math.PI;

In Calculator 2, I get 90º.

In PostGIS, I get 270º

In Calculator 1, I get 180º.

I know the Azimuth gets more and more distorted near the Poles, but that's exactly why I am testing at these spots. This variety of different solutions are confusing me. Could you please help me getting the right answer for this?

• There's also this calculator which uses the Vincenty method for solving geodesy's "inverse" problem. It gives what I would expect for the azimuth: zero. – mkennedy Aug 28 '14 at 17:43
• Sorry, I misread the first latitude value! The NGS calculator referred to in the earlier comment is hanging, but Esri's implementation (also Vincenty with a few tweaks) returns 0. I'm using GRS80 rather than a sphere. Both 0 and 180.0 are valid. One goes N over N Pole, other goes S over S Pole; same distance. – mkennedy Aug 28 '14 at 20:40
• looks like a disagreement over where 0 is - check the docs. I've seen noon, 3pm and 6pm all used. – Ian Turton Aug 29 '14 at 7:28
• Indeed, many solutions are valid, but if you consider the oblate spheroid, North and South are the correct solutions. Thank you! :) – joaorodr84 Aug 29 '14 at 11:28
• Hi, also wanted to point out geographiclib.sourceforge.net . A library with beautiful sets of functions to help you calculate everything related to geography – nickves Aug 30 '14 at 22:53

First, be careful with phrasing. Your "the angle between two locations" is unclear.

Keeping things to a simple sphere, the azimuth of any oblique great circle route depends entirely on where you measure it. It can range all the way from 0 to 360 and be correct. You probably seek the starting azimuth of a great circle route from a certain point to another point.

Your test points are at strange locations (but it's good to test those situations): they're exactly opposite one another across the globe's diameter -- they're antipodal points -- and they're near the poles. There are always an infinity of possible great circle routes from any one point to its antipodal point!

The azimuth of any oblique great circle route at its most northern and most southern points will always be exactly 90 or 270, depending only on your preferred direction of travel.

The general trend of the great circle of your test example is approximately 0 and 180, once you're well away from the poles. It would be instructive to use an example of New York to London or Moscow, and vice versa, and see any differences.

In addition to an answer over in SO, the inverse geodetic solution is not easy with near-antipodal points, as your question has. The inverse geodetic problem is solved iteratively using Vincenty's 1975 algorithm, which fails to converge for near antipodal points. However, the problem is still solvable using a different approach.

See page 40 of Rapp RH (1993) for the geodesic behaviour of near antipodal points.

Also, see Karney (2013) for general purpose algorithms for geodesics, which are implemented in GeographicLib, and handle near-antipodal cases.

• I'll be sure to check out these links. Thank you very much! :) – joaorodr84 Sep 2 '14 at 1:42