source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java@ 7319

Last change on this file since 7319 was 7319, checked in by akks, 10 years ago

Add colorbar for active GPX layer, big GpxLayer class refactoring, see #5662
new classes GpxDrawHelper and ColorScale responsible for GPX drawing and coloring
move data-related methods to GpxData
remove WayPoint.customColoringTransparent from memory, calculate it at paint-time

File size: 22.9 KB
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui.layer.gpx;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.marktr;
7
8import java.awt.BasicStroke;
9import java.awt.Color;
10import java.awt.Graphics2D;
11import java.awt.Point;
12import java.awt.RenderingHints;
13import java.awt.Stroke;
14import java.util.Collection;
15import java.util.Date;
16import java.util.List;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.gpx.GpxData;
20import org.openstreetmap.josm.data.gpx.WayPoint;
21import org.openstreetmap.josm.gui.MapView;
22import org.openstreetmap.josm.tools.ColorScale;
23
24
25/**
26 * Class that helps to draw large set of GPS tracks with different colors and options
27 * @since 7319
28 */
29public class GpxDrawHelper {
30 private GpxData data;
31
32 // draw lines between points belonging to different segments
33 private boolean forceLines;
34 // draw direction arrows on the lines
35 private boolean direction;
36 /** don't draw lines if longer than x meters **/
37 private int lineWidth;
38 private int maxLineLength;
39 private boolean lines;
40 /** paint large dots for points **/
41 private boolean large;
42 private int largesize;
43 private boolean hdopCircle;
44 /** paint direction arrow with alternate math. may be faster **/
45 private boolean alternateDirection;
46 /** don't draw arrows nearer to each other than this **/
47 private int delta;
48 private double minTrackDurationForTimeColoring;
49
50 private int hdopfactor;
51
52 private static final double PHI = Math.toRadians(15);
53
54 //// Variables used only to check cache validity
55 private boolean computeCacheInSync = false;
56 private int computeCacheMaxLineLengthUsed;
57 private Color computeCacheColorUsed;
58 private boolean computeCacheColorDynamic;
59 private ColorMode computeCacheColored;
60 private int computeCacheColorTracksTune;
61
62 //// Color-related fields
63 /** Mode of the line coloring **/
64 private ColorMode colored;
65 /** max speed for coloring - allows to tweak line coloring for different speed levels. **/
66 private int colorTracksTune;
67 private boolean colorModeDynamic;
68 private Color neutralColor;
69 private int largePointAlpha;
70
71 // default access is used to allow changing from plugins
72 ColorScale velocityScale;
73 /** Colors (without custom alpha channel, if given) for HDOP painting. **/
74 ColorScale hdopScale;
75 ColorScale dateScale;
76 ColorScale directionScale;
77
78 /** Opacity for hdop points **/
79 private int hdopAlpha;
80
81
82 // lookup array to draw arrows without doing any math
83 private static final int ll0 = 9;
84 private static final int sl4 = 5;
85 private static final int sl9 = 3;
86 private static final int[][] dir = { { +sl4, +ll0, +ll0, +sl4 }, { -sl9, +ll0, +sl9, +ll0 }, { -ll0, +sl4, -sl4, +ll0 },
87 { -ll0, -sl9, -ll0, +sl9 }, { -sl4, -ll0, -ll0, -sl4 }, { +sl9, -ll0, -sl9, -ll0 },
88 { +ll0, -sl4, +sl4, -ll0 }, { +ll0, +sl9, +ll0, -sl9 }, { +sl4, +ll0, +ll0, +sl4 },
89 { -sl9, +ll0, +sl9, +ll0 }, { -ll0, +sl4, -sl4, +ll0 }, { -ll0, -sl9, -ll0, +sl9 } };
90
91 private void setupColors() {
92 hdopAlpha = Main.pref.getInteger("hdop.color.alpha", -1);
93 velocityScale = ColorScale.createHSBScale(256).addTitle(tr("Velocity, km/h"));
94 /** Colors (without custom alpha channel, if given) for HDOP painting. **/
95 hdopScale = ColorScale.createHSBScale(256).makeReversed().addTitle(tr("HDOP, m"));
96 dateScale = ColorScale.createHSBScale(256).addTitle(tr("Time"));
97 directionScale = ColorScale.createCyclicScale(256).setIntervalCount(4).addTitle(tr("Direction"));
98 }
99
100 /**
101 * Different color modes
102 */
103 public enum ColorMode {
104 NONE, VELOCITY, HDOP, DIRECTION, TIME
105 }
106
107 public GpxDrawHelper(GpxData gpxData) {
108 data = gpxData;
109 setupColors();
110 }
111
112 /**
113 * Get the default color for gps tracks for specified layer
114 * @param layerName name of the GpxLayer
115 * @param ignoreCustom do not use preferences
116 * @return the color or null if the color is not constant
117 */
118 public Color getColor(String layerName, boolean ignoreCustom) {
119 Color c = Main.pref.getColor(marktr("gps point"), "layer " + layerName, Color.gray);
120 return ignoreCustom || getColorMode(layerName) == ColorMode.NONE ? c : null;
121 }
122
123 /**
124 * Read coloring mode for specified layer from preferences
125 * @param layerName name of the GpxLayer
126 * @return coloting mode
127 */
128 public ColorMode getColorMode(String layerName) {
129 try {
130 int i=Main.pref.getInteger("draw.rawgps.colors", "layer " + layerName, 0);
131 return ColorMode.values()[i];
132 } catch (Exception e) {
133 Main.warn(e);
134 }
135 return ColorMode.NONE;
136 }
137
138 /** Reads generic color from preferences (usually gray)
139 * @return the color
140 **/
141 public static Color getGenericColor() {
142 return Main.pref.getColor(marktr("gps point"), Color.gray);
143 }
144
145 /**
146 * Read all drawing-related settings from preferences
147 * @param layerName layer name used to access its specific preferences
148 **/
149 public void readPreferences(String layerName) {
150 String spec = "layer " + layerName;
151 forceLines = Main.pref.getBoolean("draw.rawgps.lines.force", spec, false);
152 direction = Main.pref.getBoolean("draw.rawgps.direction", spec, false);
153 lineWidth = Main.pref.getInteger("draw.rawgps.linewidth", spec, 0);
154
155 if (!data.fromServer) {
156 maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length.local", spec, -1);
157 lines = Main.pref.getBoolean("draw.rawgps.lines.local", spec, true);
158 } else {
159 maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length", spec, 200);
160 lines = Main.pref.getBoolean("draw.rawgps.lines", spec, true);
161 }
162 large = Main.pref.getBoolean("draw.rawgps.large", spec, false);
163 largesize = Main.pref.getInteger("draw.rawgps.large.size", spec, 3);
164 hdopCircle = Main.pref.getBoolean("draw.rawgps.hdopcircle", spec, false);
165 colored = getColorMode(layerName);
166 alternateDirection = Main.pref.getBoolean("draw.rawgps.alternatedirection", spec, false);
167 delta = Main.pref.getInteger("draw.rawgps.min-arrow-distance", spec, 40);
168 colorTracksTune = Main.pref.getInteger("draw.rawgps.colorTracksTune", spec, 45);
169 colorModeDynamic = Main.pref.getBoolean("draw.rawgps.colors.dynamic", spec, false);
170 hdopfactor = Main.pref.getInteger("hdop.factor", 25);
171 minTrackDurationForTimeColoring = Main.pref.getInteger("draw.rawgps.date-coloring-min-dt", 60);
172 largePointAlpha = Main.pref.getInteger("draw.rawgps.large.alpha", -1) & 0xFF;
173
174 neutralColor = getColor(layerName, true);
175 velocityScale.setNoDataColor(neutralColor);
176 dateScale.setNoDataColor(neutralColor);
177 hdopScale.setNoDataColor(neutralColor);
178 directionScale.setNoDataColor(neutralColor);
179
180 largesize += lineWidth;
181 }
182
183
184 public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
185
186 checkCache();
187
188 // STEP 2b - RE-COMPUTE CACHE DATA *********************
189 if (!computeCacheInSync) { // don't compute if the cache is good
190 calculateColors();
191 }
192
193 Stroke storedStroke = g.getStroke();
194
195 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
196 Main.pref.getBoolean("mappaint.gpx.use-antialiasing", false) ?
197 RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
198
199 if(lineWidth != 0) {
200 g.setStroke(new BasicStroke(lineWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
201 }
202 drawLines(g, mv, visibleSegments);
203 drawArrows(g, mv, visibleSegments);
204 drawPoints(g, mv, visibleSegments);
205 if(lineWidth != 0) {
206 g.setStroke(storedStroke);
207 }
208 }
209
210 public void calculateColors() {
211 double minval = +1e10;
212 double maxval = -1e10;
213 WayPoint oldWp = null;
214
215 if (colorModeDynamic) {
216 if (colored == ColorMode.VELOCITY) {
217 for (Collection<WayPoint> segment : data.getLinesIterable(null)) {
218 if(!forceLines) {
219 oldWp = null;
220 }
221 for (WayPoint trkPnt : segment) {
222 LatLon c = trkPnt.getCoor();
223 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
224 continue;
225 }
226 if (oldWp != null && trkPnt.time > oldWp.time) {
227 double vel = c.greatCircleDistance(oldWp.getCoor())
228 / (trkPnt.time - oldWp.time);
229 if(vel > maxval) {
230 maxval = vel;
231 }
232 if(vel < minval) {
233 minval = vel;
234 }
235 }
236 oldWp = trkPnt;
237 }
238 }
239 if (minval >= maxval) {
240 velocityScale.setRange(0, 120/3.6);
241 } else {
242 velocityScale.setRange(minval, maxval);
243 }
244 } else if (colored == ColorMode.HDOP) {
245 for (Collection<WayPoint> segment : data.getLinesIterable(null)) {
246 for (WayPoint trkPnt : segment) {
247 Object val = trkPnt.attr.get("hdop");
248 if (val != null) {
249 double hdop = ((Float) val).doubleValue();
250 if(hdop > maxval) {
251 maxval = hdop;
252 }
253 if(hdop < minval) {
254 minval = hdop;
255 }
256 }
257 }
258 }
259 if (minval >= maxval) {
260 hdopScale.setRange(0, 100);
261 } else {
262 hdopScale.setRange(minval, maxval);
263 }
264 }
265 oldWp = null;
266 } else { // color mode not dynamic
267 velocityScale.setRange(0, colorTracksTune);
268 hdopScale.setRange(0, 1.0/hdopfactor);
269 }
270 double now = System.currentTimeMillis()/1000.0;
271 if (colored == ColorMode.TIME) {
272 Date[] bounds = data.getMinMaxTimeForAllTracks();
273 if (bounds!=null) {
274 minval = bounds[0].getTime()/1000.0;
275 maxval = bounds[1].getTime()/1000.0;
276 } else {
277 minval = 0; maxval=now;
278 }
279 dateScale.setRange(minval, maxval);
280 }
281
282
283 // Now the colors for all the points will be assigned
284 for (Collection<WayPoint> segment : data.getLinesIterable(null)) {
285 if (!forceLines) { // don't draw lines between segments, unless forced to
286 oldWp = null;
287 }
288 for (WayPoint trkPnt : segment) {
289 LatLon c = trkPnt.getCoor();
290 trkPnt.customColoring = neutralColor;
291 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
292 continue;
293 }
294 // now we are sure some color will be assigned
295 Color color = null;
296
297 if (colored == ColorMode.HDOP) {
298 Float hdop = ((Float) trkPnt.attr.get("hdop"));
299 color = hdopScale.getColor(hdop);
300 }
301 if (oldWp != null) { // other coloring modes need segment for calcuation
302 double dist = c.greatCircleDistance(oldWp.getCoor());
303 boolean noDraw=false;
304 switch (colored) {
305 case VELOCITY:
306 double dtime = trkPnt.time - oldWp.time;
307 if(dtime > 0) {
308 color = velocityScale.getColor(dist / dtime);
309 } else {
310 color = velocityScale.getNoDataColor();
311 }
312 break;
313 case DIRECTION:
314 double dirColor = oldWp.getCoor().heading(trkPnt.getCoor());
315 color = directionScale.getColor(dirColor);
316 break;
317 case TIME:
318 double t=trkPnt.time;
319 if (t > 0 && t <= now && maxval - minval > minTrackDurationForTimeColoring) { // skip bad timestamps and very short tracks
320 color = dateScale.getColor(t);
321 } else {
322 color = dateScale.getNoDataColor();
323 }
324 break;
325 }
326 if (!noDraw && (maxLineLength == -1 || dist <= maxLineLength)) {
327 trkPnt.drawLine = true;
328 trkPnt.dir = (int) oldWp.getCoor().heading(trkPnt.getCoor());
329 } else {
330 trkPnt.drawLine = false;
331 }
332 } else { // make sure we reset outdated data
333 trkPnt.drawLine = false;
334 color = neutralColor;
335 }
336
337 trkPnt.customColoring = color;
338 oldWp = trkPnt;
339 }
340 }
341
342 computeCacheInSync = true;
343 }
344
345
346
347 private void drawLines(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
348 if (lines) {
349 Point old = null;
350 for (WayPoint trkPnt : visibleSegments) {
351 LatLon c = trkPnt.getCoor();
352 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
353 continue;
354 }
355 Point screen = mv.getPoint(trkPnt.getEastNorth());
356 if (trkPnt.drawLine) {
357 // skip points that are on the same screenposition
358 if (old != null && ((old.x != screen.x) || (old.y != screen.y))) {
359 g.setColor(trkPnt.customColoring);
360 g.drawLine(old.x, old.y, screen.x, screen.y);
361 }
362 }
363 old = screen;
364 }
365 }
366 }
367
368 private void drawArrows(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
369 /****************************************************************
370 ********** STEP 3b - DRAW NICE ARROWS **************************
371 ****************************************************************/
372 if (lines && direction && !alternateDirection) {
373 Point old = null;
374 Point oldA = null; // last arrow painted
375 for (WayPoint trkPnt : visibleSegments) {
376 LatLon c = trkPnt.getCoor();
377 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
378 continue;
379 }
380 if (trkPnt.drawLine) {
381 Point screen = mv.getPoint(trkPnt.getEastNorth());
382 // skip points that are on the same screenposition
383 if (old != null
384 && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
385 || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
386 g.setColor(trkPnt.customColoring);
387 double t = Math.atan2(screen.y - old.y, screen.x - old.x) + Math.PI;
388 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
389 (int) (screen.y + 10 * Math.sin(t - PHI)));
390 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
391 (int) (screen.y + 10 * Math.sin(t + PHI)));
392 oldA = screen;
393 }
394 old = screen;
395 }
396 } // end for trkpnt
397 }
398
399 /****************************************************************
400 ********** STEP 3c - DRAW FAST ARROWS **************************
401 ****************************************************************/
402 if (lines && direction && alternateDirection) {
403 Point old = null;
404 Point oldA = null; // last arrow painted
405 for (WayPoint trkPnt : visibleSegments) {
406 LatLon c = trkPnt.getCoor();
407 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
408 continue;
409 }
410 if (trkPnt.drawLine) {
411 Point screen = mv.getPoint(trkPnt.getEastNorth());
412 // skip points that are on the same screenposition
413 if (old != null
414 && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
415 || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
416 g.setColor(trkPnt.customColoring);
417 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
418 + dir[trkPnt.dir][1]);
419 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
420 + dir[trkPnt.dir][3]);
421 oldA = screen;
422 }
423 old = screen;
424 }
425 } // end for trkpnt
426 }
427 }
428
429 private void drawPoints(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
430 /****************************************************************
431 ********** STEP 3d - DRAW LARGE POINTS AND HDOP CIRCLE *********
432 ****************************************************************/
433 if (large || hdopCircle) {
434 final int halfSize = largesize/2;
435 for (WayPoint trkPnt : visibleSegments) {
436 LatLon c = trkPnt.getCoor();
437 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
438 continue;
439 }
440 Point screen = mv.getPoint(trkPnt.getEastNorth());
441
442
443 if (hdopCircle && trkPnt.attr.get("hdop") != null) {
444 // hdop value
445 float hdop = ((Float)trkPnt.attr.get("hdop"));
446 if (hdop < 0) {
447 hdop = 0;
448 }
449 Color customColoringTransparent = hdopAlpha<0 ? trkPnt.customColoring:
450 new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | hdopAlpha<<24, true);
451 g.setColor(customColoringTransparent);
452 // hdop cirles
453 int hdopp = mv.getPoint(new LatLon(trkPnt.getCoor().lat(), trkPnt.getCoor().lon() + 2*6*hdop*360/40000000)).x - screen.x;
454 g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
455 }
456 if (large) {
457 // color the large GPS points like the gps lines
458 Color customColoringTransparent = largePointAlpha<0 ? trkPnt.customColoring:
459 new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | largePointAlpha<<24, true);
460
461 g.setColor(customColoringTransparent);
462 g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
463 }
464 } // end for trkpnt
465 } // end if large || hdopcircle
466
467 /****************************************************************
468 ********** STEP 3e - DRAW SMALL POINTS FOR LINES ***************
469 ****************************************************************/
470 if (!large && lines) {
471 g.setColor(neutralColor);
472 for (WayPoint trkPnt : visibleSegments) {
473 LatLon c = trkPnt.getCoor();
474 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
475 continue;
476 }
477 if (!trkPnt.drawLine) {
478 Point screen = mv.getPoint(trkPnt.getEastNorth());
479 g.drawRect(screen.x, screen.y, 0, 0);
480 }
481 } // end for trkpnt
482 } // end if large
483
484 /****************************************************************
485 ********** STEP 3f - DRAW SMALL POINTS INSTEAD OF LINES ********
486 ****************************************************************/
487 if (!large && !lines) {
488 g.setColor(neutralColor);
489 for (WayPoint trkPnt : visibleSegments) {
490 LatLon c = trkPnt.getCoor();
491 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
492 continue;
493 }
494 Point screen = mv.getPoint(trkPnt.getEastNorth());
495 g.setColor(trkPnt.customColoring);
496 g.drawRect(screen.x, screen.y, 0, 0);
497 } // end for trkpnt
498 } // end if large
499 }
500
501 /**
502 * Check cache validity set necessary flags
503 */
504 private void checkCache() {
505 if ((computeCacheMaxLineLengthUsed != maxLineLength) || (!neutralColor.equals(computeCacheColorUsed))
506 || (computeCacheColored != colored) || (computeCacheColorTracksTune != colorTracksTune)
507 || (computeCacheColorDynamic != colorModeDynamic)) {
508 computeCacheMaxLineLengthUsed = maxLineLength;
509 computeCacheInSync = false;
510 computeCacheColorUsed = neutralColor;
511 computeCacheColored = colored;
512 computeCacheColorTracksTune = colorTracksTune;
513 computeCacheColorDynamic = colorModeDynamic;
514 }
515 }
516
517 public void dataChanged() {
518 computeCacheInSync = false;
519 }
520
521 public void drawColorBar(Graphics2D g, MapView mv) {
522 int w = mv.getWidth();
523 int h = mv.getHeight();
524 if (colored == ColorMode.HDOP) {
525 hdopScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
526 } else if (colored == ColorMode.VELOCITY) {
527 velocityScale.drawColorBar(g, w-30, 50, 20, 100, 3.6);
528 } else if (colored == ColorMode.DIRECTION) {
529 directionScale.drawColorBar(g, w-30, 50, 20, 100, 180.0/Math.PI);
530 }
531 }
532
533}
Note: See TracBrowser for help on using the repository browser.