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