Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / formats / nmea / NmeaTrackPoint.java
blob61d3ec2cb830da9c18c743a7526188ce973a001d
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.tracks.TrackPoint;
10 import gov.nasa.worldwind.util.Logging;
12 /**
13 * @author tag
14 * @version $Id: NmeaTrackPoint.java 2471 2007-07-31 21:50:57Z tgaskins $
16 public class NmeaTrackPoint implements TrackPoint
18 private double latitude;
19 private double longitude;
20 private double altitude;
21 private double geoidHeight;
22 private String time;
24 /**
25 * @param words
26 * @throws IllegalArgumentException if <code>words</code> is null or has length less than 1
28 public NmeaTrackPoint(String[] words)
30 if (words == null)
32 String msg = Logging.getMessage("nullValue.ArrayIsNull");
33 Logging.logger().severe(msg);
34 throw new IllegalArgumentException(msg);
36 if (words.length < 1)
38 String msg = Logging.getMessage("generic.ArrayInvalidLength", words.length);
39 Logging.logger().severe(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 = Logging.getMessage("generic.ArrayInvalidLength", words.length);
59 Logging.logger().severe(msg);
60 throw new IllegalArgumentException(msg);
63 this.time = words[1];
64 this.latitude = this.parseLatitude(words[2], words[3]);
65 this.longitude = this.parseLongitude(words[4], words[5]);
66 if (words.length >= 11)
67 this.altitude = this.parseElevation(words[9], words[10]);
68 if (words.length >= 13)
69 this.geoidHeight = this.parseElevation(words[11], words[12]);
72 private void doRMC(String[] words)
76 private double parseLatitude(String angle, String direction)
78 if (angle.length() == 0)
79 return 0;
81 double minutes = angle.length() > 2 ? Double.parseDouble(angle.substring(2, angle.length())) : 0d;
82 double degrees = Double.parseDouble(angle.substring(0, 2)) + minutes / 60d;
84 return direction.equalsIgnoreCase("S") ? -degrees : degrees;
87 private double parseLongitude(String angle, String direction)
89 if (angle.length() == 0)
90 return 0;
92 double minutes = angle.length() > 3 ? Double.parseDouble(angle.substring(3, angle.length())) : 0d;
93 double degrees = Double.parseDouble(angle.substring(0, 3)) + minutes / 60d;
95 return direction.equalsIgnoreCase("W") ? -degrees : degrees;
98 private double parseElevation(String height, String units)
100 if (height.length() == 0)
101 return 0;
103 return Double.parseDouble(height) * unitsToMeters(units);
106 private double unitsToMeters(String units)
108 double f;
110 if (units.equals("M")) // meters
111 f = 1d;
112 else if (units.equals("f")) // feet
113 f = 3.2808399;
114 else if (units.equals("F")) // fathoms
115 f = 0.5468066528;
116 else
117 f = 1d;
119 return f;
122 public double getLatitude()
124 return latitude;
128 * @param latitude
129 * @throws IllegalArgumentException if <code>latitude</code> is less than -90 or greater than 90
131 public void setLatitude(double latitude)
133 if (latitude > 90 || latitude < -90)
135 String msg = Logging.getMessage("generic.AngleOutOfRange", latitude);
136 Logging.logger().severe(msg);
137 throw new IllegalArgumentException(msg);
140 this.latitude = latitude;
143 public double getLongitude()
145 return longitude;
149 * @param longitude
150 * @throws IllegalArgumentException if <code>longitude</code> is less than -180 or greater than 180
152 public void setLongitude(double longitude)
154 if (longitude > 180 || longitude < -180)
156 String msg = Logging.getMessage("generic.AngleOutOfRange", longitude);
157 Logging.logger().severe(msg);
158 throw new IllegalArgumentException(msg);
161 this.longitude = longitude;
164 public double getElevation()
166 return this.altitude + this.geoidHeight;
169 public void setElevation(double elevation)
171 this.altitude = elevation;
172 this.geoidHeight = 0;
175 public String getTime()
177 return time;
180 public void setTime(String time)
182 this.time = time;
185 @Override
186 public String toString()
188 return String.format("(%10.8f\u00B0, %11.8f\u00B0, %10.4g m, %10.4g m, %s)", this.latitude, this.longitude,
189 this.altitude, this.geoidHeight, this.time);