| 1 | //License: GPL. Copyright 2008 by Christoph Brill
|
|---|
| 2 |
|
|---|
| 3 | package org.openstreetmap.josm.io;
|
|---|
| 4 |
|
|---|
| 5 | import java.io.BufferedReader;
|
|---|
| 6 | import java.io.File;
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.io.InputStream;
|
|---|
| 9 | import java.io.InputStreamReader;
|
|---|
| 10 | import java.text.ParsePosition;
|
|---|
| 11 | import java.text.SimpleDateFormat;
|
|---|
| 12 | import java.util.ArrayList;
|
|---|
| 13 | import java.util.Collection;
|
|---|
| 14 | import java.util.Date;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 18 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
|---|
| 19 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 20 | import org.openstreetmap.josm.tools.DateUtils;
|
|---|
| 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 | if (d == null) throw(null); // malformed
|
|---|
| 147 | return d;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | // functons for reading the error stats
|
|---|
| 151 | public NMEAParserState ps;
|
|---|
| 152 |
|
|---|
| 153 | public int getParserUnknown() {
|
|---|
| 154 | return ps.unknown;
|
|---|
| 155 | }
|
|---|
| 156 | public int getParserZeroCoordinates() {
|
|---|
| 157 | return ps.zero_coord;
|
|---|
| 158 | }
|
|---|
| 159 | public int getParserChecksumErrors() {
|
|---|
| 160 | return ps.checksum_errors+ps.no_checksum;
|
|---|
| 161 | }
|
|---|
| 162 | public int getParserMalformed() {
|
|---|
| 163 | return ps.malformed;
|
|---|
| 164 | }
|
|---|
| 165 | public int getNumberOfCoordinates() {
|
|---|
| 166 | return ps.success;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | public NmeaReader(InputStream source, File relativeMarkerPath) {
|
|---|
| 170 |
|
|---|
| 171 | // create the data tree
|
|---|
| 172 | data = new GpxData();
|
|---|
| 173 | GpxTrack currentTrack = new GpxTrack();
|
|---|
| 174 | data.tracks.add(currentTrack);
|
|---|
| 175 |
|
|---|
| 176 | try {
|
|---|
| 177 | BufferedReader rd =
|
|---|
| 178 | new BufferedReader(new InputStreamReader(source));
|
|---|
| 179 |
|
|---|
| 180 | StringBuffer sb = new StringBuffer(1024);
|
|---|
| 181 | int loopstart_char = rd.read();
|
|---|
| 182 | ps = new NMEAParserState();
|
|---|
| 183 | if(loopstart_char == -1) {// zero size file
|
|---|
| 184 | //TODO tell user about the problem?
|
|---|
| 185 | return;
|
|---|
| 186 | }
|
|---|
| 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) sb.delete(0, sb.length()-1);
|
|---|
| 192 | int c = rd.read();
|
|---|
| 193 | if(c=='$') {
|
|---|
| 194 | ParseNMEASentence(sb.toString(), ps);
|
|---|
| 195 | sb.delete(0, sb.length());
|
|---|
| 196 | sb.append('$');
|
|---|
| 197 | } else if(c == -1) {
|
|---|
| 198 | // EOF: add last WayPoint if it works out
|
|---|
| 199 | ParseNMEASentence(sb.toString(),ps);
|
|---|
| 200 | break;
|
|---|
| 201 | } else sb.append((char)c);
|
|---|
| 202 | }
|
|---|
| 203 | rd.close();
|
|---|
| 204 | currentTrack.trackSegs.add(ps.waypoints);
|
|---|
| 205 | data.recalculateBounds();
|
|---|
| 206 |
|
|---|
| 207 | } catch (final IOException e) {
|
|---|
| 208 | // TODO tell user about the problem?
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 | private class NMEAParserState {
|
|---|
| 212 | protected Collection<WayPoint> waypoints = new ArrayList<WayPoint>();
|
|---|
| 213 | protected String p_Time;
|
|---|
| 214 | protected String p_Date;
|
|---|
| 215 | protected WayPoint p_Wp;
|
|---|
| 216 |
|
|---|
| 217 | protected int success = 0; // number of successfully parsend sentences
|
|---|
| 218 | protected int malformed = 0;
|
|---|
| 219 | protected int checksum_errors = 0;
|
|---|
| 220 | protected int no_checksum = 0;
|
|---|
| 221 | protected int unknown = 0;
|
|---|
| 222 | protected int zero_coord = 0;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | // Parses split up sentences into WayPoints which are stored
|
|---|
| 226 | // in the collection in the NMEAParserState object.
|
|---|
| 227 | // Returns true if the input made sence, false otherwise.
|
|---|
| 228 | private boolean ParseNMEASentence(String s, NMEAParserState ps) {
|
|---|
| 229 | try {
|
|---|
| 230 | if(s.equals("")) throw(null);
|
|---|
| 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();
|
|---|
| 240 | int chk=0;
|
|---|
| 241 | for(int i = 1; i < chb.length; i++) chk ^= chb[i];
|
|---|
| 242 | if(Integer.parseInt(chkstrings[1].substring(0,2),16) != chk) {
|
|---|
| 243 | //System.out.println("Checksum error");
|
|---|
| 244 | ps.checksum_errors++;
|
|---|
| 245 | ps.p_Wp=null;
|
|---|
| 246 | return false;
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|
| 249 | else
|
|---|
| 250 | ps.no_checksum++;
|
|---|
| 251 | // now for the content
|
|---|
| 252 | String[] e = chkstrings[0].split(",");
|
|---|
| 253 | String accu;
|
|---|
| 254 |
|
|---|
| 255 | WayPoint currentwp = ps.p_Wp;
|
|---|
| 256 | String currentDate = ps.p_Date;
|
|---|
| 257 |
|
|---|
| 258 | // handle the packet content
|
|---|
| 259 | if(e[0].equals("$GPGGA")) {
|
|---|
| 260 | // Position
|
|---|
| 261 | LatLon latLon = parseLatLon(
|
|---|
| 262 | e[GPGGA.LATITUDE_NAME.position],
|
|---|
| 263 | e[GPGGA.LONGITUDE_NAME.position],
|
|---|
| 264 | e[GPGGA.LATITUDE.position],
|
|---|
| 265 | e[GPGGA.LONGITUDE.position]
|
|---|
| 266 | );
|
|---|
| 267 | if(latLon==null) throw(null); // malformed
|
|---|
| 268 |
|
|---|
| 269 | if((latLon.lat()==0.0) && (latLon.lon()==0.0)) {
|
|---|
| 270 | ps.zero_coord++;
|
|---|
| 271 | return false;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | // time
|
|---|
| 275 | accu = e[GPGGA.TIME.position];
|
|---|
| 276 | Date d = readTime(currentDate+accu);
|
|---|
| 277 |
|
|---|
| 278 | if((ps.p_Time==null) || (currentwp==null) || !ps.p_Time.equals(accu)) {
|
|---|
| 279 | // this node is newer than the previous, create a new waypoint.
|
|---|
| 280 | // no matter if previous WayPoint was null, we got something
|
|---|
| 281 | // better now.
|
|---|
| 282 | ps.p_Time=accu;
|
|---|
| 283 | currentwp = new WayPoint(latLon);
|
|---|
| 284 | }
|
|---|
| 285 | if(!currentwp.attr.containsKey("time")) {
|
|---|
| 286 | // As this sentence has no complete time only use it
|
|---|
| 287 | // if there is no time so far
|
|---|
| 288 | currentwp.attr.put("time", DateUtils.fromDate(d));
|
|---|
| 289 | }
|
|---|
| 290 | // elevation
|
|---|
| 291 | accu=e[GPGGA.HEIGHT_UNTIS.position];
|
|---|
| 292 | if(accu.equals("M")) {
|
|---|
| 293 | // Ignore heights that are not in meters for now
|
|---|
| 294 | accu=e[GPGGA.HEIGHT.position];
|
|---|
| 295 | if(!accu.equals("")) {
|
|---|
| 296 | Double.parseDouble(accu);
|
|---|
| 297 | // if it throws it's malformed; this should only happen if the
|
|---|
| 298 | // device sends nonstandard data.
|
|---|
| 299 | if(!accu.equals("")) currentwp.attr.put("ele", accu);
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 | // number of sattelites
|
|---|
| 303 | accu=e[GPGGA.SATELLITE_COUNT.position];
|
|---|
| 304 | int sat = 0;
|
|---|
| 305 | if(!accu.equals("")) {
|
|---|
| 306 | sat = Integer.parseInt(accu);
|
|---|
| 307 | currentwp.attr.put("sat", accu);
|
|---|
| 308 | }
|
|---|
| 309 | // h-dilution
|
|---|
| 310 | accu=e[GPGGA.HDOP.position];
|
|---|
| 311 | if(!accu.equals(""))
|
|---|
| 312 | currentwp.attr.put("hdop", Float.parseFloat(accu));
|
|---|
| 313 | // fix
|
|---|
| 314 | accu=e[GPGGA.QUALITY.position];
|
|---|
| 315 | if(!accu.equals("")) {
|
|---|
| 316 | int fixtype = Integer.parseInt(accu);
|
|---|
| 317 | switch(fixtype) {
|
|---|
| 318 | case 0:
|
|---|
| 319 | currentwp.attr.put("fix", "none");
|
|---|
| 320 | break;
|
|---|
| 321 | case 1:
|
|---|
| 322 | if(sat < 4) currentwp.attr.put("fix", "2d");
|
|---|
| 323 | else currentwp.attr.put("fix", "3d");
|
|---|
| 324 | break;
|
|---|
| 325 | case 2:
|
|---|
| 326 | currentwp.attr.put("fix", "dgps");
|
|---|
| 327 | break;
|
|---|
| 328 | default:
|
|---|
| 329 | break;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | } else if(e[0].equals("$GPVTG")) {
|
|---|
| 333 | // COURSE
|
|---|
| 334 | accu = e[GPVTG.COURSE_REF.position];
|
|---|
| 335 | if(accu.equals("T")) {
|
|---|
| 336 | // other values than (T)rue are ignored
|
|---|
| 337 | accu = e[GPVTG.COURSE.position];
|
|---|
| 338 | if(!accu.equals("")) {
|
|---|
| 339 | Double.parseDouble(accu);
|
|---|
| 340 | currentwp.attr.put("course", accu);
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 | // SPEED
|
|---|
| 344 | accu = e[GPVTG.SPEED_KMH_UNIT.position];
|
|---|
| 345 | if(accu.startsWith("K")) {
|
|---|
| 346 | accu = e[GPVTG.SPEED_KMH.position];
|
|---|
| 347 | if(!accu.equals("")) {
|
|---|
| 348 | double speed = Double.parseDouble(accu);
|
|---|
| 349 | speed /= 3.6; // speed in m/s
|
|---|
| 350 | currentwp.attr.put("speed", Double.toString(speed));
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 | } else if(e[0].equals("$GPGSA")) {
|
|---|
| 354 | // vdop
|
|---|
| 355 | accu=e[GPGSA.VDOP.position];
|
|---|
| 356 | if(!accu.equals(""))
|
|---|
| 357 | currentwp.attr.put("vdop", Float.parseFloat(accu));
|
|---|
| 358 | // hdop
|
|---|
| 359 | accu=e[GPGSA.HDOP.position];
|
|---|
| 360 | if(!accu.equals(""))
|
|---|
| 361 | currentwp.attr.put("hdop", Float.parseFloat(accu));
|
|---|
| 362 | // pdop
|
|---|
| 363 | accu=e[GPGSA.PDOP.position];
|
|---|
| 364 | if(!accu.equals(""))
|
|---|
| 365 | currentwp.attr.put("pdop", Float.parseFloat(accu));
|
|---|
| 366 | }
|
|---|
| 367 | else if(e[0].equals("$GPRMC")) {
|
|---|
| 368 | // coordinates
|
|---|
| 369 | LatLon latLon = parseLatLon(
|
|---|
| 370 | e[GPRMC.WIDTH_NORTH_NAME.position],
|
|---|
| 371 | e[GPRMC.LENGTH_EAST_NAME.position],
|
|---|
| 372 | e[GPRMC.WIDTH_NORTH.position],
|
|---|
| 373 | e[GPRMC.LENGTH_EAST.position]
|
|---|
| 374 | );
|
|---|
| 375 | if(latLon==null) throw(null);
|
|---|
| 376 | if((latLon.lat()==0.0) && (latLon.lon()==0.0)) {
|
|---|
| 377 | ps.zero_coord++;
|
|---|
| 378 | return false;
|
|---|
| 379 | }
|
|---|
| 380 | // time
|
|---|
| 381 | currentDate = e[GPRMC.DATE.position];
|
|---|
| 382 | String time = e[GPRMC.TIME.position];
|
|---|
| 383 |
|
|---|
| 384 | Date d = readTime(currentDate+time);
|
|---|
| 385 |
|
|---|
| 386 | if((ps.p_Time==null) || (currentwp==null) || !ps.p_Time.equals(time)) {
|
|---|
| 387 | // this node is newer than the previous, create a new waypoint.
|
|---|
| 388 | ps.p_Time=time;
|
|---|
| 389 | currentwp = new WayPoint(latLon);
|
|---|
| 390 | }
|
|---|
| 391 | // time: this sentence has complete time so always use it.
|
|---|
| 392 | currentwp.attr.put("time", DateUtils.fromDate(d));
|
|---|
| 393 | // speed
|
|---|
| 394 | accu = e[GPRMC.SPEED.position];
|
|---|
| 395 | if(!accu.equals("") && !currentwp.attr.containsKey("speed")) {
|
|---|
| 396 | double speed = Double.parseDouble(accu);
|
|---|
| 397 | speed *= 0.514444444; // to m/s
|
|---|
| 398 | currentwp.attr.put("speed", Double.toString(speed));
|
|---|
| 399 | }
|
|---|
| 400 | // course
|
|---|
| 401 | accu = e[GPRMC.COURSE.position];
|
|---|
| 402 | if(!accu.equals("") && !currentwp.attr.containsKey("course")) {
|
|---|
| 403 | Double.parseDouble(accu);
|
|---|
| 404 | currentwp.attr.put("course", accu);
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | // TODO fix?
|
|---|
| 408 | // * Mode (A = autonom; D = differential; E = estimated; N = not valid; S
|
|---|
| 409 | // * = simulated)
|
|---|
| 410 | // *
|
|---|
| 411 | // * @since NMEA 2.3
|
|---|
| 412 | //
|
|---|
| 413 | //MODE(12);
|
|---|
| 414 | } else {
|
|---|
| 415 | ps.unknown++;
|
|---|
| 416 | return false;
|
|---|
| 417 | }
|
|---|
| 418 | ps.p_Date = currentDate;
|
|---|
| 419 | if(ps.p_Wp != currentwp) {
|
|---|
| 420 | if(ps.p_Wp!=null) {
|
|---|
| 421 | ps.p_Wp.setTime();
|
|---|
| 422 | }
|
|---|
| 423 | ps.p_Wp = currentwp;
|
|---|
| 424 | ps.waypoints.add(currentwp);
|
|---|
| 425 | ps.success++;
|
|---|
| 426 | return true;
|
|---|
| 427 | }
|
|---|
| 428 | return true;
|
|---|
| 429 |
|
|---|
| 430 | } catch(Exception x) {
|
|---|
| 431 | // out of bounds and such
|
|---|
| 432 | // x.printStackTrace();
|
|---|
| 433 | // System.out.println("Malformed line: "+s.toString().trim());
|
|---|
| 434 | ps.malformed++;
|
|---|
| 435 | ps.p_Wp=null;
|
|---|
| 436 | return false;
|
|---|
| 437 | }
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | private LatLon parseLatLon(String ns, String ew, String dlat, String dlon)
|
|---|
| 441 | throws NumberFormatException {
|
|---|
| 442 | String widthNorth = dlat.trim();
|
|---|
| 443 | String lengthEast = dlon.trim();
|
|---|
| 444 |
|
|---|
| 445 | // return a zero latlon instead of null so it is logged as zero coordinate
|
|---|
| 446 | // instead of malformed sentence
|
|---|
| 447 | if(widthNorth.equals("")&&lengthEast.equals("")) return new LatLon(0.0,0.0);
|
|---|
| 448 |
|
|---|
| 449 | // The format is xxDDLL.LLLL
|
|---|
| 450 | // xx optional whitespace
|
|---|
| 451 | // DD (int) degres
|
|---|
| 452 | // LL.LLLL (double) latidude
|
|---|
| 453 | int latdegsep = widthNorth.indexOf('.') - 2;
|
|---|
| 454 | if (latdegsep < 0) return null;
|
|---|
| 455 |
|
|---|
| 456 | int latdeg = Integer.parseInt(widthNorth.substring(0, latdegsep));
|
|---|
| 457 | double latmin = Double.parseDouble(widthNorth.substring(latdegsep));
|
|---|
| 458 | if(latdeg < 0) // strange data with '-' sign
|
|---|
| 459 | latmin *= -1.0;
|
|---|
| 460 | double lat = latdeg + latmin / 60;
|
|---|
| 461 | if ("S".equals(ns)) {
|
|---|
| 462 | lat = -lat;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | int londegsep = lengthEast.indexOf('.') - 2;
|
|---|
| 466 | if (londegsep < 0) return null;
|
|---|
| 467 |
|
|---|
| 468 | int londeg = Integer.parseInt(lengthEast.substring(0, londegsep));
|
|---|
| 469 | double lonmin = Double.parseDouble(lengthEast.substring(londegsep));
|
|---|
| 470 | if(londeg < 0) // strange data with '-' sign
|
|---|
| 471 | lonmin *= -1.0;
|
|---|
| 472 | double lon = londeg + lonmin / 60;
|
|---|
| 473 | if ("W".equals(ew)) {
|
|---|
| 474 | lon = -lon;
|
|---|
| 475 | }
|
|---|
| 476 | return new LatLon(lat, lon);
|
|---|
| 477 | }
|
|---|
| 478 | }
|
|---|