I am trying to write a function that computes the midpoint of two geodetic coordinates (lat, lon, alt). It returns the correct latitude & longitude currently but the altitude is off.
Function
public static GeodeticPoint computeMidpoint(GeodeticPoint gp1, GeodeticPoint gp2) {
// For reference (extracted from external class)
// OrekitConfig.EARTH_FRAME = FramesFactory.getITRF(IERSConventions.IERS_2010, true);
// OrekitConfig.EARTH = new OneAxisEllipsoid(
// Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
// Constants.WGS84_EARTH_FLATTENING,
// EARTH_FRAME
// );
// Convert geodetic coordinates to Cartesian
Vector3D c1 = OrekitConfig.EARTH.transform(gp1);
Vector3D c2 = OrekitConfig.EARTH.transform(gp2);
Vector3D midpoint = c1.add(c2).scalarMultiply(0.5);
AbsoluteDate date = new AbsoluteDate(
LocalDateTime.now(ZoneOffset.UTC).toString(),
TimeScalesFactory.getUTC()
);
// Convert Cartesian back to geodetic
return OrekitConfig.EARTH.transform(
midpoint,
OrekitConfig.EARTH_FRAME,
date
);
}
In the caller, each GeodeticPoint gp comes from a SpacecraftState s, i.e.,
gp = OrekitConfig.EARTH.transform(s.getPosition(), s.getFrame(), s.getDate());
Test case
void testComputeMidpoint() {
GeodeticPoint gp1 = new GeodeticPoint(Math.toRadians(40.730610), Math.toRadians(-73.935242), 31.09); // New York, NY
GeodeticPoint gp2 = new GeodeticPoint(Math.toRadians(37.773972), Math.toRadians(-122.431297), 20.11); // San Francisco, CA
GeodeticPoint midpoint = computeMidpoint(gp1, gp2); // Ericson, NE
assertAll(
() -> assertEquals(41.78, Math.toDegrees(midpoint.getLatitude()), 0.3),
() -> assertEquals(-98.68, Math.toDegrees(midpoint.getLongitude()), 0.3),
() -> assertEquals(611, midpoint.getAltitude(), 0.1)
);
}
Results
Expected: { lat=41.78, lon=-98.68, altitude=611}
Actual: { lat=41.78, lon=-98.68, altitude=-333383.471179875}
Any insights appreciated. Thank you.