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

Last change on this file since 8510 was 8510, checked in by Don-vip, 10 years ago

checkstyle: enable relevant whitespace checks and fix them

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