Changeset 647 in josm for trunk


Ignore:
Timestamp:
2008-06-10T00:16:09+02:00 (16 years ago)
Author:
ramack
Message:

improve info on GpxLayers, closes #756 and also gives track length

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r627 r647  
    99
    1010import org.openstreetmap.josm.data.Bounds;
     11import org.openstreetmap.josm.data.coor.LatLon;
    1112
    1213/**
     
    99100                }
    100101        }
     102   
     103    /**
     104     * calculates the sum of the lengths of all track segments
     105     */
     106    public double length(){
     107        double result = 0.0; // in meters
     108        WayPoint last = null;
     109               
     110        for (GpxTrack trk : tracks) {
     111            for (Collection<WayPoint> trkseg : trk.trackSegs) {
     112                for (WayPoint tpt : trkseg) {
     113                    if(last != null){
     114                        result += calcDistance(last.latlon, tpt.latlon);
     115                    }
     116                    last = tpt;
     117                }
     118                last = null; // restart for each track segment
     119            }
     120        }
     121        return result;
     122    }
     123
     124    /**
     125     * returns the distance in meters between two LatLons
     126     */
     127    public static double calcDistance(LatLon p1, LatLon p2){
     128        double lat1, lon1, lat2, lon2;
     129        double dlon, dlat;
     130           
     131        lat1 = p1.lat() * Math.PI / 180.0;
     132        lon1 = p1.lon() * Math.PI / 180.0;
     133        lat2 = p2.lat() * Math.PI / 180.0;
     134        lon2 = p2.lon() * Math.PI / 180.0;
     135
     136        dlon = lon2 - lon1;
     137        dlat = lat2 - lat1;
     138
     139        double a = (Math.pow(Math.sin(dlat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2), 2));
     140        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
     141        return 6367000 * c;
     142    }
     143
    101144}
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r646 r647  
    1313import org.openstreetmap.josm.data.coor.LatLon;
    1414
    15 public class WayPoint extends WithAttributes {
     15public class WayPoint extends WithAttributes implements Comparable{
    1616       
    1717        public final LatLon latlon;
     
    5050        }
    5151
     52    public int compareTo(Object other){
     53        if(other instanceof WayPoint){
     54            WayPoint w = (WayPoint)other;
     55            return (int)time - (int)w.time;
     56        }
     57        return 0;
     58    }
    5259}
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r646 r647  
    2626import java.util.Comparator;
    2727import java.util.LinkedList;
     28import java.util.Date;
     29import java.text.DateFormat;
     30import java.text.DecimalFormat;
    2831
    2932import javax.swing.AbstractAction;
     
    279282                                .append("<br>");
    280283
     284                if(data.tracks.size() > 0){
     285                    boolean first = true;
     286                    WayPoint earliest = null, latest = null;
     287
     288                    for(GpxTrack trk: data.tracks){
     289                        for(Collection<WayPoint> seg:trk.trackSegs){
     290                            for(WayPoint pnt:seg){
     291                                if(first){
     292                                    latest = earliest = pnt;
     293                                    first = false;
     294                                }else{
     295                                    if(pnt.compareTo(earliest) < 0){
     296                                        earliest = pnt;
     297                                    }else{
     298                                        latest = pnt;
     299                                    }
     300                                }
     301                            }
     302                        }
     303                    }
     304                    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
     305                    info.append(tr("Timespan: ") + df.format(new Date((long)(earliest.time * 1000))) + " - " + df.format(new Date((long)(latest.time * 1000))));
     306                    int diff = (int)(latest.time - earliest.time);
     307                    info.append(" (" + (diff / 3600) + ":" + ((diff % 3600)/60) + ")");
     308                    info.append("<br>");
     309                }
     310                info.append(tr("Length: ") + new DecimalFormat("#0.00").format(data.length() / 1000) + "km");
     311                info.append("<br>");
     312               
    281313                return info.append("</html>").toString();
    282314        }
Note: See TracChangeset for help on using the changeset viewer.