1 | //License: GPLv2 or later
|
---|
2 | //Copyright 2007 by Raphael Mack and others
|
---|
3 |
|
---|
4 | package org.openstreetmap.josm.data.gpx;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.util.Date;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
13 | import org.openstreetmap.josm.tools.PrimaryDateParser;
|
---|
14 |
|
---|
15 | public class WayPoint extends WithAttributes implements Comparable<WayPoint> {
|
---|
16 |
|
---|
17 | private static ThreadLocal<PrimaryDateParser> dateParser = new ThreadLocal<PrimaryDateParser>() {
|
---|
18 | @Override protected PrimaryDateParser initialValue() {
|
---|
19 | return new PrimaryDateParser();
|
---|
20 | }
|
---|
21 | };
|
---|
22 |
|
---|
23 | public double time;
|
---|
24 | public Color customColoring;
|
---|
25 | public boolean drawLine;
|
---|
26 | public int dir;
|
---|
27 |
|
---|
28 | public WayPoint(LatLon ll) {
|
---|
29 | lat = ll.lat();
|
---|
30 | lon = ll.lon();
|
---|
31 | }
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
|
---|
35 | * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
|
---|
36 | */
|
---|
37 | private double lat = 0;
|
---|
38 | private double lon = 0;
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * internal cache of projected coordinates
|
---|
42 | */
|
---|
43 | private double east = Double.NaN;
|
---|
44 | private double north = Double.NaN;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Invalidate the internal cache of east/north coordinates.
|
---|
48 | */
|
---|
49 | public void invalidateEastNorthCache() {
|
---|
50 | this.east = Double.NaN;
|
---|
51 | this.north = Double.NaN;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public final LatLon getCoor() {
|
---|
55 | return new LatLon(lat,lon);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * <p>Replies the projected east/north coordinates.</p>
|
---|
60 | *
|
---|
61 | * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
|
---|
62 | * Internally caches the projected coordinates.</p>
|
---|
63 | *
|
---|
64 | * <p><strong>Caveat:</strong> doesn't listen to projection changes. Clients must
|
---|
65 | * {@link #reproject() trigger a reprojection} or {@link #invalidateEastNorthCache() invalidate the internal cache}.</p>
|
---|
66 | *
|
---|
67 | * @return the east north coordinates or {@code null}
|
---|
68 | * @see #invalidateEastNorthCache()
|
---|
69 | *
|
---|
70 | */
|
---|
71 | public final EastNorth getEastNorth() {
|
---|
72 | if (Double.isNaN(east) || Double.isNaN(north)) {
|
---|
73 | // projected coordinates haven't been calculated yet,
|
---|
74 | // so fill the cache of the projected waypoint coordinates
|
---|
75 | EastNorth en = Projections.project(new LatLon(lat, lon));
|
---|
76 | this.east = en.east();
|
---|
77 | this.north = en.north();
|
---|
78 | }
|
---|
79 | return new EastNorth(east, north);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String toString() {
|
---|
84 | return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + getCoor().toString() + ", " + attr + ")";
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Convert the time stamp of the waypoint into seconds from the epoch
|
---|
89 | */
|
---|
90 | public void setTime() {
|
---|
91 | if(attr.containsKey("time")) {
|
---|
92 | try {
|
---|
93 | time = dateParser.get().parse(attr.get("time").toString()).getTime() / 1000.; /* ms => seconds */
|
---|
94 | } catch(Exception e) {
|
---|
95 | time = 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public int compareTo(WayPoint w) {
|
---|
101 | return Double.compare(time, w.time);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public Date getTime() {
|
---|
105 | return new Date((long) (time * 1000));
|
---|
106 | }
|
---|
107 | }
|
---|