Ticket #20341: 20341-photo_geotagging.diff

File 20341-photo_geotagging.diff, 10.3 KB (added by Bjoeni, 5 years ago)
  • photo_geotagging/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="xxx"/>
    88
    99    <property name="plugin.author" value="Paul Hartmann"/>
    1010    <property name="plugin.class" value="org.openstreetmap.josm.plugins.photo_geotagging.GeotaggingPlugin"/>
  • photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java

     
    2222import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
    2323import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
    2424import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
     25import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossy;
    2526import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
    2627import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
    2728
     
    2829public class ExifGPSTagger {
    2930    /**
    3031     * Set the GPS values in JPEG EXIF metadata.
    31      * This is taken from one of the examples of the sanselan project.
     32     * This is based on one of the examples of the sanselan project.
    3233     *
    33      * @param jpegImageFile A source image file.
     34     * @param imageFile A source image file.
    3435     * @param dst The output file.
    3536     * @param lat latitude
    3637     * @param lon longitude
     
    4041     * @param imgDir image direction in degrees (0..360) - can be null if not available
    4142     * @throws IOException in case of I/O error
    4243     */
    43     public static void setExifGPSTag(File jpegImageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir) throws IOException {
     44    public static void setExifGPSTag(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir) throws IOException {
    4445        try {
    45             setExifGPSTagWorker(jpegImageFile, dst, lat, lon, gpsTime, speed, ele, imgDir);
     46            setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir);
    4647        } catch (ImageReadException ire) {
    4748            throw new IOException(tr("Read error: "+ire), ire);
    4849        } catch (ImageWriteException ire2) {
     
    5051        }
    5152    }
    5253
    53     public static void setExifGPSTagWorker(File jpegImageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir)
     54    public static void setExifGPSTagWorker(File imageFile, File dst, double lat, double lon, Date gpsTime, Double speed, Double ele, Double imgDir)
    5455            throws IOException, ImageReadException, ImageWriteException {
     56
    5557        TiffOutputSet outputSet = null;
     58        ImageMetadata metadata = Imaging.getMetadata(imageFile);
    5659
    57         ImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
    58         JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
    59         if (null != jpegMetadata) {
    60             TiffImageMetadata exif = jpegMetadata.getExif();
    61 
     60        if (metadata instanceof JpegImageMetadata) {
     61            TiffImageMetadata exif = ((JpegImageMetadata) metadata).getExif();
    6262            if (null != exif) {
    6363                outputSet = exif.getOutputSet();
    6464            }
     65        } else if (metadata instanceof TiffImageMetadata) {
     66            outputSet = ((TiffImageMetadata) metadata).getOutputSet();
    6567        }
    6668
    6769        if (null == outputSet) {
     
    9698            // make sure to remove old value if present (this method will
    9799            // not fail if the tag does not exist).
    98100            gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP);
    99             gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP, 
     101            gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_TIME_STAMP,
    100102                    RationalNumber.valueOf(hour),
    101103                    RationalNumber.valueOf(minute),
    102104                    RationalNumber.valueOf(second));
     
    144146        }
    145147
    146148        try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dst))) {
    147             new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);
     149            if (metadata instanceof JpegImageMetadata) {
     150                new ExifRewriter().updateExifMetadataLossless(imageFile, os, outputSet);
     151            } else if (metadata instanceof TiffImageMetadata) {
     152                new TiffImageWriterLossy().write(os, outputSet);
     153            }
    148154        }
    149155    }
    150156}
  • photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingAction.java

     
    22package org.openstreetmap.josm.plugins.photo_geotagging;
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.trn;
    56
     7import java.awt.Color;
    68import java.awt.Component;
    79import java.awt.Dimension;
    810import java.awt.GridBagLayout;
     
    3234import javax.swing.SwingUtilities;
    3335import javax.swing.UIManager;
    3436
     37import org.apache.commons.io.FilenameUtils;
    3538import org.openstreetmap.josm.gui.ExtendedDialog;
    3639import org.openstreetmap.josm.gui.MainApplication;
    3740import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    6972        GeoImageLayer layer = getLayer();
    7073
    7174        final List<ImageEntry> images = new ArrayList<>();
    72         for (ImageEntry e : layer.getImages()) {
    73              /* Only write lat/lon to the file, if the position is known and
    74                 the GPS data changed. */
    75             if (e.getPos() != null && e.hasNewGpsData()) {
    76                 images.add(e);
    77             }
    78         }
     75        int notSupportedFilesCount = 0;
     76        String notSupportedName = null;
     77        boolean hasTiff = false;
    7978
    8079        final JPanel cont = new JPanel(new GridBagLayout());
    8180        cont.add(new JLabel(tr("Write position information into the exif header of the following files:")), GBC.eol());
     
    8281
    8382        DefaultListModel<String> listModel = new DefaultListModel<>();
    8483        DecimalFormat dFormatter = new DecimalFormat("###0.000000");
    85         for (ImageEntry e : images) {
    86             listModel.addElement(e.getFile().getAbsolutePath()+
    87                 " ("+dFormatter.format(e.getPos().lat())+","+dFormatter.format(e.getPos().lon())+")");
     84
     85        for (ImageEntry e : layer.getImages()) {
     86             /* Only write lat/lon to the file, if the position is known and
     87                the GPS data changed. */
     88            if (e.getPos() != null && e.hasNewGpsData()) {
     89                String pth = e.getFile().getAbsolutePath();
     90                switch (FilenameUtils.getExtension(pth).toLowerCase()) {
     91                    case "tif":
     92                    case "tiff":
     93                        hasTiff = true;
     94                    case "jpg":
     95                    case "jpeg":
     96                        images.add(e);
     97                        listModel.addElement(pth + " (" + dFormatter.format(e.getPos().lat()) + ","
     98                                + dFormatter.format(e.getPos().lon()) + ")");
     99                        break;
     100                    default:
     101                        notSupportedFilesCount++;
     102                        if (notSupportedName == null) {
     103                            notSupportedName = e.getFile().getName();
     104                        }
     105                        break;
     106                }
     107            }
    88108        }
    89109
    90110        JList<String> entryList = new JList<>(listModel);
     
    93113        scroll.setPreferredSize(new Dimension(900, 250));
    94114        cont.add(scroll, GBC.eol().fill(GBC.BOTH));
    95115
     116        if (notSupportedFilesCount > 0) {
     117            JLabel warn = new JLabel(trn("The file \"{0}\" can not be updated. Only JPEG and TIFF images are supported.",
     118                    "{1} files can not be updated. Only JPEG and TIFF images are supported.", notSupportedFilesCount, notSupportedName, Integer.toString(notSupportedFilesCount)));
     119            warn.setForeground(Color.RED);
     120            cont.add(warn, GBC.eol());
     121        }
     122
     123        if (hasTiff) {
     124            JLabel warn = new JLabel(tr("Warning: Some metadata in TIFF files may be lost. Always keep a backup!"));
     125            warn.setForeground(Color.RED);
     126            cont.add(warn, GBC.eol());
     127        }
     128
    96129        final JPanel settingsPanel = new JPanel(new GridBagLayout());
    97130        settingsPanel.setBorder(BorderFactory.createTitledBorder(tr("settings")));
    98131        cont.add(settingsPanel, GBC.eol().insets(3,10,3,0));
     
    333366            // instead, let's use new File(), which doesn't actually create a file
    334367            // for getting a unique file name, we use UUID.randomUUID()
    335368            do {
    336                 fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ".jpg");
     369                fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ".tmp");
    337370            } while (fileTmp.exists());
    338371            if (debug) {
    339372                System.err.println("TMP: "+fileTmp.getAbsolutePath());
  • photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingPlugin.java

     
    77
    88/**
    99 * This plugin is used to write latitude and longitude information
    10  * to the EXIF header of jpg files.
     10 * to the EXIF header of jpg and tiff files.
    1111 * It extends the core geoimage feature of JOSM by adding a new entry
    1212 * to the right click menu of any image layer.
    1313 *