diff --git a/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs b/src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs index 910c7142..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) { @@ -334,17 +332,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 = AuthalicToGeodetic(Math.Atan2(n, hypot(x, y)), _authalicCoefficients, _es, _qp); + x = lamOE; + return; case Mode.N_POLE: y = -y; goto continue_S_POLE; @@ -358,14 +363,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 = AuthalicToGeodetic(ab, _authalicCoefficients, _es, _qp); // phi } private void SphericalMetersToRadians(ref double x, ref double y) @@ -389,16 +395,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/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/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..193ff071 --- /dev/null +++ b/test/ProjNet.Tests/AzimuthalPrecisionTests.cs @@ -0,0 +1,84 @@ +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); + } + + /// + /// 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() + { + 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); + } + } +}