Пример #1
0
        public GeodesicLine(Geodesic g,
                            double lat1, double lon1, double azi1,
                            int caps)
        {
            azi1 = GeoMath.AngNormalize(azi1);
            double salp1, calp1;

            // Guard against underflow in salp0.  Also -0 is converted to +0.
            GeoMath.Sincosd(GeoMath.AngRound(azi1), out salp1, out calp1);
            LineInit(g, lat1, lon1, azi1, salp1, calp1, caps); //TODO: Check this cast
        }
Пример #2
0
        /**
         * Reset the origin.
         *
         * @param[in] lat0 latitude at origin (degrees).
         * @param[in] lon0 longitude at origin (degrees).
         * @param[in] h0 height above ellipsoid at origin (meters); default 0.
         *
         * \e lat0 should be in the range [−90°, 90°].
         **********************************************************************/
        public void Reset(double lat0, double lon0, double h0 = 0)
        {
            _lat0 = GeoMath.LatFix(lat0);
            _lon0 = GeoMath.AngNormalize(lon0);
            _h0   = h0;
            _earth.Forward(_lat0, _lon0, _h0, out _x0, out _y0, out _z0);
            double sphi, cphi, slam, clam;

            GeoMath.Sincosd(_lat0, out sphi, out cphi);
            GeoMath.Sincosd(_lon0, out slam, out clam);
            Geocentric.Rotation(sphi, cphi, slam, clam, _r);
        }
Пример #3
0
        /**
         * Reverse projection, from Lambert conformal conic to geographic.
         *
         * @param[in] lon0 central meridian longitude (degrees).
         * @param[in] x easting of point (meters).
         * @param[in] y northing of point (meters).
         * @param[out] lat latitude of point (degrees).
         * @param[out] lon longitude of point (degrees).
         * @param[out] gamma meridian convergence at point (degrees).
         * @param[out] k azimuthal scale of projection at point; the radial
         *   scale is the 1/\e k.
         *
         * The latitude origin is given by AlbersEqualArea::LatitudeOrigin().  No
         * false easting or northing is added.  The value of \e lon returned is in
         * the range [−180°, 180°].  The value of \e lat returned is
         * in the range [−90°, 90°].  If the input point is outside
         * the legal projected space the nearest pole is returned.
         **********************************************************************/
        public void Reverse(double lon0, double x, double y,
                            out double lat, out double lon, out double gamma, out double k)
        {
            y *= _sign;
            double
                nx = _k0 * _n0 * x, ny = _k0 * _n0 * y, y1 = _nrho0 - ny,
                den  = GeoMath.Hypot(nx, y1) + _nrho0, // 0 implies origin with polar aspect
                drho = den != 0 ? (_k0 * x * nx - 2 * _k0 * y * _nrho0 + _k0 * y * ny) / den : 0,
            // dsxia = scxi0 * dsxi
                dsxia = -_scxi0 * (2 * _nrho0 + _n0 * drho) * drho /
                        (GeoMath.Square(_a) * _qZ),
                txi   = (_txi0 + dsxia) / Math.Sqrt(Math.Max(1 - dsxia * (2 * _txi0 + dsxia), epsx2_)),
                tphi  = tphif(txi),
                theta = Math.Atan2(nx, y1),
                lam   = _n0 != 0 ? theta / (_k2 * _n0) : x / (y1 * _k0);

            gamma = _sign * theta / GeoMath.Degree;
            lat   = GeoMath.Atand(_sign * tphi);
            lon   = lam / GeoMath.Degree;
            lon   = GeoMath.AngNormalize(lon + GeoMath.AngNormalize(lon0));
            k     = _k0 * (den != 0 ? (_nrho0 + _n0 * drho) * hyp(_fm * tphi) / _a : 1);
        }
