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