From 0a667f33015dbd456f8850f69630305abee60d9a Mon Sep 17 00:00:00 2001 From: SpaceOceanCorp Date: Wed, 11 Nov 2026 12:28:26 -0600 Subject: [PATCH 1/2] LAEA and Orthographic inverses: keep full precision close to the poles The inverses took the latitude from asin() of a value close to 1 near the poles, which loses half the digits, as in PROJ before OSGeo/PROJ#4944: - Lambert Azimuthal Equal Area, ellipsoidal (e.g. EPSG:3035 parameters, WGS 84, lat_0 52, lon_0 10): 89.99999 came back as 89.9999899791 deg, 2.3 mm off. The oblique and equatorial cases now use atan2() of the three components of the authalic latitude, and the polar case pi/2 - 2 asin(sqrt(q / 2qp)) instead of asin(1 - q / qp). - LAEA spherical, lat_0 52: 89.9999 came back 0.3 mm off; now atan2() as above. - Orthographic spherical: asin(cosc), asin(y / a) and asin(cosc * sinph0 + y * cosph0 / a); at 89.9999999 deg with lat_0 55 the result was exactly 90 (1.1 cm off). Now acos(sinc) for the polar centres and atan2() for the others. New AzimuthalPrecisionTests: 11 points near the pole, inverses of PROJ's forward values; all 3 tests fail before this change. The full suite passes (73 passed, 4 skipped, as before). --- .../LambertAzimuthalEqualAreaProjection.cs | 38 +++++++---- .../Projections/OrthographicProjection.cs | 24 +++---- test/ProjNet.Tests/AzimuthalPrecisionTests.cs | 67 +++++++++++++++++++ 3 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 test/ProjNet.Tests/AzimuthalPrecisionTests.cs diff --git a/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs b/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs index 910c7142..da409cc1 100644 --- a/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs +++ b/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs @@ -334,17 +334,24 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y) cCe = Math.Cos(sCe); sCe = Math.Sin(sCe); x *= sCe; + // rho * sin(beta), rho * cos(beta) * cos(lam) and rho * cos(beta) * sin(lam) of the + // authalic latitude beta: atan2() keeps beta accurate near the poles, where asin() + // of sin(beta) did not + double n; if (_mode == Mode.OBLIQ) { - ab = cCe * _sinb1 + y * sCe * _cosb1 / rho; + n = rho * cCe * _sinb1 + y * sCe * _cosb1; y = rho * _cosb1 * cCe - y * _sinb1 * sCe; } else { - ab = y * sCe / rho; + n = y * sCe; y = rho * cCe; } - break; + double lamOE = adjust_lon(Math.Atan2(x, y) + central_meridian); + y = authlat(Math.Atan2(n, hypot(x, y)), _apa); + x = lamOE; + return; case Mode.N_POLE: y = -y; goto continue_S_POLE; @@ -358,14 +365,15 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y) y = lat_origin; // phi return ; } - ab = 1.0 - q / _qp; + // asin(1 - q / qp), but accurate near the pole + ab = HALF_PI - 2.0 * Math.Asin(Math.Min(1.0, Math.Sqrt(0.5 * q / _qp))); if (_mode == Mode.S_POLE) ab = -ab; break; } - x = x = adjust_lon(Math.Atan2(x, y) + central_meridian); // lam - y = authlat(Math.Asin(ab), _apa); // phi + x = adjust_lon(Math.Atan2(x, y) + central_meridian); // lam + y = authlat(ab, _apa); // phi } private void SphericalMetersToRadians(ref double x, ref double y) @@ -389,16 +397,18 @@ private void SphericalMetersToRadians(ref double x, ref double y) switch (_mode) { case Mode.EQUIT: - phi = Math.Abs(rh) <= EPS10 ? 0.0 : Math.Asin(y * sinz / rh); - x *= sinz; - y = cosz * rh; - break; case Mode.OBLIQ: - phi = Math.Abs(rh) <= EPS10 ? lat_origin : - Math.Asin(cosz * _sinb1 + y * sinz * _cosb1 / rh); - x *= sinz * _cosb1; - y = (cosz - Math.Sin(phi) * _sinb1) * rh; + { + // rh * sin(phi), rh * cos(phi) * cos(lam) and rh * cos(phi) * sin(lam): atan2() + // keeps the latitude accurate near the poles, where asin() of sin(phi) did not + double s0 = _mode == Mode.EQUIT ? 0.0 : _sinb1; + double c0 = _mode == Mode.EQUIT ? 1.0 : _cosb1; + double n = rh * cosz * s0 + y * sinz * c0; + y = rh * cosz * c0 - y * sinz * s0; + x *= sinz; + phi = rh == 0.0 ? (_mode == Mode.EQUIT ? 0.0 : lat_origin) : Math.Atan2(n, hypot(x, y)); break; + } case Mode.N_POLE: y = -y; phi = HALF_PI - phi; diff --git a/src/ProjNet/CoordinateSystems/Projections/OrthographicProjection.cs b/src/ProjNet/CoordinateSystems/Projections/OrthographicProjection.cs index bdf61ce8..8d36183b 100644 --- a/src/ProjNet/CoordinateSystems/Projections/OrthographicProjection.cs +++ b/src/ProjNet/CoordinateSystems/Projections/OrthographicProjection.cs @@ -148,29 +148,29 @@ private void OrthoSInverse(ref double x, ref double y) { switch (_mode) { + // asin(cosc), asin(y / a) and asin(cosc * sinph0 + y * cosph0 / a) lost half the + // digits close to the poles; acos(sinc) and atan2() of the three components keep + // full precision there case Mode.N_POLE: - phi = Math.Asin(cosc); + phi = Math.Acos(sinc); lam = lon_origin + Math.Atan2(x, -y); break; case Mode.S_POLE: - phi = -Math.Asin(cosc); + phi = -Math.Acos(sinc); lam = lon_origin + Math.Atan2(x, y); break; case Mode.EQUIT: - if (Math.Abs(y) >= _semiMajor) - { - phi = y < 0.0 ? -HALF_PI : HALF_PI; - } - else - { - phi = Math.Asin(y/_semiMajor); - } + phi = Math.Atan2(y * sinc, hypot(x * sinc, rho * cosc)); lam = lon_origin + Math.Atan2(x / _semiMajor, cosc); break; case Mode.OBLIQ: - phi = Math.Asin(cosc * _sinph0 + (y * _cosph0 / _semiMajor)); - lam = lon_origin + Math.Atan2(x * sinc, rho * _cosph0 * cosc - y * _sinph0 * sinc); + { + double n = rho * cosc * _sinph0 + y * sinc * _cosph0; + double d = rho * cosc * _cosph0 - y * sinc * _sinph0; + phi = Math.Atan2(n, hypot(x * sinc, d)); + lam = lon_origin + Math.Atan2(x * sinc, d); break; + } default: throw new ArgumentOutOfRangeException(nameof(_mode)); } diff --git a/test/ProjNet.Tests/AzimuthalPrecisionTests.cs b/test/ProjNet.Tests/AzimuthalPrecisionTests.cs new file mode 100644 index 00000000..3c7e9239 --- /dev/null +++ b/test/ProjNet.Tests/AzimuthalPrecisionTests.cs @@ -0,0 +1,67 @@ +using System; +using NUnit.Framework; +using ProjNet.CoordinateSystems; +using ProjNet.CoordinateSystems.Transformations; + +namespace ProjNET.Tests +{ + /// + /// Inverses of Lambert Azimuthal Equal Area and Orthographic close to the poles, where asin() + /// of a value close to 1 lost half the digits (centimetres on the Earth). The projected + /// coordinates are PROJ's forward (cct -d 10) of the given longitude and latitude. + /// + [TestFixture] + public class AzimuthalPrecisionTests + { + private static readonly CoordinateSystemFactory Factory = new CoordinateSystemFactory(); + private static readonly CoordinateTransformationFactory TransformFactory = new CoordinateTransformationFactory(); + + private const string Wgs84 = "SPHEROID[\"WGS_1984\",6378137.0,298.257223563]"; + private const string Sphere = "SPHEROID[\"Sphere\",6371000.0,0.0]"; + + private static void CheckInverse(string projection, string spheroid, double lat0, double lon0, + double x, double y, double lon, double lat) + { + string geogcs = "GEOGCS[\"GCS\",DATUM[\"D\"," + spheroid + "],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]"; + var geographic = Factory.CreateFromWkt(geogcs); + var projected = Factory.CreateFromWkt( + "PROJCS[\"P\"," + geogcs + ",PROJECTION[\"" + projection + "\"]," + + "PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0]," + + $"PARAMETER[\"Central_Meridian\",{lon0}],PARAMETER[\"Latitude_Of_Origin\",{lat0}]," + + "UNIT[\"Meter\",1.0]]"); + var inverse = TransformFactory.CreateFromCoordinateSystems(projected, geographic).MathTransform; + double[] geo = inverse.Transform(new[] { x, y }); + string at = $"{projection} lat_0={lat0} at {lon}, {lat}"; + // 1e-9 degrees of latitude is about 0.1 mm + Assert.That(geo[1], Is.EqualTo(lat).Within(1e-9), at + " latitude"); + // close to the pole the longitude is ill-conditioned: compare its effect on the ground + Assert.That((geo[0] - lon) * Math.Cos(lat * Math.PI / 180), Is.EqualTo(0).Within(1e-9), at + " longitude"); + } + + [Test] + public void LambertAzimuthalEqualAreaEllipsoidal() + { + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, 5.9112198326, 4159707.1167619219, 40, 89.9999); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, -1110.9577298914, 4160077.1237620777, -100, 89.99); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 90, 0, 5.5846989779, -9.6729823747, 30, 89.9999); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, 0.5911221239, 4159715.3415663159, 40, 89.99999); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, 0.0059112217, 4159716.2462945590, 40, 89.9999999); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 90, 0, 0.0055846991, -0.0096729826, 30, 89.9999999); + } + + [Test] + public void LambertAzimuthalEqualAreaSpherical() + { + CheckInverse("Lambert_Azimuthal_Equal_Area", Sphere, 52, 10, 5.8801011732, 4148380.3189707603, 40, 89.9999); + } + + [Test] + public void OrthographicSpherical() + { + CheckInverse("Orthographic", Sphere, 55, 5, 6.3778789768, 3654248.0146953794, 40, 89.9999); + CheckInverse("Orthographic", Sphere, 90, 0, 5.5597463325, -9.6297631250, 30, 89.9999); + CheckInverse("Orthographic", Sphere, 55, 5, 0.0063778791, 3654255.4685312235, 40, 89.9999999); + CheckInverse("Orthographic", Sphere, 90, 0, 0.0055597464, -0.0096297633, 30, 89.9999999); + } + } +} From ae6aa1870ea9efcbff6bb786531e8c84123cf19b Mon Sep 17 00:00:00 2001 From: SpaceOceanCorp Date: Wed, 11 Nov 2026 13:47:55 -0600 Subject: [PATCH 2/2] LAEA: geodetic latitude from the authalic one to full precision The ellipsoidal inverse turned the authalic latitude back into the geodetic one with a 3-term series in e^2 (authlat), whose truncation error was up to 1.6 mm on WGS 84, largest near 20 degrees of latitude and the same for every latitude of origin (20.5 came back as 20.4999999858). It now uses the 6th-order series in the third flattening n of C. F. F. Karney, On auxiliary latitudes (Survey Review 56, 165-180, 2024), as PROJ does, which is accurate to double precision for |f| up to about 1/150, and for |n| >= 0.01 refines the result by Newton's method on q(phi) = qp * sin(beta), also as PROJ. authset and authlat are kept for compatibility (they are protected) but no longer used. On 600 points per latitude of origin (90, -90, 0, 52, -30) over the whole map, inverses of PROJ's forward are now within 1.7e-8 to 4.6e-7 m on WGS 84 (were 1.6 mm after the previous commit, 5 to 10 cm before it). New test LambertAzimuthalEqualAreaAuthalicLatitude: 3 points on WGS 84 and 3 on an ellipsoid with 1/f = 30 (Newton path); it fails before this change. All tests pass (74 passed, 4 skipped). --- .../LambertAzimuthalEqualAreaProjection.cs | 10 +-- .../Projections/MapProjection.cs | 74 +++++++++++++++++++ test/ProjNet.Tests/AzimuthalPrecisionTests.cs | 17 +++++ 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs b/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs index da409cc1..e9222f0c 100644 --- a/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs +++ b/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs @@ -51,7 +51,7 @@ private enum Mode private readonly Mode _mode; private readonly double _qp; private readonly double _one_es; - private readonly double[] _apa; + private readonly double[] _authalicCoefficients; //private readonly double _mmf; private readonly double _dd; private readonly double _sinb1; @@ -100,9 +100,7 @@ public LambertAzimuthalEqualAreaProjection(IEnumerable para _one_es = 1.0 - _es; _qp = qsfn(1, _e, _one_es); //_mmf = 0.5 / (1.0 - _es); - _apa = authset(_es); - if (_apa == null) - throw new ArgumentException(nameof(parameters)); + _authalicCoefficients = AuthalicSeriesCoefficients(_es); switch (_mode) { @@ -349,7 +347,7 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y) y = rho * cCe; } double lamOE = adjust_lon(Math.Atan2(x, y) + central_meridian); - y = authlat(Math.Atan2(n, hypot(x, y)), _apa); + y = AuthalicToGeodetic(Math.Atan2(n, hypot(x, y)), _authalicCoefficients, _es, _qp); x = lamOE; return; case Mode.N_POLE: @@ -373,7 +371,7 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y) } x = adjust_lon(Math.Atan2(x, y) + central_meridian); // lam - y = authlat(ab, _apa); // phi + y = AuthalicToGeodetic(ab, _authalicCoefficients, _es, _qp); // phi } private void SphericalMetersToRadians(ref double x, ref double y) diff --git a/src/ProjNet/CoordinateSystems/Projections/MapProjection.cs b/src/ProjNet/CoordinateSystems/Projections/MapProjection.cs index 4dac2046..402d2c88 100644 --- a/src/ProjNet/CoordinateSystems/Projections/MapProjection.cs +++ b/src/ProjNet/CoordinateSystems/Projections/MapProjection.cs @@ -1195,6 +1195,80 @@ protected static double authlat(double beta, double[] APA) return (beta + APA[0] * Math.Sin(t) + APA[1] * Math.Sin(t + t) + APA[2] * Math.Sin(t + t + t)); } + /// + /// Coefficients of the series for the geodetic latitude in terms of the authalic latitude, + /// to order 6 in the third flattening n (C. F. F. Karney, On auxiliary latitudes, Survey + /// Review 56, 165-180, 2024, Eq. A20; as used by PROJ). Full double precision for |f| up to + /// about 1/150; refines the result by Newton's method for + /// flatter ellipsoids. + /// + /// The square of the eccentricity + protected static double[] AuthalicSeriesCoefficients(double es) + { + // C[phi,xi], upper triangular part, row by row + double[] c = { + 4.0 / 3.0, 4.0 / 45.0, -16.0 / 35.0, -2582.0 / 14175.0, 60136.0 / 467775.0, 28112932.0 / 212837625.0, + 46.0 / 45.0, 152.0 / 945.0, -11966.0 / 14175.0, -21016.0 / 51975.0, 251310128.0 / 638512875.0, + 3044.0 / 2835.0, 3802.0 / 14175.0, -94388.0 / 66825.0, -8797648.0 / 10945935.0, + 6059.0 / 4725.0, 41072.0 / 93555.0, -1472637812.0 / 638512875.0, + 768272.0 / 467775.0, 455935736.0 / 638512875.0, + 4210684958.0 / 1915538625.0 + }; + double b = Math.Sqrt(1 - es); + double n = (1 - b) / (1 + b); // third flattening + var F = new double[6]; + double d = n; + int o = 0; + for (int l = 0; l < 6; l++) + { + int m = 5 - l; // order of the polynomial in n + double y = c[o + m]; + for (int k = m; k > 0; k--) + y = y * n + c[o + k - 1]; + F[l] = d * y; + o += m + 1; + d *= n; + } + return F; + } + + /// + /// Geodetic latitude from the authalic latitude : the series of + /// , summed by Clenshaw's method, and for + /// |n| >= 0.01 a few Newton steps on q(phi) = qp * sin(beta), as in PROJ. + /// + protected static double AuthalicToGeodetic(double beta, double[] F, double es, double qp) + { + double s = Math.Sin(beta), c = Math.Cos(beta); + double u0 = 0, u1 = 0, X = 2 * (c - s) * (c + s); // 2 * cos(2 * beta) + for (int k = F.Length; k > 0;) + { + double t = X * u0 - u1 + F[--k]; + u1 = u0; + u0 = t; + } + double phi = beta + 2 * s * c * u0; + double b = Math.Sqrt(1 - es); + if (Math.Abs((1 - b) / (1 + b)) < 0.01) + return phi; + // f(phi) = (qp * sin(beta) - q(phi)) / (1 - e^2), with + // q(phi) / (1 - e^2) = sin(phi) / (1 - e^2 sin(phi)^2) + atanh(e sin(phi)) / e + double e = Math.Sqrt(es), one_es = 1 - es; + double qDivOneEs = qp * s / one_es; + for (int i = 0; i < 10; i++) + { + double sinphi = Math.Sin(phi), cosphi = Math.Cos(phi); + double w = 1 - es * sinphi * sinphi; + double es_sin = e * sinphi; + double atanh = 0.5 * Math.Log((1 + es_sin) / (1 - es_sin)); + double dphi = w * w / (2 * cosphi) * (qDivOneEs - sinphi / w - atanh / e); + if (!(Math.Abs(dphi) >= 1e-15)) + break; + phi += dphi; + } + return phi; + } + /// /// Calculates the hypotenuse of a triangle: Sqrt(x*x + y*y); /// diff --git a/test/ProjNet.Tests/AzimuthalPrecisionTests.cs b/test/ProjNet.Tests/AzimuthalPrecisionTests.cs index 3c7e9239..193ff071 100644 --- a/test/ProjNet.Tests/AzimuthalPrecisionTests.cs +++ b/test/ProjNet.Tests/AzimuthalPrecisionTests.cs @@ -49,6 +49,23 @@ public void LambertAzimuthalEqualAreaEllipsoidal() CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 90, 0, 0.0055846991, -0.0096729826, 30, 89.9999999); } + /// + /// Away from the poles: the geodetic latitude from the authalic one used a 3-term series in e^2 + /// that was up to 1.6 mm off near 20 degrees of latitude on WGS 84; now Karney's series (as in + /// PROJ), and Newton's method for flatter ellipsoids (here 1/f = 30). + /// + [Test] + public void LambertAzimuthalEqualAreaAuthalicLatitude() + { + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, 3169886.1977156079, -2859860.5252899877, 40, 20.5); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, -8255457.8876390513, -4325335.7419390697, -60, -20); + CheckInverse("Lambert_Azimuthal_Equal_Area", Wgs84, 52, 10, 1116144.6025791967, 2246815.6491002310, 40, 70); + const string flat = "SPHEROID[\"Flat\",6378137.0,30.0]"; + CheckInverse("Lambert_Azimuthal_Equal_Area", flat, 52, 10, 3164672.4205533662, -2788115.3019215590, 40, 20.5); + CheckInverse("Lambert_Azimuthal_Equal_Area", flat, 52, 10, -8157421.9670853456, -4173006.6592939394, -60, -20); + CheckInverse("Lambert_Azimuthal_Equal_Area", flat, 52, 10, 1150784.7146271013, 2262287.8046130096, 40, 70); + } + [Test] public void LambertAzimuthalEqualAreaSpherical() {