Пример #4
0
        ///@}

        /** \name The general position function.
         **********************************************************************/
        ///@{

        /**
         * The general position function.  GeodesicLine::Position and
         * GeodesicLine::ArcPosition are defined in terms of this function.
         *
         * @param[in] arcmode boolean flag determining the meaning of the second
         *   parameter; if \e arcmode is false, then the GeodesicLine object must
         *   have been constructed with \e caps |= GeodesicLine::DISTANCE_IN.
         * @param[in] s12_a12 if \e arcmode is false, this is the distance between
         *   point 1 and point 2 (meters); otherwise it is the arc length between
         *   point 1 and point 2 (degrees); it can be negative.
         * @param[in] outmask a bitor'ed combination of GeodesicLine::mask values
         *   specifying which of the following parameters should be set.
         * @param[out] lat2 GeodesicMask.LATITUDE of point 2 (degrees).
         * @param[out] lon2 GeodesicMask.LONGITUDE of point 2 (degrees); requires that the
         *   GeodesicLine object was constructed with \e caps |=
         *   GeodesicLine::GeodesicMask.LONGITUDE.
         * @param[out] azi2 (forward) GeodesicMask.AZIMUTH at point 2 (degrees).
         * @param[out] s12 distance from point 1 to point 2 (meters); requires
         *   that the GeodesicLine object was constructed with \e caps |=
         *   GeodesicLine::DISTANCE.
         * @param[out] m12 reduced length of geodesic (meters); requires that the
         *   GeodesicLine object was constructed with \e caps |=
         *   GeodesicLine::GeodesicMask.REDUCEDLENGTH.
         * @param[out] M12 geodesic scale of point 2 relative to point 1
         *   (dimensionless); requires that the GeodesicLine object was constructed
         *   with \e caps |= GeodesicLine::GeodesicMask.GEODESICSCALE.
         * @param[out] M21 geodesic scale of point 1 relative to point 2
         *   (dimensionless); requires that the GeodesicLine object was constructed
         *   with \e caps |= GeodesicLine::GeodesicMask.GEODESICSCALE.
         * @param[out] S12 GeodesicMask.AREA under the geodesic (meters<sup>2</sup>); requires
         *   that the GeodesicLine object was constructed with \e caps |=
         *   GeodesicLine::GeodesicMask.AREA.
         * @return \e a12 arc length from point 1 to point 2 (degrees).
         *
         * The GeodesicLine::mask values possible for \e outmask are
         * - \e outmask |= GeodesicLine::GeodesicMask.LATITUDE for the GeodesicMask.LATITUDE \e lat2;
         * - \e outmask |= GeodesicLine::GeodesicMask.LONGITUDE for the GeodesicMask.LATITUDE \e lon2;
         * - \e outmask |= GeodesicLine::GeodesicMask.AZIMUTH for the GeodesicMask.LATITUDE \e azi2;
         * - \e outmask |= GeodesicLine::DISTANCE for the distance \e s12;
         * - \e outmask |= GeodesicLine::GeodesicMask.REDUCEDLENGTH for the reduced length \e
         *   m12;
         * - \e outmask |= GeodesicLine::GeodesicMask.GEODESICSCALE for the geodesic scales \e
         *   M12 and \e M21;
         * - \e outmask |= GeodesicLine::GeodesicMask.AREA for the GeodesicMask.AREA \e S12;
         * - \e outmask |= GeodesicLine::ALL for all of the above;
         * - \e outmask |= GeodesicLine::LONG_UNROLL to unroll \e lon2 instead of
         *   reducing it into the range [&minus;180&deg;, 180&deg;].
         * .
         * Requesting a value which the GeodesicLine object is not capable of
         * computing is not an error; the corresponding argument will not be
         * altered.  Note, however, that the arc length is always computed and
         * returned as the function value.
         *
         * With the GeodesicLine::LONG_UNROLL bit seout t, the quantity \e lon2 &minus;
         * \e lon1 indicates how many times and in what sense the geodesic
         * encircles the ellipsoid.
         **********************************************************************/
        public double GenPosition(bool arcmode, double s12_a12, int outmask,
                                  out double lat2, out double lon2, out double azi2,
                                  out double s12, out double m12, out double M12, out double M21,
                                  out double S12)
        {
            outmask &= (_caps & GeodesicMask.OUT_MASK);

            lat2 = double.NaN;
            lon2 = double.NaN;
            azi2 = double.NaN;
            s12  = double.NaN;
            m12  = double.NaN;
            M12  = double.NaN;
            M21  = double.NaN;
            S12  = double.NaN;

            if (!(Init() && (arcmode || (_caps & (GeodesicMask.OUT_MASK & GeodesicMask.DISTANCE_IN)) != 0)))
            {
                // Uninitialized or impossible distance calculation requested
                return(double.NaN);
            }

            // Avoid warning about uninitialized B12.
            double sig12, ssig12, csig12, B12 = 0, AB1 = 0;

            if (arcmode)
            {
                // Interpret s12_a12 as spherical arc length
                sig12 = s12_a12 * GeoMath.Degree;
                GeoMath.Sincosd(s12_a12, out ssig12, out csig12);
            }
            else
            {
                // Interpret s12_a12 as distance
                double
                    tau12 = s12_a12 / (_b * (1 + _A1m1)),
                    s     = Math.Sin(tau12),
                    c     = Math.Cos(tau12);
                // tau2 = tau1 + tau12
                B12 = -Geodesic.SinCosSeries(true,
                                             _stau1 * c + _ctau1 * s,
                                             _ctau1 * c - _stau1 * s,
                                             _C1pa);
                sig12  = tau12 - (B12 - _B11);
                ssig12 = Math.Sin(sig12); csig12 = Math.Cos(sig12);
                if (Math.Abs(_f) > 0.01)
                {
                    // Reverted distance series is inaccurate for |f| > 1/100, so correct
                    // sig12 with 1 Newton iteration.  The following table shows the
                    // approximate maximum error for a = WGS_a() and various f relative to
                    // GeodesicExact.
                    //     erri = the error in the inverse solution (nm)
                    //     errd = the error in the direct solution (series only) (nm)
                    //     errda = the error in the direct solution
                    //             (series + 1 Newton) (nm)
                    //
                    //       f     erri  errd errda
                    //     -1/5    12e6 1.2e9  69e6
                    //     -1/10  123e3  12e6 765e3
                    //     -1/20   1110 108e3  7155
                    //     -1/50  18.63 200.9 27.12
                    //     -1/100 18.63 23.78 23.37
                    //     -1/150 18.63 21.05 20.26
                    //      1/150 22.35 24.73 25.83
                    //      1/100 22.35 25.03 25.31
                    //      1/50  29.80 231.9 30.44
                    //      1/20   5376 146e3  10e3
                    //      1/10  829e3  22e6 1.5e6
                    //      1/5   157e6 3.8e9 280e6
                    double
                        sssig2 = _ssig1 * csig12 + _csig1 * ssig12,
                        scsig2 = _csig1 * csig12 - _ssig1 * ssig12;
                    B12 = Geodesic.SinCosSeries(true, sssig2, scsig2, _C1a);
                    double serr = (1 + _A1m1) * (sig12 + (B12 - _B11)) - s12_a12 / _b;
                    sig12  = sig12 - serr / Math.Sqrt(1 + _k2 * GeoMath.Square(sssig2));
                    ssig12 = Math.Sin(sig12); csig12 = Math.Cos(sig12);
                    // Update B12 below
                }
            }

            double ssig2, csig2, sbet2, cbet2, salp2, calp2;

            // sig2 = sig1 + sig12
            ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
            csig2 = _csig1 * csig12 - _ssig1 * ssig12;
            double dn2 = Math.Sqrt(1 + _k2 * GeoMath.Square(ssig2));

            if ((outmask & (GeodesicMask.DISTANCE | GeodesicMask.REDUCEDLENGTH | GeodesicMask.GEODESICSCALE)) != 0)
            {
                if (arcmode || Math.Abs(_f) > 0.01)
                {
                    B12 = Geodesic.SinCosSeries(true, ssig2, csig2, _C1a);
                }
                AB1 = (1 + _A1m1) * (B12 - _B11);
            }
            // sin(bet2) = cos(alp0) * sin(sig2)
            sbet2 = _calp0 * ssig2;
            // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
            cbet2 = GeoMath.Hypot(_salp0, _calp0 * csig2);
            if (cbet2 == 0)
            {
                // I.e., salp0 = 0, csig2 = 0.  Break the degeneracy in this case
                cbet2 = csig2 = Geodesic.tiny_;
            }
            // tan(alp0) = cos(sig2)*tan(alp2)
            salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize

            if ((outmask & GeodesicMask.DISTANCE) != 0)
            {
                s12 = arcmode ? _b * ((1 + _A1m1) * sig12 + AB1) : s12_a12;
            }

            if ((outmask & GeodesicMask.LONGITUDE) != 0)
            {
                // tan(omg2) = sin(alp0) * tan(sig2)
                double somg2 = _salp0 * ssig2, comg2 = csig2, // No need to normalize
                       E = GeoMath.CopySign(1, _salp0);       // east-going?
                                                              // omg12 = omg2 - omg1
                double omg12 = (outmask & GeodesicMask.LONG_UNROLL) != 0
                  ? E * (sig12
                         - (Math.Atan2(ssig2, csig2) - Math.Atan2(_ssig1, _csig1))
                         + (Math.Atan2(E * somg2, comg2) - Math.Atan2(E * _somg1, _comg1)))
                  : Math.Atan2(somg2 * _comg1 - comg2 * _somg1,
                               comg2 * _comg1 + somg2 * _somg1);
                double lam12 = omg12 + _A3c *
                               (sig12 + (Geodesic.SinCosSeries(true, ssig2, csig2, _C3a)
                                         - _B31));
                double lon12 = lam12 / GeoMath.Degree;
                lon2 = (outmask & GeodesicMask.LONG_UNROLL) != 0 ? _lon1 + lon12 :
                       GeoMath.AngNormalize(GeoMath.AngNormalize(_lon1) +
                                            GeoMath.AngNormalize(lon12));
            }

            if ((outmask & GeodesicMask.LATITUDE) != 0)
            {
                lat2 = GeoMath.Atan2d(sbet2, _f1 * cbet2);
            }

            if ((outmask & GeodesicMask.AZIMUTH) != 0)
            {
                azi2 = GeoMath.Atan2d(salp2, calp2);
            }

            if ((outmask & (GeodesicMask.REDUCEDLENGTH | GeodesicMask.GEODESICSCALE)) != 0)
            {
                double
                    B22 = Geodesic.SinCosSeries(true, ssig2, csig2, _C2a),
                    AB2 = (1 + _A2m1) * (B22 - _B21),
                    J12 = (_A1m1 - _A2m1) * sig12 + (AB1 - AB2);
                if ((outmask & GeodesicMask.REDUCEDLENGTH) != 0)
                {
                    // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
                    // accurate cancellation in the case of coincident points.
                    m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
                                - _csig1 * csig2 * J12);
                }
                if ((outmask & GeodesicMask.GEODESICSCALE) != 0)
                {
                    double t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
                    M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
                    M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
                }
            }

            if ((outmask & GeodesicMask.AREA) != 0)
            {
                double
                       B42 = Geodesic.SinCosSeries(false, ssig2, csig2, _C4a);
                double salp12, calp12;
                if (_calp0 == 0 || _salp0 == 0)
                {
                    // alp12 = alp2 - alp1, used in Math.Atan2 so no need to normalize
                    salp12 = salp2 * _calp1 - calp2 * _salp1;
                    calp12 = calp2 * _calp1 + salp2 * _salp1;
                    // We used to include here some patch up code that purported to deal
                    // with nearly meridional geodesics properly.  However, this turned out
                    // to be wrong once _salp1 = -0 was allowed (via
                    // Geodesic::InverseLine).  In fact, the calculation of {s,c}alp12
                    // was already correct (following the IEEE rules for handling signed
                    // zeros).  So the patch up code was unnecessary (as well as
                    // dangerous).
                }
                else
                {
                    // tan(alp) = tan(alp0) * sec(sig)
                    // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
                    // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
                    // If csig12 > 0, write
                    //   csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
                    // else
                    //   csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
                    // No need to normalize
                    salp12 = _calp0 * _salp0 *
                             (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
                              ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
                    calp12 = GeoMath.Square(_salp0) + GeoMath.Square(_calp0) * _csig1 * csig2;
                }
                S12 = _c2 * Math.Atan2(salp12, calp12) + _A4 * (B42 - _B41);
            }

            return(arcmode ? s12_a12 : sig12 / GeoMath.Degree);
        }
