| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.AlphaComposite;
|
|---|
| 8 | import java.awt.BasicStroke;
|
|---|
| 9 | import java.awt.Color;
|
|---|
| 10 | import java.awt.Composite;
|
|---|
| 11 | import java.awt.Graphics2D;
|
|---|
| 12 | import java.awt.LinearGradientPaint;
|
|---|
| 13 | import java.awt.MultipleGradientPaint;
|
|---|
| 14 | import java.awt.Paint;
|
|---|
| 15 | import java.awt.Point;
|
|---|
| 16 | import java.awt.Rectangle;
|
|---|
| 17 | import java.awt.RenderingHints;
|
|---|
| 18 | import java.awt.Stroke;
|
|---|
| 19 | import java.awt.image.BufferedImage;
|
|---|
| 20 | import java.awt.image.DataBufferInt;
|
|---|
| 21 | import java.awt.image.Raster;
|
|---|
| 22 | import java.io.BufferedReader;
|
|---|
| 23 | import java.io.IOException;
|
|---|
| 24 | import java.util.ArrayList;
|
|---|
| 25 | import java.util.Arrays;
|
|---|
| 26 | import java.util.Collections;
|
|---|
| 27 | import java.util.Date;
|
|---|
| 28 | import java.util.LinkedList;
|
|---|
| 29 | import java.util.List;
|
|---|
| 30 | import java.util.Objects;
|
|---|
| 31 | import java.util.Random;
|
|---|
| 32 |
|
|---|
| 33 | import javax.swing.ImageIcon;
|
|---|
| 34 |
|
|---|
| 35 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 36 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
|---|
| 37 | import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
|
|---|
| 38 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 39 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
|---|
| 40 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 41 | import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeEvent;
|
|---|
| 42 | import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeListener;
|
|---|
| 43 | import org.openstreetmap.josm.data.gpx.Line;
|
|---|
| 44 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 45 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
|---|
| 46 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 47 | import org.openstreetmap.josm.gui.MapViewState;
|
|---|
| 48 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 49 | import org.openstreetmap.josm.gui.layer.MapViewGraphics;
|
|---|
| 50 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
|---|
| 51 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent;
|
|---|
| 52 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
|
|---|
| 53 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
|
|---|
| 54 | import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
|
|---|
| 55 | import org.openstreetmap.josm.io.CachedFile;
|
|---|
| 56 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 57 | import org.openstreetmap.josm.tools.ColorScale;
|
|---|
| 58 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
|---|
| 59 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 60 | import org.openstreetmap.josm.tools.Stopwatch;
|
|---|
| 61 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Class that helps to draw large set of GPS tracks with different colors and options
|
|---|
| 65 | * @since 7319
|
|---|
| 66 | */
|
|---|
| 67 | public class GpxDrawHelper implements SoMChangeListener, MapViewPaintable.LayerPainter, PaintableInvalidationListener, GpxDataChangeListener {
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * The default color property that is used for drawing GPX points.
|
|---|
| 71 | * @since 15496
|
|---|
| 72 | */
|
|---|
| 73 | public static final NamedColorProperty DEFAULT_COLOR_PROPERTY = new NamedColorProperty(marktr("gps point"), Color.magenta);
|
|---|
| 74 |
|
|---|
| 75 | private final GpxData data;
|
|---|
| 76 | private final GpxLayer layer;
|
|---|
| 77 |
|
|---|
| 78 | // draw lines between points belonging to different segments
|
|---|
| 79 | private boolean forceLines;
|
|---|
| 80 | // use alpha blending for line draw
|
|---|
| 81 | private boolean alphaLines;
|
|---|
| 82 | // draw direction arrows on the lines
|
|---|
| 83 | private boolean arrows;
|
|---|
| 84 | /** width of line for paint **/
|
|---|
| 85 | private int lineWidth;
|
|---|
| 86 | /** don't draw lines if longer than x meters **/
|
|---|
| 87 | private int maxLineLength;
|
|---|
| 88 | // draw lines
|
|---|
| 89 | private boolean lines;
|
|---|
| 90 | /** paint large dots for points **/
|
|---|
| 91 | private boolean large;
|
|---|
| 92 | private int largesize;
|
|---|
| 93 | private boolean hdopCircle;
|
|---|
| 94 | /** paint direction arrow with alternate math. may be faster **/
|
|---|
| 95 | private boolean arrowsFast;
|
|---|
| 96 | /** don't draw arrows nearer to each other than this **/
|
|---|
| 97 | private int arrowsDelta;
|
|---|
| 98 | private double minTrackDurationForTimeColoring;
|
|---|
| 99 |
|
|---|
| 100 | /** maximum value of displayed HDOP, minimum is 0 */
|
|---|
| 101 | private int hdoprange;
|
|---|
| 102 |
|
|---|
| 103 | private static final double PHI = Utils.toRadians(15);
|
|---|
| 104 |
|
|---|
| 105 | //// Variables used only to check cache validity
|
|---|
| 106 | private boolean computeCacheInSync;
|
|---|
| 107 | private int computeCacheMaxLineLengthUsed;
|
|---|
| 108 | private Color computeCacheColorUsed;
|
|---|
| 109 | private boolean computeCacheColorDynamic;
|
|---|
| 110 | private ColorMode computeCacheColored;
|
|---|
| 111 | private int computeCacheVelocityTune;
|
|---|
| 112 | private int computeCacheHeatMapDrawColorTableIdx;
|
|---|
| 113 | private boolean computeCacheHeatMapDrawPointMode;
|
|---|
| 114 | private int computeCacheHeatMapDrawGain;
|
|---|
| 115 | private int computeCacheHeatMapDrawLowerLimit;
|
|---|
| 116 |
|
|---|
| 117 | private Color colorCache;
|
|---|
| 118 | private Color colorCacheTransparent;
|
|---|
| 119 |
|
|---|
| 120 | //// Color-related fields
|
|---|
| 121 | /** Mode of the line coloring **/
|
|---|
| 122 | private ColorMode colored;
|
|---|
| 123 | /** max speed for coloring - allows to tweak line coloring for different speed levels. **/
|
|---|
| 124 | private int velocityTune;
|
|---|
| 125 | private boolean colorModeDynamic;
|
|---|
| 126 | private Color neutralColor;
|
|---|
| 127 | private int largePointAlpha;
|
|---|
| 128 |
|
|---|
| 129 | // default access is used to allow changing from plugins
|
|---|
| 130 | private ColorScale velocityScale;
|
|---|
| 131 | /** Colors (without custom alpha channel, if given) for HDOP painting. **/
|
|---|
| 132 | private ColorScale hdopScale;
|
|---|
| 133 | private ColorScale qualityScale;
|
|---|
| 134 | private ColorScale dateScale;
|
|---|
| 135 | private ColorScale directionScale;
|
|---|
| 136 |
|
|---|
| 137 | /** Opacity for hdop points **/
|
|---|
| 138 | private int hdopAlpha;
|
|---|
| 139 |
|
|---|
| 140 | // lookup array to draw arrows without doing any math
|
|---|
| 141 | private static final int ll0 = 9;
|
|---|
| 142 | private static final int sl4 = 5;
|
|---|
| 143 | private static final int sl9 = 3;
|
|---|
| 144 | private static final int[][] dir = {
|
|---|
| 145 | {+sl4, +ll0, +ll0, +sl4}, {-sl9, +ll0, +sl9, +ll0},
|
|---|
| 146 | {-ll0, +sl4, -sl4, +ll0}, {-ll0, -sl9, -ll0, +sl9},
|
|---|
| 147 | {-sl4, -ll0, -ll0, -sl4}, {+sl9, -ll0, -sl9, -ll0},
|
|---|
| 148 | {+ll0, -sl4, +sl4, -ll0}, {+ll0, +sl9, +ll0, -sl9}
|
|---|
| 149 | };
|
|---|
| 150 |
|
|---|
| 151 | /** heat map parameters **/
|
|---|
| 152 |
|
|---|
| 153 | // draw small extra line
|
|---|
| 154 | private boolean heatMapDrawExtraLine;
|
|---|
| 155 | // used index for color table (parameter)
|
|---|
| 156 | private int heatMapDrawColorTableIdx;
|
|---|
| 157 | // use point or line draw mode
|
|---|
| 158 | private boolean heatMapDrawPointMode;
|
|---|
| 159 | // extra gain > 0 or < 0 attenuation, 0 = default
|
|---|
| 160 | private int heatMapDrawGain;
|
|---|
| 161 | // do not draw elements with value lower than this limit
|
|---|
| 162 | private int heatMapDrawLowerLimit;
|
|---|
| 163 |
|
|---|
| 164 | // normal buffered image and draw object (cached)
|
|---|
| 165 | private BufferedImage heatMapImgGray;
|
|---|
| 166 | private Graphics2D heatMapGraph2d;
|
|---|
| 167 |
|
|---|
| 168 | // some cached values
|
|---|
| 169 | Rectangle heatMapCacheScreenBounds = new Rectangle();
|
|---|
| 170 | MapViewState heatMapMapViewState;
|
|---|
| 171 | int heatMapCacheLineWith;
|
|---|
| 172 |
|
|---|
| 173 | // copied value for line drawing
|
|---|
| 174 | private final List<Integer> heatMapPolyX = new ArrayList<>();
|
|---|
| 175 | private final List<Integer> heatMapPolyY = new ArrayList<>();
|
|---|
| 176 |
|
|---|
| 177 | // setup color maps used by heat map
|
|---|
| 178 | private static Color[] heatMapLutColorJosmInferno = createColorFromResource("inferno");
|
|---|
| 179 | private static Color[] heatMapLutColorJosmViridis = createColorFromResource("viridis");
|
|---|
| 180 | private static Color[] heatMapLutColorJosmBrown2Green = createColorFromResource("brown2green");
|
|---|
| 181 | private static Color[] heatMapLutColorJosmRed2Blue = createColorFromResource("red2blue");
|
|---|
| 182 |
|
|---|
| 183 | private static Color[] rtkLibQualityColors = {
|
|---|
| 184 | Color.GREEN, // Fixed, solution by carrier‐based relative positioning and the integer ambiguity is properly resolved.
|
|---|
| 185 | Color.ORANGE, // Float, solution by carrier‐based relative positioning but the integer ambiguity is not resolved.
|
|---|
| 186 | Color.PINK, // Reserved
|
|---|
| 187 | Color.BLUE, // DGPS, solution by code‐based DGPS solutions or single point positioning with SBAS corrections
|
|---|
| 188 | Color.RED, // Single, solution by single point positioning
|
|---|
| 189 | Color.CYAN // PPP
|
|---|
| 190 | };
|
|---|
| 191 |
|
|---|
| 192 | // user defined heatmap color
|
|---|
| 193 | private Color[] heatMapLutColor = createColorLut(0, Color.BLACK, Color.WHITE);
|
|---|
| 194 |
|
|---|
| 195 | // The heat map was invalidated since the last draw.
|
|---|
| 196 | private boolean gpxLayerInvalidated;
|
|---|
| 197 |
|
|---|
| 198 | private void setupColors() {
|
|---|
| 199 | hdopAlpha = Config.getPref().getInt("hdop.color.alpha", -1);
|
|---|
| 200 | velocityScale = ColorScale.createHSBScale(256);
|
|---|
| 201 | /** Colors (without custom alpha channel, if given) for HDOP painting. **/
|
|---|
| 202 | hdopScale = ColorScale.createHSBScale(256).makeReversed().addTitle(tr("HDOP"));
|
|---|
| 203 | qualityScale = ColorScale.createFixedScale(rtkLibQualityColors).addTitle(tr("Quality"));
|
|---|
| 204 | dateScale = ColorScale.createHSBScale(256).addTitle(tr("Time"));
|
|---|
| 205 | directionScale = ColorScale.createCyclicScale(256).setIntervalCount(4).addTitle(tr("Direction"));
|
|---|
| 206 |
|
|---|
| 207 | systemOfMeasurementChanged(null, null);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | @Override
|
|---|
| 211 | public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
|
|---|
| 212 | SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
|
|---|
| 213 | velocityScale.addTitle(tr("Velocity, {0}", som.speedName));
|
|---|
| 214 | layer.invalidate();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Different color modes
|
|---|
| 219 | */
|
|---|
| 220 | public enum ColorMode {
|
|---|
| 221 | /**
|
|---|
| 222 | * No special colors
|
|---|
| 223 | */
|
|---|
| 224 | NONE,
|
|---|
| 225 | /**
|
|---|
| 226 | * Color by velocity
|
|---|
| 227 | */
|
|---|
| 228 | VELOCITY,
|
|---|
| 229 | /**
|
|---|
| 230 | * Color by accuracy
|
|---|
| 231 | */
|
|---|
| 232 | HDOP,
|
|---|
| 233 | /**
|
|---|
| 234 | * Color by traveling direction
|
|---|
| 235 | */
|
|---|
| 236 | DIRECTION,
|
|---|
| 237 | /**
|
|---|
| 238 | * Color by time
|
|---|
| 239 | */
|
|---|
| 240 | TIME,
|
|---|
| 241 | /**
|
|---|
| 242 | * Color using a heatmap instead of normal lines
|
|---|
| 243 | */
|
|---|
| 244 | HEATMAP,
|
|---|
| 245 | /**
|
|---|
| 246 | * Color by quality (RTKLib)
|
|---|
| 247 | */
|
|---|
| 248 | QUALITY;
|
|---|
| 249 |
|
|---|
| 250 | static ColorMode fromIndex(final int index) {
|
|---|
| 251 | return values()[index];
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | int toIndex() {
|
|---|
| 255 | return Arrays.asList(values()).indexOf(this);
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Constructs a new {@code GpxDrawHelper}.
|
|---|
| 261 | * @param gpxLayer The layer to draw
|
|---|
| 262 | * @since 12157
|
|---|
| 263 | */
|
|---|
| 264 | public GpxDrawHelper(GpxLayer gpxLayer) {
|
|---|
| 265 | layer = gpxLayer;
|
|---|
| 266 | data = gpxLayer.data;
|
|---|
| 267 | data.addChangeListener(this);
|
|---|
| 268 |
|
|---|
| 269 | layer.addInvalidationListener(this);
|
|---|
| 270 | SystemOfMeasurement.addSoMChangeListener(this);
|
|---|
| 271 | setupColors();
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * Read coloring mode for specified layer from preferences
|
|---|
| 276 | * @return coloring mode
|
|---|
| 277 | */
|
|---|
| 278 | public ColorMode getColorMode() {
|
|---|
| 279 | try {
|
|---|
| 280 | int i = optInt("colormode");
|
|---|
| 281 | if (i == -1) i = 0; //global
|
|---|
| 282 | return ColorMode.fromIndex(i);
|
|---|
| 283 | } catch (IndexOutOfBoundsException e) {
|
|---|
| 284 | Logging.warn(e);
|
|---|
| 285 | }
|
|---|
| 286 | return ColorMode.NONE;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | private String opt(String key) {
|
|---|
| 290 | return GPXSettingsPanel.getLayerPref(layer, key);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | private boolean optBool(String key) {
|
|---|
| 294 | return Boolean.parseBoolean(opt(key));
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | private int optInt(String key) {
|
|---|
| 298 | return GPXSettingsPanel.getLayerPrefInt(layer, key);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * Read all drawing-related settings from preferences
|
|---|
| 303 | **/
|
|---|
| 304 | public void readPreferences() {
|
|---|
| 305 | forceLines = optBool("lines.force");
|
|---|
| 306 | arrows = optBool("lines.arrows");
|
|---|
| 307 | arrowsFast = optBool("lines.arrows.fast");
|
|---|
| 308 | arrowsDelta = optInt("lines.arrows.min-distance");
|
|---|
| 309 | lineWidth = optInt("lines.width");
|
|---|
| 310 | alphaLines = optBool("lines.alpha-blend");
|
|---|
| 311 |
|
|---|
| 312 | int l = optInt("lines");
|
|---|
| 313 | // -1 = global (default: all)
|
|---|
| 314 | // 0 = none
|
|---|
| 315 | // 1 = local
|
|---|
| 316 | // 2 = all
|
|---|
| 317 | if (!data.fromServer) { //local settings apply
|
|---|
| 318 | maxLineLength = optInt("lines.max-length.local");
|
|---|
| 319 | lines = l != 0; // don't draw if "none"
|
|---|
| 320 | } else {
|
|---|
| 321 | maxLineLength = optInt("lines.max-length");
|
|---|
| 322 | lines = l != 0 && l != 1; //don't draw if "none" or "local only"
|
|---|
| 323 | }
|
|---|
| 324 | large = optBool("points.large");
|
|---|
| 325 | largesize = optInt("points.large.size");
|
|---|
| 326 | hdopCircle = optBool("points.hdopcircle");
|
|---|
| 327 | colored = getColorMode();
|
|---|
| 328 | velocityTune = optInt("colormode.velocity.tune");
|
|---|
| 329 | colorModeDynamic = optBool("colormode.dynamic-range");
|
|---|
| 330 | /* good HDOP's are between 1 and 3, very bad HDOP's go into 3 digit values */
|
|---|
| 331 | hdoprange = Config.getPref().getInt("hdop.range", 7);
|
|---|
| 332 | minTrackDurationForTimeColoring = optInt("colormode.time.min-distance");
|
|---|
| 333 | largePointAlpha = optInt("points.large.alpha") & 0xFF;
|
|---|
| 334 |
|
|---|
| 335 | // get heatmap parameters
|
|---|
| 336 | heatMapDrawExtraLine = optBool("colormode.heatmap.line-extra");
|
|---|
| 337 | heatMapDrawColorTableIdx = optInt("colormode.heatmap.colormap");
|
|---|
| 338 | heatMapDrawPointMode = optBool("colormode.heatmap.use-points");
|
|---|
| 339 | heatMapDrawGain = optInt("colormode.heatmap.gain");
|
|---|
| 340 | heatMapDrawLowerLimit = optInt("colormode.heatmap.lower-limit");
|
|---|
| 341 |
|
|---|
| 342 | // shrink to range
|
|---|
| 343 | heatMapDrawGain = Utils.clamp(heatMapDrawGain, -10, 10);
|
|---|
| 344 | neutralColor = DEFAULT_COLOR_PROPERTY.get();
|
|---|
| 345 | velocityScale.setNoDataColor(neutralColor);
|
|---|
| 346 | dateScale.setNoDataColor(neutralColor);
|
|---|
| 347 | hdopScale.setNoDataColor(neutralColor);
|
|---|
| 348 | qualityScale.setNoDataColor(neutralColor);
|
|---|
| 349 | directionScale.setNoDataColor(neutralColor);
|
|---|
| 350 |
|
|---|
| 351 | largesize += lineWidth;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | @Override
|
|---|
| 355 | public void paint(MapViewGraphics graphics) {
|
|---|
| 356 | Bounds clipBounds = graphics.getClipBounds().getLatLonBoundsBox();
|
|---|
| 357 | List<WayPoint> visibleSegments = listVisibleSegments(clipBounds);
|
|---|
| 358 | if (!visibleSegments.isEmpty()) {
|
|---|
| 359 | readPreferences();
|
|---|
| 360 | drawAll(graphics.getDefaultGraphics(), graphics.getMapView(), visibleSegments, clipBounds);
|
|---|
| 361 | if (graphics.getMapView().getLayerManager().getActiveLayer() == layer) {
|
|---|
| 362 | drawColorBar(graphics.getDefaultGraphics(), graphics.getMapView());
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | private List<WayPoint> listVisibleSegments(Bounds box) {
|
|---|
| 368 | WayPoint last = null;
|
|---|
| 369 | LinkedList<WayPoint> visibleSegments = new LinkedList<>();
|
|---|
| 370 |
|
|---|
| 371 | ensureTrackVisibilityLength();
|
|---|
| 372 | for (Line segment : data.getLinesIterable(layer.trackVisibility)) {
|
|---|
| 373 |
|
|---|
| 374 | for (WayPoint pt : segment) {
|
|---|
| 375 | Bounds b = new Bounds(pt.getCoor());
|
|---|
| 376 | if (pt.drawLine && last != null) {
|
|---|
| 377 | b.extend(last.getCoor());
|
|---|
| 378 | }
|
|---|
| 379 | if (b.intersects(box)) {
|
|---|
| 380 | if (last != null && (visibleSegments.isEmpty()
|
|---|
| 381 | || visibleSegments.getLast() != last)) {
|
|---|
| 382 | if (last.drawLine) {
|
|---|
| 383 | WayPoint l = new WayPoint(last);
|
|---|
| 384 | l.drawLine = false;
|
|---|
| 385 | visibleSegments.add(l);
|
|---|
| 386 | } else {
|
|---|
| 387 | visibleSegments.add(last);
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|
| 390 | visibleSegments.add(pt);
|
|---|
| 391 | }
|
|---|
| 392 | last = pt;
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 | return visibleSegments;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | /** ensures the trackVisibility array has the correct length without losing data.
|
|---|
| 399 | * TODO: Make this nicer by syncing the trackVisibility automatically.
|
|---|
| 400 | * additional entries are initialized to true;
|
|---|
| 401 | */
|
|---|
| 402 | private void ensureTrackVisibilityLength() {
|
|---|
| 403 | final int l = data.getTracks().size();
|
|---|
| 404 | if (l == layer.trackVisibility.length)
|
|---|
| 405 | return;
|
|---|
| 406 | final int m = Math.min(l, layer.trackVisibility.length);
|
|---|
| 407 | layer.trackVisibility = Arrays.copyOf(layer.trackVisibility, l);
|
|---|
| 408 | for (int i = m; i < l; i++) {
|
|---|
| 409 | layer.trackVisibility[i] = true;
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /**
|
|---|
| 414 | * Draw all enabled GPX elements of layer.
|
|---|
| 415 | * @param g the common draw object to use
|
|---|
| 416 | * @param mv the meta data to current displayed area
|
|---|
| 417 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 418 | * @param clipBounds the clipping rectangle for the current view
|
|---|
| 419 | * @since 14748 : new parameter clipBounds
|
|---|
| 420 | */
|
|---|
| 421 | public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, Bounds clipBounds) {
|
|---|
| 422 |
|
|---|
| 423 | final Stopwatch stopwatch = Stopwatch.createStarted();
|
|---|
| 424 |
|
|---|
| 425 | checkCache();
|
|---|
| 426 |
|
|---|
| 427 | // STEP 2b - RE-COMPUTE CACHE DATA *********************
|
|---|
| 428 | if (!computeCacheInSync) { // don't compute if the cache is good
|
|---|
| 429 | calculateColors();
|
|---|
| 430 | // update the WaiPoint.drawline attributes
|
|---|
| 431 | visibleSegments.clear();
|
|---|
| 432 | visibleSegments.addAll(listVisibleSegments(clipBounds));
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | fixColors(visibleSegments);
|
|---|
| 436 |
|
|---|
| 437 | // backup the environment
|
|---|
| 438 | Composite oldComposite = g.getComposite();
|
|---|
| 439 | Stroke oldStroke = g.getStroke();
|
|---|
| 440 | Paint oldPaint = g.getPaint();
|
|---|
| 441 |
|
|---|
| 442 | // set hints for the render
|
|---|
| 443 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|---|
| 444 | Config.getPref().getBoolean("mappaint.gpx.use-antialiasing", false) ?
|
|---|
| 445 | RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
|
|---|
| 446 |
|
|---|
| 447 | if (lineWidth > 0) {
|
|---|
| 448 | g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | // global enabled or select via color
|
|---|
| 452 | boolean useHeatMap = ColorMode.HEATMAP == colored;
|
|---|
| 453 |
|
|---|
| 454 | // default global alpha level
|
|---|
| 455 | float layerAlpha = 1.00f;
|
|---|
| 456 |
|
|---|
| 457 | // extract current alpha blending value
|
|---|
| 458 | if (oldComposite instanceof AlphaComposite) {
|
|---|
| 459 | layerAlpha = ((AlphaComposite) oldComposite).getAlpha();
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | // use heatmap background layer
|
|---|
| 463 | if (useHeatMap) {
|
|---|
| 464 | drawHeatMap(g, mv, visibleSegments);
|
|---|
| 465 | } else {
|
|---|
| 466 | // use normal line style or alpha-blending lines
|
|---|
| 467 | if (!alphaLines) {
|
|---|
| 468 | drawLines(g, mv, visibleSegments);
|
|---|
| 469 | } else {
|
|---|
| 470 | drawLinesAlpha(g, mv, visibleSegments, layerAlpha);
|
|---|
| 471 | }
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | // override global alpha settings (smooth overlay)
|
|---|
| 475 | if (alphaLines || useHeatMap) {
|
|---|
| 476 | g.setComposite(AlphaComposite.SrcOver.derive(0.25f * layerAlpha));
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | // normal overlays
|
|---|
| 480 | drawArrows(g, mv, visibleSegments);
|
|---|
| 481 | drawPoints(g, mv, visibleSegments);
|
|---|
| 482 |
|
|---|
| 483 | // restore environment
|
|---|
| 484 | g.setPaint(oldPaint);
|
|---|
| 485 | g.setStroke(oldStroke);
|
|---|
| 486 | g.setComposite(oldComposite);
|
|---|
| 487 |
|
|---|
| 488 | // show some debug info
|
|---|
| 489 | if (Logging.isDebugEnabled() && !visibleSegments.isEmpty()) {
|
|---|
| 490 | Logging.debug(stopwatch.toString("gpxdraw::draw") +
|
|---|
| 491 | "(" +
|
|---|
| 492 | "segments= " + visibleSegments.size() +
|
|---|
| 493 | ", per 10000 = " + Utils.getDurationString(10_000 * stopwatch.elapsed() / visibleSegments.size()) +
|
|---|
| 494 | ")"
|
|---|
| 495 | );
|
|---|
| 496 | }
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /**
|
|---|
| 500 | * Calculate colors of way segments based on latest configuration settings
|
|---|
| 501 | */
|
|---|
| 502 | public void calculateColors() {
|
|---|
| 503 | double minval = +1e10;
|
|---|
| 504 | double maxval = -1e10;
|
|---|
| 505 | WayPoint oldWp = null;
|
|---|
| 506 |
|
|---|
| 507 | if (colorModeDynamic) {
|
|---|
| 508 | if (colored == ColorMode.VELOCITY) {
|
|---|
| 509 | final List<Double> velocities = new ArrayList<>();
|
|---|
| 510 | for (Line segment : data.getLinesIterable(null)) {
|
|---|
| 511 | if (!forceLines) {
|
|---|
| 512 | oldWp = null;
|
|---|
| 513 | }
|
|---|
| 514 | for (WayPoint trkPnt : segment) {
|
|---|
| 515 | if (!trkPnt.isLatLonKnown()) {
|
|---|
| 516 | continue;
|
|---|
| 517 | }
|
|---|
| 518 | if (oldWp != null && trkPnt.getTimeInMillis() > oldWp.getTimeInMillis()) {
|
|---|
| 519 | double vel = trkPnt.getCoor().greatCircleDistance(oldWp.getCoor())
|
|---|
| 520 | / (trkPnt.getTime() - oldWp.getTime());
|
|---|
| 521 | velocities.add(vel);
|
|---|
| 522 | }
|
|---|
| 523 | oldWp = trkPnt;
|
|---|
| 524 | }
|
|---|
| 525 | }
|
|---|
| 526 | Collections.sort(velocities);
|
|---|
| 527 | if (velocities.isEmpty()) {
|
|---|
| 528 | velocityScale.setRange(0, 120/3.6);
|
|---|
| 529 | } else {
|
|---|
| 530 | minval = velocities.get(velocities.size() / 20); // 5% percentile to remove outliers
|
|---|
| 531 | maxval = velocities.get(velocities.size() * 19 / 20); // 95% percentile to remove outliers
|
|---|
| 532 | velocityScale.setRange(minval, maxval);
|
|---|
| 533 | }
|
|---|
| 534 | } else if (colored == ColorMode.HDOP) {
|
|---|
| 535 | for (Line segment : data.getLinesIterable(null)) {
|
|---|
| 536 | for (WayPoint trkPnt : segment) {
|
|---|
| 537 | Object val = trkPnt.get(GpxConstants.PT_HDOP);
|
|---|
| 538 | if (val != null) {
|
|---|
| 539 | double hdop = ((Float) val).doubleValue();
|
|---|
| 540 | if (hdop > maxval) {
|
|---|
| 541 | maxval = hdop;
|
|---|
| 542 | }
|
|---|
| 543 | if (hdop < minval) {
|
|---|
| 544 | minval = hdop;
|
|---|
| 545 | }
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 | }
|
|---|
| 549 | if (minval >= maxval) {
|
|---|
| 550 | hdopScale.setRange(0, 100);
|
|---|
| 551 | } else {
|
|---|
| 552 | hdopScale.setRange(minval, maxval);
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 | oldWp = null;
|
|---|
| 556 | } else { // color mode not dynamic
|
|---|
| 557 | velocityScale.setRange(0, velocityTune);
|
|---|
| 558 | hdopScale.setRange(0, hdoprange);
|
|---|
| 559 | qualityScale.setRange(1, rtkLibQualityColors.length);
|
|---|
| 560 | }
|
|---|
| 561 | double now = System.currentTimeMillis()/1000.0;
|
|---|
| 562 | if (colored == ColorMode.TIME) {
|
|---|
| 563 | Date[] bounds = data.getMinMaxTimeForAllTracks();
|
|---|
| 564 | if (bounds.length >= 2) {
|
|---|
| 565 | minval = bounds[0].getTime()/1000.0;
|
|---|
| 566 | maxval = bounds[1].getTime()/1000.0;
|
|---|
| 567 | } else {
|
|---|
| 568 | minval = 0;
|
|---|
| 569 | maxval = now;
|
|---|
| 570 | }
|
|---|
| 571 | dateScale.setRange(minval, maxval);
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | // Now the colors for all the points will be assigned
|
|---|
| 575 | for (Line segment : data.getLinesIterable(null)) {
|
|---|
| 576 | if (!forceLines) { // don't draw lines between segments, unless forced to
|
|---|
| 577 | oldWp = null;
|
|---|
| 578 | }
|
|---|
| 579 | for (WayPoint trkPnt : segment) {
|
|---|
| 580 | LatLon c = trkPnt.getCoor();
|
|---|
| 581 | trkPnt.customColoring = segment.getColor();
|
|---|
| 582 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
|---|
| 583 | continue;
|
|---|
| 584 | }
|
|---|
| 585 | // now we are sure some color will be assigned
|
|---|
| 586 | Color color = null;
|
|---|
| 587 |
|
|---|
| 588 | if (colored == ColorMode.HDOP) {
|
|---|
| 589 | color = hdopScale.getColor((Float) trkPnt.get(GpxConstants.PT_HDOP));
|
|---|
| 590 | } else if (colored == ColorMode.QUALITY) {
|
|---|
| 591 | color = qualityScale.getColor((Integer) trkPnt.get(GpxConstants.RTKLIB_Q));
|
|---|
| 592 | }
|
|---|
| 593 | if (oldWp != null) { // other coloring modes need segment for calcuation
|
|---|
| 594 | double dist = c.greatCircleDistance(oldWp.getCoor());
|
|---|
| 595 | boolean noDraw = false;
|
|---|
| 596 | switch (colored) {
|
|---|
| 597 | case VELOCITY:
|
|---|
| 598 | double dtime = trkPnt.getTime() - oldWp.getTime();
|
|---|
| 599 | if (dtime > 0) {
|
|---|
| 600 | color = velocityScale.getColor(dist / dtime);
|
|---|
| 601 | } else {
|
|---|
| 602 | color = velocityScale.getNoDataColor();
|
|---|
| 603 | }
|
|---|
| 604 | break;
|
|---|
| 605 | case DIRECTION:
|
|---|
| 606 | double dirColor = oldWp.getCoor().bearing(trkPnt.getCoor());
|
|---|
| 607 | color = directionScale.getColor(dirColor);
|
|---|
| 608 | break;
|
|---|
| 609 | case TIME:
|
|---|
| 610 | double t = trkPnt.getTime();
|
|---|
| 611 | // skip bad timestamps and very short tracks
|
|---|
| 612 | if (t > 0 && t <= now && maxval - minval > minTrackDurationForTimeColoring) {
|
|---|
| 613 | color = dateScale.getColor(t);
|
|---|
| 614 | } else {
|
|---|
| 615 | color = dateScale.getNoDataColor();
|
|---|
| 616 | }
|
|---|
| 617 | break;
|
|---|
| 618 | default: // Do nothing
|
|---|
| 619 | }
|
|---|
| 620 | if (!noDraw && (!segment.isUnordered() || !data.fromServer) && (maxLineLength == -1 || dist <= maxLineLength)) {
|
|---|
| 621 | trkPnt.drawLine = true;
|
|---|
| 622 | double bearing = oldWp.getCoor().bearing(trkPnt.getCoor());
|
|---|
| 623 | trkPnt.dir = ((int) (bearing / Math.PI * 4 + 1.5)) % 8;
|
|---|
| 624 | } else {
|
|---|
| 625 | trkPnt.drawLine = false;
|
|---|
| 626 | }
|
|---|
| 627 | } else { // make sure we reset outdated data
|
|---|
| 628 | trkPnt.drawLine = false;
|
|---|
| 629 | color = segment.getColor();
|
|---|
| 630 | }
|
|---|
| 631 | if (color != null) {
|
|---|
| 632 | trkPnt.customColoring = color;
|
|---|
| 633 | }
|
|---|
| 634 | oldWp = trkPnt;
|
|---|
| 635 | }
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | // heat mode
|
|---|
| 639 | if (ColorMode.HEATMAP == colored) {
|
|---|
| 640 |
|
|---|
| 641 | // get new user color map and refresh visibility level
|
|---|
| 642 | heatMapLutColor = createColorLut(heatMapDrawLowerLimit,
|
|---|
| 643 | selectColorMap(neutralColor != null ? neutralColor : Color.WHITE, heatMapDrawColorTableIdx));
|
|---|
| 644 |
|
|---|
| 645 | // force redraw of image
|
|---|
| 646 | heatMapMapViewState = null;
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | computeCacheInSync = true;
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | /**
|
|---|
| 653 | * Draw all GPX ways segments
|
|---|
| 654 | * @param g the common draw object to use
|
|---|
| 655 | * @param mv the meta data to current displayed area
|
|---|
| 656 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 657 | */
|
|---|
| 658 | private void drawLines(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
|---|
| 659 | if (lines) {
|
|---|
| 660 | Point old = null;
|
|---|
| 661 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 662 | if (!trkPnt.isLatLonKnown()) {
|
|---|
| 663 | old = null;
|
|---|
| 664 | continue;
|
|---|
| 665 | }
|
|---|
| 666 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 667 | // skip points that are on the same screenposition
|
|---|
| 668 | if (trkPnt.drawLine && old != null && ((old.x != screen.x) || (old.y != screen.y))) {
|
|---|
| 669 | g.setColor(trkPnt.customColoring);
|
|---|
| 670 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
|---|
| 671 | }
|
|---|
| 672 | old = screen;
|
|---|
| 673 | }
|
|---|
| 674 | }
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | /**
|
|---|
| 678 | * Draw all GPX arrays
|
|---|
| 679 | * @param g the common draw object to use
|
|---|
| 680 | * @param mv the meta data to current displayed area
|
|---|
| 681 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 682 | */
|
|---|
| 683 | private void drawArrows(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
|---|
| 684 | /****************************************************************
|
|---|
| 685 | ********** STEP 3b - DRAW NICE ARROWS **************************
|
|---|
| 686 | ****************************************************************/
|
|---|
| 687 | if (lines && arrows && !arrowsFast) {
|
|---|
| 688 | Point old = null;
|
|---|
| 689 | Point oldA = null; // last arrow painted
|
|---|
| 690 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 691 | if (!trkPnt.isLatLonKnown()) {
|
|---|
| 692 | old = null;
|
|---|
| 693 | continue;
|
|---|
| 694 | }
|
|---|
| 695 | if (trkPnt.drawLine) {
|
|---|
| 696 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 697 | // skip points that are on the same screenposition
|
|---|
| 698 | if (old != null
|
|---|
| 699 | && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
|
|---|
| 700 | || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
|
|---|
| 701 | g.setColor(trkPnt.customColoring);
|
|---|
| 702 | double t = Math.atan2((double) screen.y - old.y, (double) screen.x - old.x) + Math.PI;
|
|---|
| 703 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
|
|---|
| 704 | (int) (screen.y + 10 * Math.sin(t - PHI)));
|
|---|
| 705 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
|
|---|
| 706 | (int) (screen.y + 10 * Math.sin(t + PHI)));
|
|---|
| 707 | oldA = screen;
|
|---|
| 708 | }
|
|---|
| 709 | old = screen;
|
|---|
| 710 | }
|
|---|
| 711 | } // end for trkpnt
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | /****************************************************************
|
|---|
| 715 | ********** STEP 3c - DRAW FAST ARROWS **************************
|
|---|
| 716 | ****************************************************************/
|
|---|
| 717 | if (lines && arrows && arrowsFast) {
|
|---|
| 718 | Point old = null;
|
|---|
| 719 | Point oldA = null; // last arrow painted
|
|---|
| 720 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 721 | LatLon c = trkPnt.getCoor();
|
|---|
| 722 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
|---|
| 723 | continue;
|
|---|
| 724 | }
|
|---|
| 725 | if (trkPnt.drawLine) {
|
|---|
| 726 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 727 | // skip points that are on the same screenposition
|
|---|
| 728 | if (old != null
|
|---|
| 729 | && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
|
|---|
| 730 | || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
|
|---|
| 731 | g.setColor(trkPnt.customColoring);
|
|---|
| 732 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
|
|---|
| 733 | + dir[trkPnt.dir][1]);
|
|---|
| 734 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
|
|---|
| 735 | + dir[trkPnt.dir][3]);
|
|---|
| 736 | oldA = screen;
|
|---|
| 737 | }
|
|---|
| 738 | old = screen;
|
|---|
| 739 | }
|
|---|
| 740 | } // end for trkpnt
|
|---|
| 741 | }
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | /**
|
|---|
| 745 | * Draw all GPX points
|
|---|
| 746 | * @param g the common draw object to use
|
|---|
| 747 | * @param mv the meta data to current displayed area
|
|---|
| 748 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 749 | */
|
|---|
| 750 | private void drawPoints(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
|---|
| 751 | /****************************************************************
|
|---|
| 752 | ********** STEP 3d - DRAW LARGE POINTS AND HDOP CIRCLE *********
|
|---|
| 753 | ****************************************************************/
|
|---|
| 754 | if (large || hdopCircle) {
|
|---|
| 755 | final int halfSize = largesize/2;
|
|---|
| 756 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 757 | LatLon c = trkPnt.getCoor();
|
|---|
| 758 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
|---|
| 759 | continue;
|
|---|
| 760 | }
|
|---|
| 761 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 762 |
|
|---|
| 763 | if (hdopCircle && trkPnt.get(GpxConstants.PT_HDOP) != null) {
|
|---|
| 764 | // hdop value
|
|---|
| 765 | float hdop = (Float) trkPnt.get(GpxConstants.PT_HDOP);
|
|---|
| 766 | if (hdop < 0) {
|
|---|
| 767 | hdop = 0;
|
|---|
| 768 | }
|
|---|
| 769 | Color customColoringTransparent = hdopAlpha < 0 ? trkPnt.customColoring :
|
|---|
| 770 | new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (hdopAlpha << 24), true);
|
|---|
| 771 | g.setColor(customColoringTransparent);
|
|---|
| 772 | // hdop circles
|
|---|
| 773 | int hdopp = mv.getPoint(new LatLon(
|
|---|
| 774 | trkPnt.getCoor().lat(),
|
|---|
| 775 | trkPnt.getCoor().lon() + 2d*6*hdop*360/40000000d)).x - screen.x;
|
|---|
| 776 | g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
|
|---|
| 777 | }
|
|---|
| 778 | if (large) {
|
|---|
| 779 | // color the large GPS points like the gps lines
|
|---|
| 780 | if (trkPnt.customColoring != null) {
|
|---|
| 781 | if (trkPnt.customColoring.equals(colorCache) && colorCacheTransparent != null) {
|
|---|
| 782 | g.setColor(colorCacheTransparent);
|
|---|
| 783 | } else {
|
|---|
| 784 | Color customColoringTransparent = largePointAlpha < 0 ? trkPnt.customColoring :
|
|---|
| 785 | new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (largePointAlpha << 24), true);
|
|---|
| 786 |
|
|---|
| 787 | g.setColor(customColoringTransparent);
|
|---|
| 788 | colorCache = trkPnt.customColoring;
|
|---|
| 789 | colorCacheTransparent = customColoringTransparent;
|
|---|
| 790 | }
|
|---|
| 791 | }
|
|---|
| 792 | g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
|
|---|
| 793 | }
|
|---|
| 794 | } // end for trkpnt
|
|---|
| 795 | } // end if large || hdopcircle
|
|---|
| 796 |
|
|---|
| 797 | /****************************************************************
|
|---|
| 798 | ********** STEP 3e - DRAW SMALL POINTS FOR LINES ***************
|
|---|
| 799 | ****************************************************************/
|
|---|
| 800 | if (!large && lines) {
|
|---|
| 801 | g.setColor(neutralColor);
|
|---|
| 802 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 803 | LatLon c = trkPnt.getCoor();
|
|---|
| 804 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
|---|
| 805 | continue;
|
|---|
| 806 | }
|
|---|
| 807 | if (!trkPnt.drawLine) {
|
|---|
| 808 | g.setColor(trkPnt.customColoring);
|
|---|
| 809 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 810 | g.drawRect(screen.x, screen.y, 0, 0);
|
|---|
| 811 | }
|
|---|
| 812 | } // end for trkpnt
|
|---|
| 813 | } // end if large
|
|---|
| 814 |
|
|---|
| 815 | /****************************************************************
|
|---|
| 816 | ********** STEP 3f - DRAW SMALL POINTS INSTEAD OF LINES ********
|
|---|
| 817 | ****************************************************************/
|
|---|
| 818 | if (!large && !lines) {
|
|---|
| 819 | g.setColor(neutralColor);
|
|---|
| 820 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 821 | LatLon c = trkPnt.getCoor();
|
|---|
| 822 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
|---|
| 823 | continue;
|
|---|
| 824 | }
|
|---|
| 825 | Point screen = mv.getPoint(trkPnt);
|
|---|
| 826 | g.setColor(trkPnt.customColoring);
|
|---|
| 827 | g.drawRect(screen.x, screen.y, 0, 0);
|
|---|
| 828 | } // end for trkpnt
|
|---|
| 829 | } // end if large
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | /**
|
|---|
| 833 | * Draw GPX lines by using alpha blending
|
|---|
| 834 | * @param g the common draw object to use
|
|---|
| 835 | * @param mv the meta data to current displayed area
|
|---|
| 836 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 837 | * @param layerAlpha the color alpha value set for that operation
|
|---|
| 838 | */
|
|---|
| 839 | private void drawLinesAlpha(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, float layerAlpha) {
|
|---|
| 840 |
|
|---|
| 841 | // 1st. backup the paint environment ----------------------------------
|
|---|
| 842 | Composite oldComposite = g.getComposite();
|
|---|
| 843 | Stroke oldStroke = g.getStroke();
|
|---|
| 844 | Paint oldPaint = g.getPaint();
|
|---|
| 845 |
|
|---|
| 846 | // 2nd. determine current scale factors -------------------------------
|
|---|
| 847 |
|
|---|
| 848 | // adjust global settings
|
|---|
| 849 | final int globalLineWidth = Utils.clamp(lineWidth, 1, 20);
|
|---|
| 850 |
|
|---|
| 851 | // cache scale of view
|
|---|
| 852 | final double zoomScale = mv.getDist100Pixel() / 50.0f;
|
|---|
| 853 |
|
|---|
| 854 | // 3rd. determine current paint parameters -----------------------------
|
|---|
| 855 |
|
|---|
| 856 | // alpha value is based on zoom and line with combined with global layer alpha
|
|---|
| 857 | float theLineAlpha = (float) Utils.clamp((0.50 / zoomScale) / (globalLineWidth + 1), 0.01, 0.50) * layerAlpha;
|
|---|
| 858 | final int theLineWith = (int) (lineWidth / zoomScale) + 1;
|
|---|
| 859 |
|
|---|
| 860 | // 4th setup virtual paint area ----------------------------------------
|
|---|
| 861 |
|
|---|
| 862 | // set line format and alpha channel for all overlays (more lines -> few overlap -> more transparency)
|
|---|
| 863 | g.setStroke(new BasicStroke(theLineWith, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
|---|
| 864 | g.setComposite(AlphaComposite.SrcOver.derive(theLineAlpha));
|
|---|
| 865 |
|
|---|
| 866 | // last used / calculated entries
|
|---|
| 867 | Point lastPaintPnt = null;
|
|---|
| 868 |
|
|---|
| 869 | // 5th draw the layer ---------------------------------------------------
|
|---|
| 870 |
|
|---|
| 871 | // for all points
|
|---|
| 872 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 873 |
|
|---|
| 874 | // transform coordinates
|
|---|
| 875 | final Point paintPnt = mv.getPoint(trkPnt);
|
|---|
| 876 |
|
|---|
| 877 | // skip single points
|
|---|
| 878 | if (lastPaintPnt != null && trkPnt.drawLine && !lastPaintPnt.equals(paintPnt)) {
|
|---|
| 879 |
|
|---|
| 880 | // set different color
|
|---|
| 881 | g.setColor(trkPnt.customColoring);
|
|---|
| 882 |
|
|---|
| 883 | // draw it
|
|---|
| 884 | g.drawLine(lastPaintPnt.x, lastPaintPnt.y, paintPnt.x, paintPnt.y);
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | lastPaintPnt = paintPnt;
|
|---|
| 888 | }
|
|---|
| 889 |
|
|---|
| 890 | // @last restore modified paint environment -----------------------------
|
|---|
| 891 | g.setPaint(oldPaint);
|
|---|
| 892 | g.setStroke(oldStroke);
|
|---|
| 893 | g.setComposite(oldComposite);
|
|---|
| 894 | }
|
|---|
| 895 |
|
|---|
| 896 | /**
|
|---|
| 897 | * Generates a linear gradient map image
|
|---|
| 898 | *
|
|---|
| 899 | * @param width image width
|
|---|
| 900 | * @param height image height
|
|---|
| 901 | * @param colors 1..n color descriptions
|
|---|
| 902 | * @return image object
|
|---|
| 903 | */
|
|---|
| 904 | protected static BufferedImage createImageGradientMap(int width, int height, Color... colors) {
|
|---|
| 905 |
|
|---|
| 906 | // create image an paint object
|
|---|
| 907 | final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|---|
| 908 | final Graphics2D g = img.createGraphics();
|
|---|
| 909 |
|
|---|
| 910 | float[] fract = new float[ colors.length ];
|
|---|
| 911 |
|
|---|
| 912 | // distribute fractions (define position of color in map)
|
|---|
| 913 | for (int i = 0; i < colors.length; ++i) {
|
|---|
| 914 | fract[i] = i * (1.0f / colors.length);
|
|---|
| 915 | }
|
|---|
| 916 |
|
|---|
| 917 | // draw the gradient map
|
|---|
| 918 | LinearGradientPaint gradient = new LinearGradientPaint(0, 0, width, height, fract, colors,
|
|---|
| 919 | MultipleGradientPaint.CycleMethod.NO_CYCLE);
|
|---|
| 920 | g.setPaint(gradient);
|
|---|
| 921 | g.fillRect(0, 0, width, height);
|
|---|
| 922 | g.dispose();
|
|---|
| 923 |
|
|---|
| 924 | // access it via raw interface
|
|---|
| 925 | return img;
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | /**
|
|---|
| 929 | * Creates a distributed colormap by linear blending between colors
|
|---|
| 930 | * @param lowerLimit lower limit for first visible color
|
|---|
| 931 | * @param colors 1..n colors
|
|---|
| 932 | * @return array of Color objects
|
|---|
| 933 | */
|
|---|
| 934 | protected static Color[] createColorLut(int lowerLimit, Color... colors) {
|
|---|
| 935 |
|
|---|
| 936 | // number of lookup entries
|
|---|
| 937 | final int tableSize = 256;
|
|---|
| 938 |
|
|---|
| 939 | // access it via raw interface
|
|---|
| 940 | final Raster imgRaster = createImageGradientMap(tableSize, 1, colors).getData();
|
|---|
| 941 |
|
|---|
| 942 | // the pixel storage
|
|---|
| 943 | int[] pixel = new int[1];
|
|---|
| 944 |
|
|---|
| 945 | Color[] colorTable = new Color[tableSize];
|
|---|
| 946 |
|
|---|
| 947 | // map the range 0..255 to 0..pi/2
|
|---|
| 948 | final double mapTo90Deg = Math.PI / 2.0 / 255.0;
|
|---|
| 949 |
|
|---|
| 950 | // create the lookup table
|
|---|
| 951 | for (int i = 0; i < tableSize; i++) {
|
|---|
| 952 |
|
|---|
| 953 | // get next single pixel
|
|---|
| 954 | imgRaster.getDataElements(i, 0, pixel);
|
|---|
| 955 |
|
|---|
| 956 | // get color and map
|
|---|
| 957 | Color c = new Color(pixel[0]);
|
|---|
| 958 |
|
|---|
| 959 | // smooth alpha like sin curve
|
|---|
| 960 | int alpha = (i > lowerLimit) ? (int) (Math.sin((i-lowerLimit) * mapTo90Deg) * 255) : 0;
|
|---|
| 961 |
|
|---|
| 962 | // alpha with pre-offset, first color -> full transparent
|
|---|
| 963 | alpha = alpha > 0 ? (20 + alpha) : 0;
|
|---|
| 964 |
|
|---|
| 965 | // shrink to maximum bound
|
|---|
| 966 | if (alpha > 255) {
|
|---|
| 967 | alpha = 255;
|
|---|
| 968 | }
|
|---|
| 969 |
|
|---|
| 970 | // increase transparency for higher values ( avoid big saturation )
|
|---|
| 971 | if (i > 240 && 255 == alpha) {
|
|---|
| 972 | alpha -= (i - 240);
|
|---|
| 973 | }
|
|---|
| 974 |
|
|---|
| 975 | // fill entry in table, assign a alpha value
|
|---|
| 976 | colorTable[i] = new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
|
|---|
| 977 | }
|
|---|
| 978 |
|
|---|
| 979 | // transform into lookup table
|
|---|
| 980 | return colorTable;
|
|---|
| 981 | }
|
|---|
| 982 |
|
|---|
| 983 | /**
|
|---|
| 984 | * Creates a darker color
|
|---|
| 985 | * @param in Color object
|
|---|
| 986 | * @param adjust darker adjustment amount
|
|---|
| 987 | * @return new Color
|
|---|
| 988 | */
|
|---|
| 989 | protected static Color darkerColor(Color in, float adjust) {
|
|---|
| 990 |
|
|---|
| 991 | final float r = (float) in.getRed()/255;
|
|---|
| 992 | final float g = (float) in.getGreen()/255;
|
|---|
| 993 | final float b = (float) in.getBlue()/255;
|
|---|
| 994 |
|
|---|
| 995 | return new Color(r*adjust, g*adjust, b*adjust);
|
|---|
| 996 | }
|
|---|
| 997 |
|
|---|
| 998 | /**
|
|---|
| 999 | * Creates a colormap by using a static color map with 1..n colors (RGB 0.0 ..1.0)
|
|---|
| 1000 | * @param str the filename (without extension) to look for into data/gpx
|
|---|
| 1001 | * @return the parsed colormap
|
|---|
| 1002 | */
|
|---|
| 1003 | protected static Color[] createColorFromResource(String str) {
|
|---|
| 1004 |
|
|---|
| 1005 | // create resource string
|
|---|
| 1006 | final String colorFile = "resource://data/gpx/" + str + ".txt";
|
|---|
| 1007 |
|
|---|
| 1008 | List<Color> colorList = new ArrayList<>();
|
|---|
| 1009 |
|
|---|
| 1010 | // try to load the file
|
|---|
| 1011 | try (CachedFile cf = new CachedFile(colorFile); BufferedReader br = cf.getContentReader()) {
|
|---|
| 1012 |
|
|---|
| 1013 | String line;
|
|---|
| 1014 |
|
|---|
| 1015 | // process lines
|
|---|
| 1016 | while ((line = br.readLine()) != null) {
|
|---|
| 1017 |
|
|---|
| 1018 | // use comma as separator
|
|---|
| 1019 | String[] column = line.split(",", -1);
|
|---|
| 1020 |
|
|---|
| 1021 | // empty or comment line
|
|---|
| 1022 | if (column.length < 3 || column[0].startsWith("#")) {
|
|---|
| 1023 | continue;
|
|---|
| 1024 | }
|
|---|
| 1025 |
|
|---|
| 1026 | // extract RGB value
|
|---|
| 1027 | float r = Float.parseFloat(column[0]);
|
|---|
| 1028 | float g = Float.parseFloat(column[1]);
|
|---|
| 1029 | float b = Float.parseFloat(column[2]);
|
|---|
| 1030 |
|
|---|
| 1031 | // some color tables are 0..1.0 and some 0.255
|
|---|
| 1032 | float scale = (r < 1 && g < 1 && b < 1) ? 1 : 255;
|
|---|
| 1033 |
|
|---|
| 1034 | colorList.add(new Color(r/scale, g/scale, b/scale));
|
|---|
| 1035 | }
|
|---|
| 1036 | } catch (IOException e) {
|
|---|
| 1037 | throw new JosmRuntimeException(e);
|
|---|
| 1038 | }
|
|---|
| 1039 |
|
|---|
| 1040 | // fallback if empty or failed
|
|---|
| 1041 | if (colorList.isEmpty()) {
|
|---|
| 1042 | colorList.add(Color.BLACK);
|
|---|
| 1043 | colorList.add(Color.WHITE);
|
|---|
| 1044 | } else {
|
|---|
| 1045 | // add additional darker elements to end of list
|
|---|
| 1046 | final Color lastColor = colorList.get(colorList.size() - 1);
|
|---|
| 1047 | colorList.add(darkerColor(lastColor, 0.975f));
|
|---|
| 1048 | colorList.add(darkerColor(lastColor, 0.950f));
|
|---|
| 1049 | }
|
|---|
| 1050 |
|
|---|
| 1051 | return createColorLut(0, colorList.toArray(new Color[0]));
|
|---|
| 1052 | }
|
|---|
| 1053 |
|
|---|
| 1054 | /**
|
|---|
| 1055 | * Returns the next user color map
|
|---|
| 1056 | *
|
|---|
| 1057 | * @param userColor - default or fallback user color
|
|---|
| 1058 | * @param tableIdx - selected user color index
|
|---|
| 1059 | * @return color array
|
|---|
| 1060 | */
|
|---|
| 1061 | protected static Color[] selectColorMap(Color userColor, int tableIdx) {
|
|---|
| 1062 |
|
|---|
| 1063 | // generate new user color map ( dark, user color, white )
|
|---|
| 1064 | Color[] userColor1 = createColorLut(0, userColor.darker(), userColor, userColor.brighter(), Color.WHITE);
|
|---|
| 1065 |
|
|---|
| 1066 | // generate new user color map ( white -> color )
|
|---|
| 1067 | Color[] userColor2 = createColorLut(0, Color.WHITE, Color.WHITE, userColor);
|
|---|
| 1068 |
|
|---|
| 1069 | // generate new user color map
|
|---|
| 1070 | Color[] colorTrafficLights = createColorLut(0, Color.WHITE, Color.GREEN.darker(), Color.YELLOW, Color.RED);
|
|---|
| 1071 |
|
|---|
| 1072 | // decide what, keep order is sync with setting on GUI
|
|---|
| 1073 | Color[][] lut = {
|
|---|
| 1074 | userColor1,
|
|---|
| 1075 | userColor2,
|
|---|
| 1076 | colorTrafficLights,
|
|---|
| 1077 | heatMapLutColorJosmInferno,
|
|---|
| 1078 | heatMapLutColorJosmViridis,
|
|---|
| 1079 | heatMapLutColorJosmBrown2Green,
|
|---|
| 1080 | heatMapLutColorJosmRed2Blue
|
|---|
| 1081 | };
|
|---|
| 1082 |
|
|---|
| 1083 | // default case
|
|---|
| 1084 | Color[] nextUserColor = userColor1;
|
|---|
| 1085 |
|
|---|
| 1086 | // select by index
|
|---|
| 1087 | if (tableIdx < lut.length) {
|
|---|
| 1088 | nextUserColor = lut[ tableIdx ];
|
|---|
| 1089 | }
|
|---|
| 1090 |
|
|---|
| 1091 | // adjust color map
|
|---|
| 1092 | return nextUserColor;
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | /**
|
|---|
| 1096 | * Generates a Icon
|
|---|
| 1097 | *
|
|---|
| 1098 | * @param userColor selected user color
|
|---|
| 1099 | * @param tableIdx tabled index
|
|---|
| 1100 | * @param size size of the image
|
|---|
| 1101 | * @return a image icon that shows the
|
|---|
| 1102 | */
|
|---|
| 1103 | public static ImageIcon getColorMapImageIcon(Color userColor, int tableIdx, int size) {
|
|---|
| 1104 | return new ImageIcon(createImageGradientMap(size, size, selectColorMap(userColor, tableIdx)));
|
|---|
| 1105 | }
|
|---|
| 1106 |
|
|---|
| 1107 | /**
|
|---|
| 1108 | * Draw gray heat map with current Graphics2D setting
|
|---|
| 1109 | * @param gB the common draw object to use
|
|---|
| 1110 | * @param mv the meta data to current displayed area
|
|---|
| 1111 | * @param listSegm segments visible in the current scope of mv
|
|---|
| 1112 | * @param foreComp composite use to draw foreground objects
|
|---|
| 1113 | * @param foreStroke stroke use to draw foreground objects
|
|---|
| 1114 | * @param backComp composite use to draw background objects
|
|---|
| 1115 | * @param backStroke stroke use to draw background objects
|
|---|
| 1116 | */
|
|---|
| 1117 | private void drawHeatGrayLineMap(Graphics2D gB, MapView mv, List<WayPoint> listSegm,
|
|---|
| 1118 | Composite foreComp, Stroke foreStroke,
|
|---|
| 1119 | Composite backComp, Stroke backStroke) {
|
|---|
| 1120 |
|
|---|
| 1121 | // draw foreground
|
|---|
| 1122 | boolean drawForeground = foreComp != null && foreStroke != null;
|
|---|
| 1123 |
|
|---|
| 1124 | // set initial values
|
|---|
| 1125 | gB.setStroke(backStroke); gB.setComposite(backComp);
|
|---|
| 1126 |
|
|---|
| 1127 | // get last point in list
|
|---|
| 1128 | final WayPoint lastPnt = !listSegm.isEmpty() ? listSegm.get(listSegm.size() - 1) : null;
|
|---|
| 1129 |
|
|---|
| 1130 | // for all points, draw single lines by using optimized drawing
|
|---|
| 1131 | for (WayPoint trkPnt : listSegm) {
|
|---|
| 1132 |
|
|---|
| 1133 | // get transformed coordinates
|
|---|
| 1134 | final Point paintPnt = mv.getPoint(trkPnt);
|
|---|
| 1135 |
|
|---|
| 1136 | // end of line segment or end of list reached
|
|---|
| 1137 | if (!trkPnt.drawLine || (lastPnt == trkPnt)) {
|
|---|
| 1138 |
|
|---|
| 1139 | // convert to primitive type
|
|---|
| 1140 | final int[] polyXArr = heatMapPolyX.stream().mapToInt(Integer::intValue).toArray();
|
|---|
| 1141 | final int[] polyYArr = heatMapPolyY.stream().mapToInt(Integer::intValue).toArray();
|
|---|
| 1142 |
|
|---|
| 1143 | // a.) draw background
|
|---|
| 1144 | gB.drawPolyline(polyXArr, polyYArr, polyXArr.length);
|
|---|
| 1145 |
|
|---|
| 1146 | // b.) draw extra foreground
|
|---|
| 1147 | if (drawForeground && heatMapDrawExtraLine) {
|
|---|
| 1148 |
|
|---|
| 1149 | gB.setStroke(foreStroke); gB.setComposite(foreComp);
|
|---|
| 1150 | gB.drawPolyline(polyXArr, polyYArr, polyXArr.length);
|
|---|
| 1151 | gB.setStroke(backStroke); gB.setComposite(backComp);
|
|---|
| 1152 | }
|
|---|
| 1153 |
|
|---|
| 1154 | // drop used points
|
|---|
| 1155 | heatMapPolyX.clear(); heatMapPolyY.clear();
|
|---|
| 1156 | }
|
|---|
| 1157 |
|
|---|
| 1158 | // store only the integer part (make sense because pixel is 1:1 here)
|
|---|
| 1159 | heatMapPolyX.add((int) paintPnt.getX());
|
|---|
| 1160 | heatMapPolyY.add((int) paintPnt.getY());
|
|---|
| 1161 | }
|
|---|
| 1162 | }
|
|---|
| 1163 |
|
|---|
| 1164 | /**
|
|---|
| 1165 | * Map the gray map to heat map and draw them with current Graphics2D setting
|
|---|
| 1166 | * @param g the common draw object to use
|
|---|
| 1167 | * @param imgGray gray scale input image
|
|---|
| 1168 | * @param sampleRaster the line with for drawing
|
|---|
| 1169 | * @param outlineWidth line width for outlines
|
|---|
| 1170 | */
|
|---|
| 1171 | private void drawHeatMapGrayMap(Graphics2D g, BufferedImage imgGray, int sampleRaster, int outlineWidth) {
|
|---|
| 1172 |
|
|---|
| 1173 | final int[] imgPixels = ((DataBufferInt) imgGray.getRaster().getDataBuffer()).getData();
|
|---|
| 1174 |
|
|---|
| 1175 | // samples offset and bounds are scaled with line width derived from zoom level
|
|---|
| 1176 | final int offX = Math.max(1, sampleRaster);
|
|---|
| 1177 | final int offY = Math.max(1, sampleRaster);
|
|---|
| 1178 |
|
|---|
| 1179 | final int maxPixelX = imgGray.getWidth();
|
|---|
| 1180 | final int maxPixelY = imgGray.getHeight();
|
|---|
| 1181 |
|
|---|
| 1182 | // always full or outlines at big samples rasters
|
|---|
| 1183 | final boolean drawOutlines = (outlineWidth > 0) && ((0 == sampleRaster) || (sampleRaster > 10));
|
|---|
| 1184 |
|
|---|
| 1185 | // backup stroke
|
|---|
| 1186 | final Stroke oldStroke = g.getStroke();
|
|---|
| 1187 |
|
|---|
| 1188 | // use basic stroke for outlines and default transparency
|
|---|
| 1189 | g.setStroke(new BasicStroke(outlineWidth));
|
|---|
| 1190 |
|
|---|
| 1191 | int lastPixelX = 0;
|
|---|
| 1192 | int lastPixelColor = 0;
|
|---|
| 1193 |
|
|---|
| 1194 | // resample gray scale image with line linear weight of next sample in line
|
|---|
| 1195 | // process each line and draw pixels / rectangles with same color with one operations
|
|---|
| 1196 | for (int y = 0; y < maxPixelY; y += offY) {
|
|---|
| 1197 |
|
|---|
| 1198 | // the lines offsets
|
|---|
| 1199 | final int lastLineOffset = maxPixelX * (y+0);
|
|---|
| 1200 | final int nextLineOffset = maxPixelX * (y+1);
|
|---|
| 1201 |
|
|---|
| 1202 | for (int x = 0; x < maxPixelX; x += offX) {
|
|---|
| 1203 |
|
|---|
| 1204 | int thePixelColor = 0; int thePixelCount = 0;
|
|---|
| 1205 |
|
|---|
| 1206 | // sample the image (it is gray scale)
|
|---|
| 1207 | int offset = lastLineOffset + x;
|
|---|
| 1208 |
|
|---|
| 1209 | // merge next pixels of window of line
|
|---|
| 1210 | for (int k = 0; k < offX && (offset + k) < nextLineOffset; k++) {
|
|---|
| 1211 | thePixelColor += imgPixels[offset+k] & 0xFF;
|
|---|
| 1212 | thePixelCount++;
|
|---|
| 1213 | }
|
|---|
| 1214 |
|
|---|
| 1215 | // mean value
|
|---|
| 1216 | thePixelColor = thePixelCount > 0 ? (thePixelColor / thePixelCount) : 0;
|
|---|
| 1217 |
|
|---|
| 1218 | // restart -> use initial sample
|
|---|
| 1219 | if (0 == x) {
|
|---|
| 1220 | lastPixelX = 0; lastPixelColor = thePixelColor - 1;
|
|---|
| 1221 | }
|
|---|
| 1222 |
|
|---|
| 1223 | boolean bDrawIt = false;
|
|---|
| 1224 |
|
|---|
| 1225 | // when one of segment is mapped to black
|
|---|
| 1226 | bDrawIt = bDrawIt || (lastPixelColor == 0) || (thePixelColor == 0);
|
|---|
| 1227 |
|
|---|
| 1228 | // different color
|
|---|
| 1229 | bDrawIt = bDrawIt || (Math.abs(lastPixelColor-thePixelColor) > 0);
|
|---|
| 1230 |
|
|---|
| 1231 | // when line is finished draw always
|
|---|
| 1232 | bDrawIt = bDrawIt || (y >= (maxPixelY-offY));
|
|---|
| 1233 |
|
|---|
| 1234 | if (bDrawIt) {
|
|---|
| 1235 |
|
|---|
| 1236 | // draw only foreground pixels
|
|---|
| 1237 | if (lastPixelColor > 0) {
|
|---|
| 1238 |
|
|---|
| 1239 | // gray to RGB mapping
|
|---|
| 1240 | g.setColor(heatMapLutColor[ lastPixelColor ]);
|
|---|
| 1241 |
|
|---|
| 1242 | // box from from last Y pixel to current pixel
|
|---|
| 1243 | if (drawOutlines) {
|
|---|
| 1244 | g.drawRect(lastPixelX, y, offX + x - lastPixelX, offY);
|
|---|
| 1245 | } else {
|
|---|
| 1246 | g.fillRect(lastPixelX, y, offX + x - lastPixelX, offY);
|
|---|
| 1247 | }
|
|---|
| 1248 | }
|
|---|
| 1249 |
|
|---|
| 1250 | // restart detection
|
|---|
| 1251 | lastPixelX = x; lastPixelColor = thePixelColor;
|
|---|
| 1252 | }
|
|---|
| 1253 | }
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 | // recover
|
|---|
| 1257 | g.setStroke(oldStroke);
|
|---|
| 1258 | }
|
|---|
| 1259 |
|
|---|
| 1260 | /**
|
|---|
| 1261 | * Collect and draw GPS segments and displays a heat-map
|
|---|
| 1262 | * @param g the common draw object to use
|
|---|
| 1263 | * @param mv the meta data to current displayed area
|
|---|
| 1264 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 1265 | */
|
|---|
| 1266 | private void drawHeatMap(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
|---|
| 1267 |
|
|---|
| 1268 | // get bounds of screen image and projection, zoom and adjust input parameters
|
|---|
| 1269 | final Rectangle screenBounds = new Rectangle(mv.getWidth(), mv.getHeight());
|
|---|
| 1270 | final MapViewState mapViewState = mv.getState();
|
|---|
| 1271 | final double zoomScale = mv.getDist100Pixel() / 50.0f;
|
|---|
| 1272 |
|
|---|
| 1273 | // adjust global settings ( zero = default line width )
|
|---|
| 1274 | final int globalLineWidth = (0 == lineWidth) ? 1 : Utils.clamp(lineWidth, 1, 20);
|
|---|
| 1275 |
|
|---|
| 1276 | // 1st setup virtual paint area ----------------------------------------
|
|---|
| 1277 |
|
|---|
| 1278 | // new image buffer needed
|
|---|
| 1279 | final boolean imageSetup = null == heatMapImgGray || !heatMapCacheScreenBounds.equals(screenBounds);
|
|---|
| 1280 |
|
|---|
| 1281 | // screen bounds changed, need new image buffer ?
|
|---|
| 1282 | if (imageSetup) {
|
|---|
| 1283 | // we would use a "pure" grayscale image, but there is not efficient way to map gray scale values to RGB)
|
|---|
| 1284 | heatMapImgGray = new BufferedImage(screenBounds.width, screenBounds.height, BufferedImage.TYPE_INT_ARGB);
|
|---|
| 1285 | heatMapGraph2d = heatMapImgGray.createGraphics();
|
|---|
| 1286 | heatMapGraph2d.setBackground(new Color(0, 0, 0, 255));
|
|---|
| 1287 | heatMapGraph2d.setColor(Color.WHITE);
|
|---|
| 1288 |
|
|---|
| 1289 | // fast draw ( maybe help or not )
|
|---|
| 1290 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
|---|
| 1291 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
|
|---|
| 1292 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
|
|---|
| 1293 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
|
|---|
| 1294 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
|---|
| 1295 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
|---|
| 1296 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
|
|---|
| 1297 |
|
|---|
| 1298 | // cache it
|
|---|
| 1299 | heatMapCacheScreenBounds = screenBounds;
|
|---|
| 1300 | }
|
|---|
| 1301 |
|
|---|
| 1302 | // 2nd. determine current scale factors -------------------------------
|
|---|
| 1303 |
|
|---|
| 1304 | // the line width (foreground: draw extra small footprint line of track)
|
|---|
| 1305 | int lineWidthB = (int) Math.max(1.5f * (globalLineWidth / zoomScale) + 1, 2);
|
|---|
| 1306 | int lineWidthF = lineWidthB > 2 ? (globalLineWidth - 1) : 0;
|
|---|
| 1307 |
|
|---|
| 1308 | // global alpha adjustment
|
|---|
| 1309 | float lineAlpha = (float) Utils.clamp((0.40 / zoomScale) / (globalLineWidth + 1), 0.01, 0.40);
|
|---|
| 1310 |
|
|---|
| 1311 | // adjust 0.15 .. 1.85
|
|---|
| 1312 | float scaleAlpha = 1.0f + ((heatMapDrawGain/10.0f) * 0.85f);
|
|---|
| 1313 |
|
|---|
| 1314 | // add to calculated values
|
|---|
| 1315 | float lineAlphaBPoint = (float) Utils.clamp((lineAlpha * 0.65) * scaleAlpha, 0.001, 0.90);
|
|---|
| 1316 | float lineAlphaBLine = (float) Utils.clamp((lineAlpha * 1.00) * scaleAlpha, 0.001, 0.90);
|
|---|
| 1317 | float lineAlphaFLine = (float) Utils.clamp((lineAlpha / 1.50) * scaleAlpha, 0.001, 0.90);
|
|---|
| 1318 |
|
|---|
| 1319 | // 3rd Calculate the heat map data by draw GPX traces with alpha value ----------
|
|---|
| 1320 |
|
|---|
| 1321 | // recalculation of image needed
|
|---|
| 1322 | final boolean imageRecalc = !mapViewState.equalsInWindow(heatMapMapViewState)
|
|---|
| 1323 | || gpxLayerInvalidated
|
|---|
| 1324 | || heatMapCacheLineWith != globalLineWidth;
|
|---|
| 1325 |
|
|---|
| 1326 | // need re-generation of gray image ?
|
|---|
| 1327 | if (imageSetup || imageRecalc) {
|
|---|
| 1328 |
|
|---|
| 1329 | // clear background
|
|---|
| 1330 | heatMapGraph2d.clearRect(0, 0, heatMapImgGray.getWidth(), heatMapImgGray.getHeight());
|
|---|
| 1331 |
|
|---|
| 1332 | // point or line blending
|
|---|
| 1333 | if (heatMapDrawPointMode) {
|
|---|
| 1334 | heatMapGraph2d.setComposite(AlphaComposite.SrcOver.derive(lineAlphaBPoint));
|
|---|
| 1335 | drawHeatGrayDotMap(heatMapGraph2d, mv, visibleSegments, lineWidthB);
|
|---|
| 1336 |
|
|---|
| 1337 | } else {
|
|---|
| 1338 | drawHeatGrayLineMap(heatMapGraph2d, mv, visibleSegments,
|
|---|
| 1339 | lineWidthF > 1 ? AlphaComposite.SrcOver.derive(lineAlphaFLine) : null,
|
|---|
| 1340 | new BasicStroke(lineWidthF, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND),
|
|---|
| 1341 | AlphaComposite.SrcOver.derive(lineAlphaBLine),
|
|---|
| 1342 | new BasicStroke(lineWidthB, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
|---|
| 1343 | }
|
|---|
| 1344 |
|
|---|
| 1345 | // remember draw parameter
|
|---|
| 1346 | heatMapMapViewState = mapViewState;
|
|---|
| 1347 | heatMapCacheLineWith = globalLineWidth;
|
|---|
| 1348 | gpxLayerInvalidated = false;
|
|---|
| 1349 | }
|
|---|
| 1350 |
|
|---|
| 1351 | // 4th. Draw data on target layer, map data via color lookup table --------------
|
|---|
| 1352 | drawHeatMapGrayMap(g, heatMapImgGray, lineWidthB > 2 ? (int) (lineWidthB*1.25f) : 1, lineWidth > 2 ? (lineWidth - 2) : 1);
|
|---|
| 1353 | }
|
|---|
| 1354 |
|
|---|
| 1355 | /**
|
|---|
| 1356 | * Draw a dotted heat map
|
|---|
| 1357 | *
|
|---|
| 1358 | * @param gB the common draw object to use
|
|---|
| 1359 | * @param mv the meta data to current displayed area
|
|---|
| 1360 | * @param listSegm segments visible in the current scope of mv
|
|---|
| 1361 | * @param drawSize draw size of draw element
|
|---|
| 1362 | */
|
|---|
| 1363 | private static void drawHeatGrayDotMap(Graphics2D gB, MapView mv, List<WayPoint> listSegm, int drawSize) {
|
|---|
| 1364 |
|
|---|
| 1365 | // typical rendering rate -> use realtime preview instead of accurate display
|
|---|
| 1366 | final double maxSegm = 25_000, nrSegms = listSegm.size();
|
|---|
| 1367 |
|
|---|
| 1368 | // determine random drop rate
|
|---|
| 1369 | final double randomDrop = Math.min(nrSegms > maxSegm ? (nrSegms - maxSegm) / nrSegms : 0, 0.70f);
|
|---|
| 1370 |
|
|---|
| 1371 | // http://www.nstb.tc.faa.gov/reports/PAN94_0716.pdf#page=22
|
|---|
| 1372 | // Global Average Position Domain Accuracy, typical -> not worst case !
|
|---|
| 1373 | // < 4.218 m Vertical
|
|---|
| 1374 | // < 2.168 m Horizontal
|
|---|
| 1375 | final double pixelRmsX = (100 / mv.getDist100Pixel()) * 2.168;
|
|---|
| 1376 | final double pixelRmsY = (100 / mv.getDist100Pixel()) * 4.218;
|
|---|
| 1377 |
|
|---|
| 1378 | Point lastPnt = null;
|
|---|
| 1379 |
|
|---|
| 1380 | // for all points, draw single lines
|
|---|
| 1381 | for (WayPoint trkPnt : listSegm) {
|
|---|
| 1382 |
|
|---|
| 1383 | // get transformed coordinates
|
|---|
| 1384 | final Point paintPnt = mv.getPoint(trkPnt);
|
|---|
| 1385 |
|
|---|
| 1386 | // end of line segment or end of list reached
|
|---|
| 1387 | if (trkPnt.drawLine && null != lastPnt) {
|
|---|
| 1388 | drawHeatSurfaceLine(gB, paintPnt, lastPnt, drawSize, pixelRmsX, pixelRmsY, randomDrop);
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | // remember
|
|---|
| 1392 | lastPnt = paintPnt;
|
|---|
| 1393 | }
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | /**
|
|---|
| 1397 | * Draw a dotted surface line
|
|---|
| 1398 | *
|
|---|
| 1399 | * @param g the common draw object to use
|
|---|
| 1400 | * @param fromPnt start point
|
|---|
| 1401 | * @param toPnt end point
|
|---|
| 1402 | * @param drawSize size of draw elements
|
|---|
| 1403 | * @param rmsSizeX RMS size of circle for X (width)
|
|---|
| 1404 | * @param rmsSizeY RMS size of circle for Y (height)
|
|---|
| 1405 | * @param dropRate Pixel render drop rate
|
|---|
| 1406 | */
|
|---|
| 1407 | private static void drawHeatSurfaceLine(Graphics2D g,
|
|---|
| 1408 | Point fromPnt, Point toPnt, int drawSize, double rmsSizeX, double rmsSizeY, double dropRate) {
|
|---|
| 1409 |
|
|---|
| 1410 | // collect frequently used items
|
|---|
| 1411 | final long fromX = (long) fromPnt.getX(); final long deltaX = (long) (toPnt.getX() - fromX);
|
|---|
| 1412 | final long fromY = (long) fromPnt.getY(); final long deltaY = (long) (toPnt.getY() - fromY);
|
|---|
| 1413 |
|
|---|
| 1414 | // use same random values for each point
|
|---|
| 1415 | final Random heatMapRandom = new Random(fromX+fromY+deltaX+deltaY);
|
|---|
| 1416 |
|
|---|
| 1417 | // cache distance between start and end point
|
|---|
| 1418 | final int dist = (int) Math.abs(fromPnt.distance(toPnt));
|
|---|
| 1419 |
|
|---|
| 1420 | // number of increment ( fill wide distance tracks )
|
|---|
| 1421 | double scaleStep = Math.max(1.0f / dist, dist > 100 ? 0.10f : 0.20f);
|
|---|
| 1422 |
|
|---|
| 1423 | // number of additional random points
|
|---|
| 1424 | int rounds = Math.min(drawSize/2, 1)+1;
|
|---|
| 1425 |
|
|---|
| 1426 | // decrease random noise at high drop rate ( more accurate draw of fewer points )
|
|---|
| 1427 | rmsSizeX *= (1.0d - dropRate);
|
|---|
| 1428 | rmsSizeY *= (1.0d - dropRate);
|
|---|
| 1429 |
|
|---|
| 1430 | double scaleVal = 0;
|
|---|
| 1431 |
|
|---|
| 1432 | // interpolate line draw ( needs separate point instead of line )
|
|---|
| 1433 | while (scaleVal < (1.0d-0.0001d)) {
|
|---|
| 1434 |
|
|---|
| 1435 | // get position
|
|---|
| 1436 | final double pntX = fromX + scaleVal * deltaX;
|
|---|
| 1437 | final double pntY = fromY + scaleVal * deltaY;
|
|---|
| 1438 |
|
|---|
| 1439 | // add random distribution around sampled point
|
|---|
| 1440 | for (int k = 0; k < rounds; k++) {
|
|---|
| 1441 |
|
|---|
| 1442 | // add error distribution, first point with less error
|
|---|
| 1443 | int x = (int) (pntX + heatMapRandom.nextGaussian() * (k > 0 ? rmsSizeX : rmsSizeX/4));
|
|---|
| 1444 | int y = (int) (pntY + heatMapRandom.nextGaussian() * (k > 0 ? rmsSizeY : rmsSizeY/4));
|
|---|
| 1445 |
|
|---|
| 1446 | // draw it, even drop is requested
|
|---|
| 1447 | if (heatMapRandom.nextDouble() >= dropRate) {
|
|---|
| 1448 | g.fillRect(x-drawSize, y-drawSize, drawSize, drawSize);
|
|---|
| 1449 | }
|
|---|
| 1450 | }
|
|---|
| 1451 | scaleVal += scaleStep;
|
|---|
| 1452 | }
|
|---|
| 1453 | }
|
|---|
| 1454 |
|
|---|
| 1455 | /**
|
|---|
| 1456 | * Apply default color configuration to way segments
|
|---|
| 1457 | * @param visibleSegments segments visible in the current scope of mv
|
|---|
| 1458 | */
|
|---|
| 1459 | private void fixColors(List<WayPoint> visibleSegments) {
|
|---|
| 1460 | for (WayPoint trkPnt : visibleSegments) {
|
|---|
| 1461 | if (trkPnt.customColoring == null) {
|
|---|
| 1462 | trkPnt.customColoring = neutralColor;
|
|---|
| 1463 | }
|
|---|
| 1464 | }
|
|---|
| 1465 | }
|
|---|
| 1466 |
|
|---|
| 1467 | /**
|
|---|
| 1468 | * Check cache validity set necessary flags
|
|---|
| 1469 | */
|
|---|
| 1470 | private void checkCache() {
|
|---|
| 1471 | // CHECKSTYLE.OFF: BooleanExpressionComplexity
|
|---|
| 1472 | if ((computeCacheMaxLineLengthUsed != maxLineLength)
|
|---|
| 1473 | || (computeCacheColored != colored)
|
|---|
| 1474 | || (computeCacheVelocityTune != velocityTune)
|
|---|
| 1475 | || (computeCacheColorDynamic != colorModeDynamic)
|
|---|
| 1476 | || (computeCacheHeatMapDrawColorTableIdx != heatMapDrawColorTableIdx)
|
|---|
| 1477 | || !Objects.equals(neutralColor, computeCacheColorUsed)
|
|---|
| 1478 | || (computeCacheHeatMapDrawPointMode != heatMapDrawPointMode)
|
|---|
| 1479 | || (computeCacheHeatMapDrawGain != heatMapDrawGain)
|
|---|
| 1480 | || (computeCacheHeatMapDrawLowerLimit != heatMapDrawLowerLimit)
|
|---|
| 1481 | ) {
|
|---|
| 1482 | // CHECKSTYLE.ON: BooleanExpressionComplexity
|
|---|
| 1483 | computeCacheMaxLineLengthUsed = maxLineLength;
|
|---|
| 1484 | computeCacheInSync = false;
|
|---|
| 1485 | computeCacheColorUsed = neutralColor;
|
|---|
| 1486 | computeCacheColored = colored;
|
|---|
| 1487 | computeCacheVelocityTune = velocityTune;
|
|---|
| 1488 | computeCacheColorDynamic = colorModeDynamic;
|
|---|
| 1489 | computeCacheHeatMapDrawColorTableIdx = heatMapDrawColorTableIdx;
|
|---|
| 1490 | computeCacheHeatMapDrawPointMode = heatMapDrawPointMode;
|
|---|
| 1491 | computeCacheHeatMapDrawGain = heatMapDrawGain;
|
|---|
| 1492 | computeCacheHeatMapDrawLowerLimit = heatMapDrawLowerLimit;
|
|---|
| 1493 | }
|
|---|
| 1494 | }
|
|---|
| 1495 |
|
|---|
| 1496 | /**
|
|---|
| 1497 | * callback when data is changed, invalidate cached configuration parameters
|
|---|
| 1498 | */
|
|---|
| 1499 | @Override
|
|---|
| 1500 | public void gpxDataChanged(GpxDataChangeEvent e) {
|
|---|
| 1501 | computeCacheInSync = false;
|
|---|
| 1502 | }
|
|---|
| 1503 |
|
|---|
| 1504 | /**
|
|---|
| 1505 | * Draw all GPX arrays
|
|---|
| 1506 | * @param g the common draw object to use
|
|---|
| 1507 | * @param mv the meta data to current displayed area
|
|---|
| 1508 | */
|
|---|
| 1509 | public void drawColorBar(Graphics2D g, MapView mv) {
|
|---|
| 1510 | int w = mv.getWidth();
|
|---|
| 1511 |
|
|---|
| 1512 | // set do default
|
|---|
| 1513 | g.setComposite(AlphaComposite.SrcOver.derive(1.00f));
|
|---|
| 1514 |
|
|---|
| 1515 | if (colored == ColorMode.HDOP) {
|
|---|
| 1516 | hdopScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
|
|---|
| 1517 | } else if (colored == ColorMode.QUALITY) {
|
|---|
| 1518 | qualityScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
|
|---|
| 1519 | } else if (colored == ColorMode.VELOCITY) {
|
|---|
| 1520 | SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
|
|---|
| 1521 | velocityScale.drawColorBar(g, w-30, 50, 20, 100, som.speedValue);
|
|---|
| 1522 | } else if (colored == ColorMode.DIRECTION) {
|
|---|
| 1523 | directionScale.drawColorBar(g, w-30, 50, 20, 100, 180.0/Math.PI);
|
|---|
| 1524 | }
|
|---|
| 1525 | }
|
|---|
| 1526 |
|
|---|
| 1527 | @Override
|
|---|
| 1528 | public void paintableInvalidated(PaintableInvalidationEvent event) {
|
|---|
| 1529 | gpxLayerInvalidated = true;
|
|---|
| 1530 | }
|
|---|
| 1531 |
|
|---|
| 1532 | @Override
|
|---|
| 1533 | public void detachFromMapView(MapViewEvent event) {
|
|---|
| 1534 | SystemOfMeasurement.removeSoMChangeListener(this);
|
|---|
| 1535 | layer.removeInvalidationListener(this);
|
|---|
| 1536 | data.removeChangeListener(this);
|
|---|
| 1537 | }
|
|---|
| 1538 | }
|
|---|