Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added spline interpolation #232

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 21 additions & 35 deletions app/src/main/java/eu/basicairdata/graziano/gpslogger/EGM96.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down