1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.BeforeAll;
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.spi.preferences.IPreferences;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
23 | import org.openstreetmap.josm.tools.date.DateUtilsTest;
|
---|
24 |
|
---|
25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Unit tests of {@link GpxImageCorrelation} class.
|
---|
29 | */
|
---|
30 | class GpxImageCorrelationTest {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Setup test.
|
---|
34 | */
|
---|
35 | @RegisterExtension
|
---|
36 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
37 | public JOSMTestRules test = new JOSMTestRules();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Setup test.
|
---|
41 | */
|
---|
42 | @BeforeAll
|
---|
43 | public static void setUp() {
|
---|
44 | DateUtilsTest.setTimeZone(DateUtils.UTC);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Tests matching of images to a GPX track.
|
---|
49 | * @throws Exception if the track cannot be parsed
|
---|
50 | */
|
---|
51 | @Test
|
---|
52 | void testMatchGpxTrack() throws Exception {
|
---|
53 | IPreferences s = Config.getPref();
|
---|
54 | final GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "tracks/tracks.gpx");
|
---|
55 | assertEquals(5, gpx.tracks.size());
|
---|
56 | assertEquals(1, gpx.tracks.iterator().next().getSegments().size());
|
---|
57 | assertEquals(128, gpx.tracks.iterator().next().getSegments().iterator().next().getWayPoints().size());
|
---|
58 |
|
---|
59 | final GpxImageEntry ib = new GpxImageEntry();
|
---|
60 | ib.setExifTime(DateUtils.fromString("2016:01:03 11:54:58")); // 5 minutes before start of GPX
|
---|
61 | ib.createTmp();
|
---|
62 | final GpxImageEntry i0 = new GpxImageEntry();
|
---|
63 | i0.setExifTime(DateUtils.fromString("2016:01:03 11:59:54")); // 4 sec before start of GPX
|
---|
64 | i0.createTmp();
|
---|
65 | final GpxImageEntry i1 = new GpxImageEntry();
|
---|
66 | i1.setExifTime(DateUtils.fromString("2016:01:03 12:04:01"));
|
---|
67 | i1.createTmp();
|
---|
68 | final GpxImageEntry i2 = new GpxImageEntry();
|
---|
69 | i2.setExifTime(DateUtils.fromString("2016:01:03 12:04:57"));
|
---|
70 | i2.createTmp();
|
---|
71 | final GpxImageEntry i3 = new GpxImageEntry();
|
---|
72 | i3.setExifTime(DateUtils.fromString("2016:01:03 12:05:05"));
|
---|
73 | i3.createTmp();
|
---|
74 | final GpxImageEntry i4 = new GpxImageEntry(); //Image close to two points with elevation, but without time
|
---|
75 | i4.setExifTime(DateUtils.fromString("2016:01:03 12:05:20"));
|
---|
76 | i4.createTmp();
|
---|
77 | final GpxImageEntry i5 = new GpxImageEntry(); //between two tracks, closer to first
|
---|
78 | i5.setExifTime(DateUtils.fromString("2016:01:03 12:07:00"));
|
---|
79 | i5.createTmp();
|
---|
80 | final GpxImageEntry i6 = new GpxImageEntry(); //between two tracks, closer to second (more than 1 minute from any track)
|
---|
81 | i6.setExifTime(DateUtils.fromString("2016:01:03 12:07:45"));
|
---|
82 | i6.createTmp();
|
---|
83 |
|
---|
84 | List<GpxImageEntry> images = Arrays.asList(ib, i0, i1, i2, i3, i4, i5, i6);
|
---|
85 |
|
---|
86 | // TEST #1: default settings
|
---|
87 | // tag images within 2 minutes to tracks/segments, interpolate between segments only
|
---|
88 | assertEquals(7, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
89 | assertEquals(null, ib.getPos());
|
---|
90 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), i0.getPos()); // start of track
|
---|
91 | assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos()); // exact match
|
---|
92 | assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos()); // exact match
|
---|
93 | assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2),
|
---|
94 | i2.getPos()); // interpolated
|
---|
95 | assertEquals(new CachedLatLon(47.197568312311816, 8.790292849679897),
|
---|
96 | i4.getPos()); // interpolated between points without timestamp
|
---|
97 | assertEquals(new CachedLatLon(47.19819249585271, 8.78536943346262),
|
---|
98 | i5.getPos()); // tagged to last WP of first track, because closer and within 2 min (default setting)
|
---|
99 | assertEquals(new CachedLatLon(47.20138901844621, 8.774476982653141),
|
---|
100 | i6.getPos()); // tagged to first WP of second track, because closer and within 2 min (default setting)
|
---|
101 | assertFalse(ib.hasNewGpsData());
|
---|
102 | assertTrue(i0.hasNewGpsData() && i1.hasNewGpsData() && i2.hasNewGpsData() && i3.hasNewGpsData()
|
---|
103 | && i4.hasNewGpsData() && i5.hasNewGpsData() && i6.hasNewGpsData());
|
---|
104 | // First waypoint has no speed in matchGpxTrack(). Speed is calculated
|
---|
105 | // and not taken from GPX track.
|
---|
106 | assertEquals(null, ib.getSpeed());
|
---|
107 | assertEquals(null, i0.getSpeed());
|
---|
108 | assertEquals(Double.valueOf(11.675317966018756), i1.getSpeed(), 0.000001);
|
---|
109 | assertEquals(Double.valueOf(24.992418392716967), i2.getSpeed(), 0.000001);
|
---|
110 | assertEquals(Double.valueOf(27.307968754679223), i3.getSpeed(), 0.000001);
|
---|
111 | assertEquals(null, ib.getElevation());
|
---|
112 | assertEquals(null, i0.getElevation());
|
---|
113 | assertEquals(Double.valueOf(489.29), i1.getElevation(), 0.000001);
|
---|
114 | assertEquals(Double.valueOf((490.40 + 489.75) / 2), i2.getElevation(), 0.000001);
|
---|
115 | assertEquals(Double.valueOf(486.368333333), i3.getElevation(), 0.000001);
|
---|
116 | // interpolated elevation between trackpoints with interpolated timestamps
|
---|
117 | assertEquals(Double.valueOf(475.393978719), i4.getElevation(), 0.000001);
|
---|
118 | assertEquals(null, i5.getElevation());
|
---|
119 | assertEquals(null, i6.getElevation());
|
---|
120 |
|
---|
121 | assertEquals(null, ib.getGpsTime());
|
---|
122 | assertEquals(DateUtils.fromString("2016:01:03 11:59:54"), i0.getGpsTime()); // original time is kept
|
---|
123 | assertEquals(DateUtils.fromString("2016:01:03 12:04:01"), i1.getGpsTime());
|
---|
124 | assertEquals(DateUtils.fromString("2016:01:03 12:04:57"), i2.getGpsTime());
|
---|
125 | assertEquals(DateUtils.fromString("2016:01:03 12:05:05"), i3.getGpsTime());
|
---|
126 |
|
---|
127 | clearTmp(images);
|
---|
128 |
|
---|
129 | // TEST #2: Disable all interpolation and tagging close to tracks. Only i1-i4 are tagged
|
---|
130 |
|
---|
131 | s.putBoolean("geoimage.trk.tag", false);
|
---|
132 | s.putBoolean("geoimage.trk.int", false);
|
---|
133 | s.putBoolean("geoimage.seg.tag", false);
|
---|
134 | s.putBoolean("geoimage.seg.int", false);
|
---|
135 |
|
---|
136 | assertEquals(4, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
137 | assertEquals(null, ib.getPos());
|
---|
138 | assertEquals(null, i0.getPos());
|
---|
139 | assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos());
|
---|
140 | assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2), i2.getPos());
|
---|
141 | assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos());
|
---|
142 | assertEquals(new CachedLatLon(47.197568312311816, 8.790292849679897), i4.getPos());
|
---|
143 | assertEquals(null, i5.getPos());
|
---|
144 | assertEquals(null, i6.getPos());
|
---|
145 |
|
---|
146 | clearTmp(images);
|
---|
147 |
|
---|
148 | // TEST #3: Disable all interpolation and allow tagging within 1 minute of a track. i0-i5 are tagged.
|
---|
149 | // i6 will not be tagged, because it's 68 seconds away from the next waypoint in either direction
|
---|
150 |
|
---|
151 | s.putBoolean("geoimage.trk.tag", true);
|
---|
152 | s.putBoolean("geoimage.trk.tag.time", true);
|
---|
153 | s.putInt("geoimage.trk.tag.time.val", 1);
|
---|
154 |
|
---|
155 | s.putBoolean("geoimage.trk.int", false);
|
---|
156 | s.putBoolean("geoimage.seg.tag", false);
|
---|
157 | s.putBoolean("geoimage.seg.int", false);
|
---|
158 |
|
---|
159 | assertEquals(6, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
160 | assertEquals(null, ib.getPos());
|
---|
161 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), i0.getPos());
|
---|
162 | assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos());
|
---|
163 | assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2), i2.getPos());
|
---|
164 | assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos());
|
---|
165 | assertEquals(new CachedLatLon(47.197568312311816, 8.790292849679897), i4.getPos());
|
---|
166 | assertEquals(new CachedLatLon(47.19819249585271, 8.78536943346262), i5.getPos());
|
---|
167 | assertEquals(null, i6.getPos());
|
---|
168 |
|
---|
169 | clearTmp(images);
|
---|
170 |
|
---|
171 | // TEST #4: Force tagging (parameter forceTags=true, no change of configuration). All images will be tagged
|
---|
172 | // i5-i6 will now be interpolated, therefore it will have an elevation and different coordinates than in tests above
|
---|
173 |
|
---|
174 | assertEquals(8, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, true));
|
---|
175 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), ib.getPos());
|
---|
176 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), i0.getPos());
|
---|
177 | assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos());
|
---|
178 | assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2), i2.getPos());
|
---|
179 | assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos());
|
---|
180 | assertEquals(new CachedLatLon(47.197568312311816, 8.790292849679897), i4.getPos());
|
---|
181 | assertEquals(new CachedLatLon(47.198845306804905, 8.783144918860685), i5.getPos()); // interpolated between tracks
|
---|
182 | assertEquals(new CachedLatLon(47.19985828931693, 8.77969308585768), i6.getPos()); // different values than in tests #1 and #3!
|
---|
183 |
|
---|
184 | assertEquals(Double.valueOf(447.894014085), i5.getElevation(), 0.000001);
|
---|
185 | assertEquals(Double.valueOf(437.395070423), i6.getElevation(), 0.000001);
|
---|
186 |
|
---|
187 | clearTmp(images);
|
---|
188 |
|
---|
189 | // TEST #5: Force tagging (parameter forceTags=false, but configuration changed).
|
---|
190 | // Results same as #4
|
---|
191 |
|
---|
192 | s.putBoolean("geoimage.trk.tag", true);
|
---|
193 | s.putBoolean("geoimage.trk.tag.time", false);
|
---|
194 | s.putBoolean("geoimage.trk.int", true);
|
---|
195 | s.putBoolean("geoimage.trk.int.time", false);
|
---|
196 | s.putBoolean("geoimage.trk.int.dist", false);
|
---|
197 | s.putBoolean("geoimage.seg.tag", false);
|
---|
198 | s.putBoolean("geoimage.seg.int", false);
|
---|
199 | s.putBoolean("geoimage.seg.tag.time", false);
|
---|
200 | s.putBoolean("geoimage.seg.int", true);
|
---|
201 | s.putBoolean("geoimage.seg.int.time", false);
|
---|
202 | s.putBoolean("geoimage.seg.int.dist", false);
|
---|
203 |
|
---|
204 | assertEquals(8, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
205 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), ib.getPos());
|
---|
206 | assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), i0.getPos());
|
---|
207 | assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos());
|
---|
208 | assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2), i2.getPos());
|
---|
209 | assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos());
|
---|
210 | assertEquals(new CachedLatLon(47.197568312311816, 8.790292849679897), i4.getPos());
|
---|
211 | assertEquals(new CachedLatLon(47.198845306804905, 8.783144918860685), i5.getPos());
|
---|
212 | assertEquals(new CachedLatLon(47.19985828931693, 8.77969308585768), i6.getPos());
|
---|
213 |
|
---|
214 | assertEquals(Double.valueOf(447.894014085), i5.getElevation(), 0.000001);
|
---|
215 | assertEquals(Double.valueOf(437.395070423), i6.getElevation(), 0.000001);
|
---|
216 |
|
---|
217 | clearTmp(images);
|
---|
218 |
|
---|
219 | // TEST #6: Disable tagging but allow interpolation when tracks are less than 500m apart. i0-i4 are tagged.
|
---|
220 | // i5-i6 will not be tagged, because the tracks are 897m apart.
|
---|
221 | // not checking all the coordinates again, did that 5 times already, just the number of matched images
|
---|
222 |
|
---|
223 | s.putBoolean("geoimage.trk.tag", false);
|
---|
224 | s.putBoolean("geoimage.trk.int", true);
|
---|
225 | s.putBoolean("geoimage.trk.int.time", false);
|
---|
226 | s.putBoolean("geoimage.trk.int.dist", true);
|
---|
227 | s.putInt("geoimage.trk.int.dist.val", 500);
|
---|
228 | s.putBoolean("geoimage.seg.tag", false);
|
---|
229 | s.putBoolean("geoimage.seg.int", false);
|
---|
230 |
|
---|
231 | assertEquals(4, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
232 | clearTmp(images);
|
---|
233 |
|
---|
234 | // TEST #7: Disable tagging but allow interpolation when tracks are less than 1000m apart. i0-i6 are tagged.
|
---|
235 | // i5-i6 will be tagged, because the tracks are 897m apart.
|
---|
236 |
|
---|
237 | s.putInt("geoimage.trk.int.dist.val", 1000);
|
---|
238 |
|
---|
239 | assertEquals(6, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
240 | clearTmp(images);
|
---|
241 |
|
---|
242 | // TEST #8: Disable tagging but allow interpolation when tracks are less than 2 min apart. i0-i4 are tagged.
|
---|
243 | // i5-i6 will not be tagged, because the tracks are 2.5min apart.
|
---|
244 |
|
---|
245 | s.putBoolean("geoimage.trk.tag", false);
|
---|
246 | s.putBoolean("geoimage.trk.int", true);
|
---|
247 | s.putBoolean("geoimage.trk.int.time", true);
|
---|
248 | s.putInt("geoimage.trk.int.time.val", 2);
|
---|
249 | s.putBoolean("geoimage.trk.int.dist", false);
|
---|
250 | s.putBoolean("geoimage.seg.tag", false);
|
---|
251 | s.putBoolean("geoimage.seg.int", false);
|
---|
252 |
|
---|
253 | assertEquals(4, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
254 | clearTmp(images);
|
---|
255 |
|
---|
256 | // TEST #9: Disable tagging but allow interpolation when tracks are less than 3 min apart. i0-i6 are tagged.
|
---|
257 | // i5-i6 will be tagged, because the tracks are 2.5min apart.
|
---|
258 |
|
---|
259 | s.putInt("geoimage.trk.int.time.val", 3);
|
---|
260 |
|
---|
261 | assertEquals(6, GpxImageCorrelation.matchGpxTrack(images, gpx, 0, false));
|
---|
262 |
|
---|
263 | }
|
---|
264 |
|
---|
265 | private void clearTmp(List<GpxImageEntry> imgs) {
|
---|
266 | for (GpxImageEntry i : imgs) {
|
---|
267 | i.discardTmp();
|
---|
268 | i.createTmp();
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Unit test of {@link GpxImageCorrelation#getElevation}
|
---|
274 | */
|
---|
275 | @Test
|
---|
276 | void testGetElevation() {
|
---|
277 | assertNull(GpxImageCorrelation.getElevation(null));
|
---|
278 | WayPoint wp = new WayPoint(LatLon.ZERO);
|
---|
279 | assertNull(GpxImageCorrelation.getElevation(wp));
|
---|
280 | wp.put(GpxConstants.PT_ELE, "");
|
---|
281 | assertNull(GpxImageCorrelation.getElevation(wp));
|
---|
282 | wp.put(GpxConstants.PT_ELE, "not a number");
|
---|
283 | assertNull(GpxImageCorrelation.getElevation(wp));
|
---|
284 | wp.put(GpxConstants.PT_ELE, "150.0");
|
---|
285 | assertEquals(Double.valueOf(150.0d), GpxImageCorrelation.getElevation(wp));
|
---|
286 | }
|
---|
287 | }
|
---|