Ejemplo n.º 1
0
        private static PhiLam Convert(PhiLam input, bool inverse, NadTable table)
        {
            if (input.Lambda == HUGE_VAL)
            {
                return(input);
            }
            // Normalize input to ll origin
            if (!table.Filled)
            {
                table.FillData();
            }
            PhiLam tb = input;

            tb.Lambda -= table.LowerLeft.Lambda;
            tb.Phi    -= table.LowerLeft.Phi;
            tb.Lambda  = Proj.Adjlon(tb.Lambda - Math.PI) + Math.PI;
            PhiLam t = NadInterpolate(tb, table);

            if (inverse)
            {
                PhiLam del, dif;
                int    i = MAX_TRY;
                if (t.Lambda == HUGE_VAL)
                {
                    return(t);
                }
                t.Lambda = tb.Lambda + t.Lambda;
                t.Phi    = tb.Phi - t.Phi;
                do
                {
                    del = NadInterpolate(t, table);

                    /* This case used to return failure, but I have
                     *     changed it to return the first order approximation
                     *     of the inverse shift.  This avoids cases where the
                     *     grid shift *into* this grid came from another grid.
                     *     While we aren't returning optimally correct results
                     *     I feel a close result in this case is better than
                     *     no result.  NFW
                     *     To demonstrate use -112.5839956 49.4914451 against
                     *     the NTv2 grid shift file from Canada. */
                    if (del.Lambda == HUGE_VAL)
                    {
                        Debug.WriteLine(ProjectionMessages.InverseShiftFailed);
                        break;
                    }
                    t.Lambda -= dif.Lambda = t.Lambda - del.Lambda - tb.Lambda;
                    t.Phi    -= dif.Phi = t.Phi + del.Phi - tb.Phi;
                } while (i-- > 0 && Math.Abs(dif.Lambda) > TOL && Math.Abs(dif.Phi) > TOL);
                if (i < 0)
                {
                    Debug.WriteLine(ProjectionMessages.InvShiftConvergeFailed);
                    t.Lambda = t.Phi = HUGE_VAL;
                    return(t);
                }
                input.Lambda = Proj.Adjlon(t.Lambda + table.LowerLeft.Lambda);
                input.Phi    = t.Phi + table.LowerLeft.Phi;
            }
            else
            {
                if (t.Lambda == HUGE_VAL)
                {
                    input = t;
                }
                else
                {
                    input.Lambda -= t.Lambda;
                    input.Phi    += t.Phi;
                }
            }
            return(input);
        }