001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.gpx; 003 004import java.awt.Color; 005import java.util.Collection; 006import java.util.Collections; 007import java.util.HashMap; 008import java.util.List; 009import java.util.Map; 010import java.util.Map.Entry; 011import java.util.Optional; 012 013import org.openstreetmap.josm.data.Bounds; 014import org.openstreetmap.josm.tools.ListenerList; 015import org.openstreetmap.josm.tools.Logging; 016import org.openstreetmap.josm.tools.StreamUtils; 017import org.openstreetmap.josm.tools.Utils; 018 019/** 020 * GPX track. 021 * Note that the color attributes are not immutable and may be modified by the user. 022 * @since 15496 023 */ 024public class GpxTrack extends WithAttributes implements IGpxTrack { 025 026 private final List<IGpxTrackSegment> segments; 027 private final double length; 028 private final Bounds bounds; 029 private Color colorCache; 030 private final ListenerList<IGpxTrack.GpxTrackChangeListener> listeners = ListenerList.create(); 031 private static final HashMap<Color, String> closestGarminColorCache = new HashMap<>(); 032 private ColorFormat colorFormat; 033 034 /** 035 * Constructs a new {@code GpxTrack}. 036 * @param trackSegs track segments 037 * @param attributes track attributes 038 */ 039 public GpxTrack(Collection<Collection<WayPoint>> trackSegs, Map<String, Object> attributes) { 040 this.segments = trackSegs.stream() 041 .filter(trackSeg -> !Utils.isEmpty(trackSeg)) 042 .map(GpxTrackSegment::new) 043 .collect(StreamUtils.toUnmodifiableList()); 044 this.length = calculateLength(); 045 this.bounds = calculateBounds(); 046 this.attr = new HashMap<>(attributes); 047 } 048 049 /** 050 * Constructs a new {@code GpxTrack} from {@code GpxTrackSegment} objects. 051 * @param trackSegs The segments to build the track from. Input is not deep-copied, 052 * which means the caller may reuse the same segments to build 053 * multiple GpxTrack instances from. This should not be 054 * a problem, since this object cannot modify {@code this.segments}. 055 * @param attributes Attributes for the GpxTrack, the input map is copied. 056 */ 057 public GpxTrack(List<IGpxTrackSegment> trackSegs, Map<String, Object> attributes) { 058 this.attr = new HashMap<>(attributes); 059 this.segments = Collections.unmodifiableList(trackSegs); 060 this.length = calculateLength(); 061 this.bounds = calculateBounds(); 062 } 063 064 private double calculateLength() { 065 return segments.stream().mapToDouble(IGpxTrackSegment::length).sum(); 066 } 067 068 private Bounds calculateBounds() { 069 Bounds result = null; 070 for (IGpxTrackSegment segment: segments) { 071 Bounds segBounds = segment.getBounds(); 072 if (segBounds != null) { 073 if (result == null) { 074 result = new Bounds(segBounds); 075 } else { 076 result.extend(segBounds); 077 } 078 } 079 } 080 return result; 081 } 082 083 @Override 084 public void setColor(Color color) { 085 setColorExtensionGPXD(color, true); 086 colorCache = color; 087 } 088 089 private void setColorExtensionGPXD(Color color, boolean invalidate) { 090 getExtensions().findAndRemove("gpxx", "DisplayColor"); 091 if (color == null) { 092 getExtensions().findAndRemove("gpxd", "color"); 093 } else { 094 getExtensions().addOrUpdate("gpxd", "color", String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue())); 095 } 096 colorFormat = ColorFormat.GPXD; 097 if (invalidate) { 098 fireInvalidate(); 099 } 100 } 101 102 @Override 103 public Color getColor() { 104 if (colorCache == null) { 105 colorCache = getColorFromExtension(); 106 } 107 return colorCache; 108 } 109 110 private Color getColorFromExtension() { 111 if (!hasExtensions()) { 112 return null; 113 } 114 GpxExtension gpxd = getExtensions().find("gpxd", "color"); 115 if (gpxd != null) { 116 colorFormat = ColorFormat.GPXD; 117 String cs = gpxd.getValue(); 118 try { 119 return Color.decode(cs); 120 } catch (NumberFormatException ex) { 121 Logging.warn("Could not read gpxd color: " + cs); 122 } 123 } else { 124 GpxExtension gpxx = getExtensions().find("gpxx", "DisplayColor"); 125 if (gpxx != null) { 126 colorFormat = ColorFormat.GPXX; 127 String cs = gpxx.getValue(); 128 if (cs != null) { 129 Color cc = GARMIN_COLORS.get(cs); 130 if (cc != null) { 131 return cc; 132 } 133 } 134 Logging.warn("Could not read garmin color: " + cs); 135 } 136 } 137 return null; 138 } 139 140 /** 141 * Converts the color to the given format, if present. 142 * @param cFormat can be a {@link GpxConstants.ColorFormat} 143 */ 144 public void convertColor(ColorFormat cFormat) { 145 Color c = getColor(); 146 if (c == null) return; 147 148 if (cFormat != this.colorFormat) { 149 if (cFormat == null) { 150 // just hide the extensions, don't actually remove them 151 Optional.ofNullable(getExtensions().find("gpxx", "DisplayColor")).ifPresent(GpxExtension::hide); 152 Optional.ofNullable(getExtensions().find("gpxd", "color")).ifPresent(GpxExtension::hide); 153 } else if (cFormat == ColorFormat.GPXX) { 154 getExtensions().findAndRemove("gpxd", "color"); 155 String colorString = null; 156 if (closestGarminColorCache.containsKey(c)) { 157 colorString = closestGarminColorCache.get(c); 158 } else { 159 //find closest garmin color 160 double closestDiff = -1; 161 for (Entry<String, Color> e : GARMIN_COLORS.entrySet()) { 162 double diff = colorDist(e.getValue(), c); 163 if (closestDiff < 0 || diff < closestDiff) { 164 colorString = e.getKey(); 165 closestDiff = diff; 166 if (closestDiff == 0) break; 167 } 168 } 169 } 170 closestGarminColorCache.put(c, colorString); 171 getExtensions().addIfNotPresent("gpxx", "TrackExtension").getExtensions().addOrUpdate("gpxx", "DisplayColor", colorString); 172 } else if (cFormat == ColorFormat.GPXD) { 173 setColorExtensionGPXD(c, false); 174 } 175 colorFormat = cFormat; 176 } 177 } 178 179 private double colorDist(Color c1, Color c2) { 180 // Simple Euclidean distance between two colors 181 return Math.sqrt(Math.pow(c1.getRed() - c2.getRed(), 2) 182 + Math.pow(c1.getGreen() - c2.getGreen(), 2) 183 + Math.pow(c1.getBlue() - c2.getBlue(), 2)); 184 } 185 186 @Override 187 public void put(String key, Object value) { 188 super.put(key, value); 189 fireInvalidate(); 190 } 191 192 private void fireInvalidate() { 193 listeners.fireEvent(l -> l.gpxDataChanged(new IGpxTrack.GpxTrackChangeEvent(this))); 194 } 195 196 @Override 197 public Bounds getBounds() { 198 return bounds == null ? null : new Bounds(bounds); 199 } 200 201 @Override 202 public double length() { 203 return length; 204 } 205 206 @Override 207 public Collection<IGpxTrackSegment> getSegments() { 208 return segments; 209 } 210 211 @Override 212 public int hashCode() { 213 return 31 * super.hashCode() + ((segments == null) ? 0 : segments.hashCode()) 214 + ((attr == null) ? 0 : attr.hashCode()); 215 } 216 217 @Override 218 public boolean equals(Object obj) { 219 if (this == obj) 220 return true; 221 if (obj == null) 222 return false; 223 if (!super.equals(obj)) 224 return false; 225 if (getClass() != obj.getClass()) 226 return false; 227 GpxTrack other = (GpxTrack) obj; 228 if (segments == null) { 229 if (other.segments != null) 230 return false; 231 } else if (!segments.equals(other.segments)) 232 return false; 233 if (attr == null) { 234 if (other.attr != null) 235 return false; 236 } else if (!attr.equals(other.attr)) 237 return false; 238 return true; 239 } 240 241 @Override 242 public void addListener(IGpxTrack.GpxTrackChangeListener l) { 243 listeners.addListener(l); 244 } 245 246 @Override 247 public void removeListener(IGpxTrack.GpxTrackChangeListener l) { 248 listeners.removeListener(l); 249 } 250 251 /** 252 * Resets the color cache 253 */ 254 public void invalidate() { 255 colorCache = null; 256 } 257}