source: josm/trunk/src/org/openstreetmap/josm/io/NmeaReader.java@ 999

Last change on this file since 999 was 999, checked in by stoecker, 16 years ago

close bug #1588, code cleanup by bruce89

File size: 4.9 KB
Line 
1//License: GPL. Copyright 2008 by Christoph Brill
2
3package org.openstreetmap.josm.io;
4
5import java.io.BufferedReader;
6import java.io.File;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.util.ArrayList;
11import java.util.Collection;
12
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.gpx.GpxData;
15import org.openstreetmap.josm.data.gpx.GpxTrack;
16import org.openstreetmap.josm.data.gpx.WayPoint;
17
18/**
19 * Read a nmea file. Based on information from
20 * http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm
21 *
22 * @author cbrill
23 */
24public class NmeaReader {
25
26 /** Handler for the different types that NMEA speaks. */
27 public static enum NMEA_TYPE {
28
29 /** RMC = recommended minimum sentence C. */
30 GPRMC("$GPRMC"),
31 /** GPS positions. */
32 GPGGA("$GPGGA"),
33 /** SA = satellites active. */
34 GPGSA("$GPGSA");
35
36 private final String type;
37
38 NMEA_TYPE(String type) {
39 this.type = type;
40 }
41
42 public String getType() {
43 return this.type;
44 }
45
46 public boolean equals(String type) {
47 return this.type.equals(type);
48 }
49 }
50
51 private static final int TYPE = 0;
52
53 // The following only applies to GPRMC
54 public static enum GPRMC {
55 TIME(1),
56 /** Warning from the receiver (A = data ok, V = warning) */
57 RECEIVER_WARNING(2),
58 WIDTH_NORTH(3), WIDTH_NORTH_NAME(4),
59 LENGTH_EAST(5), LENGTH_EAST_NAME(6),
60 /** Speed in knots */
61 SPEED(7), COURSE(8), DATE(9),
62 /** magnetic declination */
63 MAGNETIC_DECLINATION(10), UNKNOWN(11),
64 /**
65 * Mode (A = autonom; D = differential; E = estimated; N = not valid; S
66 * = simulated)
67 *
68 * @since NMEA 2.3
69 */
70 MODE(12);
71
72 public final int position;
73
74 GPRMC(int position) {
75 this.position = position;
76 }
77 }
78
79 // The following only applies to GPGGA
80 public static enum GPGGA {
81 TIME(1), LATITUDE(2), LATITUDE_NAME(3), LONGITUDE(4), LONGITUDE_NAME(5),
82 /**
83 * Quality (0 = invalid, 1 = GPS, 2 = DGPS, 6 = estimanted (@since NMEA
84 * 2.3))
85 */
86 QUALITY(6), SATELLITE_COUNT(7),
87 /** HDOP (horizontal dilution of precision) */
88 HDOP(8),
89 /** height above NN (above geoid) */
90 HEIGHT(9), HEIGHT_UNTIS(10),
91 /** height geoid - height ellipsoid (WGS84) */
92 HEIGHT_2(11), HEIGHT_2_UNTIS(12);
93
94 public final int position;
95
96 GPGGA(int position) {
97 this.position = position;
98 }
99 }
100
101 // The following only applies to GPGGA
102 public static enum GPGSA {
103 AUTOMATIC(1),
104 /** 1 = not fixed, 2 = 2D fixed, 3 = 3D fixed) */
105 FIX_TYPE(2),
106 // PRN numbers for max 12 satellites
107 PRN_1(3), PRN_2(4), PRN_3(5), PRN_4(6), PRN_5(7), PRN_6(8), PRN_7(9), PRN_8(
108 10), PRN_9(11), PRN_10(12), PRN_11(13), PRN_12(14),
109 /** PDOP (precision) */
110 PDOP(15),
111 /** HDOP (horizontal precision) */
112 HDOP(16),
113 /** VDOP (vertical precision) */
114 VDOP(17), ;
115
116 public final int position;
117
118 GPGSA(int position) {
119 this.position = position;
120 }
121 }
122
123 public GpxData data;
124
125 public NmeaReader(InputStream source, File relativeMarkerPath) {
126 data = new GpxData();
127 GpxTrack currentTrack = new GpxTrack();
128 Collection<WayPoint> currentTrackSeg = new ArrayList<WayPoint>();
129 currentTrack.trackSegs.add(currentTrackSeg);
130 data.tracks.add(currentTrack);
131
132 BufferedReader rd;
133 String nmeaWithChecksum;
134
135 try {
136 rd = new BufferedReader(new InputStreamReader(source));
137 while ((nmeaWithChecksum = rd.readLine()) != null) {
138 String[] nmeaAndChecksum = nmeaWithChecksum.split("\\*");
139 String nmea = nmeaAndChecksum[0];
140 // XXX: No need for it: String checksum = nmeaAndChecksum[1];
141 String[] e = nmea.split(",");
142 if (NMEA_TYPE.GPRMC.equals(e[TYPE])) {
143 LatLon latLon = parseLatLon(e);
144 if (latLon == null) {
145 continue;
146 }
147 WayPoint currentWayPoint = new WayPoint(latLon);
148 currentTrackSeg.add(currentWayPoint);
149 }
150 }
151 rd.close();
152 } catch (final IOException e) {
153 System.out.println("Error reading file");
154 }
155
156 }
157
158 private LatLon parseLatLon(String[] e) throws NumberFormatException {
159 String widthNorth = e[GPRMC.WIDTH_NORTH.position].trim();
160 String lengthEast = e[GPRMC.LENGTH_EAST.position].trim();
161 if ("".equals(widthNorth) || "".equals(lengthEast)) {
162 return null;
163 }
164
165 // The format is xxDDLL.LLLL
166 // xx optional whitespace
167 // DD (int) degres
168 // LL.LLLL (double) latidude
169 int latdegsep = widthNorth.indexOf('.') - 2;
170 if (latdegsep < 0) {
171 return null;
172 }
173 int latdeg = Integer.parseInt(widthNorth.substring(0, latdegsep));
174 double latmin = Double.parseDouble(widthNorth.substring(latdegsep));
175 double lat = latdeg + latmin / 60;
176 if ("S".equals(e[GPRMC.WIDTH_NORTH_NAME.position])) {
177 lat = -lat;
178 }
179
180 int londegsep = lengthEast.indexOf('.') - 2;
181 if (londegsep < 0) {
182 return null;
183 }
184 int londeg = Integer.parseInt(lengthEast.substring(0, londegsep));
185 double lonmin = Double.parseDouble(lengthEast.substring(londegsep));
186 double lon = londeg + lonmin / 60;
187 if ("W".equals(e[GPRMC.LENGTH_EAST_NAME.position])) {
188 lon = -lon;
189 }
190
191 return new LatLon(lat, lon);
192 }
193}
Note: See TracBrowser for help on using the repository browser.