Changeset 2931 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2010-02-03T09:48:51+01:00 (14 years ago)
Author:
bastiK
Message:

cleanup for geoimage code (mainly getters and setters)

Location:
trunk/src/org/openstreetmap/josm/gui/layer/geoimage
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

    r2907 r2931  
    22// Copyright 2007 by Christian Gallioz (aka khris78)
    33// Parts of code from Geotagged plugin (by Rob Neild)
    4 // and the core JOSM source code (by Immanuel Scholz and others)
    54
    65package org.openstreetmap.josm.gui.layer.geoimage;
     
    333332            imgList = new JList(new AbstractListModel() {
    334333                public Object getElementAt(int i) {
    335                     return yLayer.data.get(i).file.getName();
     334                    return yLayer.data.get(i).getFile().getName();
    336335                }
    337336
     
    345344                public void valueChanged(ListSelectionEvent arg0) {
    346345                    int index = imgList.getSelectedIndex();
    347                     imgDisp.setImage(yLayer.data.get(index).file);
    348                     Date date = yLayer.data.get(index).time;
     346                    imgDisp.setImage(yLayer.data.get(index).getFile());
     347                    Date date = yLayer.data.get(index).getExifTime();
    349348                    if (date != null) {
    350349                        lbExifTime.setText(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date));
     
    649648                    return tr("No gpx selected");
    650649
    651                 lastNumMatched = matchGpxTrack(dateImgLst, selGpx.data, (long) (timezone * 3600) + delta);
     650                final long offset_ms = ((long) (timezone * 3600) + delta) * 1000; // in milliseconds
     651                lastNumMatched = matchGpxTrack(dateImgLst, selGpx.data, offset_ms);
    652652
    653653                return trn("<html>Matched <b>{0}</b> of <b>{1}</b> photo to GPX track.</html>",
     
    746746                    Main.pref.put("geoimage.showThumbs", yLayer.useThumbs);
    747747
    748                     yLayer.useThumbs = cbShowThumbs.isSelected();//FIXME
     748                    yLayer.useThumbs = cbShowThumbs.isSelected();
    749749                    yLayer.loadThumbs();
    750750
     
    955955
    956956            // Init variables
    957             long firstExifDate = imgs.get(0).time.getTime()/1000;
     957            long firstExifDate = imgs.get(0).getExifTime().getTime()/1000;
    958958
    959959            long firstGPXDate = -1;
     
    10411041        ArrayList<ImageEntry> dateImgLst = new ArrayList<ImageEntry>(yLayer.data.size());
    10421042        for (ImageEntry e : yLayer.data) {
    1043             if (e.time == null) {
     1043            if (e.getExifTime() == null) {
    10441044                continue;
    10451045            }
    10461046
    1047             if (e.exifCoor != null) {
     1047            if (e.getExifCoor() != null) {
    10481048                if (!exif) {
    10491049                    continue;
     
    10511051            }
    10521052
    1053             if (e.isTagged() && e.exifCoor == null) {
     1053            if (e.isTagged() && e.getExifCoor() == null) {
    10541054                if (!tagged) {
    10551055                    continue;
     
    10621062        Collections.sort(dateImgLst, new Comparator<ImageEntry>() {
    10631063            public int compare(ImageEntry arg0, ImageEntry arg1) {
    1064                 return arg0.time.compareTo(arg1.time);
     1064                return arg0.getExifTime().compareTo(arg1.getExifTime());
    10651065            }
    10661066        });
     
    10821082    }
    10831083
    1084     private int matchGpxTrack(ArrayList<ImageEntry> dateImgLst, GpxData selectedGpx, long offset) {
     1084    /**
     1085     * Match a list of photos to a gpx track with a given offset.
     1086     * All images need a exifTime attribute and the List must be sorted according to these times.
     1087     */
     1088    private int matchGpxTrack(ArrayList<ImageEntry> images, GpxData selectedGpx, long offset) {
    10851089        int ret = 0;
    10861090
     
    10901094            for (GpxTrackSegment segment : trk.getSegments()) {
    10911095
    1092                 long prevDateWp = 0;
     1096                long prevWpTime = 0;
    10931097                WayPoint prevWp = null;
    10941098
    10951099                for (WayPoint curWp : segment.getWayPoints()) {
    10961100
    1097                     String curDateWpStr = (String) curWp.attr.get("time");
    1098                     if (curDateWpStr != null) {
     1101                    String curWpTimeStr = (String) curWp.attr.get("time");
     1102                    if (curWpTimeStr != null) {
    10991103
    11001104                        try {
    1101                             long curDateWp = dateParser.parse(curDateWpStr).getTime()/1000 + offset;
    1102                             ret += matchPoints(dateImgLst, prevWp, prevDateWp, curWp, curDateWp);
     1105                            long curWpTime = dateParser.parse(curWpTimeStr).getTime() + offset;
     1106                            ret += matchPoints(images, prevWp, prevWpTime, curWp, curWpTime, offset);
    11031107
    11041108                            prevWp = curWp;
    1105                             prevDateWp = curDateWp;
     1109                            prevWpTime = curWpTime;
    11061110
    11071111                        } catch(ParseException e) {
    1108                             System.err.println("Error while parsing date \"" + curDateWpStr + '"');
     1112                            System.err.println("Error while parsing date \"" + curWpTimeStr + '"');
    11091113                            e.printStackTrace();
    11101114                            prevWp = null;
    1111                             prevDateWp = 0;
     1115                            prevWpTime = 0;
    11121116                        }
    11131117                    } else {
    11141118                        prevWp = null;
    1115                         prevDateWp = 0;
     1119                        prevWpTime = 0;
    11161120                    }
    11171121                }
     
    11211125    }
    11221126
    1123     private int matchPoints(ArrayList<ImageEntry> dateImgLst, WayPoint prevWp, long prevDateWp,
    1124             WayPoint curWp, long curDateWp) {
     1127    private int matchPoints(ArrayList<ImageEntry> images, WayPoint prevWp, long prevWpTime,
     1128            WayPoint curWp, long curWpTime, long offset) {
    11251129        // Time between the track point and the previous one, 5 sec if first point, i.e. photos take
    11261130        // 5 sec before the first track point can be assumed to be take at the starting position
    1127         long interval = prevDateWp > 0 ? ((int)Math.abs(curDateWp - prevDateWp)) : 5;
     1131        long interval = prevWpTime > 0 ? ((long)Math.abs(curWpTime - prevWpTime)) : 5*1000;
    11281132        int ret = 0;
    11291133
    11301134        // i is the index of the timewise last photo that has the same or earlier EXIF time
    1131         int i = getLastIndexOfListBefore(dateImgLst, curDateWp);
     1135        int i = getLastIndexOfListBefore(images, curWpTime);
    11321136
    11331137        // no photos match
     
    11421146            double distance = prevWp.getCoor().greatCircleDistance(curWp.getCoor());
    11431147            // This is in km/h, 3.6 * m/s
    1144             if (curDateWp > prevDateWp) {
    1145                 speed = 3.6 * distance / (curDateWp - prevDateWp);
     1148            if (curWpTime > prevWpTime) {
     1149                speed = 3.6 * distance / (curWpTime - prevWpTime);
    11461150            }
    11471151            try {
     
    11561160        // First trackpoint, then interval is set to five seconds, i.e. photos up to five seconds
    11571161        // before the first point will be geotagged with the starting point
    1158         if(prevDateWp == 0 || curDateWp <= prevDateWp) {
    1159             while(i >= 0 && (dateImgLst.get(i).time.getTime()/1000) <= curDateWp
    1160                     && (dateImgLst.get(i).time.getTime()/1000) >= (curDateWp - interval)) {
    1161                 if(dateImgLst.get(i).tmp.getPos() == null) {
    1162                     dateImgLst.get(i).tmp.setCoor(curWp.getCoor());
    1163                     dateImgLst.get(i).tmp.setSpeed(speed);
    1164                     dateImgLst.get(i).tmp.setElevation(curElevation);
     1162        if(prevWpTime == 0 || curWpTime <= prevWpTime) {
     1163            while (true) {
     1164                if (i < 0)
     1165                    break;
     1166                final ImageEntry curImg = images.get(i);
     1167                if (curImg.getExifTime().getTime() > curWpTime
     1168                    || curImg.getExifTime().getTime() < curWpTime - interval)
     1169                        break;
     1170                if(curImg.tmp.getPos() == null) {
     1171                    curImg.tmp.setPos(curWp.getCoor());
     1172                    curImg.tmp.setSpeed(speed);
     1173                    curImg.tmp.setElevation(curElevation);
     1174                    curImg.tmp.setGpsTime(new Date(curImg.getExifTime().getTime() - offset));
    11651175                    ret++;
    11661176                }
     
    11721182        // This code gives a simple linear interpolation of the coordinates between current and
    11731183        // previous track point assuming a constant speed in between
    1174         long imgDate;
    1175         while(i >= 0 && (imgDate = dateImgLst.get(i).time.getTime()/1000) >= prevDateWp) {
    1176 
    1177             if(dateImgLst.get(i).tmp.getPos() == null) {
     1184        while (true) {
     1185            if (i < 0)
     1186                break;
     1187            ImageEntry curImg = images.get(i);
     1188            long imgTime = curImg.getExifTime().getTime();
     1189            if (imgTime < prevWpTime)
     1190                break;
     1191
     1192            if(curImg.tmp.getPos() == null) {
    11781193                // The values of timeDiff are between 0 and 1, it is not seconds but a dimensionless
    11791194                // variable
    1180                 double timeDiff = (double)(imgDate - prevDateWp) / interval;
    1181                 dateImgLst.get(i).tmp.setCoor(prevWp.getCoor().interpolate(curWp.getCoor(), timeDiff));
    1182                 dateImgLst.get(i).tmp.setSpeed(speed);
    1183 
     1195                double timeDiff = (double)(imgTime - prevWpTime) / interval;
     1196                curImg.tmp.setPos(prevWp.getCoor().interpolate(curWp.getCoor(), timeDiff));
     1197                curImg.tmp.setSpeed(speed);
    11841198                if (curElevation != null && prevElevation != null) {
    1185                     dateImgLst.get(i).setElevation(prevElevation + (curElevation - prevElevation) * timeDiff);
    1186                 }
     1199                    curImg.setElevation(prevElevation + (curElevation - prevElevation) * timeDiff);
     1200                }
     1201                curImg.tmp.setGpsTime(new Date(curImg.getExifTime().getTime() - offset));
    11871202
    11881203                ret++;
     
    11931208    }
    11941209
    1195     private int getLastIndexOfListBefore(ArrayList<ImageEntry> dateImgLst, long searchedDate) {
    1196         int lstSize= dateImgLst.size();
     1210    private int getLastIndexOfListBefore(ArrayList<ImageEntry> images, long searchedTime) {
     1211        int lstSize= images.size();
    11971212
    11981213        // No photos or the first photo taken is later than the search period
    1199         if(lstSize == 0 || searchedDate < dateImgLst.get(0).time.getTime()/1000)
     1214        if(lstSize == 0 || searchedTime < images.get(0).getExifTime().getTime())
    12001215            return -1;
    12011216
    12021217        // The search period is later than the last photo
    1203         if (searchedDate > dateImgLst.get(lstSize - 1).time.getTime() / 1000)
     1218        if (searchedTime > images.get(lstSize - 1).getExifTime().getTime())
    12041219            return lstSize-1;
    12051220
     
    12101225        while (endIndex - startIndex > 1) {
    12111226            curIndex= (endIndex + startIndex) / 2;
    1212             if (searchedDate > dateImgLst.get(curIndex).time.getTime()/1000) {
     1227            if (searchedTime > images.get(curIndex).getExifTime().getTime()) {
    12131228                startIndex= curIndex;
    12141229            } else {
     
    12161231            }
    12171232        }
    1218         if (searchedDate < dateImgLst.get(endIndex).time.getTime()/1000)
     1233        if (searchedTime < images.get(endIndex).getExifTime().getTime())
    12191234            return startIndex;
    12201235
    12211236        // This final loop is to check if photos with the exact same EXIF time follows
    1222         while ((endIndex < (lstSize-1)) && (dateImgLst.get(endIndex).time.getTime()
    1223                 == dateImgLst.get(endIndex + 1).time.getTime())) {
     1237        while ((endIndex < (lstSize-1)) && (images.get(endIndex).getExifTime().getTime()
     1238                == images.get(endIndex + 1).getExifTime().getTime())) {
    12241239            endIndex++;
    12251240        }
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

    r2909 r2931  
    146146
    147147                try {
    148                     e.time = ExifReader.readTime(f);
     148                    e.setExifTime(ExifReader.readTime(f));
    149149                } catch (ParseException e1) {
    150                     e.time = null;
    151                 }
    152                 e.file = f;
     150                    e.setExifTime(null);
     151                }
     152                e.setFile(f);
    153153                extractExif(e);
    154154                data.add(e);
     
    313313        entries.add(new JSeparator());
    314314        entries.add(correlateItem);
     315        if (!menuAdditions.isEmpty()) {
     316            entries.add(new JSeparator());
     317        }
    315318        for (LayerMenuAddition addition : menuAdditions) {
    316319            entries.add(addition.getComponent(this));
     
    365368            for (int i = data.size() - 2; i >= 0; i--) {
    366369                cur = data.get(i);
    367                 if (cur.file.equals(prev.file)) {
     370                if (cur.getFile().equals(prev.getFile())) {
    368371                    data.remove(i);
    369372                } else {
     
    505508            double lon, lat;
    506509
    507             Metadata metadata = JpegMetadataReader.readMetadata(e.file);
     510            Metadata metadata = JpegMetadataReader.readMetadata(e.getFile());
    508511            Directory dir = metadata.getDirectory(GpsDirectory.class);
    509512
    510513            // longitude
    511514
    512             Rational[] components = dir
    513             .getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE);
     515            Rational[] components = dir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE);
    514516
    515517            deg = components[0].intValue();
     
    539541            // Store values
    540542
    541             e.setCoor(new LatLon(lat, lon));
    542             e.exifCoor = e.getPos();
     543            e.setExifCoor(new LatLon(lat, lon));
     544            e.setPos(e.getExifCoor());
    543545
    544546        } catch (CompoundException p) {
    545             e.exifCoor = null;
     547            e.setExifCoor(null);
    546548            e.setPos(null);
    547549        }
     
    607609            .setButtonIcons(new String[] {"cancel.png", "dialogs/delete.png"})
    608610            .setContent(new JLabel(tr("<html><h3>Delete the file {0} from disk?<p>The image file will be permanently lost!</h3></html>"
    609                     ,toDelete.file.getName()), ImageProvider.get("dialogs/geoimage/deletefromdisk"),SwingConstants.LEFT))
     611                    ,toDelete.getFile().getName()), ImageProvider.get("dialogs/geoimage/deletefromdisk"),SwingConstants.LEFT))
    610612                    .toggleEnable("geoimage.deleteimagefromdisk")
    611613                    .setCancelButton(1)
     
    626628                }
    627629
    628                 if (toDelete.file.delete()) {
    629                     System.out.println("File "+toDelete.file.toString()+" deleted. ");
     630                if (toDelete.getFile().delete()) {
     631                    System.out.println("File "+toDelete.getFile().toString()+" deleted. ");
    630632                } else {
    631633                    JOptionPane.showMessageDialog(
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageEntry.java

    r2904 r2931  
    1818
    1919final public class ImageEntry implements Comparable<ImageEntry>, Cloneable {
    20     File file;
    21     Date time;
    22     LatLon exifCoor;
     20    private File file;
     21    private LatLon exifCoor;
     22    private Date exifTime;
     23    Image thumbnail;
    2324
     25    /** The following values are computed from the correlation with the gpx track */
    2426    private CachedLatLon pos;
    2527    /** Speed in kilometer per second */
     
    2729    /** Elevation (altitude) in meters */
    2830    private Double elevation;
    29 
    30     Image thumbnail;
     31    /** The time after correlation with a gpx track */
     32    private Date gpsTime;
    3133
    3234    /**
     
    4042    ImageEntry tmp;
    4143
     44    /**
     45     * getter methods that refer to the temporary value
     46     */
    4247    public CachedLatLon getPos() {
    4348        if (tmp != null)
     
    5560        return elevation;
    5661    }
     62    public Date getGpsTime() {
     63        if (tmp != null)
     64            return tmp.gpsTime;
     65        return gpsTime;
     66    }
     67
     68    /**
     69     * other getter methods
     70     */
     71    public File getFile() {
     72        return file;
     73    }
     74    public Date getExifTime() {
     75        return exifTime;
     76    }
     77    LatLon getExifCoor() {
     78        return exifCoor;
     79    }
     80    /**
     81     * setter methods
     82     */
    5783    public void setPos(CachedLatLon pos) {
    5884        this.pos = pos;
     85    }
     86    public void setPos(LatLon pos) {
     87        this.pos = new CachedLatLon(pos);
    5988    }
    6089    public void setSpeed(Double speed) {
     
    6493        this.elevation = elevation;
    6594    }
    66    
    67     public File getFile() {
    68         return file;
     95    void setFile(File file) {
     96        this.file = file;
     97    }
     98    void setExifTime(Date exifTime) {
     99        this.exifTime = exifTime;
     100    }
     101    void setGpsTime(Date gpsTime) {
     102        this.gpsTime = gpsTime;
     103    }
     104    void setExifCoor(LatLon exifCoor) {
     105        this.exifCoor = exifCoor;
    69106    }
    70107
     
    80117    }
    81118
    82     public void setCoor(LatLon latlon)
    83     {
    84         pos = new CachedLatLon(latlon);
    85     }
    86 
    87119    public int compareTo(ImageEntry image) {
    88         if (time != null && image.time != null)
    89             return time.compareTo(image.time);
    90         else if (time == null && image.time == null)
     120        if (exifTime != null && image.exifTime != null)
     121            return exifTime.compareTo(image.exifTime);
     122        else if (exifTime == null && image.exifTime == null)
    91123            return 0;
    92         else if (time == null)
     124        else if (exifTime == null)
    93125            return -1;
    94126        else
     
    96128    }
    97129
    98     public void applyTmp() {
    99         if (tmp != null) {
    100             pos = tmp.pos;
    101             speed = tmp.speed;
    102             elevation = tmp.elevation;
    103             tmp = null;
    104         }
    105     }
     130    /**
     131     * Make a fresh copy and save it in the temporary variable.
     132     */
    106133    public void cleanTmp() {
    107134        tmp = clone();
     
    110137    }
    111138
     139    /**
     140     * Copy the values from the temporary variable to the main instance.
     141     */
     142    public void applyTmp() {
     143        if (tmp != null) {
     144            pos = tmp.pos;
     145            speed = tmp.speed;
     146            elevation = tmp.elevation;
     147            gpsTime = tmp.gpsTime;
     148            tmp = null;
     149        }
     150    }
     151
     152    /**
     153     * If it has been tagged i.e. matched to a gpx track or retrieved lat/lon from exif
     154     */
    112155    public boolean isTagged() {
    113156        return pos != null;
     
    115158
    116159    /**
    117      * only partial info
     160     * String representation. (only partial info)
    118161     */
    119162    @Override
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java

    r2909 r2931  
    1414import java.awt.GridBagLayout;
    1515import java.awt.event.ActionEvent;
    16 import java.awt.event.ActionListener;
    1716import java.awt.event.KeyEvent;
    1817import java.awt.event.WindowEvent;
     
    238237
    239238        if (entry != null) {
    240             imgDisplay.setImage(entry.file);
    241             setTitle("Geotagged Images" + (entry.file != null ? " - " + entry.file.getName() : ""));
    242             StringBuffer osd = new StringBuffer(entry.file != null ? entry.file.getName() : "");
     239            imgDisplay.setImage(entry.getFile());
     240            setTitle("Geotagged Images" + (entry.getFile() != null ? " - " + entry.getFile().getName() : ""));
     241            StringBuffer osd = new StringBuffer(entry.getFile() != null ? entry.getFile().getName() : "");
    243242            if (entry.getElevation() != null) {
    244243                osd.append(tr("\nAltitude: {0} m", entry.getElevation().longValue()));
     
    250249            //    osd.append(tr("\nlat: {0}, lon: {1}", Double.toString(entry.getPos().lat()), Double.toString(entry.getPos().lon())));
    251250            //}
     251            //osd.append(tr("\nfile mtime: {0}", Long.toString(entry.getFile().lastModified())));
     252            //osd.append(tr("\nImage exif time: {0}", Long.toString(entry.getExifTime().getTime())));
     253            //if (entry.getGpsTime() != null) {
     254            //    osd.append(tr("\nImage gps time: {0}", Long.toString(entry.getGpsTime().getTime())));
     255            //}
     256           
    252257            imgDisplay.setOsdText(osd.toString());
    253258        } else {
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java

    r2662 r2931  
    7272
    7373        private BufferedImage loadThumb(ImageEntry entry) {
    74             final String cacheIdent = entry.file.toString()+":"+maxSize;
     74            final String cacheIdent = entry.getFile().toString()+":"+maxSize;
    7575
    7676            if (!cacheOff) {
     
    8282            }
    8383
    84             Image img = Toolkit.getDefaultToolkit().createImage(entry.file.getPath());
     84            Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
    8585            tracker.addImage(img, 0);
    8686            try {
Note: See TracChangeset for help on using the changeset viewer.