From 97651cbb322e36ec6bade897ea3e97b925a6be70 Mon Sep 17 00:00:00 2001 From: Suvrat1629 Date: Thu, 17 Oct 2024 19:48:47 +0530 Subject: [PATCH] added spline interpolation --- .../graziano/gpslogger/EGM96.java | 56 +++++++------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/eu/basicairdata/graziano/gpslogger/EGM96.java b/app/src/main/java/eu/basicairdata/graziano/gpslogger/EGM96.java index a25d5e85..384f6d42 100644 --- a/app/src/main/java/eu/basicairdata/graziano/gpslogger/EGM96.java +++ b/app/src/main/java/eu/basicairdata/graziano/gpslogger/EGM96.java @@ -167,44 +167,30 @@ public boolean isLoading() { * @param longitude the longitude of the location * @return the altitude correction in meters */ - public double getEGMCorrection(double latitude, double longitude) { - if (isEGMGridLoaded) { - double Lat = 90.0 - latitude; - double Lon = longitude; - if (Lon < 0) Lon += 360.0; - - int ilon = (int) (Lon / 0.25) + BOUNDARY; - int ilat = (int) (Lat / 0.25) + BOUNDARY; - - try { - // Creating points for interpolation - short hc11 = EGMGrid[ilon][ilat]; - short hc12 = EGMGrid[ilon][ilat + 1]; - short hc21 = EGMGrid[ilon + 1][ilat]; - short hc22 = EGMGrid[ilon + 1][ilat + 1]; - - // Bilinear Interpolation: - // Latitude - double hc1 = hc11 + (hc12 - hc11) * (Lat % 0.25) / 0.25; - double hc2 = hc21 + (hc22 - hc21) * (Lat % 0.25) / 0.25; - // Longitude - //double hc = (hc1 + (hc2 - hc1) * (Lon % 0.25) / 0.25) / 100; - //Log.w("myApp", "[#] EGM96.java - getEGMCorrection(" + latitude + ", " + longitude + ") = " + hc); - - return ((hc1 + (hc2 - hc1) * (Lon % 0.25) / 0.25) / 100); - } catch (ArrayIndexOutOfBoundsException e) { - return EGM96_VALUE_INVALID; - } + + public double getSplineEGMCorrection(double latitude, double longitude) { + if (!isEGMGridLoaded) return EGM96_VALUE_INVALID; + + double Lat = 90.0 - latitude; + double Lon = longitude; + if (Lon < 0) Lon += 360.0; + + int ilon = (int) (Lon / 0.25) + BOUNDARY; + int ilat = (int) (Lat / 0.25) + BOUNDARY; + + try { + // Prepare control points for spline interpolation + double[] x = {Lon, Lon + 0.25, Lon - 0.25}; + double[] y = {EGMGrid[ilon][ilat], EGMGrid[ilon + 1][ilat], EGMGrid[ilon - 1][ilat]}; + + // Call a method to compute cubic spline coefficients and evaluate + double correctedAltitude = cubicSplineInterpolation(x, y, Lon); + return correctedAltitude / 100; // Adjust if necessary + } catch (ArrayIndexOutOfBoundsException e) { + return EGM96_VALUE_INVALID; } - else return EGM96_VALUE_INVALID; } - /** - * Makes a copy of a file into a specified destination. - * - * @param in the source stream - * @param out the destination stream - */ private void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int read;