ECEF to lla (lat,lon,alt) in java

coordinate-systems, gps, java, latitude-longitude, math

Solution

OK so i got this working.

The problem was a misplaced variable, so for the sake of our future here's the working JAVA implementation :

public static final double a = 6378137;
public static final double f = 0.0034;
public static final double b = 6.3568e6;
public static final double e = Math.sqrt((Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2));
public static final double e2 = Math.sqrt((Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(b, 2));

public static double[] ecef2lla(double x, double y, double z) {

    double[] lla = { 0, 0, 0 };
    double lan, lon, height, N , theta, p;

    p = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

    theta = Math.atan((z * a) / (p * b));

    lon = Math.atan(y / x);

    lat = Math.atan(((z + Math.pow(e2, 2) * b * Math.pow(Math.sin(theta), 3)) / ((p - Math.pow(e, 2) * a * Math.pow(Math.cos(theta), 3)))));
    N = a / (Math.sqrt(1 - (Math.pow(e, 2) * Math.pow(Math.sin(lat), 2))));

    double m = (p / Math.cos(lat));
    height = m - N;


    lon = lon * 180 / Math.PI;
    lat = lat * 180 / Math.PI; 
    lla[0] = lat;
    lla[1] = lon;
    lla[2] = height;
    return lla;
}

Note : The units for the ECEF X Y Z are in Meters !

Problem

Ive looked through the posts in the site and haven't found my problem ... as the head line says im trying to convert from ecef to lla . I'm using this document : Conversion article in the direct formula , not the iterate formula and this site for result comparison : ECEF2LLA Im developing in java so my code is as follows : ``` public static final double a = 6378137; public static final double f = 1/298.257223563; public static final double b = a*(1-f); public static final double e = Math.sqrt((Math.pow(a, 2)-Math.pow(b, 2))/Math.pow(a, 2)); public static final double e2 = Math.sqrt((Math.pow(a, 2)-Math.pow(b, 2))/Math.pow(b, 2)); public static double[] ecef2lla(double x , double y , double z){ double[] lla = {0,0,0}; double lon,lat,height,N; double p = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2)); double theta = Math.atan((z*a)/(p*b)); lon = Math.atan(y/x); lon = lon*180/Math.PI; lat = Math.atan(((z+Math.pow(e2, 2)*b*Math.pow(Math.sin(theta), 3))/((p-Math.pow(e,2)*a*Math.pow(Math.cos(theta), 3))))); lat = (lat*180)/Math.PI; N= a/(Math.sqrt(1-Math.pow(e*Math.sin(lat), 2))); height = (p/Math.cos(theta)) - N; lla[0] = lon; lla[1] = lat; lla[2] = height; return lla; } ``` I'm getting wrong height data. I've tried to move to radians and degrees and what not . Thank you in advance !

Original source