Ticket #21766: 21766.patch

File 21766.patch, 13.7 KB (added by taylor.smock, 4 years ago)
  • build.xml

     
    44    <!-- enter the SVN commit message -->
    55    <property name="commit.message" value=""/>
    66    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    7     <property name="plugin.main.version" value="14153"/>
     7    <property name="plugin.main.version" value="17715"/>
    88
    99    <property name="plugin.author" value="Paul Hartmann"/>
    1010    <property name="plugin.class" value="org.openstreetmap.josm.plugins.photo_geotagging.GeotaggingPlugin"/>
  • src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java

     
    88import java.io.FileOutputStream;
    99import java.io.IOException;
    1010import java.text.DecimalFormat;
     11import java.time.Instant;
    1112import java.util.Calendar;
    1213import java.util.Date;
    1314import java.util.GregorianCalendar;
     
    4243     * @param lossy whether to use lossy approach when writing metadata (overwriting unknown tags)
    4344     * @throws IOException in case of I/O error
    4445     */
    45     public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, Date gpsTime, 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 {
    4647        try {
    4748            setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir, lossy);
    4849        } catch (ImageReadException ire) {
     
    5253        }
    5354    }
    5455
    55     public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, Date gpsTime, 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)
    5657            throws IOException, ImageReadException, ImageWriteException {
    5758
    5859        TiffOutputSet outputSet = null;
     
    7778
    7879        if (gpsTime != null) {
    7980            Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    80             calendar.setTime(gpsTime);
     81            calendar.setTimeInMillis(gpsTime.toEpochMilli());
    8182
    8283            final int year =   calendar.get(Calendar.YEAR);
    8384            final int month =  calendar.get(Calendar.MONTH) + 1;
  • src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingAction.java

     
    1414import java.nio.file.Files;
    1515import java.nio.file.NoSuchFileException;
    1616import java.text.DecimalFormat;
     17import java.time.Instant;
    1718import java.util.ArrayList;
    1819import java.util.Date;
    1920import java.util.List;
     
    5556 */
    5657class GeotaggingAction extends AbstractAction implements LayerAction {
    5758
    58     final static boolean debug = false;
    5959    final static String KEEP_BACKUP = "plugins.photo_geotagging.keep_backup";
    6060    final static String CHANGE_MTIME = "plugins.photo_geotagging.change-mtime";
    6161    final static String MTIME_MODE = "plugins.photo_geotagging.mtime-mode";
     
    168168
    169169        int result = new ExtendedDialog(
    170170                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")
    174173            .setContent(cont)
    175174            .setCancelButton(2)
    176175            .setDefaultButton(1)
     
    284283                if (canceled)
    285284                    return exifFailedEntries;
    286285                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());
    290287                try {
    291288                    processEntry(e, lossy);
    292289                } catch (final IOException ioe) {
     
    335332
    336333                progressMonitor.subTask(tr("Writing position information to image files... Estimated time left: {0}", timeLeft));
    337334
    338                 if (debug) {
    339                     System.err.println("finished " + e.getFile());
    340                 }
     335                Logging.trace("photo_geotagging: GeotaggingAction: finished {0}", e.getFile());
    341336                currentIndex++;
    342337            }
    343338            return exifFailedEntries;
     
    352347                testMTimeReadAndWrite(e.getFile());
    353348            }
    354349
    355             Long mTime = null;
     350            Instant mTime = null;
    356351            if (mTimeMode == MTIME_MODE_GPS) {
    357352                // check GPS time fields, do nothing if all fails
    358                 Date time;
    359353                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();
    363357                }
    364                 if (time != null) {
    365                     mTime = time.getTime();
    366                 }
    367358            }
    368359            if ( mTimeMode == MTIME_MODE_PREVIOUS_VALUE
    369360                 // this is also the fallback if one of the other
    370361                 // modes failed to determine the modification time
    371362                 || (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))
    374365                    throw new IOException(tr("Could not read mtime."));
    375366            }
    376367
     
    377368            chooseFiles(e.getFile());
    378369            if (canceled) return;
    379370            ExifGPSTagger.setExifGPSTag(fileFrom, fileTo, e.getPos().lat(), e.getPos().lon(),
    380                     e.getGpsTime(), e.getSpeed(), e.getElevation(), e.getExifImgDir(), lossy);
     371                    e.getGpsInstant(), e.getSpeed(), e.getElevation(), e.getExifImgDir(), lossy);
    381372
    382373            if (mTime != null) {
    383                 if (!fileTo.setLastModified(mTime))
     374                if (!fileTo.setLastModified(mTime.toEpochMilli()))
    384375                    throw new IOException(tr("Could not write mtime."));
    385376            }
    386377
     
    389380        }
    390381
    391382        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());
    395384
    396385            if (!keep_backup) {
    397386                chooseFilesNoBackup(file);
     
    431420            do {
    432421                fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ".tmp");
    433422            } while (fileTmp.exists());
    434             if (debug) {
    435                 System.err.println("TMP: "+fileTmp.getAbsolutePath());
    436             }
     423            Logging.trace("photo_geotagging: GeotaggingAction: TMP: {0}", fileTmp.getAbsolutePath());
    437424            try {
    438425                Files.move(file.toPath(), fileTmp.toPath());
    439426            } catch (IOException e) {
     
    454441                    l.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
    455442                    int override = new ExtendedDialog(
    456443                            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")
    460447                        .setContent(l)
    461448                        .setCancelButton(1)
    462449                        .setDefaultButton(2)
     
    471458                    }
    472459                });
    473460            } catch (Exception e) {
    474                 System.err.println(e);
     461                Logging.error(e);
    475462                canceled = true;
    476463            }
    477464        }
  • test/unit/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTaggerTest.java

     
    11package org.openstreetmap.josm.plugins.photo_geotagging;
    22
    3 import static org.junit.Assert.assertFalse;
     3import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
     4import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertFalse;
    46
    57import java.io.ByteArrayOutputStream;
    68import java.io.File;
    7 import java.util.Date;
     9import java.time.Instant;
    810import java.util.Scanner;
    911
    1012import org.apache.commons.imaging.Imaging;
     
    1214import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
    1315import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
    1416import 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;
     17import org.junit.jupiter.api.Disabled;
     18import org.junit.jupiter.api.Test;
     19import org.junit.jupiter.api.io.TempDir;
    1920import org.openstreetmap.josm.TestUtils;
    2021
    21 public class ExifGPSTaggerTest {
     22class ExifGPSTaggerTest {
    2223
    23     @Rule
    24     public final TemporaryFolder tempFolder = new TemporaryFolder();
     24    @TempDir
     25    File tempFolder;
    2526
    2627    @Test
    27     public void testTicket11757() throws Exception {
     28    void testTicket11757() {
    2829        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));
    3032    }
    3133
    3234    @Test
    33     public void testTicket11757WriteWithoutChange() throws Exception {
     35    void testTicket11757WriteWithoutChange() throws Exception {
    3436        final File in = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg");
     37        final long lastModified = in.lastModified();
    3538        final TiffImageMetadata exif = ((JpegImageMetadata) Imaging.getMetadata(in)).getExif();
    3639        final TiffOutputSet outputSet = exif.getOutputSet();
    3740        new ExifRewriter().updateExifMetadataLossless(in, new ByteArrayOutputStream(), outputSet);
     41        assertEquals(lastModified, in.lastModified());
    3842    }
    3943
    4044    @Test
    41     @Ignore("To enable after https://josm.openstreetmap.de/ticket/11902 is fixed")
    42     public void testTicket11902() throws Exception {
     45    @Disabled("To enable after https://josm.openstreetmap.de/ticket/11902 is fixed")
     46    void testTicket11902() throws Exception {
    4347        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);
    4650        final Process jhead = Runtime.getRuntime().exec(new String[]{"jhead", out.getAbsolutePath()});
    4751        final String stdout = new Scanner(jhead.getErrorStream()).useDelimiter("\\A").next();
    4852        System.out.println(stdout);
  • test/unit/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingActionTest.java

     
    11package org.openstreetmap.josm.plugins.photo_geotagging;
    22
    3 import static org.junit.Assert.assertEquals;
    4 import static org.junit.Assert.assertTrue;
    53
    64import java.io.File;
    75import java.io.IOException;
     
    108import java.util.Arrays;
    119import java.util.List;
    1210
    13 import org.junit.Test;
     11import org.junit.jupiter.api.Test;
    1412import org.openstreetmap.josm.TestUtils;
    1513import org.openstreetmap.josm.data.coor.LatLon;
    1614import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry;
    1715import org.openstreetmap.josm.plugins.photo_geotagging.GeotaggingAction.GeoTaggingRunnable;
    1816
    19 public class GeotaggingActionTest {
     17import static org.junit.jupiter.api.Assertions.assertEquals;
     18import static org.junit.jupiter.api.Assertions.assertTrue;
    2019
     20class GeotaggingActionTest {
     21
    2122    @Test
    22     public void testProcessEntries() throws IOException {
     23    void testProcessEntries() throws IOException {
    2324        File original = new File(TestUtils.getTestDataRoot(), "_DSC1234.jpg");
    2425        assertTrue(original.exists());
    2526