1 | //License: GPL. See README for details.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.text.ParsePosition;
|
---|
8 | import java.text.SimpleDateFormat;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.Date;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
17 | import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
|
---|
18 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
19 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Read a nmea file. Based on information from
|
---|
24 | * http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm
|
---|
25 | *
|
---|
26 | * @author cbrill
|
---|
27 | */
|
---|
28 | public class NmeaReader {
|
---|
29 |
|
---|
30 | /** Handler for the different types that NMEA speaks. */
|
---|
31 | public static enum NMEA_TYPE {
|
---|
32 |
|
---|
33 | /** RMC = recommended minimum sentence C. */
|
---|
34 | GPRMC("$GPRMC"),
|
---|
35 | /** GPS positions. */
|
---|
36 | GPGGA("$GPGGA"),
|
---|
37 | /** SA = satellites active. */
|
---|
38 | GPGSA("$GPGSA"),
|
---|
39 | /** Course over ground and ground speed */
|
---|
40 | GPVTG("$GPVTG");
|
---|
41 |
|
---|
42 | private final String type;
|
---|
43 |
|
---|
44 | NMEA_TYPE(String type) {
|
---|
45 | this.type = type;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public String getType() {
|
---|
49 | return this.type;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public boolean equals(String type) {
|
---|
53 | return this.type.equals(type);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | // GPVTG
|
---|
58 | public static enum GPVTG {
|
---|
59 | COURSE(1),COURSE_REF(2), // true course
|
---|
60 | COURSE_M(3), COURSE_M_REF(4), // magnetic course
|
---|
61 | SPEED_KN(5), SPEED_KN_UNIT(6), // speed in knots
|
---|
62 | SPEED_KMH(7), SPEED_KMH_UNIT(8), // speed in km/h
|
---|
63 | REST(9); // version-specific rest
|
---|
64 |
|
---|
65 | public final int position;
|
---|
66 |
|
---|
67 | GPVTG(int position) {
|
---|
68 | this.position = position;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // The following only applies to GPRMC
|
---|
73 | public static enum GPRMC {
|
---|
74 | TIME(1),
|
---|
75 | /** Warning from the receiver (A = data ok, V = warning) */
|
---|
76 | RECEIVER_WARNING(2),
|
---|
77 | WIDTH_NORTH(3), WIDTH_NORTH_NAME(4), // Latitude, NS
|
---|
78 | LENGTH_EAST(5), LENGTH_EAST_NAME(6), // Longitude, EW
|
---|
79 | SPEED(7), COURSE(8), DATE(9), // Speed in knots
|
---|
80 | MAGNETIC_DECLINATION(10), UNKNOWN(11), // magnetic declination
|
---|
81 | /**
|
---|
82 | * Mode (A = autonom; D = differential; E = estimated; N = not valid; S
|
---|
83 | * = simulated)
|
---|
84 | *
|
---|
85 | * @since NMEA 2.3
|
---|
86 | */
|
---|
87 | MODE(12);
|
---|
88 |
|
---|
89 | public final int position;
|
---|
90 |
|
---|
91 | GPRMC(int position) {
|
---|
92 | this.position = position;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // The following only applies to GPGGA
|
---|
97 | public static enum GPGGA {
|
---|
98 | TIME(1), LATITUDE(2), LATITUDE_NAME(3), LONGITUDE(4), LONGITUDE_NAME(5),
|
---|
99 | /**
|
---|
100 | * Quality (0 = invalid, 1 = GPS, 2 = DGPS, 6 = estimanted (@since NMEA
|
---|
101 | * 2.3))
|
---|
102 | */
|
---|
103 | QUALITY(6), SATELLITE_COUNT(7),
|
---|
104 | HDOP(8), // HDOP (horizontal dilution of precision)
|
---|
105 | HEIGHT(9), HEIGHT_UNTIS(10), // height above NN (above geoid)
|
---|
106 | HEIGHT_2(11), HEIGHT_2_UNTIS(12), // height geoid - height ellipsoid (WGS84)
|
---|
107 | GPS_AGE(13),// Age of differential GPS data
|
---|
108 | REF(14); // REF station
|
---|
109 |
|
---|
110 | public final int position;
|
---|
111 | GPGGA(int position) {
|
---|
112 | this.position = position;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static enum GPGSA {
|
---|
117 | AUTOMATIC(1),
|
---|
118 | FIX_TYPE(2), // 1 = not fixed, 2 = 2D fixed, 3 = 3D fixed)
|
---|
119 | // PRN numbers for max 12 satellites
|
---|
120 | PRN_1(3), PRN_2(4), PRN_3(5), PRN_4(6), PRN_5(7), PRN_6(8),
|
---|
121 | PRN_7(9), PRN_8(10), PRN_9(11), PRN_10(12), PRN_11(13), PRN_12(14),
|
---|
122 | PDOP(15), // PDOP (precision)
|
---|
123 | HDOP(16), // HDOP (horizontal precision)
|
---|
124 | VDOP(17), ; // VDOP (vertical precision)
|
---|
125 |
|
---|
126 | public final int position;
|
---|
127 | GPGSA(int position) {
|
---|
128 | this.position = position;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public GpxData data;
|
---|
133 |
|
---|
134 | // private final static SimpleDateFormat GGATIMEFMT =
|
---|
135 | // new SimpleDateFormat("HHmmss.SSS");
|
---|
136 | private final static SimpleDateFormat RMCTIMEFMT =
|
---|
137 | new SimpleDateFormat("ddMMyyHHmmss.SSS");
|
---|
138 | private final static SimpleDateFormat RMCTIMEFMTSTD =
|
---|
139 | new SimpleDateFormat("ddMMyyHHmmss");
|
---|
140 |
|
---|
141 | private Date readTime(String p)
|
---|
142 | {
|
---|
143 | Date d = RMCTIMEFMT.parse(p, new ParsePosition(0));
|
---|
144 | if (d == null) {
|
---|
145 | d = RMCTIMEFMTSTD.parse(p, new ParsePosition(0));
|
---|
146 | }
|
---|
147 | if (d == null)
|
---|
148 | throw new RuntimeException("Date is malformed"); // malformed
|
---|
149 | return d;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // functons for reading the error stats
|
---|
153 | public NMEAParserState ps;
|
---|
154 |
|
---|
155 | public int getParserUnknown() {
|
---|
156 | return ps.unknown;
|
---|
157 | }
|
---|
158 | public int getParserZeroCoordinates() {
|
---|
159 | return ps.zero_coord;
|
---|
160 | }
|
---|
161 | public int getParserChecksumErrors() {
|
---|
162 | return ps.checksum_errors+ps.no_checksum;
|
---|
163 | }
|
---|
164 | public int getParserMalformed() {
|
---|
165 | return ps.malformed;
|
---|
166 | }
|
---|
167 | public int getNumberOfCoordinates() {
|
---|
168 | return ps.success;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public NmeaReader(InputStream source) {
|
---|
172 |
|
---|
173 | // create the data tree
|
---|
174 | data = new GpxData();
|
---|
175 | Collection<Collection<WayPoint>> currentTrack = new ArrayList<Collection<WayPoint>>();
|
---|
176 |
|
---|
177 | BufferedReader rd = null;
|
---|
178 | try {
|
---|
179 | rd = new BufferedReader(new InputStreamReader(source));
|
---|
180 |
|
---|
181 | StringBuilder sb = new StringBuilder(1024);
|
---|
182 | int loopstart_char = rd.read();
|
---|
183 | ps = new NMEAParserState();
|
---|
184 | if(loopstart_char == -1)
|
---|
185 | //TODO tell user about the problem?
|
---|
186 | return;
|
---|
187 | sb.append((char)loopstart_char);
|
---|
188 | ps.p_Date="010100"; // TODO date problem
|
---|
189 | while(true) {
|
---|
190 | // don't load unparsable files completely to memory
|
---|
191 | if(sb.length()>=1020) {
|
---|
192 | sb.delete(0, sb.length()-1);
|
---|
193 | }
|
---|
194 | int c = rd.read();
|
---|
195 | if(c=='$') {
|
---|
196 | parseNMEASentence(sb.toString(), ps);
|
---|
197 | sb.delete(0, sb.length());
|
---|
198 | sb.append('$');
|
---|
199 | } else if(c == -1) {
|
---|
200 | // EOF: add last WayPoint if it works out
|
---|
201 | parseNMEASentence(sb.toString(),ps);
|
---|
202 | break;
|
---|
203 | } else {
|
---|
204 | sb.append((char)c);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | currentTrack.add(ps.waypoints);
|
---|
208 | data.tracks.add(new ImmutableGpxTrack(currentTrack, Collections.<String, Object>emptyMap()));
|
---|
209 |
|
---|
210 | } catch (Exception e) {
|
---|
211 | Main.warn(e);
|
---|
212 | } finally {
|
---|
213 | Utils.close(rd);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | private static class NMEAParserState {
|
---|
217 | protected Collection<WayPoint> waypoints = new ArrayList<WayPoint>();
|
---|
218 | protected String p_Time;
|
---|
219 | protected String p_Date;
|
---|
220 | protected WayPoint p_Wp;
|
---|
221 |
|
---|
222 | protected int success = 0; // number of successfully parsend sentences
|
---|
223 | protected int malformed = 0;
|
---|
224 | protected int checksum_errors = 0;
|
---|
225 | protected int no_checksum = 0;
|
---|
226 | protected int unknown = 0;
|
---|
227 | protected int zero_coord = 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | // Parses split up sentences into WayPoints which are stored
|
---|
231 | // in the collection in the NMEAParserState object.
|
---|
232 | // Returns true if the input made sence, false otherwise.
|
---|
233 | private boolean parseNMEASentence(String s, NMEAParserState ps) throws IllegalDataException {
|
---|
234 | try {
|
---|
235 | if (s.isEmpty()) {
|
---|
236 | throw new IllegalArgumentException("s is empty");
|
---|
237 | }
|
---|
238 |
|
---|
239 | // checksum check:
|
---|
240 | // the bytes between the $ and the * are xored
|
---|
241 | // if there is no * or other meanities it will throw
|
---|
242 | // and result in a malformed packet.
|
---|
243 | String[] chkstrings = s.split("\\*");
|
---|
244 | if(chkstrings.length > 1)
|
---|
245 | {
|
---|
246 | byte[] chb = chkstrings[0].getBytes();
|
---|
247 | int chk=0;
|
---|
248 | for (int i = 1; i < chb.length; i++) {
|
---|
249 | chk ^= chb[i];
|
---|
250 | }
|
---|
251 | if (Integer.parseInt(chkstrings[1].substring(0,2),16) != chk) {
|
---|
252 | ps.checksum_errors++;
|
---|
253 | ps.p_Wp=null;
|
---|
254 | return false;
|
---|
255 | }
|
---|
256 | } else {
|
---|
257 | ps.no_checksum++;
|
---|
258 | }
|
---|
259 | // now for the content
|
---|
260 | String[] e = chkstrings[0].split(",");
|
---|
261 | String accu;
|
---|
262 |
|
---|
263 | WayPoint currentwp = ps.p_Wp;
|
---|
264 | String currentDate = ps.p_Date;
|
---|
265 |
|
---|
266 | // handle the packet content
|
---|
267 | if(e[0].equals("$GPGGA") || e[0].equals("$GNGGA")) {
|
---|
268 | // Position
|
---|
269 | LatLon latLon = parseLatLon(
|
---|
270 | e[GPGGA.LATITUDE_NAME.position],
|
---|
271 | e[GPGGA.LONGITUDE_NAME.position],
|
---|
272 | e[GPGGA.LATITUDE.position],
|
---|
273 | e[GPGGA.LONGITUDE.position]
|
---|
274 | );
|
---|
275 | if (latLon==null) {
|
---|
276 | throw new IllegalDataException("Malformed lat/lon");
|
---|
277 | }
|
---|
278 |
|
---|
279 | if ((latLon.lat()==0.0) && (latLon.lon()==0.0)) {
|
---|
280 | ps.zero_coord++;
|
---|
281 | return false;
|
---|
282 | }
|
---|
283 |
|
---|
284 | // time
|
---|
285 | accu = e[GPGGA.TIME.position];
|
---|
286 | Date d = readTime(currentDate+accu);
|
---|
287 |
|
---|
288 | if((ps.p_Time==null) || (currentwp==null) || !ps.p_Time.equals(accu)) {
|
---|
289 | // this node is newer than the previous, create a new waypoint.
|
---|
290 | // no matter if previous WayPoint was null, we got something
|
---|
291 | // better now.
|
---|
292 | ps.p_Time=accu;
|
---|
293 | currentwp = new WayPoint(latLon);
|
---|
294 | }
|
---|
295 | if(!currentwp.attr.containsKey("time")) {
|
---|
296 | // As this sentence has no complete time only use it
|
---|
297 | // if there is no time so far
|
---|
298 | currentwp.attr.put("time", DateUtils.fromDate(d));
|
---|
299 | }
|
---|
300 | // elevation
|
---|
301 | accu=e[GPGGA.HEIGHT_UNTIS.position];
|
---|
302 | if(accu.equals("M")) {
|
---|
303 | // Ignore heights that are not in meters for now
|
---|
304 | accu=e[GPGGA.HEIGHT.position];
|
---|
305 | if(!accu.isEmpty()) {
|
---|
306 | Double.parseDouble(accu);
|
---|
307 | // if it throws it's malformed; this should only happen if the
|
---|
308 | // device sends nonstandard data.
|
---|
309 | if(!accu.isEmpty()) { // FIX ? same check
|
---|
310 | currentwp.attr.put("ele", accu);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | // number of sattelites
|
---|
315 | accu=e[GPGGA.SATELLITE_COUNT.position];
|
---|
316 | int sat = 0;
|
---|
317 | if(!accu.isEmpty()) {
|
---|
318 | sat = Integer.parseInt(accu);
|
---|
319 | currentwp.attr.put("sat", accu);
|
---|
320 | }
|
---|
321 | // h-dilution
|
---|
322 | accu=e[GPGGA.HDOP.position];
|
---|
323 | if(!accu.isEmpty()) {
|
---|
324 | currentwp.attr.put("hdop", Float.parseFloat(accu));
|
---|
325 | }
|
---|
326 | // fix
|
---|
327 | accu=e[GPGGA.QUALITY.position];
|
---|
328 | if(!accu.isEmpty()) {
|
---|
329 | int fixtype = Integer.parseInt(accu);
|
---|
330 | switch(fixtype) {
|
---|
331 | case 0:
|
---|
332 | currentwp.attr.put("fix", "none");
|
---|
333 | break;
|
---|
334 | case 1:
|
---|
335 | if(sat < 4) {
|
---|
336 | currentwp.attr.put("fix", "2d");
|
---|
337 | } else {
|
---|
338 | currentwp.attr.put("fix", "3d");
|
---|
339 | }
|
---|
340 | break;
|
---|
341 | case 2:
|
---|
342 | currentwp.attr.put("fix", "dgps");
|
---|
343 | break;
|
---|
344 | default:
|
---|
345 | break;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | } else if(e[0].equals("$GPVTG") || e[0].equals("$GNVTG")) {
|
---|
349 | // COURSE
|
---|
350 | accu = e[GPVTG.COURSE_REF.position];
|
---|
351 | if(accu.equals("T")) {
|
---|
352 | // other values than (T)rue are ignored
|
---|
353 | accu = e[GPVTG.COURSE.position];
|
---|
354 | if(!accu.isEmpty()) {
|
---|
355 | Double.parseDouble(accu);
|
---|
356 | currentwp.attr.put("course", accu);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | // SPEED
|
---|
360 | accu = e[GPVTG.SPEED_KMH_UNIT.position];
|
---|
361 | if(accu.startsWith("K")) {
|
---|
362 | accu = e[GPVTG.SPEED_KMH.position];
|
---|
363 | if(!accu.isEmpty()) {
|
---|
364 | double speed = Double.parseDouble(accu);
|
---|
365 | speed /= 3.6; // speed in m/s
|
---|
366 | currentwp.attr.put("speed", Double.toString(speed));
|
---|
367 | }
|
---|
368 | }
|
---|
369 | } else if(e[0].equals("$GPGSA") || e[0].equals("$GNGSA")) {
|
---|
370 | // vdop
|
---|
371 | accu=e[GPGSA.VDOP.position];
|
---|
372 | if(!accu.isEmpty()) {
|
---|
373 | currentwp.attr.put("vdop", Float.parseFloat(accu));
|
---|
374 | }
|
---|
375 | // hdop
|
---|
376 | accu=e[GPGSA.HDOP.position];
|
---|
377 | if(!accu.isEmpty()) {
|
---|
378 | currentwp.attr.put("hdop", Float.parseFloat(accu));
|
---|
379 | }
|
---|
380 | // pdop
|
---|
381 | accu=e[GPGSA.PDOP.position];
|
---|
382 | if(!accu.isEmpty()) {
|
---|
383 | currentwp.attr.put("pdop", Float.parseFloat(accu));
|
---|
384 | }
|
---|
385 | }
|
---|
386 | else if(e[0].equals("$GPRMC") || e[0].equals("$GNRMC")) {
|
---|
387 | // coordinates
|
---|
388 | LatLon latLon = parseLatLon(
|
---|
389 | e[GPRMC.WIDTH_NORTH_NAME.position],
|
---|
390 | e[GPRMC.LENGTH_EAST_NAME.position],
|
---|
391 | e[GPRMC.WIDTH_NORTH.position],
|
---|
392 | e[GPRMC.LENGTH_EAST.position]
|
---|
393 | );
|
---|
394 | if((latLon.lat()==0.0) && (latLon.lon()==0.0)) {
|
---|
395 | ps.zero_coord++;
|
---|
396 | return false;
|
---|
397 | }
|
---|
398 | // time
|
---|
399 | currentDate = e[GPRMC.DATE.position];
|
---|
400 | String time = e[GPRMC.TIME.position];
|
---|
401 |
|
---|
402 | Date d = readTime(currentDate+time);
|
---|
403 |
|
---|
404 | if((ps.p_Time==null) || (currentwp==null) || !ps.p_Time.equals(time)) {
|
---|
405 | // this node is newer than the previous, create a new waypoint.
|
---|
406 | ps.p_Time=time;
|
---|
407 | currentwp = new WayPoint(latLon);
|
---|
408 | }
|
---|
409 | // time: this sentence has complete time so always use it.
|
---|
410 | currentwp.attr.put("time", DateUtils.fromDate(d));
|
---|
411 | // speed
|
---|
412 | accu = e[GPRMC.SPEED.position];
|
---|
413 | if(!accu.isEmpty() && !currentwp.attr.containsKey("speed")) {
|
---|
414 | double speed = Double.parseDouble(accu);
|
---|
415 | speed *= 0.514444444; // to m/s
|
---|
416 | currentwp.attr.put("speed", Double.toString(speed));
|
---|
417 | }
|
---|
418 | // course
|
---|
419 | accu = e[GPRMC.COURSE.position];
|
---|
420 | if(!accu.isEmpty() && !currentwp.attr.containsKey("course")) {
|
---|
421 | Double.parseDouble(accu);
|
---|
422 | currentwp.attr.put("course", accu);
|
---|
423 | }
|
---|
424 |
|
---|
425 | // TODO fix?
|
---|
426 | // * Mode (A = autonom; D = differential; E = estimated; N = not valid; S
|
---|
427 | // * = simulated)
|
---|
428 | // *
|
---|
429 | // * @since NMEA 2.3
|
---|
430 | //
|
---|
431 | //MODE(12);
|
---|
432 | } else {
|
---|
433 | ps.unknown++;
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 | ps.p_Date = currentDate;
|
---|
437 | if(ps.p_Wp != currentwp) {
|
---|
438 | if(ps.p_Wp!=null) {
|
---|
439 | ps.p_Wp.setTime();
|
---|
440 | }
|
---|
441 | ps.p_Wp = currentwp;
|
---|
442 | ps.waypoints.add(currentwp);
|
---|
443 | ps.success++;
|
---|
444 | return true;
|
---|
445 | }
|
---|
446 | return true;
|
---|
447 |
|
---|
448 | } catch (RuntimeException x) {
|
---|
449 | // out of bounds and such
|
---|
450 | ps.malformed++;
|
---|
451 | ps.p_Wp=null;
|
---|
452 | return false;
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | private LatLon parseLatLon(String ns, String ew, String dlat, String dlon)
|
---|
457 | throws NumberFormatException {
|
---|
458 | String widthNorth = dlat.trim();
|
---|
459 | String lengthEast = dlon.trim();
|
---|
460 |
|
---|
461 | // return a zero latlon instead of null so it is logged as zero coordinate
|
---|
462 | // instead of malformed sentence
|
---|
463 | if(widthNorth.isEmpty() && lengthEast.isEmpty()) return new LatLon(0.0,0.0);
|
---|
464 |
|
---|
465 | // The format is xxDDLL.LLLL
|
---|
466 | // xx optional whitespace
|
---|
467 | // DD (int) degres
|
---|
468 | // LL.LLLL (double) latidude
|
---|
469 | int latdegsep = widthNorth.indexOf('.') - 2;
|
---|
470 | if (latdegsep < 0) return null;
|
---|
471 |
|
---|
472 | int latdeg = Integer.parseInt(widthNorth.substring(0, latdegsep));
|
---|
473 | double latmin = Double.parseDouble(widthNorth.substring(latdegsep));
|
---|
474 | if(latdeg < 0) {
|
---|
475 | latmin *= -1.0;
|
---|
476 | }
|
---|
477 | double lat = latdeg + latmin / 60;
|
---|
478 | if ("S".equals(ns)) {
|
---|
479 | lat = -lat;
|
---|
480 | }
|
---|
481 |
|
---|
482 | int londegsep = lengthEast.indexOf('.') - 2;
|
---|
483 | if (londegsep < 0) return null;
|
---|
484 |
|
---|
485 | int londeg = Integer.parseInt(lengthEast.substring(0, londegsep));
|
---|
486 | double lonmin = Double.parseDouble(lengthEast.substring(londegsep));
|
---|
487 | if(londeg < 0) {
|
---|
488 | lonmin *= -1.0;
|
---|
489 | }
|
---|
490 | double lon = londeg + lonmin / 60;
|
---|
491 | if ("W".equals(ew)) {
|
---|
492 | lon = -lon;
|
---|
493 | }
|
---|
494 | return new LatLon(lat, lon);
|
---|
495 | }
|
---|
496 | }
|
---|