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

Last change on this file since 12169 was 12169, checked in by michael2402, 7 years ago

Fix lat/lon swapped for GPX conversion, add asymetric test case

  • 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;
8import java.util.Objects;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.coor.ILatLon;
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.data.projection.Projecting;
16import org.openstreetmap.josm.tools.UncheckedParseException;
17import org.openstreetmap.josm.tools.date.DateUtils;
18import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
19
20/**
21 * A point in the GPX data
22 * @since 12167 implements ILatLon
23 */
24public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider, ILatLon {
25
26 /**
27 * The seconds (not milliseconds!) since 1970-01-01 00:00 UTC
28 */
29 public double time;
30 public Color customColoring;
31 public boolean drawLine;
32 public int dir;
33
34 /**
35 * Constructs a new {@code WayPoint} from an existing one.
36 * @param p existing waypoint
37 */
38 public WayPoint(WayPoint p) {
39 attr.putAll(p.attr);
40 lat = p.lat;
41 lon = p.lon;
42 east = p.east;
43 north = p.north;
44 eastNorthCacheKey = p.eastNorthCacheKey;
45 time = p.time;
46 customColoring = p.customColoring;
47 drawLine = p.drawLine;
48 dir = p.dir;
49 }
50
51 /**
52 * Constructs a new {@code WayPoint} from lat/lon coordinates.
53 * @param ll lat/lon coordinates
54 */
55 public WayPoint(LatLon ll) {
56 lat = ll.lat();
57 lon = ll.lon();
58 }
59
60 /*
61 * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
62 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
63 */
64 private final double lat;
65 private final double lon;
66
67 /*
68 * internal cache of projected coordinates
69 */
70 private double east = Double.NaN;
71 private double north = Double.NaN;
72 private Object eastNorthCacheKey = null;
73
74 /**
75 * Invalidate the internal cache of east/north coordinates.
76 */
77 public void invalidateEastNorthCache() {
78 this.east = Double.NaN;
79 this.north = Double.NaN;
80 }
81
82 /**
83 * Returns the waypoint coordinates.
84 * @return the waypoint coordinates
85 */
86 public final LatLon getCoor() {
87 return new LatLon(lat, lon);
88 }
89
90 @Override
91 public double lon() {
92 return lon;
93 }
94
95 @Override
96 public double lat() {
97 return lat;
98 }
99
100 @Override
101 public final EastNorth getEastNorth(Projecting projecting) {
102 Object newCacheKey = projecting.getCacheKey();
103 if (Double.isNaN(east) || Double.isNaN(north) || !Objects.equals(newCacheKey, this.eastNorthCacheKey)) {
104 // projected coordinates haven't been calculated yet,
105 // so fill the cache of the projected waypoint coordinates
106 EastNorth en = projecting.latlon2eastNorth(this);
107 this.east = en.east();
108 this.north = en.north();
109 this.eastNorthCacheKey = newCacheKey;
110 }
111 return new EastNorth(east, north);
112 }
113
114 @Override
115 public String toString() {
116 return "WayPoint (" + (attr.containsKey(GPX_NAME) ? get(GPX_NAME) + ", " : "") + getCoor() + ", " + attr + ')';
117 }
118
119 /**
120 * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
121 *
122 * @param time the time to set
123 * @since 9383
124 */
125 public void setTime(Date time) {
126 this.time = time.getTime() / 1000.;
127 this.attr.put(PT_TIME, DateUtils.fromDate(time));
128 }
129
130 /**
131 * Convert the time stamp of the waypoint into seconds from the epoch
132 */
133 public void setTime() {
134 setTimeFromAttribute();
135 }
136
137 /**
138 * Convert the time stamp of the waypoint into seconds from the epoch
139 * @return The parsed time if successful, or {@code null}
140 * @since 9383
141 */
142 public Date setTimeFromAttribute() {
143 if (attr.containsKey(PT_TIME)) {
144 try {
145 final Date time = DateUtils.fromString(get(PT_TIME).toString());
146 this.time = time.getTime() / 1000.;
147 return time;
148 } catch (UncheckedParseException e) {
149 Main.warn(e);
150 time = 0;
151 }
152 }
153 return null;
154 }
155
156 @Override
157 public int compareTo(WayPoint w) {
158 return Double.compare(time, w.time);
159 }
160
161 /**
162 * Returns the waypoint time.
163 * @return the waypoint time
164 */
165 public Date getTime() {
166 return new Date((long) (time * 1000));
167 }
168
169 @Override
170 public Object getTemplateValue(String name, boolean special) {
171 if (!special)
172 return get(name);
173 else
174 return null;
175 }
176
177 @Override
178 public boolean evaluateCondition(Match condition) {
179 throw new UnsupportedOperationException();
180 }
181
182 @Override
183 public List<String> getTemplateKeys() {
184 return new ArrayList<>(attr.keySet());
185 }
186
187 @Override
188 public int hashCode() {
189 final int prime = 31;
190 int result = super.hashCode();
191 long temp = Double.doubleToLongBits(lat);
192 result = prime * result + (int) (temp ^ (temp >>> 32));
193 temp = Double.doubleToLongBits(lon);
194 result = prime * result + (int) (temp ^ (temp >>> 32));
195 temp = Double.doubleToLongBits(time);
196 result = prime * result + (int) (temp ^ (temp >>> 32));
197 return result;
198 }
199
200 @Override
201 public boolean equals(Object obj) {
202 if (this == obj)
203 return true;
204 if (obj == null || !super.equals(obj) || getClass() != obj.getClass())
205 return false;
206 WayPoint other = (WayPoint) obj;
207 return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat)
208 && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon)
209 && Double.doubleToLongBits(time) == Double.doubleToLongBits(other.time);
210 }
211}
Note: See TracBrowser for help on using the repository browser.