1 18 package org.osgi.util.position; 19 20 import org.osgi.util.measurement.Measurement; 21 import org.osgi.util.measurement.Unit; 22 23 39 public class Position { 40 private Measurement altitude; 41 private Measurement longitude; 42 private Measurement latitude; 43 private Measurement speed; 44 private Measurement track; 45 46 60 public Position(Measurement lat, Measurement lon, Measurement alt, 61 Measurement speed, Measurement track) { 62 if (lat != null) { 63 if (!Unit.rad.equals(lat.getUnit())) { 64 throw new IllegalArgumentException ("Invalid Latitude"); 65 } 66 this.latitude = lat; 67 } 68 if (lon != null) { 69 if (!Unit.rad.equals(lon.getUnit())) { 70 throw new IllegalArgumentException ("Invalid Longitude"); 71 } 72 this.longitude = lon; 73 } 74 normalizeLatLon(); 75 if (alt != null) { 76 if (!Unit.m.equals(alt.getUnit())) { 77 throw new IllegalArgumentException ("Invalid Altitude"); 78 } 79 this.altitude = alt; 80 } 81 if (speed != null) { 82 if (!Unit.m_s.equals(speed.getUnit())) { 83 throw new IllegalArgumentException ("Invalid Speed"); 84 } 85 this.speed = speed; 86 } 87 if (track != null) { 88 if (!Unit.rad.equals(track.getUnit())) { 89 throw new IllegalArgumentException ("Invalid Track"); 90 } 91 this.track = normalizeTrack(track); 92 } 93 } 94 95 102 public Measurement getAltitude() { 103 return altitude; 104 } 105 106 113 public Measurement getLongitude() { 114 return longitude; 115 } 116 117 124 public Measurement getLatitude() { 125 return latitude; 126 } 127 128 135 public Measurement getSpeed() { 136 return speed; 137 } 138 139 148 public Measurement getTrack() { 149 return track; 150 } 151 152 private static final double LON_RANGE = Math.PI; 153 private static final double LAT_RANGE = Math.PI / 2.0D; 154 155 165 private void normalizeLatLon() { 166 if (longitude == null || latitude == null) 167 return; 168 double dlon = longitude.getValue(); 169 double dlat = latitude.getValue(); 170 if (dlon >= -LON_RANGE && dlon < LON_RANGE && dlat >= -LAT_RANGE 171 && dlat <= LAT_RANGE) 172 return; 173 dlon = normalize(dlon, LON_RANGE); 174 dlat = normalize(dlat, LAT_RANGE * 2.0D); if (dlat > LAT_RANGE || dlat < -LAT_RANGE) { 177 dlon = normalize(dlon - LON_RANGE, LON_RANGE); 178 dlat = normalize((LAT_RANGE * 2.0D) - dlat, LAT_RANGE); 179 } 180 longitude = new Measurement(dlon, longitude.getError(), longitude 181 .getUnit(), longitude.getTime()); 182 latitude = new Measurement(dlat, latitude.getError(), latitude 183 .getUnit(), latitude.getTime()); 184 } 185 186 204 private double normalize(double value, double range) { 205 double twiceRange = 2.0D * range; 206 while (value >= range) { 207 value -= twiceRange; 208 } 209 while (value < -range) { 210 value += twiceRange; 211 } 212 return value; 213 } 214 215 private static final double TRACK_RANGE = Math.PI * 2.0D; 216 217 225 private Measurement normalizeTrack(Measurement track) { 226 double value = track.getValue(); 227 if ((0.0D <= value) && (value < TRACK_RANGE)) { 228 return track; 229 } 230 value %= TRACK_RANGE; 231 if (value < 0.0D) { 232 value += TRACK_RANGE; 233 } 234 return new Measurement(value, track.getError(), track.getUnit(), track 235 .getTime()); 236 } 237 } 238 | Popular Tags |