function [Lat,Lon,Hi]=toLatLonHi(x,y,z,degrees)

    if nargin<4
        degrees= true;
    end
    a = 6378137.0; 
    b = 6356752.3142;
    XY2 = x*x + y*y;
    XY = sqrt(XY2);
    en2 = (a * a - b * b) / b;
    ed2 = en2 * b / a;
    den2 = z*z * (a * a) + XY2 * (b * b);
    den = sqrt(den2);

    Lat = atan2(z + (en2 * a * a * a) * z*z*z / (den2 * den), XY - (ed2 * b * b * b) * XY2 * XY / (den2 * den));
    Lon = atan2(y, x);

    CLat = cos(Lat);
    SLat = sin(Lat);
    N = (a * a) / sqrt(a * a * CLat * CLat + b * b * SLat * SLat);
    Hi = XY / CLat - N;

    if degrees
        Lat=rad2deg(Lat);
        Lon=rad2deg(Lon);
    end
    Hi = -Hi;
