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

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

see #10294: gpx single color mode was broke (regression of r7319)

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 if (color!=null) {
337 trkPnt.customColoring = color;
338 }
339 oldWp = trkPnt;
340 }
341 }
342
343 computeCacheInSync = true;
344 }
345
346
347
348 private void drawLines(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
349 if (lines) {
350 Point old = null;
351 for (WayPoint trkPnt : visibleSegments) {
352 LatLon c = trkPnt.getCoor();
353 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
354 continue;
355 }
356 Point screen = mv.getPoint(trkPnt.getEastNorth());
357 if (trkPnt.drawLine) {
358 // skip points that are on the same screenposition
359 if (old != null && ((old.x != screen.x) || (old.y != screen.y))) {
360 g.setColor(trkPnt.customColoring);
361 g.drawLine(old.x, old.y, screen.x, screen.y);
362 }
363 }
364 old = screen;
365 }
366 }
367 }
368
369 private void drawArrows(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
370 /****************************************************************
371 ********** STEP 3b - DRAW NICE ARROWS **************************
372 ****************************************************************/
373 if (lines && direction && !alternateDirection) {
374 Point old = null;
375 Point oldA = null; // last arrow painted
376 for (WayPoint trkPnt : visibleSegments) {
377 LatLon c = trkPnt.getCoor();
378 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
379 continue;
380 }
381 if (trkPnt.drawLine) {
382 Point screen = mv.getPoint(trkPnt.getEastNorth());
383 // skip points that are on the same screenposition
384 if (old != null
385 && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
386 || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
387 g.setColor(trkPnt.customColoring);
388 double t = Math.atan2(screen.y - old.y, screen.x - old.x) + Math.PI;
389 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
390 (int) (screen.y + 10 * Math.sin(t - PHI)));
391 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
392 (int) (screen.y + 10 * Math.sin(t + PHI)));
393 oldA = screen;
394 }
395 old = screen;
396 }
397 } // end for trkpnt
398 }
399
400 /****************************************************************
401 ********** STEP 3c - DRAW FAST ARROWS **************************
402 ****************************************************************/
403 if (lines && direction && alternateDirection) {
404 Point old = null;
405 Point oldA = null; // last arrow painted
406 for (WayPoint trkPnt : visibleSegments) {
407 LatLon c = trkPnt.getCoor();
408 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
409 continue;
410 }
411 if (trkPnt.drawLine) {
412 Point screen = mv.getPoint(trkPnt.getEastNorth());
413 // skip points that are on the same screenposition
414 if (old != null
415 && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
416 || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
417 g.setColor(trkPnt.customColoring);
418 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
419 + dir[trkPnt.dir][1]);
420 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
421 + dir[trkPnt.dir][3]);
422 oldA = screen;
423 }
424 old = screen;
425 }
426 } // end for trkpnt
427 }
428 }
429
430 private void drawPoints(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
431 /****************************************************************
432 ********** STEP 3d - DRAW LARGE POINTS AND HDOP CIRCLE *********
433 ****************************************************************/
434 if (large || hdopCircle) {
435 final int halfSize = largesize/2;
436 for (WayPoint trkPnt : visibleSegments) {
437 LatLon c = trkPnt.getCoor();
438 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
439 continue;
440 }
441 Point screen = mv.getPoint(trkPnt.getEastNorth());
442
443
444 if (hdopCircle && trkPnt.attr.get("hdop") != null) {
445 // hdop value
446 float hdop = ((Float)trkPnt.attr.get("hdop"));
447 if (hdop < 0) {
448 hdop = 0;
449 }
450 Color customColoringTransparent = hdopAlpha<0 ? trkPnt.customColoring:
451 new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | hdopAlpha<<24, true);
452 g.setColor(customColoringTransparent);
453 // hdop cirles
454 int hdopp = mv.getPoint(new LatLon(trkPnt.getCoor().lat(), trkPnt.getCoor().lon() + 2*6*hdop*360/40000000)).x - screen.x;
455 g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
456 }
457 if (large) {
458 // color the large GPS points like the gps lines
459 Color customColoringTransparent = largePointAlpha<0 ? trkPnt.customColoring:
460 new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | largePointAlpha<<24, true);
461
462 g.setColor(customColoringTransparent);
463 g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
464 }
465 } // end for trkpnt
466 } // end if large || hdopcircle
467
468 /****************************************************************
469 ********** STEP 3e - DRAW SMALL POINTS FOR LINES ***************
470 ****************************************************************/
471 if (!large && lines) {
472 g.setColor(neutralColor);
473 for (WayPoint trkPnt : visibleSegments) {
474 LatLon c = trkPnt.getCoor();
475 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
476 continue;
477 }
478 if (!trkPnt.drawLine) {
479 Point screen = mv.getPoint(trkPnt.getEastNorth());
480 g.drawRect(screen.x, screen.y, 0, 0);
481 }
482 } // end for trkpnt
483 } // end if large
484
485 /****************************************************************
486 ********** STEP 3f - DRAW SMALL POINTS INSTEAD OF LINES ********
487 ****************************************************************/
488 if (!large && !lines) {
489 g.setColor(neutralColor);
490 for (WayPoint trkPnt : visibleSegments) {
491 LatLon c = trkPnt.getCoor();
492 if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
493 continue;
494 }
495 Point screen = mv.getPoint(trkPnt.getEastNorth());
496 g.setColor(trkPnt.customColoring);
497 g.drawRect(screen.x, screen.y, 0, 0);
498 } // end for trkpnt
499 } // end if large
500 }
501
502 /**
503 * Check cache validity set necessary flags
504 */
505 private void checkCache() {
506 if ((computeCacheMaxLineLengthUsed != maxLineLength) || (!neutralColor.equals(computeCacheColorUsed))
507 || (computeCacheColored != colored) || (computeCacheColorTracksTune != colorTracksTune)
508 || (computeCacheColorDynamic != colorModeDynamic)) {
509 computeCacheMaxLineLengthUsed = maxLineLength;
510 computeCacheInSync = false;
511 computeCacheColorUsed = neutralColor;
512 computeCacheColored = colored;
513 computeCacheColorTracksTune = colorTracksTune;
514 computeCacheColorDynamic = colorModeDynamic;
515 }
516 }
517
518 public void dataChanged() {
519 computeCacheInSync = false;
520 }
521
522 public void drawColorBar(Graphics2D g, MapView mv) {
523 int w = mv.getWidth();
524 int h = mv.getHeight();
525 if (colored == ColorMode.HDOP) {
526 hdopScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
527 } else if (colored == ColorMode.VELOCITY) {
528 velocityScale.drawColorBar(g, w-30, 50, 20, 100, 3.6);
529 } else if (colored == ColorMode.DIRECTION) {
530 directionScale.drawColorBar(g, w-30, 50, 20, 100, 180.0/Math.PI);
531 }
532 }
533
534}
Note: See TracBrowser for help on using the repository browser.