Popular Posts

Yatharth Sharma the coolest guy on the planet welcomes you all. please keep visiting for updates and don't forget to send in your suggestions....

Thursday, August 1, 2013

ECEF TO LLA C# code

public static double[] convert_ecef_to_wgs84(double x, double y, double z)
        {
            double lon = 0;
            double lat = 0;
            double alt = 0;
            double[] output = new double[3];
            double a = 6378137;
            double e = 8.1819190842622e-2;
            double b = Math.Sqrt(Math.Pow(a, 2) * (1 - Math.Pow(e, 2)));
            double ep = Math.Sqrt((Math.Pow(a, 2) - Math.Pow(b, 2)) / Math.Pow(b, 2));
            double p = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
            double th = Math.Atan2(a * z, b * p);
            lon = Math.Atan2(y, x);
            lat = Math.Atan2((z + Math.Pow(ep, 2) * b * Math.Pow(Math.Sin(th), 3)), (p - Math.Pow(e, 2) * a * Math.Pow(Math.Cos(th), 3)));
            double N = a / (Math.Sqrt(1 - Math.Pow(e, 2) * Math.Pow(Math.Sin(lat), 2)));
            alt = p / Math.Cos(lat) - N;

            lon = lon % (2 * Math.PI);

            bool k = Math.Abs(x) < 1.0 && Math.Abs(y) < 1.0;
            if (k)
                alt = Math.Abs(z) - b;

            lon = lon * 180 / Math.PI;
            lat = lat * 180 / Math.PI;
            output[0] = lon;
            output[1] = lat;
            output[2] = alt;
            return output;

        }

No comments:

Post a Comment