Ignore:
Timestamp:
2009-07-03T22:19:22+02:00 (10 years ago)
Author:
stoecker
Message:

some more changes and bug fixes related to new projection stuff - GPX should now work also

Location:
trunk/src/org/openstreetmap/josm/data/gpx
Files:
2 edited

Legend:

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

    r1722 r1724  
    8484        for (WayPoint wpt : waypoints) {
    8585            if (bounds == null) {
    86                 bounds = new Bounds(wpt.latlon, wpt.latlon);
     86                bounds = new Bounds(wpt.getCoor());
    8787            } else {
    88                 bounds.extend(wpt.latlon);
     88                bounds.extend(wpt.getCoor());
    8989            }
    9090        }
     
    9292            for (WayPoint wpt : rte.routePoints) {
    9393                if (bounds == null) {
    94                     bounds = new Bounds(wpt.latlon, wpt.latlon);
     94                    bounds = new Bounds(wpt.getCoor());
    9595                } else {
    96                     bounds.extend(wpt.latlon);
     96                    bounds.extend(wpt.getCoor());
    9797                }
    9898            }
     
    102102                for (WayPoint wpt : trkseg) {
    103103                    if (bounds == null) {
    104                         bounds = new Bounds(wpt.latlon, wpt.latlon);
     104                        bounds = new Bounds(wpt.getCoor());
    105105                    } else {
    106                         bounds.extend(wpt.latlon);
     106                        bounds.extend(wpt.getCoor());
    107107                    }
    108108                }
     
    123123                for (WayPoint tpt : trkseg) {
    124124                    if(last != null){
    125                         result += calcDistance(last.latlon, tpt.latlon);
     125                        result += last.getCoor().greatCircleDistance(tpt.getCoor());
    126126                    }
    127127                    last = tpt;
     
    132132        return result;
    133133    }
    134 
    135     /**
    136      * returns the distance in meters between two LatLons
    137      */
    138     public static double calcDistance(LatLon p1, LatLon p2){
    139         double lat1, lon1, lat2, lon2;
    140         double dlon, dlat;
    141 
    142         lat1 = p1.lat() * Math.PI / 180.0;
    143         lon1 = p1.lon() * Math.PI / 180.0;
    144         lat2 = p2.lat() * Math.PI / 180.0;
    145         lon2 = p2.lon() * Math.PI / 180.0;
    146 
    147         dlon = lon2 - lon1;
    148         dlat = lat2 - lat1;
    149 
    150         double a = (Math.pow(Math.sin(dlat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2), 2));
    151         double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    152         return 6367000 * c;
    153     }
    154 
    155134}
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r1721 r1724  
    44package org.openstreetmap.josm.data.gpx;
    55
    6 import java.text.ParsePosition;
    7 import java.text.SimpleDateFormat;
    86import java.util.Date;
    9 import java.util.regex.Pattern;
    107import java.awt.Color;
    118
    129import org.openstreetmap.josm.Main;
     10import org.openstreetmap.josm.data.coor.CachedLatLon;
    1311import org.openstreetmap.josm.data.coor.EastNorth;
    1412import org.openstreetmap.josm.data.coor.LatLon;
     13import org.openstreetmap.josm.data.projection.Projection;
     14import org.openstreetmap.josm.tools.DateUtils;
    1515
    1616public class WayPoint extends WithAttributes implements Comparable<WayPoint>
    1717{
    18     public final LatLon latlon;
    19     public final EastNorth eastNorth;
    2018    public double time;
    2119    public Color customColoring;
     
    2321    public int dir;
    2422
     23    private CachedLatLon coor;
     24
     25    public final LatLon getCoor() {
     26        return coor;
     27    }
     28
     29    public final EastNorth getEastNorth() {
     30        return coor.getEastNorth();
     31    }
     32
    2533    public WayPoint(LatLon ll) {
    26         latlon = ll;
    27         eastNorth = Main.proj.latlon2eastNorth(ll);
     34        coor = new CachedLatLon(ll);
    2835    }
    2936
    3037    @Override
    3138    public String toString() {
    32         return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + latlon.toString() + ", " + attr + ")";
     39        return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + coor.toString() + ", " + attr + ")";
    3340    }
    3441
     
    3643     * Convert the time stamp of the waypoint into seconds from the epoch
    3744     */
    38     public final static SimpleDateFormat GPXTIMEFMT =
    39         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    40     public final static SimpleDateFormat GPXTIMEFMT_nofrac =
    41         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    42     public final static SimpleDateFormat GPXTIMEFMT_tz =
    43         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    44     public final static SimpleDateFormat GPXTIMEFMT_tz_nofrac =
    45         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    46 
    47         private final static Pattern colontz = Pattern.compile(".*[+-][0-9][0-9]:[0-9][0-9]\\z");
    48         private final static Pattern colontzreplacement = Pattern.compile("([+-][0-9][0-9]):([0-9][0-9])\\z");
    49 
    50         public void setTime() {
    51         if (! attr.containsKey("time")) {
    52             return;
    53         }
    54         String timestring = attr.get("time").toString();
    55        
    56         /* make the string timzeone be conanonical - unfortunately the allowed timezone in a
    57          * GPX is Z or +/-hh:mm whereas in simpledateformat it is +/-hhmm only (no colon)
    58          * If no timezone is given, the time will be interpreted as local time by parse. */
    59         if (timestring.substring(timestring.length() - 1).equals("Z")) {
    60                 timestring = timestring.substring(0, timestring.length() - 1) + "+0000";
    61         } else if (colontz.matcher(timestring).matches()) {
    62                 timestring = colontzreplacement.matcher(timestring).replaceFirst("$1$2");
    63         }
    64         Date d = GPXTIMEFMT_tz.parse(timestring, new ParsePosition(0));
    65         if (d == null) {
    66                 d = GPXTIMEFMT_tz_nofrac.parse(timestring, new ParsePosition(0));
    67                 if (d == null) {
    68                         /* try without a zimezone indication */
    69                         d = GPXTIMEFMT.parse(timestring, new ParsePosition(0));                 
    70                         if (d == null) {
    71                                 d = GPXTIMEFMT_nofrac.parse(timestring, new ParsePosition(0));                 
    72                         }
    73                 // date has parsed in local time, and been adjusted to UTC by parse
     45    public void setTime() {
     46        for(String key : new String[]{"time", "cmt", "desc"})
     47        {
     48            if(attr.containsKey("time"))
     49            {
     50                double t = DateUtils.fromString(attr.get("time").toString()).getTime();
     51                if(t != 0.0)
     52                {
     53                    time = t / 1000.0; /* ms => seconds */
     54                    break;
     55                }
    7456            }
    75         }
    76         if (d != null /* parsing ok */) {
    77             time = d.getTime() / 1000.0; /* ms => seconds */
    78         }
    79 }
    80     /**
    81      * Convert a time stamp of the waypoint from the <cmt> or <desc> field
    82      * into seconds from the epoch. Handles the date format as it is used by
    83      * Garmin handhelds. Does not overwrite an existing timestamp (!= 0.0).
    84      * A value of <time> fields overwrites values set with by method.
    85      * Does nothing if specified key does not exist or text cannot be parsed.
    86      *
    87      * @param key The key that contains the text to convert.
    88      */
    89     public void setGarminCommentTime(String key) {
    90         // do not overwrite time if already set
    91         if (time != 0.0) {
    92             return;
    93         }
    94         if (! attr.containsKey(key)) {
    95             return;
    96         }
    97         // example date format "18-AUG-08 13:33:03"
    98         SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss"); // Garmin wpts have no timezone
    99         Date d = f.parse(attr.get(key).toString(), new ParsePosition(0));
    100         if (d != null /* parsing OK */) {
    101             time = d.getTime() / 1000.0; /* ms => seconds */
    10257        }
    10358    }
Note: See TracChangeset for help on using the changeset viewer.