1 | package org.openstreetmap.josm.gui.layer;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.beans.PropertyChangeEvent;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.Bounds;
|
---|
14 | import org.openstreetmap.josm.data.GeoPoint;
|
---|
15 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
16 | import org.openstreetmap.josm.gui.ImageProvider;
|
---|
17 | import org.openstreetmap.josm.gui.MapView;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A layer holding data from a gps source.
|
---|
21 | * The data is read only.
|
---|
22 | *
|
---|
23 | * @author imi
|
---|
24 | */
|
---|
25 | public class RawGpsDataLayer extends Layer {
|
---|
26 |
|
---|
27 | private static Icon icon;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A list of tracks which containing a list of points.
|
---|
31 | */
|
---|
32 | private final Collection<Collection<GeoPoint>> data;
|
---|
33 |
|
---|
34 | public RawGpsDataLayer(Collection<Collection<GeoPoint>> data, String name) {
|
---|
35 | super(name);
|
---|
36 | this.data = data;
|
---|
37 | Main.pref.addPropertyChangeListener(new PropertyChangeListener(){
|
---|
38 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
39 | if (Main.main.getMapFrame() == null) {
|
---|
40 | Main.pref.removePropertyChangeListener(this);
|
---|
41 | return;
|
---|
42 | }
|
---|
43 | if (evt.getPropertyName().equals("drawRawGpsLines") ||
|
---|
44 | evt.getPropertyName().equals("drawRawGpsLines"))
|
---|
45 | Main.main.getMapFrame().repaint();
|
---|
46 | }
|
---|
47 | });
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Return a static icon.
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public Icon getIcon() {
|
---|
55 | if (icon == null)
|
---|
56 | icon = ImageProvider.get("layer", "rawgps");
|
---|
57 | return icon;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void paint(Graphics g, MapView mv) {
|
---|
62 | g.setColor(Color.GRAY);
|
---|
63 | Point old = null;
|
---|
64 | for (Collection<GeoPoint> c : data) {
|
---|
65 | if (!Main.pref.isForceRawGpsLines())
|
---|
66 | old = null;
|
---|
67 | for (GeoPoint p : c) {
|
---|
68 | Point screen = mv.getScreenPoint(p);
|
---|
69 | if (Main.pref.isDrawRawGpsLines() && old != null) {
|
---|
70 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
71 | } else {
|
---|
72 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
73 | old = screen;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String getToolTipText() {
|
---|
81 | return data.size()+" tracks.";
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void mergeFrom(Layer from) {
|
---|
86 | RawGpsDataLayer layer = (RawGpsDataLayer)from;
|
---|
87 | data.addAll(layer.data);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public boolean isMergable(Layer other) {
|
---|
92 | return other instanceof RawGpsDataLayer;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public Bounds getBoundsLatLon() {
|
---|
97 | GeoPoint min = null;
|
---|
98 | GeoPoint max = null;
|
---|
99 | for (Collection<GeoPoint> c : data) {
|
---|
100 | for (GeoPoint p : c) {
|
---|
101 | if (min == null) {
|
---|
102 | min = p.clone();
|
---|
103 | max = p.clone();
|
---|
104 | } else {
|
---|
105 | min.lat = Math.min(min.lat, p.lat);
|
---|
106 | min.lon = Math.min(min.lon, p.lon);
|
---|
107 | max.lat = Math.max(max.lat, p.lat);
|
---|
108 | max.lon = Math.max(max.lon, p.lon);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if (min == null)
|
---|
113 | return null;
|
---|
114 | return new Bounds(min, max);
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public Bounds getBoundsXY() {
|
---|
119 | GeoPoint min = null;
|
---|
120 | GeoPoint max = null;
|
---|
121 | for (Collection<GeoPoint> c : data) {
|
---|
122 | for (GeoPoint p : c) {
|
---|
123 | if (min == null) {
|
---|
124 | min = p.clone();
|
---|
125 | max = p.clone();
|
---|
126 | } else {
|
---|
127 | min.x = Math.min(min.x, p.x);
|
---|
128 | min.y = Math.min(min.y, p.y);
|
---|
129 | max.x = Math.max(max.x, p.x);
|
---|
130 | max.y = Math.max(max.y, p.y);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if (min == null)
|
---|
135 | return null;
|
---|
136 | return new Bounds(min, max);
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void init(Projection projection) {
|
---|
141 | for (Collection<GeoPoint> c : data)
|
---|
142 | for (GeoPoint p : c)
|
---|
143 | projection.latlon2xy(p);
|
---|
144 | }
|
---|
145 | }
|
---|