Ticket #8895: upgrade-metadata-extractor2.patch

File upgrade-metadata-extractor2.patch, 8.7 KB (added by ebourg, 12 years ago)

Updated patch addressing #5220 and #6162

  • src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

     
    6868import com.drew.metadata.Directory;
    6969import com.drew.metadata.Metadata;
    7070import com.drew.metadata.MetadataException;
    71 import com.drew.metadata.exif.ExifDirectory;
     71import com.drew.metadata.exif.ExifIFD0Directory;
    7272import com.drew.metadata.exif.GpsDirectory;
    7373
    7474public class GeoImageLayer extends Layer implements PropertyChangeListener, JumpToMarkerLayer {
     
    510510        double deg;
    511511        double min, sec;
    512512        double lon, lat;
    513         Metadata metadata = null;
    514         Directory dirExif = null, dirGps = null;
     513        Metadata metadata;
     514        Directory dirExif;
     515        GpsDirectory dirGps;
    515516
    516517        try {
    517518            metadata = JpegMetadataReader.readMetadata(e.getFile());
    518             dirExif = metadata.getDirectory(ExifDirectory.class);
     519            dirExif = metadata.getDirectory(ExifIFD0Directory.class);
    519520            dirGps = metadata.getDirectory(GpsDirectory.class);
    520521        } catch (CompoundException p) {
    521522            e.setExifCoor(null);
    522523            e.setPos(null);
    523524            return;
     525        } catch (IOException p) {
     526            e.setExifCoor(null);
     527            e.setPos(null);
     528            return;
    524529        }
    525530
    526531        try {
    527             int orientation = dirExif.getInt(ExifDirectory.TAG_ORIENTATION);
     532            int orientation = dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    528533            e.setExifOrientation(orientation);
    529534        } catch (MetadataException ex) {
    530535        }
     
    543548            // longitude
    544549
    545550            Rational[] components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE);
     551            if (components != null) {
     552                deg = components[0].doubleValue();
     553                min = components[1].doubleValue();
     554                sec = components[2].doubleValue();
    546555
    547             deg = components[0].doubleValue();
    548             min = components[1].doubleValue();
    549             sec = components[2].doubleValue();
     556                if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
     557                    throw new IllegalArgumentException();
    550558
    551             if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
    552                 throw new IllegalArgumentException();
     559                lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
    553560
    554             lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
    555 
    556             if (dirGps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') {
    557                 lon = -lon;
     561                if (dirGps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') {
     562                    lon = -lon;
     563                }
     564            } else {
     565                // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
     566                lon = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE);
    558567            }
    559568
    560569            // latitude
    561570
    562571            components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE);
     572            if (components != null) {
     573                deg = components[0].doubleValue();
     574                min = components[1].doubleValue();
     575                sec = components[2].doubleValue();
    563576
    564             deg = components[0].doubleValue();
    565             min = components[1].doubleValue();
    566             sec = components[2].doubleValue();
     577                if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
     578                    throw new IllegalArgumentException();
    567579
    568             if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
    569                 throw new IllegalArgumentException();
     580                lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
    570581
    571             lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
     582                if (Double.isNaN(lat))
     583                    throw new IllegalArgumentException();
    572584
    573             if (Double.isNaN(lat))
    574                 throw new IllegalArgumentException();
    575 
    576             if (dirGps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') {
    577                 lat = -lat;
     585                if (dirGps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') {
     586                    lat = -lat;
     587                }
     588            } else {
     589                lat = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE);
    578590            }
    579591
    580592            // Store values
     
    582594            e.setExifCoor(new LatLon(lat, lon));
    583595            e.setPos(e.getExifCoor());
    584596
    585         } catch (CompoundException p) {
    586             // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
    587             try {
    588                 Double longitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE);
    589                 Double latitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE);
    590                 if (longitude == null || latitude == null)
    591                     throw new CompoundException("");
    592 
    593                 // Store values
    594 
    595                 e.setExifCoor(new LatLon(latitude, longitude));
    596                 e.setPos(e.getExifCoor());
    597             } catch (CompoundException ex) {
    598                 e.setExifCoor(null);
    599                 e.setPos(null);
    600             }
    601597        } catch (Exception ex) { // (other exceptions, e.g. #5271)
    602598            System.err.println("Error reading EXIF from file: "+ex);
    603599            e.setExifCoor(null);
  • src/org/openstreetmap/josm/tools/ExifReader.java

     
    22package org.openstreetmap.josm.tools;
    33
    44import java.io.File;
     5import java.io.IOException;
    56import java.text.ParseException;
    67import java.util.Date;
    7 import java.util.Iterator;
    88
    99import com.drew.imaging.jpeg.JpegMetadataReader;
    1010import com.drew.imaging.jpeg.JpegProcessingException;
     
    1212import com.drew.metadata.Metadata;
    1313import com.drew.metadata.MetadataException;
    1414import com.drew.metadata.Tag;
    15 import com.drew.metadata.exif.ExifDirectory;
     15import com.drew.metadata.exif.ExifIFD0Directory;
     16import com.drew.metadata.exif.ExifSubIFDDirectory;
    1617
    1718/**
    1819 * Read out exif file information from a jpeg file
     
    2526            Metadata metadata = JpegMetadataReader.readMetadata(filename);
    2627            String dateStr = null;
    2728            OUTER:
    28             for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) {
    29                 for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) {
    30                     Tag tag = tagIt.next();
    31                     if (tag.getTagType() == ExifDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
     29            for (Directory dirIt : metadata.getDirectories()) {
     30                for (Tag tag : dirIt.getTags()) {
     31                    if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
    3232                        dateStr = tag.getDescription();
    3333                        break OUTER; // prefer this tag
    3434                    }
    35                     if (tag.getTagType() == ExifDirectory.TAG_DATETIME /* 0x0132 */ ||
    36                         tag.getTagType() == ExifDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
     35                    if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ ||
     36                        tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
    3737                        dateStr = tag.getDescription();
    3838                    }
    3939                }
     
    5454        Integer orientation = null;
    5555        try {
    5656            final Metadata metadata = JpegMetadataReader.readMetadata(filename);
    57             final Directory dir = metadata.getDirectory(ExifDirectory.class);
    58             orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION);
     57            final Directory dir = metadata.getDirectory(ExifIFD0Directory.class);
     58            orientation = dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    5959        } catch (JpegProcessingException e) {
    6060            e.printStackTrace();
    6161        } catch (MetadataException e) {
    6262            e.printStackTrace();
     63        } catch (IOException e) {
     64            e.printStackTrace();
    6365        }
    6466        return orientation;
    6567    }