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

Last change on this file since 11893 was 11893, checked in by Don-vip, 7 years ago

sonar - squid:S1126 - Return of boolean expressions should not be wrapped into an "if-then-else" statement

  • Property svn:eol-style set to native
File size: 6.1 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 /**
29 * Constructs a new {@code WayPoint} from an existing one.
30 * @param p existing waypoint
31 */
32 public WayPoint(WayPoint p) {
33 attr.putAll(p.attr);
34 lat = p.lat;
35 lon = p.lon;
36 east = p.east;
37 north = p.north;
38 time = p.time;
39 customColoring = p.customColoring;
40 drawLine = p.drawLine;
41 dir = p.dir;
42 }
43
44 /**
45 * Constructs a new {@code WayPoint} from lat/lon coordinates.
46 * @param ll lat/lon coordinates
47 */
48 public WayPoint(LatLon ll) {
49 lat = ll.lat();
50 lon = ll.lon();
51 }
52
53 /*
54 * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
55 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
56 */
57 private final double lat;
58 private final double lon;
59
60 /*
61 * internal cache of projected coordinates
62 */
63 private double east = Double.NaN;
64 private double north = Double.NaN;
65
66 /**
67 * Invalidate the internal cache of east/north coordinates.
68 */
69 public void invalidateEastNorthCache() {
70 this.east = Double.NaN;
71 this.north = Double.NaN;
72 }
73
74 /**
75 * Returns the waypoint coordinates.
76 * @return the waypoint coordinates
77 */
78 public final LatLon getCoor() {
79 return new LatLon(lat, lon);
80 }
81
82 /**
83 * <p>Replies the projected east/north coordinates.</p>
84 *
85 * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
86 * Internally caches the projected coordinates.</p>
87 *
88 * <p><strong>Caveat:</strong> doesn't listen to projection changes. Clients must
89 * {@link #invalidateEastNorthCache() invalidate the internal cache}.</p>
90 *
91 * @return the east north coordinates or {@code null}
92 * @see #invalidateEastNorthCache()
93 */
94 public final EastNorth getEastNorth() {
95 if (Double.isNaN(east) || Double.isNaN(north)) {
96 // projected coordinates haven't been calculated yet,
97 // so fill the cache of the projected waypoint coordinates
98 EastNorth en = Projections.project(new LatLon(lat, lon));
99 this.east = en.east();
100 this.north = en.north();
101 }
102 return new EastNorth(east, north);
103 }
104
105 @Override
106 public String toString() {
107 return "WayPoint (" + (attr.containsKey(GPX_NAME) ? get(GPX_NAME) + ", " : "") + getCoor() + ", " + attr + ')';
108 }
109
110 /**
111 * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
112 *
113 * @param time the time to set
114 * @since 9383
115 */
116 public void setTime(Date time) {
117 this.time = time.getTime() / 1000.;
118 this.attr.put(PT_TIME, DateUtils.fromDate(time));
119 }
120
121 /**
122 * Convert the time stamp of the waypoint into seconds from the epoch
123 */
124 public void setTime() {
125 setTimeFromAttribute();
126 }
127
128 /**
129 * Convert the time stamp of the waypoint into seconds from the epoch
130 * @return The parsed time if successful, or {@code null}
131 * @since 9383
132 */
133 public Date setTimeFromAttribute() {
134 if (attr.containsKey(PT_TIME)) {
135 try {
136 final Date time = DateUtils.fromString(get(PT_TIME).toString());
137 this.time = time.getTime() / 1000.;
138 return time;
139 } catch (UncheckedParseException e) {
140 Main.warn(e);
141 time = 0;
142 }
143 }
144 return null;
145 }
146
147 @Override
148 public int compareTo(WayPoint w) {
149 return Double.compare(time, w.time);
150 }
151
152 /**
153 * Returns the waypoint time.
154 * @return the waypoint time
155 */
156 public Date getTime() {
157 return new Date((long) (time * 1000));
158 }
159
160 @Override
161 public Object getTemplateValue(String name, boolean special) {
162 if (!special)
163 return get(name);
164 else
165 return null;
166 }
167
168 @Override
169 public boolean evaluateCondition(Match condition) {
170 throw new UnsupportedOperationException();
171 }
172
173 @Override
174 public List<String> getTemplateKeys() {
175 return new ArrayList<>(attr.keySet());
176 }
177
178 @Override
179 public int hashCode() {
180 final int prime = 31;
181 int result = super.hashCode();
182 long temp = Double.doubleToLongBits(lat);
183 result = prime * result + (int) (temp ^ (temp >>> 32));
184 temp = Double.doubleToLongBits(lon);
185 result = prime * result + (int) (temp ^ (temp >>> 32));
186 temp = Double.doubleToLongBits(time);
187 result = prime * result + (int) (temp ^ (temp >>> 32));
188 return result;
189 }
190
191 @Override
192 public boolean equals(Object obj) {
193 if (this == obj)
194 return true;
195 if (obj == null || !super.equals(obj) || getClass() != obj.getClass())
196 return false;
197 WayPoint other = (WayPoint) obj;
198 return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat)
199 && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon)
200 && Double.doubleToLongBits(time) == Double.doubleToLongBits(other.time);
201 }
202}
Note: See TracBrowser for help on using the repository browser.