Ignore:
Timestamp:
2009-07-03T22:19:22+02:00 (17 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
Files:
1 added
14 edited

Legend:

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

    r1722 r1724  
    2727    }
    2828
     29    public Bounds(LatLon b) {
     30        this.min = b;
     31        this.max = b;
     32    }
     33
    2934    @Override public String toString() {
    3035        return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
    … …  
    3439     * @return Center of the bounding box.
    3540     */
    36     public LatLon center() {
    37         // FIXME: not sure whether this calculation is right; maybe there is some
    38         // more complex calculation needed to get a center of a spherical
    39         // dimension?
    40         return new LatLon((min.lat()+max.lat())/2, (min.lon()+max.lon())/2);
     41    public LatLon getCenter()
     42    {
     43        return min.getCenter(max);
    4144    }
    4245
  • trunk/src/org/openstreetmap/josm/data/ProjectionBounds.java

    r1722 r1724  
    2525        this.max = max;
    2626    }
     27    public ProjectionBounds(EastNorth p) {
     28        this.min = p;
     29        this.max = p;
     30    }
    2731    public ProjectionBounds(EastNorth center, double east, double north) {
    2832        this.min = new EastNorth(center.east()-east/2.0, center.north()-north/2.0);
    … …  
    3236    {
    3337        if (e.east() < min.east() || e.north() < min.north())
    34             min = e;
    35         else if (e.east() > max.east() || e.north() > max.north())
    36             max = e;
     38            min = new EastNorth(Math.min(e.east(), min.east()), Math.min(e.north(), min.north()));
     39        if (e.east() > max.east() || e.north() > max.north())
     40            max = new EastNorth(Math.max(e.east(), max.east()), Math.max(e.north(), max.north()));
    3741    }
    3842    public EastNorth getCenter()
    3943    {
    40         return  new EastNorth(min.east()/2+max.east()/2, min.north()/2+max.north()/2);
     44        return min.getCenter(max);
     45    }
     46
     47    @Override public String toString() {
     48        return "ProjectionBounds["+min.east()+","+min.north()+","+max.east()+","+max.north()+"]";
    4149    }
    4250}
  • trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java

    r1209 r1724  
    3030        return new EastNorth(this.x + proportion * (en2.x - this.x),
    3131            this.y + proportion * (en2.y - this.y));
     32    }
     33
     34    public EastNorth getCenter(EastNorth en2) {
     35        return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
    3236    }
    3337
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r1722 r1724  
    143143    }
    144144
     145    public LatLon interpolate(LatLon ll2, double proportion) {
     146        return new LatLon(this.x + proportion * (ll2.x - this.x),
     147            this.y + proportion * (ll2.y - this.y));
     148    }
     149
     150    public LatLon getCenter(LatLon ll2) {
     151        return new LatLon((this.x + ll2.x)/2.0, (this.y + ll2.y)/2.0);
     152    }
     153
    145154    @Override public String toString() {
    146155        return "LatLon[lat="+lat()+",lon="+lon()+"]";
  • 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    }
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r1722 r1724  
    66import org.openstreetmap.josm.Main;
    77import org.openstreetmap.josm.data.coor.EastNorth;
     8import org.openstreetmap.josm.data.coor.CachedLatLon;
    89import org.openstreetmap.josm.data.coor.LatLon;
    910import org.openstreetmap.josm.data.coor.LatLon.CoordinateFormat;
    … …  
    1112import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1213import org.openstreetmap.josm.data.osm.Node;
    13 
    1414
    1515/**
    … …  
    2020public final class Node extends OsmPrimitive {
    2121
    22     private LatLon coor;
    23 
    24     private EastNorth eastNorth;
    25     private Projection proj;
    26 
     22    private CachedLatLon coor;
    2723
    2824    public final void setCoor(LatLon coor) {
    29         this.coor = coor;
    30         proj = null;
     25        if(this.coor == null)
     26            this.coor = new CachedLatLon(coor);
     27        else
     28            this.coor.setCoor(coor);
    3129    }
    3230
    … …  
    3634
    3735    public final void setEastNorth(EastNorth eastNorth) {
    38         proj = Main.proj;
    39         eastNorth = eastNorth;
    40         this.coor = proj.eastNorth2latlon(eastNorth);
     36        coor.setEastNorth(eastNorth);
    4137    }
    4238
    4339    public final EastNorth getEastNorth() {
    44         if(proj != Main.proj)
    45         {
    46             proj = Main.proj;
    47             eastNorth = proj.latlon2eastNorth(coor);
    48         }
    49         return eastNorth;
     40        return coor.getEastNorth();
    5041    }
    5142
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java

    r1722 r1724  
    66import org.openstreetmap.josm.data.ProjectionBounds;
    77import org.openstreetmap.josm.data.coor.EastNorth;
     8import org.openstreetmap.josm.data.coor.CachedLatLon;
    89import org.openstreetmap.josm.data.coor.LatLon;
    910import org.openstreetmap.josm.data.osm.Node;
    … …  
    4243        if(b != null)
    4344        {
    44             visit(Main.proj.latlon2eastNorth(b.min));
    45             visit(Main.proj.latlon2eastNorth(b.max));
     45            visit(b.min);
     46            visit(b.max);
    4647        }
    4748    }
    … …  
    4950    public void visit(ProjectionBounds b) {
    5051        if(b != null)
    51             bounds = new ProjectionBounds(b.min, b.max);
     52        {
     53            visit(b.min);
     54            visit(b.max);
     55        }
     56    }
     57
     58    public void visit(LatLon latlon) {
     59        if(latlon != null)
     60        {
     61            if(latlon instanceof CachedLatLon)
     62                visit(((CachedLatLon)latlon).getEastNorth());
     63            else
     64                visit(Main.proj.latlon2eastNorth(latlon));
     65        }
    5266    }
    5367
    … …  
    5569        if (eastNorth != null) {
    5670            if (bounds == null)
    57                 bounds = new ProjectionBounds(eastNorth, eastNorth);
     71                bounds = new ProjectionBounds(eastNorth);
    5872            else
    5973                bounds.extend(eastNorth);
    … …  
    98112        Main.proj.latlon2eastNorth(new LatLon(maxLatlon.lat() + enlargeDegree, maxLatlon.lon() + enlargeDegree)));
    99113    }
     114
     115    @Override public String toString() {
     116        return "BoundingXYVisitor["+bounds+"]";
     117    }
    100118}
  • trunk/src/org/openstreetmap/josm/data/projection/Epsg4326.java

    r1722 r1724  
    2525
    2626    @Override public String toString() {
    27         return tr("EPSG:4326");
     27        return tr("WGS84 Geographisch");
    2828    }
    2929
    … …  
    3434    public String getCacheDirectoryName() {
    3535        return "epsg4326";
    36     }
    37 
    38     @Override public boolean equals(Object o) {
    39         return o instanceof Epsg4326;
    4036    }
    4137
  • trunk/src/org/openstreetmap/josm/data/projection/Lambert.java

    r1722 r1724  
    158158    }
    159159
    160     @Override
    161     public boolean equals(Object o) {
    162         return o instanceof Lambert;
    163     }
    164 
    165160    /**
    166161     * Initializes from geographic coordinates. Note that reference ellipsoid
  • trunk/src/org/openstreetmap/josm/data/projection/LambertEST.java

    r1722 r1724  
    107107    }
    108108
    109     @Override
    110     public boolean equals(Object o) {
    111         return o instanceof LambertEST;
    112     }
    113 
    114109    public ProjectionBounds getWorldBounds()
    115110    {
  • trunk/src/org/openstreetmap/josm/data/projection/Mercator.java

    r1722 r1724  
    4747    }
    4848
    49     @Override public boolean equals(Object o) {
    50         return o instanceof Mercator;
    51     }
    52 
    5349    public ProjectionBounds getWorldBounds()
    5450    {
  • trunk/src/org/openstreetmap/josm/data/projection/SwissGrid.java

    r1722 r1724  
    101101    }
    102102
    103     @Override
    104     public boolean equals(Object o) {
    105         return o instanceof SwissGrid;
    106     }
    107 
    108103    public ProjectionBounds getWorldBounds()
    109104    {
  • trunk/src/org/openstreetmap/josm/data/projection/UTM.java

    r1722 r1724  
    345345    }
    346346
    347     @Override public boolean equals(Object o) {
    348         return o instanceof UTM;
    349     }
    350 
    351347    public ProjectionBounds getWorldBounds()
    352348    {
Note: See TracChangeset for help on using the changeset viewer.