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