Import: re-order TrackPoints by time, so statistics can be computed.

Fixes #796.
This commit is contained in:
Dennis Guse
2021-06-01 17:45:23 +02:00
parent f1ee0ea035
commit 5b2d9a5773
4 changed files with 68 additions and 1 deletions
@@ -87,6 +87,40 @@ public class KMLImportTest {
GPXImportTest.assertTrackpoint(importedTrackPoints.get(4), TrackPoint.Type.SEGMENT_END_MANUAL, "2021-05-29T18:06:22.512Z", null, null, null);
}
/**
* Coordinates / when might not be ordered by increasing time.
*/
@LargeTest
@Test
public void kml22_time_decreases() throws IOException {
// given
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.kml22_time_decreases);
// when
// 1. import
importTrackId = importer.importFile(inputStream).get(0);
// then
// 1. track
Track importedTrack = contentProviderUtils.getTrack(importTrackId);
assertNotNull(importedTrack);
assertEquals("", importedTrack.getCategory());
assertEquals("", importedTrack.getDescription());
assertEquals("", importedTrack.getName());
assertEquals("", importedTrack.getIcon());
// 2. markers
assertEquals(0, contentProviderUtils.getMarkerCount(importTrackId));
// 3. trackpoints
List<TrackPoint> importedTrackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, importTrackId);
assertEquals(2, importedTrackPoints.size());
GPXImportTest.assertTrackpoint(importedTrackPoints.get(0), TrackPoint.Type.TRACKPOINT, "2021-05-29T18:06:21.767Z", 14.0, 3.0, 10.0);
GPXImportTest.assertTrackpoint(importedTrackPoints.get(1), TrackPoint.Type.SEGMENT_START_MANUAL, "2021-05-29T18:06:22.042Z", null, null, null);
}
/**
* Check that data with statistics markers is imported and those ignored.
* Statistics marker were created to avoid recomputing the track statistics (e.g., distance until a certain time).
@@ -9,7 +9,6 @@
<gx:interpolate>1</gx:interpolate>
<gx:Track>
<when>2021-05-29T18:06:21.767Z</when>
<gx:coord />
<gx:coord>14.0 3.0 10.0</gx:coord>
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<gx:MultiTrack>
<altitudeMode>absolute</altitudeMode>
<gx:interpolate>1</gx:interpolate>
<gx:Track>
<when>2021-05-29T18:06:22.042Z</when>
<gx:coord />
<when>2021-05-29T18:06:21.767Z</when>
<gx:coord>14.0 3.0 10.0</gx:coord>
</gx:Track>
</gx:MultiTrack>
</Placemark>
</Document>
</kml>
@@ -12,6 +12,7 @@ import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
@@ -139,6 +140,19 @@ public class TrackImporter {
track.setUuid(UUID.randomUUID());
}
Collections.sort(trackPoints, new Comparator<TrackPoint>() {
@Override
public int compare(TrackPoint o1, TrackPoint o2) {
if (o1.getTime().isBefore(o2.getTime())) {
return -1;
}
if (o1.getTime().isAfter(o2.getTime())) {
return 1;
}
return 0;
}
});
TrackStatisticsUpdater updater = new TrackStatisticsUpdater();
updater.addTrackPoints(trackPoints, recordingDistanceInterval);
track.setTrackStatistics(updater.getTrackStatistics());