Ticket #21766: 21766.patch
| File 21766.patch, 13.7 KB (added by , 4 years ago) |
|---|
-
build.xml
4 4 <!-- enter the SVN commit message --> 5 5 <property name="commit.message" value=""/> 6 6 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 7 <property name="plugin.main.version" value="1 4153"/>7 <property name="plugin.main.version" value="17715"/> 8 8 9 9 <property name="plugin.author" value="Paul Hartmann"/> 10 10 <property name="plugin.class" value="org.openstreetmap.josm.plugins.photo_geotagging.GeotaggingPlugin"/> -
src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java
8 8 import java.io.FileOutputStream; 9 9 import java.io.IOException; 10 10 import java.text.DecimalFormat; 11 import java.time.Instant; 11 12 import java.util.Calendar; 12 13 import java.util.Date; 13 14 import java.util.GregorianCalendar; … … 42 43 * @param lossy whether to use lossy approach when writing metadata (overwriting unknown tags) 43 44 * @throws IOException in case of I/O error 44 45 */ 45 public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, DategpsTime, Double speed, Double ele, Double imgDir, boolean lossy) throws IOException {46 public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, Instant gpsTime, Double speed, Double ele, Double imgDir, boolean lossy) throws IOException { 46 47 try { 47 48 setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir, lossy); 48 49 } catch (ImageReadException ire) { … … 52 53 } 53 54 } 54 55 55 public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, DategpsTime, Double speed, Double ele, Double imgDir, boolean lossy)56 public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, Instant gpsTime, Double speed, Double ele, Double imgDir, boolean lossy) 56 57 throws IOException, ImageReadException, ImageWriteException { 57 58 58 59 TiffOutputSet outputSet = null; … … 77 78 78 79 if (gpsTime != null) { 79 80 Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 80 calendar.setTime (gpsTime);81 calendar.setTimeInMillis(gpsTime.toEpochMilli()); 81 82 82 83 final int year = calendar.get(Calendar.YEAR); 83 84 final int month = calendar.get(Calendar.MONTH) + 1; -
src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingAction.java
14 14 import java.nio.file.Files; 15 15 import java.nio.file.NoSuchFileException; 16 16 import java.text.DecimalFormat; 17 import java.time.Instant; 17 18 import java.util.ArrayList; 18 19 import java.util.Date; 19 20 import java.util.List; … … 55 56 */ 56 57 class GeotaggingAction extends AbstractAction implements LayerAction { 57 58 58 final static boolean debug = false;59 59 final static String KEEP_BACKUP = "plugins.photo_geotagging.keep_backup"; 60 60 final static String CHANGE_MTIME = "plugins.photo_geotagging.change-mtime"; 61 61 final static String MTIME_MODE = "plugins.photo_geotagging.mtime-mode"; … … 168 168 169 169 int result = new ExtendedDialog( 170 170 MainApplication.getMainFrame(), 171 tr("Photo Geotagging Plugin"), 172 new String[] {tr("OK"), tr("Cancel")}) 173 .setButtonIcons(new String[] {"ok", "cancel"}) 171 tr("Photo Geotagging Plugin"), tr("OK"), tr("Cancel")) 172 .setButtonIcons("ok", "cancel") 174 173 .setContent(cont) 175 174 .setCancelButton(2) 176 175 .setDefaultButton(1) … … 284 283 if (canceled) 285 284 return exifFailedEntries; 286 285 ImageEntry e = entries.get(currentIndex); 287 if (debug) { 288 System.err.print("i:" + currentIndex + " " + e.getFile().getName() + " "); 289 } 286 Logging.trace("photo_geotagging: GeotaggingAction: i: {0} {1} ", currentIndex, e.getFile().getName()); 290 287 try { 291 288 processEntry(e, lossy); 292 289 } catch (final IOException ioe) { … … 335 332 336 333 progressMonitor.subTask(tr("Writing position information to image files... Estimated time left: {0}", timeLeft)); 337 334 338 if (debug) { 339 System.err.println("finished " + e.getFile()); 340 } 335 Logging.trace("photo_geotagging: GeotaggingAction: finished {0}", e.getFile()); 341 336 currentIndex++; 342 337 } 343 338 return exifFailedEntries; … … 352 347 testMTimeReadAndWrite(e.getFile()); 353 348 } 354 349 355 LongmTime = null;350 Instant mTime = null; 356 351 if (mTimeMode == MTIME_MODE_GPS) { 357 352 // check GPS time fields, do nothing if all fails 358 Date time;359 353 if (e.hasGpsTime()) { 360 time = e.getGpsTime();361 } else {362 time = e.getExifGpsTime();354 mTime = e.getGpsInstant(); 355 } else if (e.hasExifGpsTime()) { 356 mTime = e.getExifGpsInstant(); 363 357 } 364 if (time != null) {365 mTime = time.getTime();366 }367 358 } 368 359 if ( mTimeMode == MTIME_MODE_PREVIOUS_VALUE 369 360 // this is also the fallback if one of the other 370 361 // modes failed to determine the modification time 371 362 || (mTimeMode != 0 && mTime == null)) { 372 mTime = e.getFile().lastModified();373 if ( mTime.equals(0L))363 mTime = Instant.ofEpochMilli(e.getFile().lastModified()); 364 if (Instant.EPOCH.equals(mTime)) 374 365 throw new IOException(tr("Could not read mtime.")); 375 366 } 376 367 … … 377 368 chooseFiles(e.getFile()); 378 369 if (canceled) return; 379 370 ExifGPSTagger.setExifGPSTag(fileFrom, fileTo, e.getPos().lat(), e.getPos().lon(), 380 e.getGps Time(), e.getSpeed(), e.getElevation(), e.getExifImgDir(), lossy);371 e.getGpsInstant(), e.getSpeed(), e.getElevation(), e.getExifImgDir(), lossy); 381 372 382 373 if (mTime != null) { 383 if (!fileTo.setLastModified(mTime ))374 if (!fileTo.setLastModified(mTime.toEpochMilli())) 384 375 throw new IOException(tr("Could not write mtime.")); 385 376 } 386 377 … … 389 380 } 390 381 391 382 private void chooseFiles(File file) throws IOException { 392 if (debug) { 393 System.err.println("f: "+file.getAbsolutePath()); 394 } 383 Logging.trace("photo_geotagging: GeotaggingAction: f: "+file.getAbsolutePath()); 395 384 396 385 if (!keep_backup) { 397 386 chooseFilesNoBackup(file); … … 431 420 do { 432 421 fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ".tmp"); 433 422 } while (fileTmp.exists()); 434 if (debug) { 435 System.err.println("TMP: "+fileTmp.getAbsolutePath()); 436 } 423 Logging.trace("photo_geotagging: GeotaggingAction: TMP: {0}", fileTmp.getAbsolutePath()); 437 424 try { 438 425 Files.move(file.toPath(), fileTmp.toPath()); 439 426 } catch (IOException e) { … … 454 441 l.setIcon(UIManager.getIcon("OptionPane.warningIcon")); 455 442 int override = new ExtendedDialog( 456 443 progressMonitor.getWindowParent(), 457 tr("Override old backup files?"), 458 new String[] {tr("Cancel"), tr("Keep old backups and continue"), tr("Override")})459 .setButtonIcons( new String[] {"cancel", "ok", "dialogs/delete"})444 tr("Override old backup files?"), tr("Cancel"), tr("Keep old backups and continue"), 445 tr("Override")) 446 .setButtonIcons("cancel", "ok", "dialogs/delete") 460 447 .setContent(l) 461 448 .setCancelButton(1) 462 449 .setDefaultButton(2) … … 471 458 } 472 459 }); 473 460 } catch (Exception e) { 474 System.err.println(e);461 Logging.error(e); 475 462 canceled = true; 476 463 } 477 464 } -
test/unit/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTaggerTest.java
1 1 package org.openstreetmap.josm.plugins.photo_geotagging; 2 2 3 import static org.junit.Assert.assertFalse; 3 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertFalse; 4 6 5 7 import java.io.ByteArrayOutputStream; 6 8 import java.io.File; 7 import java. util.Date;9 import java.time.Instant; 8 10 import java.util.Scanner; 9 11 10 12 import org.apache.commons.imaging.Imaging; … … 12 14 import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter; 13 15 import org.apache.commons.imaging.formats.tiff.TiffImageMetadata; 14 16 import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet; 15 import org.junit.Ignore; 16 import org.junit.Rule; 17 import org.junit.Test; 18 import org.junit.rules.TemporaryFolder; 17 import org.junit.jupiter.api.Disabled; 18 import org.junit.jupiter.api.Test; 19 import org.junit.jupiter.api.io.TempDir; 19 20 import org.openstreetmap.josm.TestUtils; 20 21 21 publicclass ExifGPSTaggerTest {22 class ExifGPSTaggerTest { 22 23 23 @ Rule24 public final TemporaryFolder tempFolder = new TemporaryFolder();24 @TempDir 25 File tempFolder; 25 26 26 27 @Test 27 public void testTicket11757() throws Exception{28 void testTicket11757() { 28 29 final File in = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg"); 29 ExifGPSTagger.setExifGPSTag(in, tempFolder.newFile(), 12, 34, new Date(), 12.34, Math.E, Math.PI, true); 30 final File out = new File(tempFolder, in.getName()); 31 assertDoesNotThrow(() -> ExifGPSTagger.setExifGPSTag(in, out, 12, 34, Instant.now(), 12.34, Math.E, Math.PI, true)); 30 32 } 31 33 32 34 @Test 33 publicvoid testTicket11757WriteWithoutChange() throws Exception {35 void testTicket11757WriteWithoutChange() throws Exception { 34 36 final File in = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg"); 37 final long lastModified = in.lastModified(); 35 38 final TiffImageMetadata exif = ((JpegImageMetadata) Imaging.getMetadata(in)).getExif(); 36 39 final TiffOutputSet outputSet = exif.getOutputSet(); 37 40 new ExifRewriter().updateExifMetadataLossless(in, new ByteArrayOutputStream(), outputSet); 41 assertEquals(lastModified, in.lastModified()); 38 42 } 39 43 40 44 @Test 41 @ Ignore("To enable after https://josm.openstreetmap.de/ticket/11902 is fixed")42 publicvoid testTicket11902() throws Exception {45 @Disabled("To enable after https://josm.openstreetmap.de/ticket/11902 is fixed") 46 void testTicket11902() throws Exception { 43 47 final File in = new File(TestUtils.getTestDataRoot(), "IMG_7250_small.JPG"); 44 final File out = tempFolder.newFile();45 ExifGPSTagger.setExifGPSTag(in, out, 12, 34, new Date(), 12.34, Math.E, Math.PI, false);48 final File out = new File(tempFolder, in.getName()); 49 ExifGPSTagger.setExifGPSTag(in, out, 12, 34, Instant.now(), 12.34, Math.E, Math.PI, false); 46 50 final Process jhead = Runtime.getRuntime().exec(new String[]{"jhead", out.getAbsolutePath()}); 47 51 final String stdout = new Scanner(jhead.getErrorStream()).useDelimiter("\\A").next(); 48 52 System.out.println(stdout); -
test/unit/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingActionTest.java
1 1 package org.openstreetmap.josm.plugins.photo_geotagging; 2 2 3 import static org.junit.Assert.assertEquals;4 import static org.junit.Assert.assertTrue;5 3 6 4 import java.io.File; 7 5 import java.io.IOException; … … 10 8 import java.util.Arrays; 11 9 import java.util.List; 12 10 13 import org.junit. Test;11 import org.junit.jupiter.api.Test; 14 12 import org.openstreetmap.josm.TestUtils; 15 13 import org.openstreetmap.josm.data.coor.LatLon; 16 14 import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry; 17 15 import org.openstreetmap.josm.plugins.photo_geotagging.GeotaggingAction.GeoTaggingRunnable; 18 16 19 public class GeotaggingActionTest { 17 import static org.junit.jupiter.api.Assertions.assertEquals; 18 import static org.junit.jupiter.api.Assertions.assertTrue; 20 19 20 class GeotaggingActionTest { 21 21 22 @Test 22 publicvoid testProcessEntries() throws IOException {23 void testProcessEntries() throws IOException { 23 24 File original = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg"); 24 25 assertTrue(original.exists()); 25 26
