toGeodetic method

LatLngAlt toGeodetic(
  1. Planet planet,
  2. Angle gmst
)

Implementation

LatLngAlt toGeodetic(Planet planet, Angle gmst) {
  // http://www.celestrak.com/columns/v02n03/
  final a = planet.radius;
  final f = planet.flattening;
  final e2 = (2 * f) - (f * f);

  final R = sqrt((x * x) + (y * y));

  var longitude = atan2(y, x) - gmst.radians;
  while (longitude < -pi) {
    longitude += pi * 2;
  }
  while (longitude > pi) {
    longitude -= pi * 2;
  }

  const int kmax = 20;
  var k = 0;
  var latitude = atan2(z, sqrt((x * x) + (y * y)));
  var C = 1.0;
  while (k < kmax) {
    C = 1 / sqrt(1 - (e2 * (sin(latitude) * sin(latitude))));
    latitude = atan2(z + (a * C * e2 * sin(latitude)), R);
    k += 1;
  }

  final height = (R / cos(latitude)) - (a * C);

  return LatLngAlt(
    Angle.radian(latitude),
    Angle.radian(longitude),
    height,
  );
}