Пример #5
0
        /**
         * Forward projection, from geographic to Cassini-Soldner.
         *
         * @param[in] lat latitude of point (degrees).
         * @param[in] lon longitude of point (degrees).
         * @param[out] x easting of point (meters).
         * @param[out] y northing of point (meters).
         * @param[out] azi azimuth of easting direction at point (degrees).
         * @param[out] rk reciprocal of azimuthal northing scale at point.
         *
         * \e lat should be in the range [&minus;90&deg;, 90&deg;].  A call to
         * Forward followed by a call to Reverse will return the original (\e lat,
         * \e lon) (to within roundoff).  The routine does nothing if the origin
         * has not been set.
         **********************************************************************/
        public void Forward(double lat, double lon,
                            out double x, out double y, out double azi, out double rk)
        {
            if (!Init())
            {
                x   = double.NaN;
                y   = double.NaN;
                azi = double.NaN;
                rk  = double.NaN;
            }

            double e;
            double dlon = GeoMath.AngDiff(LongitudeOrigin(), lon, out e);
            double sig12, s12, azi1, azi2;

            sig12  = _earth.Inverse(lat, -Math.Abs(dlon), lat, Math.Abs(dlon), out s12, out azi1, out azi2);
            sig12 *= 0.5;
            s12   *= 0.5;
            if (s12 == 0)
            {
                double da = GeoMath.AngDiff(azi1, azi2, out e) / 2;
                if (Math.Abs(dlon) <= 90)
                {
                    azi1 = 90 - da;
                    azi2 = 90 + da;
                }
                else
                {
                    azi1 = -90 - da;
                    azi2 = -90 + da;
                }
            }
            if (dlon < 0)
            {
                azi2  = azi1;
                s12   = -s12;
                sig12 = -sig12;
            }
            x   = s12;
            azi = GeoMath.AngNormalize(azi2);
            GeodesicLine perp = _earth.Line(lat, dlon, azi, GeodesicMask.GEODESICSCALE);
            double       t;

            perp.GenPosition(true, -sig12,
                             GeodesicMask.GEODESICSCALE,
                             out t, out t, out t, out t, out t, out t, out rk, out t);

            double salp0, calp0;

            GeoMath.Sincosd(perp.EquatorialAzimuth(), out salp0, out calp0);
            double
                sbet1  = lat >= 0 ? calp0 : -calp0,
                cbet1  = Math.Abs(dlon) <= 90 ? Math.Abs(salp0) : -Math.Abs(salp0),
                sbet01 = sbet1 * _cbet0 - cbet1 * _sbet0,
                cbet01 = cbet1 * _cbet0 + sbet1 * _sbet0,
                sig01  = Math.Atan2(sbet01, cbet01) / GeoMath.Degree;

            _meridian.GenPosition(true, sig01,
                                  GeodesicMask.DISTANCE,
                                  out t, out t, out t, out y, out t, out t, out t, out t);
        }
