From 2843d1945099790d9022bf5d672800b9606a6ab4 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 2 May 2021 19:06:42 +0200 Subject: [PATCH] UI presents altitude in EGM2008. --- .../opentracks/util/EGM2008UtilsTest.java | 16 ++++++------ .../opentracks/content/TrackDataHub.java | 25 +++++++++++++++++++ .../opentracks/util/EGM2008Utils.java | 4 +-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/util/EGM2008UtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/util/EGM2008UtilsTest.java index 2000e63a2..a64c93e31 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/util/EGM2008UtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/util/EGM2008UtilsTest.java @@ -75,7 +75,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(-14.8980, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -90,7 +90,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(30.15, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -105,7 +105,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(30.15, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -120,7 +120,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(30.15, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -135,7 +135,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(-17.2260, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -150,7 +150,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(-39.4865, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); @@ -170,7 +170,7 @@ public class EGM2008UtilsTest { trackPoint2.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint1.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint1.getLocation()); // then assertNotEquals(altitude_egm2008.correctAltitude(trackPoint1.getLocation()), altitude_egm2008.correctAltitude(trackPoint2.getLocation()), 0.0001); @@ -185,7 +185,7 @@ public class EGM2008UtilsTest { trackPoint.setAltitude(0); // when - EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createEGM2008Correction(context, trackPoint.getLocation()); + EGM2008Utils.EGM2008Correction altitude_egm2008 = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); // then assertEquals(-85.824, altitude_egm2008.correctAltitude(trackPoint.getLocation()), MAX_BILINEAR_ERROR); diff --git a/src/main/java/de/dennisguse/opentracks/content/TrackDataHub.java b/src/main/java/de/dennisguse/opentracks/content/TrackDataHub.java index 773270053..f0a5c5fd9 100644 --- a/src/main/java/de/dennisguse/opentracks/content/TrackDataHub.java +++ b/src/main/java/de/dennisguse/opentracks/content/TrackDataHub.java @@ -27,9 +27,11 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; +import java.io.IOException; import java.util.Collections; import java.util.Set; +import de.dennisguse.opentracks.content.data.Altitude; import de.dennisguse.opentracks.content.data.Distance; import de.dennisguse.opentracks.content.data.Marker; import de.dennisguse.opentracks.content.data.MarkerColumns; @@ -41,6 +43,7 @@ import de.dennisguse.opentracks.content.provider.ContentProviderUtils; import de.dennisguse.opentracks.content.provider.TrackPointIterator; import de.dennisguse.opentracks.services.TrackRecordingService; import de.dennisguse.opentracks.stats.TrackStatisticsUpdater; +import de.dennisguse.opentracks.util.EGM2008Utils; /** * Track data hub. @@ -72,6 +75,8 @@ public class TrackDataHub { private final ContentProviderUtils contentProviderUtils; private final int targetNumPoints; + private EGM2008Utils.EGM2008Correction egm2008Correction; + private boolean started; private HandlerThread handlerThread; private Handler handler; @@ -364,6 +369,8 @@ public class TrackDataHub { break; } + correctAltitude(trackPoint); + if (localFirstSeenTrackPointId == null) { localFirstSeenTrackPointId = trackPointId; } @@ -407,6 +414,24 @@ public class TrackDataHub { } } + private void correctAltitude(TrackPoint trackPoint) { + if (!trackPoint.hasLocation() || !trackPoint.hasAltitude()) { + Log.d(TAG, "No altitude correction necessary."); + return; + } + + if (egm2008Correction == null || !egm2008Correction.canCorrect(trackPoint.getLocation())) { + try { + egm2008Correction = EGM2008Utils.createCorrection(context, trackPoint.getLocation()); + } catch (IOException e) { + Log.e(TAG, "Could not load altitude correction for " + trackPoint, e); + return; + } + } + + trackPoint.setAltitude(Altitude.EGM2008.of(egm2008Correction.correctAltitude(trackPoint.getLocation()))); + } + /** * Resets the track points sampling states. */ diff --git a/src/main/java/de/dennisguse/opentracks/util/EGM2008Utils.java b/src/main/java/de/dennisguse/opentracks/util/EGM2008Utils.java index 71c2655d8..739a5f2b5 100644 --- a/src/main/java/de/dennisguse/opentracks/util/EGM2008Utils.java +++ b/src/main/java/de/dennisguse/opentracks/util/EGM2008Utils.java @@ -33,7 +33,7 @@ public class EGM2008Utils { private EGM2008Utils() { } - public static EGM2008Correction createEGM2008Correction(Context context, Location location) throws IOException { + public static EGM2008Correction createCorrection(Context context, Location location) throws IOException { Indices indices = getIndices(location); try (DataInputStream dataInputStream = new DataInputStream(context.getResources().openRawResource(EGM2008_5_DATA))) { @@ -84,7 +84,7 @@ public class EGM2008Utils { protected final int v01; protected final int v11; - public EGM2008Correction(Indices indices, DataInputStream dataInputStream) throws IOException { + private EGM2008Correction(Indices indices, DataInputStream dataInputStream) throws IOException { this.indices = indices; v00 = getUndulationRaw(dataInputStream, indices);