001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.gpx; 003 004import java.util.Arrays; 005import java.util.ArrayList; 006import java.util.Collection; 007import java.util.List; 008import java.util.Locale; 009import java.util.concurrent.TimeUnit; 010 011import org.openstreetmap.josm.data.coor.ILatLon; 012import org.openstreetmap.josm.data.coor.LatLon; 013import org.openstreetmap.josm.data.projection.Projection; 014import org.openstreetmap.josm.data.projection.ProjectionRegistry; 015import org.openstreetmap.josm.spi.preferences.Config; 016import org.openstreetmap.josm.tools.Logging; 017import org.openstreetmap.josm.tools.Pair; 018import org.openstreetmap.josm.tools.Utils; 019 020/** 021 * Correlation logic for {@code CorrelateGpxWithImages}. 022 * @since 14205 023 */ 024public final class GpxImageCorrelation { 025 026 private GpxImageCorrelation() { 027 // Hide public constructor 028 } 029 030 /** 031 * Match a list of photos to a gpx track with given settings. 032 * All images need a exifTime attribute and the List must be sorted according to these times. 033 * @param images images to match 034 * @param selectedGpx selected GPX data 035 * @param settings correlation settings 036 * @return number of matched points 037 */ 038 public static int matchGpxTrack(List<? extends GpxImageEntry> images, GpxData selectedGpx, GpxImageCorrelationSettings settings) { 039 if (Logging.isDebugEnabled()) { 040 Logging.debug("Correlating {0} images to {1} GPX track segments using {2}", 041 images.size(), selectedGpx.getTrackSegsCount(), settings); 042 } 043 044 boolean trkInt, trkTag, segInt, segTag; 045 int trkTime, trkDist, trkTagTime, segTime, segDist, segTagTime; 046 047 if (settings.isForceTags()) { 048 // temporary option to override advanced settings and activate all possible interpolations / tagging methods 049 trkInt = trkTag = segInt = segTag = true; 050 trkTime = trkDist = trkTagTime = segTime = segDist = segTagTime = Integer.MAX_VALUE; 051 } else { 052 // Load the settings 053 trkInt = Config.getPref().getBoolean("geoimage.trk.int", false); 054 trkTime = Config.getPref().getBoolean("geoimage.trk.int.time", false) ? 055 Config.getPref().getInt("geoimage.trk.int.time.val", 60) : Integer.MAX_VALUE; 056 trkDist = Config.getPref().getBoolean("geoimage.trk.int.dist", false) ? 057 Config.getPref().getInt("geoimage.trk.int.dist.val", 50) : Integer.MAX_VALUE; 058 059 trkTag = Config.getPref().getBoolean("geoimage.trk.tag", true); 060 trkTagTime = Config.getPref().getBoolean("geoimage.trk.tag.time", true) ? 061 Config.getPref().getInt("geoimage.trk.tag.time.val", 2) : Integer.MAX_VALUE; 062 063 segInt = Config.getPref().getBoolean("geoimage.seg.int", true); 064 segTime = Config.getPref().getBoolean("geoimage.seg.int.time", true) ? 065 Config.getPref().getInt("geoimage.seg.int.time.val", 60) : Integer.MAX_VALUE; 066 segDist = Config.getPref().getBoolean("geoimage.seg.int.dist", true) ? 067 Config.getPref().getInt("geoimage.seg.int.dist.val", 50) : Integer.MAX_VALUE; 068 069 segTag = Config.getPref().getBoolean("geoimage.seg.tag", true); 070 segTagTime = Config.getPref().getBoolean("geoimage.seg.tag.time", true) ? 071 Config.getPref().getInt("geoimage.seg.tag.time.val", 2) : Integer.MAX_VALUE; 072 } 073 074 final GpxImageDirectionPositionSettings dirpos = settings.getDirectionPositionSettings(); 075 final GpxImageDatumSettings datumSettings = settings.getDatumSettings(); 076 final long offset = settings.getOffset(); 077 final TimeSource imgTimeSource = settings.getImgTimeSource(); 078 079 boolean isFirst = true; 080 long prevWpTime = 0; 081 WayPoint prevWp = null; 082 083 int ret = 0; 084 085 for (List<List<WayPoint>> segs : loadTracks(selectedGpx.getTracks())) { 086 boolean firstSegment = true; 087 for (List<WayPoint> wps : segs) { 088 int size = wps.size(); 089 for (int i = 0; i < size; i++) { 090 final WayPoint curWp = wps.get(i); 091 // Interpolate timestamps in the segment, if one or more waypoints miss them 092 if (!curWp.hasDate()) { 093 //check if any of the following waypoints has a timestamp... 094 if (i > 0 && wps.get(i - 1).hasDate()) { 095 long prevWpTimeNoOffset = wps.get(i - 1).getTimeInMillis(); 096 double totalDist = 0; 097 List<Pair<Double, WayPoint>> nextWps = new ArrayList<>(); 098 for (int j = i; j < size; j++) { 099 totalDist += wps.get(j - 1).greatCircleDistance(wps.get(j)); 100 nextWps.add(new Pair<>(totalDist, wps.get(j))); 101 if (wps.get(j).hasDate()) { 102 // ...if yes, interpolate everything in between 103 long timeDiff = wps.get(j).getTimeInMillis() - prevWpTimeNoOffset; 104 for (Pair<Double, WayPoint> pair : nextWps) { 105 pair.b.setTimeInMillis((long) (prevWpTimeNoOffset + (timeDiff * (pair.a / totalDist)))); 106 } 107 break; 108 } 109 } 110 if (!curWp.hasDate()) { 111 break; //It's pointless to continue with this segment, because none of the following waypoints had a timestamp 112 } 113 } else { 114 // Timestamps on waypoints without preceding timestamps in the same segment can not be interpolated, so try next one 115 continue; 116 } 117 } 118 119 final long curWpTime = curWp.getTimeInMillis() + offset; 120 boolean interpolate = true; 121 int tagTime = 0; 122 if (i == 0) { 123 if (firstSegment) { 124 // First segment of the track, so apply settings for tracks 125 firstSegment = false; 126 if (!trkInt || isFirst || prevWp == null || 127 Math.abs(curWpTime - prevWpTime) > TimeUnit.MINUTES.toMillis(trkTime) || 128 prevWp.greatCircleDistance(curWp) > trkDist) { 129 isFirst = false; 130 interpolate = false; 131 if (trkTag) { 132 tagTime = trkTagTime; 133 } 134 } 135 } else { 136 // Apply settings for segments 137 if (!segInt || prevWp == null || 138 Math.abs(curWpTime - prevWpTime) > TimeUnit.MINUTES.toMillis(segTime) || 139 prevWp.greatCircleDistance(curWp) > segDist) { 140 interpolate = false; 141 if (segTag) { 142 tagTime = segTagTime; 143 } 144 } 145 } 146 } 147 WayPoint nextWp = i < size - 1 ? wps.get(i + 1) : null; 148 ret += matchPoints(images, prevWp, prevWpTime, curWp, curWpTime, imgTimeSource, offset, 149 interpolate, tagTime, nextWp, dirpos, datumSettings); 150 prevWp = curWp; 151 prevWpTime = curWpTime; 152 } 153 } 154 } 155 if (trkTag && prevWp != null) { 156 ret += matchPoints(images, prevWp, prevWpTime, prevWp, prevWpTime, imgTimeSource, offset, 157 false, trkTagTime, null, dirpos, datumSettings); 158 } 159 Logging.debug("Correlated {0} total points", ret); 160 return ret; 161 } 162 163 static List<List<List<WayPoint>>> loadTracks(Collection<IGpxTrack> tracks) { 164 List<List<List<WayPoint>>> trks = new ArrayList<>(); 165 for (IGpxTrack trk : tracks) { 166 List<List<WayPoint>> segs = new ArrayList<>(); 167 for (IGpxTrackSegment seg : trk.getSegments()) { 168 List<WayPoint> wps = new ArrayList<>(seg.getWayPoints()); 169 if (!wps.isEmpty()) { 170 //remove waypoints at the beginning of the track/segment without timestamps 171 int wp; 172 for (wp = 0; wp < wps.size(); wp++) { 173 if (wps.get(wp).hasDate()) { 174 break; 175 } 176 } 177 if (wp == 0) { 178 segs.add(wps); 179 } else if (wp < wps.size()) { 180 segs.add(wps.subList(wp, wps.size())); 181 } 182 } 183 } 184 //sort segments by first waypoint 185 if (!segs.isEmpty()) { 186 segs.sort((o1, o2) -> { 187 if (o1.isEmpty() || o2.isEmpty()) 188 return 0; 189 return o1.get(0).compareTo(o2.get(0)); 190 }); 191 trks.add(segs); 192 } 193 } 194 //sort tracks by first waypoint of first segment 195 trks.sort((o1, o2) -> { 196 if (o1.isEmpty() || o1.get(0).isEmpty() 197 || o2.isEmpty() || o2.get(0).isEmpty()) 198 return 0; 199 return o1.get(0).get(0).compareTo(o2.get(0).get(0)); 200 }); 201 return trks; 202 } 203 204 /** 205 * Gets the elevation value from a WayPoint's attributes. 206 * @param wp the WayPoint 207 * @return the WayPoint's elevation (in meters) 208 */ 209 static Double getElevation(WayPoint wp) { 210 if (wp != null) { 211 String value = wp.getString(GpxConstants.PT_ELE); 212 if (!Utils.isEmpty(value)) { 213 try { 214 return Double.valueOf(value); 215 } catch (NumberFormatException e) { 216 Logging.warn(e); 217 } 218 } 219 } 220 return null; 221 } 222 223 /** 224 * Gets the horizontal positioning estimated error value from a WayPoint's attributes. 225 * @param wp the WayPoint 226 * @return the WayPoint's horizontal positioning error (in meters) 227 * @since 19387 228 */ 229 static Double getHPosErr(WayPoint wp) { 230 if (wp != null) { 231 if (wp.attr.get(GpxConstants.PT_STD_HDEV) instanceof Float) { 232 Float hposerr = (Float) wp.attr.get(GpxConstants.PT_STD_HDEV); 233 if (hposerr != null) { 234 return hposerr.doubleValue(); 235 } 236 } else if (wp.attr.get(GpxConstants.PT_STD_HDEV) instanceof Double) { 237 Double hposerr = (Double) wp.attr.get(GpxConstants.PT_STD_HDEV); 238 if (hposerr != null) { 239 return hposerr; 240 } 241 } 242 } 243 return null; 244 } 245 246 /** 247 * Retrieves GPS Dilution of Precision (DOP) from a WayPoint's attributes. 248 * Checks for Position DOP (PDOP) first, then falls back to Horizontal DOP (HDOP) if PDOP isn't available. 249 * Converts Float values to Double for consistent return type. 250 * @param wp the WayPoint 251 * @return the WayPoint's DOP value as Double (PDOP preferred, HDOP fallback) 252 * @since 19387 253 */ 254 static Double getGpsDop(WayPoint wp) { 255 if (wp != null) { 256 if (wp.attr.get(GpxConstants.PT_PDOP) != null) { 257 if (wp.attr.get(GpxConstants.PT_PDOP) instanceof Float) { 258 Float pdopvalue = (Float) wp.attr.get(GpxConstants.PT_PDOP); 259 return pdopvalue.doubleValue(); 260 } else if (wp.attr.get(GpxConstants.PT_PDOP) instanceof Double) { 261 return (Double) wp.attr.get(GpxConstants.PT_PDOP); 262 } 263 } else if (wp.attr.get(GpxConstants.PT_HDOP) != null) { 264 if (wp.attr.get(GpxConstants.PT_HDOP) instanceof Float) { 265 Float hdopvalue = (Float) wp.attr.get(GpxConstants.PT_HDOP); 266 return hdopvalue.doubleValue(); 267 } else if (wp.attr.get(GpxConstants.PT_HDOP) instanceof Double) { 268 return (Double) wp.attr.get(GpxConstants.PT_HDOP); 269 } 270 } 271 } 272 return null; 273 } 274 275 /** 276 * Gets the track direction angle value from a waypoint in a GNSS track. 277 * This angle is the GNSS receiver movement direction. 278 * @param wp the waypoint 279 * @return the waypoint direction (in degrees) 280 * @since 19387 281 */ 282 static Double getGpsTrack(WayPoint wp) { 283 if (wp != null) { 284 String trackvalue = wp.getString(GpxConstants.PT_COURSE); 285 Logging.debug("track angle value: {0}", trackvalue); 286 if (!Utils.isEmpty(trackvalue)) { 287 try { 288 return Double.valueOf(trackvalue); 289 } catch (NumberFormatException e) { 290 Logging.warn(e); 291 } 292 } 293 } 294 return null; 295 } 296 297 /** 298 * Determines the GPS processing method based on previous and current GNSS fix modes. 299 * This method compares the positioning modes of the previous and current GNSS fixes, 300 * selects the most basic processing method (lowest index in positioningModes list), 301 * and formats the output string according to predefined conventions. 302 * Because the returned processing method depends on a time correlation between an image 303 * and a waypoint timestamp, the term 'CORRELATION' is added. 304 * @param prevGpsFixMode the previous GNSS fix mode (e.g., "SINGLE", "DGNSS", "RTK_FIX") 305 * @param curGpsFixMode the current GNSS fix mode 306 * @param positioningModes list of positioning modes ordered by accuracy 307 * @return formatted processing method string 308 * @since 19387 309 */ 310 static String getGpsProcMethod(String prevGpsFixMode, final String curGpsFixMode, 311 final List<String> positioningModes) { 312 String gpsProcMethod = null; 313 Integer lowestProcIndex = null; 314 int lowestGnssModeIdx = 3; // 2d or higher index in positioningModes list are Gnss methods 315 try { 316 lowestProcIndex = Math.min(positioningModes.indexOf(prevGpsFixMode), positioningModes.indexOf(curGpsFixMode)); 317 if (lowestProcIndex < 0) { 318 return null; 319 } 320 gpsProcMethod = "GNSS" + " " + positioningModes.get(lowestProcIndex).toUpperCase(Locale.ENGLISH) + " " + "CORRELATION"; 321 if (lowestProcIndex < lowestGnssModeIdx) { 322 gpsProcMethod = positioningModes.get(lowestProcIndex).toUpperCase(Locale.ENGLISH) + " " + "CORRELATION"; 323 } else { 324 gpsProcMethod = "GNSS" + " " + positioningModes.get(lowestProcIndex).toUpperCase(Locale.ENGLISH) + " " + "CORRELATION"; 325 } 326 gpsProcMethod = gpsProcMethod.replace("FLOAT RTK", "RTK_FLOAT"); 327 gpsProcMethod = gpsProcMethod.replace(" RTK ", " RTK_FIX "); 328 } catch (ArrayIndexOutOfBoundsException ex) { 329 Logging.warn(ex); 330 } 331 return gpsProcMethod; 332 } 333 334 /** 335 * Determines if the GNSS mode is 2d or 3d, based on previous and current GNSS fix modes. 336 * This method compares the positioning modes of the previous and current GNSS fixes, 337 * selects the most basic processing method (lowest index in positioningModes list), 338 * and return the lowest value between 2d or 3d mode, or null if it's not a gnss mode (e.g. estimated, manual). 339 * @param prevGpsFixMode the previous GNSS mode 340 * @param curGpsFixMode the current GNSS mode 341 * @param positioningModes list of positioning modes ordered by accuracy 342 * @return 2 for 2d, 3 for 3d, or null 343 * @since 19387 344 */ 345 static Integer getGps2d3dMode(String prevGpsFixMode, final String curGpsFixMode, 346 final List<String> positioningModes) { 347 Integer lowestMode = null; 348 lowestMode = Math.min(positioningModes.indexOf(prevGpsFixMode), positioningModes.indexOf(curGpsFixMode)); 349 if (lowestMode > 3) { 350 return 3; 351 } 352 if (lowestMode > 2) { 353 return 2; 354 } 355 return null; 356 } 357 358 // CHECKSTYLE.OFF: ParameterNumber 359 private static int matchPoints(List<? extends GpxImageEntry> 360 images, 361 WayPoint prevWp, 362 long prevWpTime, 363 WayPoint curWp, 364 long curWpTime, 365 TimeSource imgTimeSource, 366 long offset, 367 boolean interpolate, 368 int tagTime, 369 WayPoint nextWp, 370 GpxImageDirectionPositionSettings dirpos, 371 GpxImageDatumSettings datumSettings) { 372 373 final boolean isLast = nextWp == null; 374 375 // i is the index of the timewise last photo that has the same or earlier EXIF time 376 int i; 377 if (isLast) { 378 i = images.size() - 1; 379 } else { 380 i = getLastIndexOfListBefore(images, curWpTime, imgTimeSource); 381 } 382 383 if (Logging.isDebugEnabled()) { 384 Logging.debug("Correlating images for i={1} - curWp={2}/{3} - prevWp={4}/{5} - nextWp={6} - tagTime={7} - interpolate={8}", 385 i, curWp, curWpTime, prevWp, prevWpTime, nextWp, tagTime, interpolate); 386 } 387 388 // no photos match 389 if (i < 0) { 390 Logging.debug("Correlated nothing, no photos match"); 391 return 0; 392 } 393 394 int ret = 0; 395 Double speed = null; 396 Double prevElevation = null; 397 Double prevHPosErr = null; 398 Double prevGpsDop = null; 399 Double prevGpsTrack = null; 400 String prevGpsFixMode = null; 401 //list of differential GPS mode 402 //TODO move these lists in Gpx.Constants? 403 final List<String> diffMode = Arrays.asList("dgps", "float rtk", "rtk"); 404 final List<String> positioningModes = Arrays.asList("none", "manual", "estimated", "2d", "3d", "dgps", "float rtk", "rtk"); 405 406 407 if (prevWp != null && interpolate) { 408 double distance = prevWp.greatCircleDistance(curWp); 409 // This is in km/h, 3.6 * m/s 410 if (curWpTime > prevWpTime) { 411 speed = 3600 * distance / (curWpTime - prevWpTime); 412 } 413 prevElevation = getElevation(prevWp); 414 prevHPosErr = getHPosErr(prevWp); 415 prevGpsDop = getGpsDop(prevWp); 416 prevGpsTrack = getGpsTrack(prevWp); 417 prevGpsFixMode = prevWp.getString(GpxConstants.PT_FIX); 418 } 419 420 final Double curElevation = getElevation(curWp); 421 final Double curHPosErr = getHPosErr(curWp); 422 final Double curGpsDop = getGpsDop(curWp); 423 final Double curGpsTrack = getGpsTrack(curWp); 424 final String curGpsFixMode = curWp.getString(GpxConstants.PT_FIX); 425 426 if (!interpolate || isLast) { 427 final long half = Math.abs(curWpTime - prevWpTime) / 2; 428 while (i >= 0) { 429 final GpxImageEntry curImg = images.get(i); 430 final GpxImageEntry curTmp = curImg.getTmp(); 431 final long time = curImg.getTimeSourceInstant(imgTimeSource).toEpochMilli(); 432 if ((!isLast && time > curWpTime) || time < prevWpTime) { 433 break; 434 } 435 long tagms = TimeUnit.MINUTES.toMillis(tagTime); 436 if (!curTmp.hasNewGpsData() && 437 (Math.abs(time - curWpTime) <= tagms 438 || Math.abs(prevWpTime - time) <= tagms)) { 439 if (prevWp != null && time < curWpTime - half) { 440 curTmp.setPos(prevWp.getCoor()); 441 } else { 442 curTmp.setPos(curWp.getCoor()); 443 } 444 //TODO fix this, nextWp doesn't exist here 445 if (nextWp != null && dirpos.isSetImageDirection()) { 446 double direction = curWp.bearing(nextWp); 447 curTmp.setExifImgDir(computeDirection(direction, dirpos.getImageDirectionAngleOffset())); 448 } else if (nextWp == null && isLast && dirpos.isSetImageDirection()) { 449 //last image in the sequence without any next waypoint-> get previous direction angle 450 double direction = prevWp.bearing(curWp); 451 curTmp.setExifImgDir(computeDirection(direction, dirpos.getImageDirectionAngleOffset())); 452 } 453 curTmp.setGpsTime(curImg.getExifInstant().minusMillis(offset)); 454 curTmp.flagNewGpsData(); 455 curImg.tmpUpdated(); 456 457 ret++; 458 } 459 i--; 460 } 461 } else if (prevWp != null) { 462 // This code gives a simple linear interpolation of the coordinates between current and 463 // previous track point assuming a constant speed in between 464 @SuppressWarnings("null") 465 LatLon nextCoorForDirection = nextWp.getCoor(); 466 while (i >= 0) { 467 final GpxImageEntry curImg = images.get(i); 468 final long imgTime = curImg.getTimeSourceInstant(imgTimeSource).toEpochMilli(); 469 if (imgTime < prevWpTime) { 470 break; 471 } 472 final GpxImageEntry curTmp = curImg.getTmp(); 473 if (!curTmp.hasNewGpsData()) { 474 // The values of timeDiff are between 0 and 1, it is not seconds but a dimensionless variable 475 final double timeDiff = (double) (imgTime - prevWpTime) / Math.abs(curWpTime - prevWpTime); 476 final boolean shiftXY = dirpos.getShiftImageX() != 0d || dirpos.getShiftImageY() != 0d; 477 final LatLon prevCoor = prevWp.getCoor(); 478 final LatLon curCoor = curWp.getCoor(); 479 LatLon position = prevCoor.interpolate(curCoor, timeDiff); 480 if (nextCoorForDirection != null && (shiftXY || dirpos.isSetImageDirection())) { 481 double direction = position.bearing((ILatLon) nextCoorForDirection); 482 if (dirpos.isSetImageDirection()) { 483 curTmp.setExifImgDir(computeDirection(direction, dirpos.getImageDirectionAngleOffset())); 484 } 485 if (shiftXY) { 486 final Projection proj = ProjectionRegistry.getProjection(); 487 final double offsetX = dirpos.getShiftImageX(); 488 final double offsetY = dirpos.getShiftImageY(); 489 final double r = Math.sqrt(offsetX * offsetX + offsetY * offsetY); 490 final double orientation = (direction + LatLon.ZERO.bearing((ILatLon) new LatLon(offsetX, offsetY))) % (2 * Math.PI); 491 position = proj.eastNorth2latlon(proj.latlon2eastNorth(position) 492 .add(r * Math.sin(orientation), r * Math.cos(orientation))); 493 } 494 } 495 curTmp.setPos(position); 496 curTmp.setSpeed(speed); 497 if (curElevation != null && prevElevation != null) { 498 curTmp.setElevation(prevElevation + (curElevation - prevElevation) * timeDiff + dirpos.getElevationShift()); 499 } 500 501 // Add exif GpsHPositioningerror interpolated value 502 if (curHPosErr != null && prevHPosErr != null) { 503 Double interpolatedValue = prevHPosErr + (curHPosErr - prevHPosErr) * timeDiff; 504 curTmp.setExifHPosErr(Math.round(interpolatedValue*10000)/10000.0); 505 } 506 507 // Add exif GpsDifferentialMode 508 // Get previous and current waypoint differential. As no interpolation is possible, 509 // set differential mode to 0 if any waypoint isn't in differential mode. 510 if (prevGpsFixMode != null) { 511 if (diffMode.contains(prevGpsFixMode) && diffMode.contains(curGpsFixMode)) { 512 curTmp.setGpsDiffMode(1); 513 } else { 514 curTmp.setGpsDiffMode(0); 515 } 516 } 517 518 // Add exif GpsMeasureMode 519 if (prevGpsFixMode != null && curGpsFixMode != null) { 520 Integer gps2d3dMode = getGps2d3dMode(prevGpsFixMode, curGpsFixMode, positioningModes); 521 if (gps2d3dMode != null) { 522 curTmp.setGps2d3dMode(gps2d3dMode); 523 } 524 } 525 526 // Add exif GpsProcessingMethod. As no interpolation is possible, 527 // set processing method to the "lowest" previous and current processing method value. 528 if (prevGpsFixMode != null && curGpsFixMode != null) { 529 String gpsProcMethod = getGpsProcMethod(prevGpsFixMode, curGpsFixMode, positioningModes); 530 if (gpsProcMethod != null) { 531 curTmp.setExifGpsProcMethod(gpsProcMethod); 532 } 533 } 534 535 // Add Exif GpsDop with interpolated GPS DOP value 536 if (curGpsDop != null && prevGpsDop != null) { 537 Double interpolatedValue = prevGpsDop + (curGpsDop - prevGpsDop) * timeDiff; 538 curTmp.setExifGpsDop(Math.round(interpolatedValue*100)/100.0); 539 } 540 541 // Add Exif GpsTrack tag 542 if (dirpos.isSetGpxTrackDirection()) { 543 if (curGpsTrack != null && prevGpsTrack != null) { 544 curTmp.setExifGpsTrack(prevGpsTrack + (curGpsTrack - prevGpsTrack) * timeDiff); 545 } 546 } 547 548 // Add GpsDatum tag 549 if (datumSettings.isSetImageGpsDatum()) { 550 if (diffMode.contains(prevGpsFixMode) && diffMode.contains(curGpsFixMode)) { 551 curTmp.setExifGpsDatum(datumSettings.getImageGpsDatum()); 552 } else //without differential mode, datum is WGS-84 553 curTmp.setExifGpsDatum("WGS-84"); 554 } 555 556 curTmp.setGpsTime(curImg.getTimeSourceInstant(imgTimeSource).minusMillis(offset)); 557 curTmp.flagNewGpsData(); 558 curImg.tmpUpdated(); 559 560 nextCoorForDirection = curCoor; 561 ret++; 562 } 563 i--; 564 } 565 } 566 Logging.debug("Correlated {0} image(s)", ret); 567 return ret; 568 } 569 // CHECKSTYLE.ON: ParameterNumber 570 571 /** 572 * Computes an adjusted direction by applying an angular offset and normalizing the result between 0° and 360°. 573 * @param direction initial direction angle (in radians) 574 * @param angleOffset angular offset to apply (in degrees) 575 * @return resulting direction normalized to the range [0, 360) degrees 576 */ 577 private static double computeDirection(double direction, double angleOffset) { 578 return (Utils.toDegrees(direction) + angleOffset) % 360d; 579 } 580 581 /** 582 * Finds the last image in the sorted image list which is before the given time 583 * @param images list of images 584 * @param searchedTime time to search 585 * @return index of last image before given time 586 */ 587 private static int getLastIndexOfListBefore(List<? extends GpxImageEntry> images, long searchedTime, TimeSource imgTimeSource) { 588 int lstSize = images.size(); 589 590 // No photos or the first photo taken is later than the search period 591 if (lstSize == 0 || searchedTime < images.get(0).getTimeSourceInstant(imgTimeSource).toEpochMilli()) 592 return -1; 593 594 // The search period is later than the last photo 595 if (searchedTime > images.get(lstSize - 1).getTimeSourceInstant(imgTimeSource).toEpochMilli()) 596 return lstSize-1; 597 598 // The searched index is somewhere in the middle, do a binary search from the beginning 599 int curIndex; 600 int startIndex = 0; 601 int endIndex = lstSize-1; 602 while (endIndex - startIndex > 1) { 603 curIndex = (endIndex + startIndex) / 2; 604 if (searchedTime > images.get(curIndex).getTimeSourceInstant(imgTimeSource).toEpochMilli()) { 605 startIndex = curIndex; 606 } else { 607 endIndex = curIndex; 608 } 609 } 610 if (searchedTime < images.get(endIndex).getTimeSourceInstant(imgTimeSource).toEpochMilli()) 611 return startIndex; 612 613 // This final loop is to check if photos with the exact same EXIF time follows 614 while ((endIndex < (lstSize - 1)) && (images.get(endIndex).getTimeSourceInstant(imgTimeSource).toEpochMilli() 615 == images.get(endIndex + 1).getTimeSourceInstant(imgTimeSource).toEpochMilli())) { 616 endIndex++; 617 } 618 return endIndex; 619 } 620}