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

Last change on this file since 11710 was 10906, checked in by Don-vip, 8 years ago

add more non-regression NMEA unit tests

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.gpx;
3
4import java.awt.Color;
5import java.util.ArrayList;
6import java.util.Date;
7import java.util.List;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
11import org.openstreetmap.josm.data.coor.EastNorth;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.projection.Projections;
14import org.openstreetmap.josm.tools.UncheckedParseException;
15import org.openstreetmap.josm.tools.date.DateUtils;
16import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
17
18public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider {
19
20 /**
21 * The seconds (not milliseconds!) since 1970-01-01 00:00 UTC
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 final double lat;
50 private final double lon;
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 #invalidateEastNorthCache() invalidate the internal cache}.</p>
78 *
79 * @return the east north coordinates or {@code null}
80 * @see #invalidateEastNorthCache()
81 */
82 public final EastNorth getEastNorth() {
83 if (Double.isNaN(east) || Double.isNaN(north)) {
84 // projected coordinates haven't been calculated yet,
85 // so fill the cache of the projected waypoint coordinates
86 EastNorth en = Projections.project(new LatLon(lat, lon));
87 this.east = en.east();
88 this.north = en.north();
89 }
90 return new EastNorth(east, north);
91 }
92
93 @Override
94 public String toString() {
95 return "WayPoint (" + (attr.containsKey(GPX_NAME) ? get(GPX_NAME) + ", " : "") + getCoor() + ", " + attr + ')';
96 }
97
98 /**
99 * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
100 *
101 * @param time the time to set
102 * @since 9383
103 */
104 public void setTime(Date time) {
105 this.time = time.getTime() / 1000.;
106 this.attr.put(PT_TIME, DateUtils.fromDate(time));
107 }
108
109 /**
110 * Convert the time stamp of the waypoint into seconds from the epoch
111 */
112 public void setTime() {
113 setTimeFromAttribute();
114 }
115
116 /**
117 * Convert the time stamp of the waypoint into seconds from the epoch
118 * @return The parsed time if successful, or {@code null}
119 * @since 9383
120 */
121 public Date setTimeFromAttribute() {
122 if (attr.containsKey(PT_TIME)) {
123 try {
124 final Date time = DateUtils.fromString(get(PT_TIME).toString());
125 this.time = time.getTime() / 1000.;
126 return time;
127 } catch (UncheckedParseException e) {
128 Main.warn(e);
129 time = 0;
130 }
131 }
132 return null;
133 }
134
135 @Override
136 public int compareTo(WayPoint w) {
137 return Double.compare(time, w.time);
138 }
139
140 public Date getTime() {
141 return new Date((long) (time * 1000));
142 }
143
144 @Override
145 public Object getTemplateValue(String name, boolean special) {
146 if (!special)
147 return get(name);
148 else
149 return null;
150 }
151
152 @Override
153 public boolean evaluateCondition(Match condition) {
154 throw new UnsupportedOperationException();
155 }
156
157 @Override
158 public List<String> getTemplateKeys() {
159 return new ArrayList<>(attr.keySet());
160 }
161
162 @Override
163 public int hashCode() {
164 final int prime = 31;
165 int result = super.hashCode();
166 long temp = Double.doubleToLongBits(lat);
167 result = prime * result + (int) (temp ^ (temp >>> 32));
168 temp = Double.doubleToLongBits(lon);
169 result = prime * result + (int) (temp ^ (temp >>> 32));
170 temp = Double.doubleToLongBits(time);
171 result = prime * result + (int) (temp ^ (temp >>> 32));
172 return result;
173 }
174
175 @Override
176 public boolean equals(Object obj) {
177 if (this == obj)
178 return true;
179 if (!super.equals(obj))
180 return false;
181 if (getClass() != obj.getClass())
182 return false;
183 WayPoint other = (WayPoint) obj;
184 if (Double.doubleToLongBits(lat) != Double.doubleToLongBits(other.lat))
185 return false;
186 if (Double.doubleToLongBits(lon) != Double.doubleToLongBits(other.lon))
187 return false;
188 if (Double.doubleToLongBits(time) != Double.doubleToLongBits(other.time))
189 return false;
190 return true;
191 }
192}
Note: See TracBrowser for help on using the repository browser.