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

Last change on this file since 10001 was 9738, checked in by simon04, 8 years ago

GPX: Avoid unnecessary update of PT_TIME

Relates to r9383.

  • Property svn:eol-style set to native
File size: 4.6 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.date.DateUtils;
15import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
16
17public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider {
18
19 /**
20 * The seconds (not milliseconds!) since 1970-01-01.
21 */
22 public double time;
23 public Color customColoring;
24 public boolean drawLine;
25 public int dir;
26
27 public WayPoint(WayPoint p) {
28 attr.putAll(p.attr);
29 lat = p.lat;
30 lon = p.lon;
31 east = p.east;
32 north = p.north;
33 time = p.time;
34 customColoring = p.customColoring;
35 drawLine = p.drawLine;
36 dir = p.dir;
37 }
38
39 public WayPoint(LatLon ll) {
40 lat = ll.lat();
41 lon = ll.lon();
42 }
43
44 /*
45 * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
46 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
47 */
48 private final double lat;
49 private final double lon;
50
51 /*
52 * internal cache of projected coordinates
53 */
54 private double east = Double.NaN;
55 private double north = Double.NaN;
56
57 /**
58 * Invalidate the internal cache of east/north coordinates.
59 */
60 public void invalidateEastNorthCache() {
61 this.east = Double.NaN;
62 this.north = Double.NaN;
63 }
64
65 public final LatLon getCoor() {
66 return new LatLon(lat, lon);
67 }
68
69 /**
70 * <p>Replies the projected east/north coordinates.</p>
71 *
72 * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
73 * Internally caches the projected coordinates.</p>
74 *
75 * <p><strong>Caveat:</strong> doesn't listen to projection changes. Clients must
76 * {@link #invalidateEastNorthCache() invalidate the internal cache}.</p>
77 *
78 * @return the east north coordinates or {@code null}
79 * @see #invalidateEastNorthCache()
80 */
81 public final EastNorth getEastNorth() {
82 if (Double.isNaN(east) || Double.isNaN(north)) {
83 // projected coordinates haven't been calculated yet,
84 // so fill the cache of the projected waypoint coordinates
85 EastNorth en = Projections.project(new LatLon(lat, lon));
86 this.east = en.east();
87 this.north = en.north();
88 }
89 return new EastNorth(east, north);
90 }
91
92 @Override
93 public String toString() {
94 return "WayPoint (" + (attr.containsKey(GPX_NAME) ? get(GPX_NAME) + ", " : "") + getCoor() + ", " + attr + ')';
95 }
96
97 /**
98 * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
99 *
100 * @param time the time to set
101 * @since 9383
102 */
103 public void setTime(Date time) {
104 this.time = time.getTime() / 1000.;
105 this.attr.put(PT_TIME, DateUtils.fromDate(time));
106 }
107
108 /**
109 * Convert the time stamp of the waypoint into seconds from the epoch
110 */
111 public void setTime() {
112 setTimeFromAttribute();
113 }
114
115 /**
116 * Convert the time stamp of the waypoint into seconds from the epoch
117 * @return The parsed time if successful, or {@code null}
118 * @since 9383
119 */
120 public Date setTimeFromAttribute() {
121 if (attr.containsKey(PT_TIME)) {
122 try {
123 final Date time = DateUtils.fromString(get(PT_TIME).toString());
124 this.time = time.getTime() / 1000.;
125 return time;
126 } catch (Exception e) {
127 Main.warn(e);
128 time = 0;
129 }
130 }
131 return null;
132 }
133
134 @Override
135 public int compareTo(WayPoint w) {
136 return Double.compare(time, w.time);
137 }
138
139 public Date getTime() {
140 return new Date((long) (time * 1000));
141 }
142
143 @Override
144 public Object getTemplateValue(String name, boolean special) {
145 if (!special)
146 return get(name);
147 else
148 return null;
149 }
150
151 @Override
152 public boolean evaluateCondition(Match condition) {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public List<String> getTemplateKeys() {
158 return new ArrayList<>(attr.keySet());
159 }
160}
Note: See TracBrowser for help on using the repository browser.