source: josm/trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java@ 4234

Last change on this file since 4234 was 4129, checked in by stoecker, 13 years ago

speedup GPX drawing due to better clipping

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1//License: GPLv2 or later
2//Copyright 2007 by Raphael Mack and others
3
4package org.openstreetmap.josm.data.gpx;
5
6import java.awt.Color;
7import java.util.Date;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.coor.LatLon;
12import org.openstreetmap.josm.data.projection.Projections;
13import org.openstreetmap.josm.tools.PrimaryDateParser;
14
15public class WayPoint extends WithAttributes implements Comparable<WayPoint> {
16
17 private static ThreadLocal<PrimaryDateParser> dateParser = new ThreadLocal<PrimaryDateParser>() {
18 @Override protected PrimaryDateParser initialValue() {
19 return new PrimaryDateParser();
20 }
21 };
22
23 public double time;
24 public Color customColoring;
25 public boolean drawLine;
26 public int dir;
27
28 public WayPoint(WayPoint p) {
29 attr.putAll(p.attr);
30 lat = p.lat;
31 lon = p.lon;
32 east = p.east;
33 north = p.north;
34 time = p.time;
35 customColoring = p.customColoring;
36 drawLine = p.drawLine;
37 dir = p.dir;
38 }
39
40 public WayPoint(LatLon ll) {
41 lat = ll.lat();
42 lon = ll.lon();
43 }
44
45 /*
46 * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
47 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
48 */
49 private double lat = 0;
50 private double lon = 0;
51
52 /*
53 * internal cache of projected coordinates
54 */
55 private double east = Double.NaN;
56 private double north = Double.NaN;
57
58 /**
59 * Invalidate the internal cache of east/north coordinates.
60 */
61 public void invalidateEastNorthCache() {
62 this.east = Double.NaN;
63 this.north = Double.NaN;
64 }
65
66 public final LatLon getCoor() {
67 return new LatLon(lat,lon);
68 }
69
70 /**
71 * <p>Replies the projected east/north coordinates.</p>
72 *
73 * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
74 * Internally caches the projected coordinates.</p>
75 *
76 * <p><strong>Caveat:</strong> doesn't listen to projection changes. Clients must
77 * {@link #reproject() trigger a reprojection} or {@link #invalidateEastNorthCache() invalidate the internal cache}.</p>
78 *
79 * @return the east north coordinates or {@code null}
80 * @see #invalidateEastNorthCache()
81 *
82 */
83 public final EastNorth getEastNorth() {
84 if (Double.isNaN(east) || Double.isNaN(north)) {
85 // projected coordinates haven't been calculated yet,
86 // so fill the cache of the projected waypoint coordinates
87 EastNorth en = Projections.project(new LatLon(lat, lon));
88 this.east = en.east();
89 this.north = en.north();
90 }
91 return new EastNorth(east, north);
92 }
93
94 @Override
95 public String toString() {
96 return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + getCoor().toString() + ", " + attr + ")";
97 }
98
99 /**
100 * Convert the time stamp of the waypoint into seconds from the epoch
101 */
102 public void setTime() {
103 if(attr.containsKey("time")) {
104 try {
105 time = dateParser.get().parse(attr.get("time").toString()).getTime() / 1000.; /* ms => seconds */
106 } catch(Exception e) {
107 time = 0;
108 }
109 }
110 }
111
112 public int compareTo(WayPoint w) {
113 return Double.compare(time, w.time);
114 }
115
116 public Date getTime() {
117 return new Date((long) (time * 1000));
118 }
119}
Note: See TracBrowser for help on using the repository browser.