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