Пример #6
0
        /**
         * Reverse projection, from transverse Mercator to geographic.
         *
         * @param[in] lon0 central meridian of the projection (degrees).
         * @param[in] x easting of point (meters).
         * @param[in] y northing of point (meters).
         * @param[out] lat latitude of point (degrees).
         * @param[out] lon longitude of point (degrees).
         * @param[out] gamma meridian convergence at point (degrees).
         * @param[out] k scale of projection at point.
         *
         * No false easting or northing is added.  The value of \e lon returned is
         * in the range [&minus;180&deg;, 180&deg;].
         **********************************************************************/
        public void Reverse(double lon0, double x, double y,
                            out double lat, out double lon, out double gamma, out double k)
        {
            // This undoes the steps in Forward.  The wrinkles are: (1) Use of the
            // reverted series to express zeta' in terms of zeta. (2) Newton's method
            // to solve for phi in terms of tan(phi).
            double
                xi  = y / (_a1 * _k0),
                eta = x / (_a1 * _k0);
            // Explicitly enforce the parity
            int
                xisign  = (xi < 0) ? -1 : 1,
                etasign = (eta < 0) ? -1 : 1;

            xi  *= xisign;
            eta *= etasign;
            bool backside = xi > Math.PI / 2;

            if (backside)
            {
                xi = Math.PI - xi;
            }
            double
                c0 = Math.Cos(2 * xi), ch0 = Math.Cosh(2 * eta),
                s0 = Math.Sin(2 * xi), sh0 = Math.Sinh(2 * eta);


            int     n  = maxpow_;
            Complex a  = new Complex(2 * c0 * ch0, -2 * s0 * sh0); // 2 * Math.Cos(2*zeta')
            Complex y0 = new Complex((n & 1) != 0 ? -_bet[n] : 0, 0);
            Complex y1;                                            // default initializer is 0+i0
            Complex z0 = new Complex((n & 1) != 0 ? -2 * n * _bet[n] : 0, 0);
            Complex z1;

            if ((n & 1) != 0)
            {
                --n;
            }
            while (n > 0)
            {
                y1 = a * y0 - y1 - _bet[n];
                z1 = a * z0 - z1 - 2 * n * _bet[n];
                --n;
                y0 = a * y1 - y0 - _bet[n];
                z0 = a * z1 - z0 - 2 * n * _bet[n];
                --n;
            }
            a /= 2;                               // Math.Cos(2*zeta)
            z1 = 1 - z1 + a * z0;
            a  = new Complex(s0 * ch0, c0 * sh0); // Math.Sin(2*zeta)
            y1 = new Complex(xi, eta) + a * y0;
            // Convergence and scale for Gauss-Schreiber TM to Gauss-Krueger TM.
            gamma = GeoMath.Atan2d(z1.Imaginary, z1.Real);
            k     = _b1 / Complex.Abs(z1);
            // JHS 154 has
            //
            //   phi' = asin(Math.Sin(xi') / Math.Cosh(eta')) (Krueger p 17 (25))
            //   lam = asin(tanh(eta') / Math.Cos(phi')
            //   psi = asinh(tan(phi'))
            double
                xip = y1.Real, etap = y1.Imaginary,
                s = Math.Sinh(etap),
                c = Math.Max(0, Math.Cos(xip)), // Math.Cos(pi/2) might be negative
                r = GeoMath.Hypot(s, c);

            if (r != 0)
            {
                lon = GeoMath.Atan2d(s, c); // Krueger p 17 (25)
                                            // Use Newton's method to solve for tau
                double
                    sxip = Math.Sin(xip),
                    tau  = GeoMath.Tauf(sxip / r, _es);
                gamma += GeoMath.Atan2d(sxip * Math.Tanh(etap), c); // Krueger p 19 (31)
                lat    = GeoMath.Atand(tau);
                // Note Math.Cos(phi') * Math.Cosh(eta') = r
                k *= Math.Sqrt(_e2m + _e2 / (1 + GeoMath.Square(tau))) *
                     GeoMath.Hypot(1, tau) * r;
            }
            else
            {
                lat = 90;
                lon = 0;
                k  *= _c;
            }
            lat *= xisign;
            if (backside)
            {
                lon = 180 - lon;
            }
            lon *= etasign;
            lon  = GeoMath.AngNormalize(lon + lon0);
            if (backside)
            {
                gamma = 180 - gamma;
            }
            gamma *= xisign * etasign;
            gamma  = GeoMath.AngNormalize(gamma);
            k     *= _k0;
        }
