| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.io;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.BufferedReader;
|
|---|
| 5 | import java.io.File;
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.io.InputStream;
|
|---|
| 8 | import java.io.InputStreamReader;
|
|---|
| 9 | import java.util.ArrayList;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 12 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 13 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
|---|
| 14 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 15 |
|
|---|
| 16 | public class NmeaReader {
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * The resulting gpx data
|
|---|
| 20 | */
|
|---|
| 21 | public GpxData data;
|
|---|
| 22 | public enum state { init, metadata, wpt, rte, trk, ext, author, link, trkseg }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Parse the input stream and store the result in trackData and markerData
|
|---|
| 26 | *
|
|---|
| 27 | * @param relativeMarkerPath The directory to use as relative path for all <wpt>
|
|---|
| 28 | * marker tags. Maybe <code>null</code>, in which case no relative urls are constructed for the markers.
|
|---|
| 29 | */
|
|---|
| 30 | public NmeaReader(InputStream source, File relativeMarkerPath) throws IOException {
|
|---|
| 31 |
|
|---|
| 32 | BufferedReader reader = new BufferedReader( new InputStreamReader(source));
|
|---|
| 33 | ArrayList<WayPoint> waypoints = new ArrayList<WayPoint>();
|
|---|
| 34 | int latdeg,londeg;
|
|---|
| 35 | double latmin,lonmin;
|
|---|
| 36 | LatLon ll;
|
|---|
| 37 | String line=null;
|
|---|
| 38 | while((line = reader.readLine())!=null){
|
|---|
| 39 | if(line.startsWith("$GPRMC")){
|
|---|
| 40 | // Parse in:
|
|---|
| 41 | /* GPRMC,191410,A,4735.5634,N,00739.3538,E,0.0,0.0,181102,0.4,E,A*19
|
|---|
| 42 | ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
|
|---|
| 43 | | | | | | | | | | |
|
|---|
| 44 | | | | | | | | | | Checksum
|
|---|
| 45 | | | | | | | | | |
|
|---|
| 46 | | | | | | | | | Mode (A,D,E,N,S)
|
|---|
| 47 | | | | | | | | |
|
|---|
| 48 | | | | | | | | magnetic
|
|---|
| 49 | | | | | | | | Declination 0.4° E
|
|---|
| 50 | | | | | | | |
|
|---|
| 51 | | | | | | | Datum: 18.11.02
|
|---|
| 52 | | | | | | |
|
|---|
| 53 | | | | | | Heading (without motion: 0)
|
|---|
| 54 | | | | | |
|
|---|
| 55 | | | | | Speed over ground (Knots)
|
|---|
| 56 | | | | |
|
|---|
| 57 | | | | longitude 007° 39.3538' East
|
|---|
| 58 | | | |
|
|---|
| 59 | | | latitude 47° 35.5634' North
|
|---|
| 60 | | |
|
|---|
| 61 | | Receiver warning, A = Data OK, V = Warning
|
|---|
| 62 | |
|
|---|
| 63 | 19:14:10 UTC time */
|
|---|
| 64 | try {
|
|---|
| 65 | String[] parts = line.split(",");
|
|---|
| 66 | if("A".equals(parts[2])){
|
|---|
| 67 | latdeg=Integer.parseInt(parts[3].substring(0,2));
|
|---|
| 68 | if("S".equals(parts[4]))
|
|---|
| 69 | latdeg=-latdeg;
|
|---|
| 70 | latmin=Double.parseDouble(parts[3].substring(2));
|
|---|
| 71 |
|
|---|
| 72 | londeg=Integer.parseInt(parts[5].substring(0,3));
|
|---|
| 73 | if("W".equals(parts[6]))
|
|---|
| 74 | londeg=-londeg;
|
|---|
| 75 | lonmin=Double.parseDouble(parts[5].substring(3));
|
|---|
| 76 |
|
|---|
| 77 | ll=new LatLon(latdeg+latmin/60,londeg+lonmin/60);
|
|---|
| 78 | WayPoint wp = new WayPoint(ll);
|
|---|
| 79 | waypoints.add(wp);
|
|---|
| 80 | }
|
|---|
| 81 | } catch (NumberFormatException e) {
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | GpxTrack track = new GpxTrack();
|
|---|
| 86 | track.trackSegs.add(waypoints);
|
|---|
| 87 | ArrayList<GpxTrack>tracks=new ArrayList<GpxTrack>();
|
|---|
| 88 | tracks.add(track);
|
|---|
| 89 | data=new GpxData();
|
|---|
| 90 | data.tracks=tracks;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|