Ticket #20341: 20341-photo_geotagging.diff
| File 20341-photo_geotagging.diff, 10.3 KB (added by , 5 years ago) |
|---|
-
photo_geotagging/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=" 14153"/>7 <property name="plugin.main.version" value="xxx"/> 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"/> -
photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/ExifGPSTagger.java
22 22 import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter; 23 23 import org.apache.commons.imaging.formats.tiff.TiffImageMetadata; 24 24 import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants; 25 import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossy; 25 26 import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory; 26 27 import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet; 27 28 … … 28 29 public class ExifGPSTagger { 29 30 /** 30 31 * Set the GPS values in JPEG EXIF metadata. 31 * This is taken fromone of the examples of the sanselan project.32 * This is based on one of the examples of the sanselan project. 32 33 * 33 * @param jpegImageFile A source image file.34 * @param imageFile A source image file. 34 35 * @param dst The output file. 35 36 * @param lat latitude 36 37 * @param lon longitude … … 40 41 * @param imgDir image direction in degrees (0..360) - can be null if not available 41 42 * @throws IOException in case of I/O error 42 43 */ 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 { 44 45 try { 45 setExifGPSTagWorker( jpegImageFile, dst, lat, lon, gpsTime, speed, ele, imgDir);46 setExifGPSTagWorker(imageFile, dst, lat, lon, gpsTime, speed, ele, imgDir); 46 47 } catch (ImageReadException ire) { 47 48 throw new IOException(tr("Read error: "+ire), ire); 48 49 } catch (ImageWriteException ire2) { … … 50 51 } 51 52 } 52 53 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) 54 55 throws IOException, ImageReadException, ImageWriteException { 56 55 57 TiffOutputSet outputSet = null; 58 ImageMetadata metadata = Imaging.getMetadata(imageFile); 56 59 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(); 62 62 if (null != exif) { 63 63 outputSet = exif.getOutputSet(); 64 64 } 65 } else if (metadata instanceof TiffImageMetadata) { 66 outputSet = ((TiffImageMetadata) metadata).getOutputSet(); 65 67 } 66 68 67 69 if (null == outputSet) { … … 96 98 // make sure to remove old value if present (this method will 97 99 // not fail if the tag does not exist). 98 100 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, 100 102 RationalNumber.valueOf(hour), 101 103 RationalNumber.valueOf(minute), 102 104 RationalNumber.valueOf(second)); … … 144 146 } 145 147 146 148 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 } 148 154 } 149 155 } 150 156 } -
photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingAction.java
2 2 package org.openstreetmap.josm.plugins.photo_geotagging; 3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.trn; 5 6 7 import java.awt.Color; 6 8 import java.awt.Component; 7 9 import java.awt.Dimension; 8 10 import java.awt.GridBagLayout; … … 32 34 import javax.swing.SwingUtilities; 33 35 import javax.swing.UIManager; 34 36 37 import org.apache.commons.io.FilenameUtils; 35 38 import org.openstreetmap.josm.gui.ExtendedDialog; 36 39 import org.openstreetmap.josm.gui.MainApplication; 37 40 import org.openstreetmap.josm.gui.PleaseWaitRunnable; … … 69 72 GeoImageLayer layer = getLayer(); 70 73 71 74 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; 79 78 80 79 final JPanel cont = new JPanel(new GridBagLayout()); 81 80 cont.add(new JLabel(tr("Write position information into the exif header of the following files:")), GBC.eol()); … … 82 81 83 82 DefaultListModel<String> listModel = new DefaultListModel<>(); 84 83 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 } 88 108 } 89 109 90 110 JList<String> entryList = new JList<>(listModel); … … 93 113 scroll.setPreferredSize(new Dimension(900, 250)); 94 114 cont.add(scroll, GBC.eol().fill(GBC.BOTH)); 95 115 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 96 129 final JPanel settingsPanel = new JPanel(new GridBagLayout()); 97 130 settingsPanel.setBorder(BorderFactory.createTitledBorder(tr("settings"))); 98 131 cont.add(settingsPanel, GBC.eol().insets(3,10,3,0)); … … 333 366 // instead, let's use new File(), which doesn't actually create a file 334 367 // for getting a unique file name, we use UUID.randomUUID() 335 368 do { 336 fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ". jpg");369 fileTmp = new File(file.getParentFile(), "img" + UUID.randomUUID() + ".tmp"); 337 370 } while (fileTmp.exists()); 338 371 if (debug) { 339 372 System.err.println("TMP: "+fileTmp.getAbsolutePath()); -
photo_geotagging/src/org/openstreetmap/josm/plugins/photo_geotagging/GeotaggingPlugin.java
7 7 8 8 /** 9 9 * 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. 11 11 * It extends the core geoimage feature of JOSM by adding a new entry 12 12 * to the right click menu of any image layer. 13 13 *
