001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.gpx; 003 004import java.awt.Color; 005import java.time.Instant; 006import java.util.ArrayList; 007import java.util.HashMap; 008import java.util.List; 009import java.util.Objects; 010 011import org.openstreetmap.josm.data.coor.EastNorth; 012import org.openstreetmap.josm.data.coor.ILatLon; 013import org.openstreetmap.josm.data.coor.LatLon; 014import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match; 015import org.openstreetmap.josm.data.projection.Projecting; 016import org.openstreetmap.josm.tools.Logging; 017import org.openstreetmap.josm.tools.Utils; 018import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider; 019 020/** 021 * A point in the GPX data 022 * @since 12167 implements ILatLon 023 */ 024public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider, ILatLon { 025 026 /** 027 * The color to draw the segment before this point in 028 * @see #drawLine 029 */ 030 public Color customColoring; 031 032 /** 033 * <code>true</code> indicates that the line before this point should be drawn 034 */ 035 public boolean drawLine; 036 037 /** 038 * The direction of the line before this point. Used as cache to speed up drawing. Should not be relied on. 039 */ 040 public int dir; 041 042 /* 043 * We "inline" lat/lon, rather than using a LatLon internally => reduces memory overhead. Relevant 044 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server. 045 */ 046 private final double lat; 047 private final double lon; 048 049 /* 050 * internal cache of projected coordinates 051 */ 052 private double east = Double.NaN; 053 private double north = Double.NaN; 054 private Object eastNorthCacheKey; 055 056 /** 057 * Constructs a new {@code WayPoint} from an existing one. 058 * 059 * Except for PT_TIME attribute, all attribute objects are shallow copied. 060 * This means modification of attr objects will affect original and new {@code WayPoint}. 061 * 062 * @param p existing waypoint 063 */ 064 public WayPoint(WayPoint p) { 065 attr = new HashMap<>(0); 066 attr.putAll(p.attr); 067 lat = p.lat; 068 lon = p.lon; 069 east = p.east; 070 north = p.north; 071 eastNorthCacheKey = p.eastNorthCacheKey; 072 customColoring = p.customColoring; 073 drawLine = p.drawLine; 074 dir = p.dir; 075 } 076 077 /** 078 * Constructs a new {@code WayPoint} from lat/lon coordinates. 079 * @param ll lat/lon coordinates 080 */ 081 public WayPoint(LatLon ll) { 082 attr = new HashMap<>(0); 083 lat = ll.lat(); 084 lon = ll.lon(); 085 } 086 087 /** 088 * Invalidate the internal cache of east/north coordinates. 089 */ 090 public void invalidateEastNorthCache() { 091 this.east = Double.NaN; 092 this.north = Double.NaN; 093 } 094 095 /** 096 * Returns the waypoint coordinates. 097 * @return the waypoint coordinates 098 */ 099 public final LatLon getCoor() { 100 return new LatLon(lat, lon); 101 } 102 103 @Override 104 public double lon() { 105 return lon; 106 } 107 108 @Override 109 public double lat() { 110 return lat; 111 } 112 113 @Override 114 public final EastNorth getEastNorth(Projecting projecting) { 115 Object newCacheKey = projecting.getCacheKey(); 116 if (Double.isNaN(east) || Double.isNaN(north) || !Objects.equals(newCacheKey, this.eastNorthCacheKey)) { 117 // projected coordinates haven't been calculated yet, 118 // so fill the cache of the projected waypoint coordinates 119 EastNorth en = projecting.latlon2eastNorth(this); 120 this.east = en.east(); 121 this.north = en.north(); 122 this.eastNorthCacheKey = newCacheKey; 123 } 124 return new EastNorth(east, north); 125 } 126 127 @Override 128 public String toString() { 129 return "WayPoint (" + (attr.containsKey(GPX_NAME) ? get(GPX_NAME) + ", " : "") + getCoor() + ", " + attr + ')'; 130 } 131 132 /** 133 * Sets the {@link #PT_TIME} attribute to the specified time. 134 * 135 * @param ts milliseconds from the epoch 136 * @since 14434 137 */ 138 public void setTimeInMillis(long ts) { 139 setInstant(Instant.ofEpochMilli(ts)); 140 } 141 142 /** 143 * Sets the {@link #PT_TIME} attribute to the specified time. 144 * 145 * @param instant the time to set 146 */ 147 public void setInstant(Instant instant) { 148 attr.put(PT_TIME, instant); 149 } 150 151 @Override 152 public int compareTo(WayPoint w) { 153 return Long.compare(getTimeInMillis(), w.getTimeInMillis()); 154 } 155 156 /** 157 * Returns the waypoint time in seconds since the epoch. 158 * 159 * @return the waypoint time 160 */ 161 public double getTime() { 162 return getTimeInMillis() / 1000.; 163 } 164 165 /** 166 * Returns the waypoint time in milliseconds since the epoch. 167 * 168 * @return the waypoint time 169 * @since 14456 170 */ 171 public long getTimeInMillis() { 172 Instant d = getInstant(); 173 return d == null ? 0 : d.toEpochMilli(); 174 } 175 176 /** 177 * Returns true if this waypoint has a time. 178 * 179 * @return true if a time is set, false otherwise 180 * @since 14456 181 */ 182 public boolean hasDate() { 183 return attr.get(PT_TIME) instanceof Instant; 184 } 185 186 /** 187 * Returns the waypoint instant. 188 * 189 * @return the instant associated with this waypoint 190 * @since 14456 191 */ 192 public Instant getInstant() { 193 if (attr != null) { 194 final Object obj = attr.get(PT_TIME); 195 196 if (obj instanceof Instant) { 197 return (Instant) obj; 198 } else if (obj == null) { 199 Logging.trace("Waypoint {0} value unset", PT_TIME); 200 } else { 201 Logging.warn("Unsupported waypoint {0} value: {1}", PT_TIME, obj); 202 } 203 } 204 205 return null; 206 } 207 208 @Override 209 public Object getTemplateValue(String name, boolean special) { 210 if (special) { 211 return null; 212 } else if ("desc".equals(name)) { 213 final Object value = get(name); 214 return value instanceof String ? Utils.stripHtml(((String) value)) : value; 215 } else { 216 return get(name); 217 } 218 } 219 220 @Override 221 public boolean evaluateCondition(Match condition) { 222 throw new UnsupportedOperationException(); 223 } 224 225 @Override 226 public List<String> getTemplateKeys() { 227 return new ArrayList<>(attr.keySet()); 228 } 229 230 @Override 231 public int hashCode() { 232 return Objects.hash(super.hashCode(), lat, lon, getTimeInMillis()); 233 } 234 235 @Override 236 public boolean equals(Object obj) { 237 if (this == obj) 238 return true; 239 if (obj == null || !super.equals(obj) || getClass() != obj.getClass()) 240 return false; 241 WayPoint other = (WayPoint) obj; 242 return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat) 243 && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon) 244 && getTimeInMillis() == other.getTimeInMillis(); 245 } 246}