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