Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / formats / nmea / NmeaTrackPoint.java
blobea69569d6073d291c5c39b87cb80b600a4fc7314
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.formats.nmea;
9 import gov.nasa.worldwind.*;
11 /**
12 * @author tag
13 * @version $Id: NmeaTrackPoint.java 513 2007-01-18 00:33:16Z ericdalgliesh $
15 public class NmeaTrackPoint implements gov.nasa.worldwind.TrackPoint
17 private double latitude;
18 private double longitude;
19 private double altitude;
20 private double geoidHeight;
21 private String time;
23 /**
24 * @param words
25 * @throws IllegalArgumentException if <code>words</code> is null or has length less than 1
27 public NmeaTrackPoint(String[] words)
29 if (words == null)
31 String msg = WorldWind.retrieveErrMsg("nullValue.ArrayIsNull");
32 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
33 throw new IllegalArgumentException(msg);
35 if (words.length < 1)
37 String msg = WorldWind.retrieveErrMsg("generic.arrayInvalidLength") + WorldWind.retrieveErrMsg(
38 "punctuation.space") + words.length;
39 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
40 throw new IllegalArgumentException(msg);
43 if (words[0].equalsIgnoreCase("GPGGA"))
44 this.doGGA(words);
45 else if (words[0].equalsIgnoreCase("GPRMC"))
46 this.doRMC(words);
49 /**
50 * @param words
51 * @throws IllegalArgumentException if <code>words</code> is null or has length less than 6
53 private void doGGA(String[] words)
55 // words won't be null, but it could be the wrong length
56 if (words.length < 6)
58 String msg = WorldWind.retrieveErrMsg("generic.arrayInvalidLength") + WorldWind.retrieveErrMsg(
59 "punctuation.space") + words.length;
60 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
61 throw new IllegalArgumentException(msg);
64 this.time = words[1];
65 this.latitude = this.parseLatitude(words[2], words[3]);
66 this.longitude = this.parseLongitude(words[4], words[5]);
67 if (words.length >= 11)
68 this.altitude = this.parseElevation(words[9], words[10]);
69 if (words.length >= 13)
70 this.geoidHeight = this.parseElevation(words[11], words[12]);
73 private void doRMC(String[] words)
77 private double parseLatitude(String angle, String direction)
79 if (angle.length() == 0)
80 return 0;
82 double minutes = angle.length() > 2 ? Double.parseDouble(angle.substring(2, angle.length())) : 0d;
83 double degrees = Double.parseDouble(angle.substring(0, 2)) + minutes / 60d;
85 return direction.equalsIgnoreCase("S") ? -degrees : degrees;
88 private double parseLongitude(String angle, String direction)
90 if (angle.length() == 0)
91 return 0;
93 double minutes = angle.length() > 3 ? Double.parseDouble(angle.substring(3, angle.length())) : 0d;
94 double degrees = Double.parseDouble(angle.substring(0, 3)) + minutes / 60d;
96 return direction.equalsIgnoreCase("W") ? -degrees : degrees;
99 private double parseElevation(String height, String units)
101 if (height.length() == 0)
102 return 0;
104 return Double.parseDouble(height) * unitsToMeters(units);
107 private double unitsToMeters(String units)
109 double f;
111 if (units.equals("M")) // meters
112 f = 1d;
113 else if (units.equals("f")) // feet
114 f = 3.2808399;
115 else if (units.equals("F")) // fathoms
116 f = 0.5468066528;
117 else
118 f = 1d;
120 return f;
123 public double getLatitude()
125 return latitude;
129 * @param latitude
130 * @throws IllegalArgumentException if <code>latitude</code> is less than -90 or greater than 90
132 public void setLatitude(double latitude)
134 if (latitude > 90 || latitude < -90)
136 String msg = WorldWind.retrieveErrMsg("generic.angleOutOfRange") + WorldWind.retrieveErrMsg(
137 "punctuation.space") + latitude;
138 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
139 throw new IllegalArgumentException(msg);
142 this.latitude = latitude;
145 public double getLongitude()
147 return longitude;
151 * @param longitude
152 * @throws IllegalArgumentException if <code>longitude</code> is less than -180 or greater than 180
154 public void setLongitude(double longitude)
156 if (longitude > 180 || longitude < -180)
158 String msg = WorldWind.retrieveErrMsg("generic.angleOutOfRange") + WorldWind.retrieveErrMsg(
159 "punctuation.space") + longitude;
160 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
161 throw new IllegalArgumentException(msg);
164 this.longitude = longitude;
167 public double getElevation()
169 return this.altitude + this.geoidHeight;
172 public void setElevation(double elevation)
174 this.altitude = elevation;
175 this.geoidHeight = 0;
178 public String getTime()
180 return time;
183 public void setTime(String time)
185 this.time = time;
188 @Override
189 public String toString()
191 return String.format("(%10.8f\u00B0, %11.8f\u00B0, %10.4g m, %10.4g m, %s)", this.latitude, this.longitude,
192 this.altitude, this.geoidHeight, this.time);