X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FSpeedCalculator.java;h=49fd05665a90e8788e3806e5f740d773f7c4cc0f;hp=e80e21498e6b3ffa04e3b30b3a959854a5d2563b;hb=ff33ebba6b7c62834f6dae16ce33eb2c710b160e;hpb=0a2480df5845e2d7190dfdec9b2653b1609e853d diff --git a/tim/prune/data/SpeedCalculator.java b/tim/prune/data/SpeedCalculator.java index e80e214..49fd056 100644 --- a/tim/prune/data/SpeedCalculator.java +++ b/tim/prune/data/SpeedCalculator.java @@ -50,14 +50,14 @@ public abstract class SpeedCalculator do { p = inTrack.getPoint(index); - boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isAfter(point.getTimestamp()); + boolean timeOk = p != null && p.hasTimestamp() && p.getTimestamp().isBefore(point.getTimestamp()); boolean pValid = timeOk && !p.isWaypoint(); if (pValid) { totalRadians += DataPoint.calculateRadiansBetween(p, q); earlyStamp = p.getTimestamp(); } - stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp()) - || p.getSegmentStart()); + + stop = (p == null) || p.getSegmentStart() || hasSufficientTimeDifference(p, point); index--; if (p != null && !p.isWaypoint()) { q = p; @@ -78,8 +78,8 @@ public abstract class SpeedCalculator totalRadians += DataPoint.calculateRadiansBetween(p, q); lateStamp = p.getTimestamp(); } - stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp()) - || p.getSegmentStart()); + + stop = (p == null) || p.getSegmentStart() || hasSufficientTimeDifference(point, p); index++; if (p != null && !p.isWaypoint()) { q = p; @@ -146,14 +146,14 @@ public abstract class SpeedCalculator do { p = inTrack.getPoint(index); - boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isAfter(point.getTimestamp()); + boolean timeOk = p != null && p.hasTimestamp() && p.getTimestamp().isBefore(point.getTimestamp()); boolean pValid = timeOk && !p.isWaypoint(); if (pValid) { earlyStamp = p.getTimestamp(); if (p.hasAltitude()) firstAlt = p.getAltitude(); } - stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp()) - || p.getSegmentStart()); + + stop = (p == null) || p.getSegmentStart() || hasSufficientTimeDifference(p, point); index--; } while (!stop); @@ -172,8 +172,8 @@ public abstract class SpeedCalculator lateStamp = p.getTimestamp(); if (p.hasAltitude()) lastAlt = p.getAltitude(); } - stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp()) - || p.getSegmentStart()); + + stop = (p == null) || p.getSegmentStart() || hasSufficientTimeDifference(point, p); index++; } while (!stop); @@ -194,4 +194,20 @@ public abstract class SpeedCalculator inValue.setValue(speedValue); } } + + /** + * Check whether the time difference between P1 and P2 is sufficiently large + * @param inP1 earlier point + * @param inP2 later point + * @return true if we can stop looking now, found a point early/late enough + */ + private static boolean hasSufficientTimeDifference(DataPoint inP1, DataPoint inP2) + { + if (inP1 == null || inP2 == null) + return true; // we have to give up now + if (!inP1.hasTimestamp() || !inP2.hasTimestamp()) + return false; // keep looking + final long MIN_TIME_DIFFERENCE_MS = 1000L; + return inP2.getTimestamp().getMillisecondsSince(inP1.getTimestamp()) >= MIN_TIME_DIFFERENCE_MS; + } }