Пример #7
0
        /**
         * Forward projection, from geographic to transverse Mercator.
         *
         * @param[in] lon0 central meridian of the projection (degrees).
         * @param[in] lat latitude of point (degrees).
         * @param[in] lon longitude of point (degrees).
         * @param[out] x easting of point (meters).
         * @param[out] y northing of point (meters).
         * @param[out] gamma meridian convergence at point (degrees).
         * @param[out] k scale of projection at point.
         *
         * No false easting or northing is added. \e lat should be in the range
         * [&minus;90&deg;, 90&deg;].
         **********************************************************************/
        public void Forward(double lon0, double lat, double lon,
                            out double x, out double y, out double gamma, out double k)
        {
            double e;

            lat = GeoMath.LatFix(lat);
            lon = GeoMath.AngDiff(lon0, lon, out e);
            // Explicitly enforce the parity
            int
                latsign = (lat < 0) ? -1 : 1,
                lonsign = (lon < 0) ? -1 : 1;

            lon *= lonsign;
            lat *= latsign;
            bool backside = lon > 90;

            if (backside)
            {
                if (lat == 0)
                {
                    latsign = -1;
                }
                lon = 180 - lon;
            }
            double sphi, cphi, slam, clam;

            GeoMath.Sincosd(lat, out sphi, out cphi);
            GeoMath.Sincosd(lon, out slam, out clam);
            // phi = latitude
            // phi' = conformal latitude
            // psi = isometric latitude
            // tau = tan(phi)
            // tau' = tan(phi')
            // [xi', eta'] = Gauss-Schreiber TM coordinates
            // [xi, eta] = Gauss-Krueger TM coordinates
            //
            // We use
            //   tan(phi') = Math.Sinh(psi)
            //   Math.Sin(phi') = tanh(psi)
            //   Math.Cos(phi') = sech(psi)
            //   denom^2    = 1-Math.Cos(phi')^2*Math.Sin(lam)^2 = 1-sech(psi)^2*Math.Sin(lam)^2
            //   Math.Sin(xip)   = Math.Sin(phi')/denom          = tanh(psi)/denom
            //   Math.Cos(xip)   = Math.Cos(phi')*Math.Cos(lam)/denom = sech(psi)*Math.Cos(lam)/denom
            //   Math.Cosh(etap) = 1/denom                  = 1/denom
            //   Math.Sinh(etap) = Math.Cos(phi')*Math.Sin(lam)/denom = sech(psi)*Math.Sin(lam)/denom
            double etap, xip;

            if (lat != 90)
            {
                double
                    tau  = sphi / cphi,
                    taup = GeoMath.Taupf(tau, _es);
                xip = Math.Atan2(taup, clam);
                // Used to be
                //   etap = Math::atanh(Math.Sin(lam) / Math.Cosh(psi));
                etap = GeoMath.Asinh(slam / GeoMath.Hypot(taup, clam));
                // convergence and scale for Gauss-Schreiber TM (xip, etap) -- gamma0 =
                // atan(tan(xip) * tanh(etap)) = atan(tan(lam) * Math.Sin(phi'));
                // Math.Sin(phi') = tau'/Math.Sqrt(1 + tau'^2)
                // Krueger p 22 (44)
                gamma = GeoMath.Atan2d(slam * taup, clam * GeoMath.Hypot(1, taup));
                // k0 = Math.Sqrt(1 - _e2 * Math.Sin(phi)^2) * (Math.Cos(phi') / Math.Cos(phi)) * Math.Cosh(etap)
                // Note 1/Math.Cos(phi) = Math.Cosh(psip);
                // and Math.Cos(phi') * Math.Cosh(etap) = 1/hypot(Math.Sinh(psi), Math.Cos(lam))
                //
                // This form has cancelling errors.  This property is lost if Math.Cosh(psip)
                // is replaced by 1/Math.Cos(phi), even though it's using "primary" data (phi
                // instead of psip).
                k = Math.Sqrt(_e2m + _e2 * GeoMath.Square(cphi)) * GeoMath.Hypot(1, tau)
                    / GeoMath.Hypot(taup, clam);
            }
            else
            {
                xip   = Math.PI / 2;
                etap  = 0;
                gamma = lon;
                k     = _c;
            }
            // {xi',eta'} is {northing,easting} for Gauss-Schreiber transverse Mercator
            // (for eta' = 0, xi' = bet). {xi,eta} is {northing,easting} for transverse
            // Mercator with ant scale on the central meridian (for eta = 0, xip =
            // rectifying latitude).  Define
            //
            //   zeta = xi + i*eta
            //   zeta' = xi' + i*eta'
            //
            // The conversion from conformal to rectifying latitude can be expressed as
            // a series in _n:
            //
            //   zeta = zeta' + sum(h[j-1]' * Math.Sin(2 * j * zeta'), j = 1..maxpow_)
            //
            // where h[j]' = O(_n^j).  The reversion of this series gives
            //
            //   zeta' = zeta - sum(h[j-1] * Math.Sin(2 * j * zeta), j = 1..maxpow_)
            //
            // which is used in Reverse.
            //
            // Evaluate sums via Clenshaw method.  See
            //    https://en.wikipedia.org/wiki/Clenshaw_algorithm
            //
            // Let
            //
            //    S = sum(a[k] * phi[k](x), k = 0..n)
            //    phi[k+1](x) = alpha[k](x) * phi[k](x) + beta[k](x) * phi[k-1](x)
            //
            // Evaluate S with
            //
            //    b[n+2] = b[n+1] = 0
            //    b[k] = alpha[k](x) * b[k+1] + beta[k+1](x) * b[k+2] + a[k]
            //    S = (a[0] + beta[1](x) * b[2]) * phi[0](x) + b[1] * phi[1](x)
            //
            // Here we have
            //
            //    x = 2 * zeta'
            //    phi[k](x) = Math.Sin(k * x)
            //    alpha[k](x) = 2 * Math.Cos(x)
            //    beta[k](x) = -1
            //    [ Math.Sin(A+B) - 2*Math.Cos(B)*Math.Sin(A) + Math.Sin(A-B) = 0, A = k*x, B = x ]
            //    n = maxpow_
            //    a[k] = _alp[k]
            //    S = b[1] * Math.Sin(x)
            //
            // For the derivative we have
            //
            //    x = 2 * zeta'
            //    phi[k](x) = Math.Cos(k * x)
            //    alpha[k](x) = 2 * Math.Cos(x)
            //    beta[k](x) = -1
            //    [ Math.Cos(A+B) - 2*Math.Cos(B)*Math.Cos(A) + Math.Cos(A-B) = 0, A = k*x, B = x ]
            //    a[0] = 1; a[k] = 2*k*_alp[k]
            //    S = (a[0] - b[2]) + b[1] * Math.Cos(x)
            //
            // Matrix formulation (not used here):
            //    phi[k](x) = [Math.Sin(k * x); k * Math.Cos(k * x)]
            //    alpha[k](x) = 2 * [Math.Cos(x), 0; -Math.Sin(x), Math.Cos(x)]
            //    beta[k](x) = -1 * [1, 0; 0, 1]
            //    a[k] = _alp[k] * [1, 0; 0, 1]
            //    b[n+2] = b[n+1] = [0, 0; 0, 0]
            //    b[k] = alpha[k](x) * b[k+1] + beta[k+1](x) * b[k+2] + a[k]
            //    N.B., for all k: b[k](1,2) = 0; b[k](1,1) = b[k](2,2)
            //    S = (a[0] + beta[1](x) * b[2]) * phi[0](x) + b[1] * phi[1](x)
            //    phi[0](x) = [0; 0]
            //    phi[1](x) = [Math.Sin(x); Math.Cos(x)]
            double
                c0 = Math.Cos(2 * xip), ch0 = Math.Cosh(2 * etap),
                s0 = Math.Sin(2 * xip), sh0 = Math.Sinh(2 * etap);

            int     n  = maxpow_;
            Complex a  = new Complex(2 * c0 * ch0, -2 * s0 * sh0); // 2 * Math.Cos(2*zeta')
            Complex y0 = new Complex((n & 1) != 0 ? _alp[n] : 0, 0);
            Complex y1;                                            // default initializer is 0+i0
            Complex z0 = new Complex((n & 1) != 0 ? 2 * n * _alp[n] : 0, 0);
            Complex z1;

            if ((n & 1) != 0)
            {
                --n;
            }
            while (n > 0)
            {
                y1 = a * y0 - y1 + _alp[n];
                z1 = a * z0 - z1 + 2 * n * _alp[n];
                --n;
                y0 = a * y1 - y0 + _alp[n];
                z0 = a * z1 - z0 + 2 * n * _alp[n];
                --n;
            }
            a /= 2;                               // Math.Cos(2*zeta')
            z1 = 1 - z1 + a * z0;
            a  = new Complex(s0 * ch0, c0 * sh0); // Math.Sin(2*zeta')
            y1 = new Complex(xip, etap) + a * y0;
            // Fold in change in convergence and scale for Gauss-Schreiber TM to
            // Gauss-Krueger TM.
            gamma -= GeoMath.Atan2d(z1.Imaginary, z1.Real);
            k     *= _b1 * Complex.Abs(z1);
            double xi = y1.Real, eta = y1.Imaginary;

            y = _a1 * _k0 * (backside ? Math.PI - xi : xi) * latsign;
            x = _a1 * _k0 * eta * lonsign;
            if (backside)
            {
                gamma = 180 - gamma;
            }
            gamma *= latsign * lonsign;
            gamma  = GeoMath.AngNormalize(gamma);
            k     *= _k0;
        }