1 | package livegps;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.beans.PropertyChangeEvent;
|
---|
9 | import java.beans.PropertyChangeListener;
|
---|
10 | import java.text.SimpleDateFormat;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Date;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
18 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
19 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
20 | import org.openstreetmap.josm.gui.MapView;
|
---|
21 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
22 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
23 |
|
---|
24 | public class LiveGpsLayer extends GpxLayer implements PropertyChangeListener {
|
---|
25 | public static final String LAYER_NAME = tr("LiveGPS layer");
|
---|
26 | public static final String KEY_LIVEGPS_COLOR ="color.livegps.position";
|
---|
27 | LatLon lastPos;
|
---|
28 | WayPoint lastPoint;
|
---|
29 | GpxTrack trackBeingWritten;
|
---|
30 | Collection<WayPoint> trackSegment;
|
---|
31 | float speed;
|
---|
32 | float course;
|
---|
33 | String status;
|
---|
34 | //JLabel lbl;
|
---|
35 | boolean autocenter;
|
---|
36 | private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
|
---|
37 |
|
---|
38 | public LiveGpsLayer(GpxData data)
|
---|
39 | {
|
---|
40 | super (data, LAYER_NAME);
|
---|
41 | trackBeingWritten = new GpxTrack();
|
---|
42 | trackBeingWritten.attr.put("desc", "josm live gps");
|
---|
43 | trackSegment = new ArrayList<WayPoint>();
|
---|
44 | trackBeingWritten.trackSegs.add(trackSegment);
|
---|
45 | data.tracks.add(trackBeingWritten);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void setCurrentPosition(double lat, double lon)
|
---|
49 | {
|
---|
50 | //System.out.println("adding pos " + lat + "," + lon);
|
---|
51 | LatLon thisPos = new LatLon(lat, lon);
|
---|
52 | if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) {
|
---|
53 | // no change in position
|
---|
54 | // maybe show a "paused" cursor or some such
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | lastPos = thisPos;
|
---|
59 | lastPoint = new WayPoint(thisPos);
|
---|
60 | lastPoint.attr.put("time", dateFormat.format(new Date()));
|
---|
61 | // synchronize when adding data, as otherwise the autosave action
|
---|
62 | // needs concurrent access and this results in an exception!
|
---|
63 | synchronized (LiveGpsLock.class) {
|
---|
64 | trackSegment.add(lastPoint);
|
---|
65 | }
|
---|
66 | if (autocenter) {
|
---|
67 | center();
|
---|
68 | }
|
---|
69 |
|
---|
70 | //Main.map.repaint();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void center()
|
---|
74 | {
|
---|
75 | if (lastPoint != null)
|
---|
76 | Main.map.mapView.zoomTo(lastPoint.eastNorth, Main.map.mapView.getScale());
|
---|
77 | }
|
---|
78 |
|
---|
79 | // void setStatus(String status)
|
---|
80 | // {
|
---|
81 | // this.status = status;
|
---|
82 | // Main.map.repaint();
|
---|
83 | // System.out.println("LiveGps status: " + status);
|
---|
84 | // }
|
---|
85 |
|
---|
86 | void setSpeed(float metresPerSecond)
|
---|
87 | {
|
---|
88 | speed = metresPerSecond;
|
---|
89 | //Main.map.repaint();
|
---|
90 | }
|
---|
91 |
|
---|
92 | void setCourse(float degrees)
|
---|
93 | {
|
---|
94 | course = degrees;
|
---|
95 | //Main.map.repaint();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void setAutoCenter(boolean ac)
|
---|
99 | {
|
---|
100 | autocenter = ac;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override public void paint(Graphics g, MapView mv)
|
---|
104 | {
|
---|
105 | //System.out.println("in paint");
|
---|
106 | synchronized (LiveGpsLock.class) {
|
---|
107 | //System.out.println("in synced paint");
|
---|
108 | super.paint(g, mv);
|
---|
109 | // int statusHeight = 50;
|
---|
110 | // Rectangle mvs = mv.getBounds();
|
---|
111 | // mvs.y = mvs.y + mvs.height - statusHeight;
|
---|
112 | // mvs.height = statusHeight;
|
---|
113 | // g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.8f));
|
---|
114 | // g.fillRect(mvs.x, mvs.y, mvs.width, mvs.height);
|
---|
115 |
|
---|
116 | if (lastPoint != null)
|
---|
117 | {
|
---|
118 | Point screen = mv.getPoint(lastPoint.eastNorth);
|
---|
119 | g.setColor(Main.pref.getColor(KEY_LIVEGPS_COLOR, Color.RED));
|
---|
120 | g.drawOval(screen.x-10, screen.y-10,20,20);
|
---|
121 | g.drawOval(screen.x-9, screen.y-9,18,18);
|
---|
122 | }
|
---|
123 |
|
---|
124 | // lbl.setText("gpsd: "+status+" Speed: " + speed + " Course: "+course);
|
---|
125 | // lbl.setBounds(0, 0, mvs.width-10, mvs.height-10);
|
---|
126 | // Graphics sub = g.create(mvs.x+5, mvs.y+5, mvs.width-10, mvs.height-10);
|
---|
127 | // lbl.paint(sub);
|
---|
128 |
|
---|
129 | // if(status != null) {
|
---|
130 | // g.setColor(Color.WHITE);
|
---|
131 | // g.drawString("gpsd: " + status, 5, mv.getBounds().height - 15); // lower left corner
|
---|
132 | // }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* (non-Javadoc)
|
---|
137 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
|
---|
138 | */
|
---|
139 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
140 | if(!visible) {
|
---|
141 | return;
|
---|
142 | }
|
---|
143 | if("gpsdata".equals(evt.getPropertyName())) {
|
---|
144 | LiveGpsData data = (LiveGpsData) evt.getNewValue();
|
---|
145 | if(data.isFix()) {
|
---|
146 | setCurrentPosition(data.getLatitude(), data.getLongitude());
|
---|
147 | if(!Float.isNaN(data.getSpeed())) {
|
---|
148 | setSpeed(data.getSpeed());
|
---|
149 | }
|
---|
150 | if(!Float.isNaN(data.getCourse())) {
|
---|
151 | setCourse(data.getCourse());
|
---|
152 | }
|
---|
153 | Main.map.repaint();
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | }
|
---|