1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
14 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
15 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
16 | import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
|
---|
17 |
|
---|
18 | public 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 | }
|
---|