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

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

sonar - Immutable Field

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