Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Position.java
blob0cd374f224c5313b6f5bb84c3e7296894412d3a8
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.geom;
9 import gov.nasa.worldwind.util.Logging;
11 /**
12 * @author tag
13 * @version $Id: Position.java 2568 2007-08-16 22:28:18Z tgaskins $
15 public class Position
17 private final Angle latitude;
18 private final Angle longitude;
19 private final double elevation;
21 public static final Position ZERO = new Position(Angle.ZERO, Angle.ZERO, 0d);
23 public static Position fromRadians(double latitude, double longitude, double elevation)
25 return new Position(Angle.fromRadians(latitude), Angle.fromRadians(longitude), elevation);
28 public static Position fromDegrees(double latitude, double longitude, double elevation)
30 return new Position(Angle.fromDegrees(latitude), Angle.fromDegrees(longitude), elevation);
33 public Position(Angle latitude, Angle longitude, double elevation)
35 if (latitude == null || longitude == null)
37 String message = Logging.getMessage("nullValue.LatitudeOrLongitudeIsNull");
38 Logging.logger().severe(message);
39 throw new IllegalArgumentException(message);
42 this.latitude = latitude;
43 this.longitude = longitude;
44 this.elevation = elevation;
47 public Position(LatLon latLon, double elevation)
49 if (latLon == null)
51 String message = Logging.getMessage("nullValue.LatLonIsNull");
52 Logging.logger().severe(message);
53 throw new IllegalArgumentException(message);
56 this.latitude = latLon.getLatitude();
57 this.longitude = latLon.getLongitude();
58 this.elevation = elevation;
61 /**
62 * Obtains the latitude of this position
64 * @return this position's latitude
66 public final Angle getLatitude()
68 return this.latitude;
71 /**
72 * Obtains the longitude of this position
74 * @return this position's longitude
76 public final Angle getLongitude()
78 return this.longitude;
81 public LatLon getLatLon()
83 return new LatLon(this.getLatitude(), this.getLongitude());
86 public Position add(Position that)
88 Angle lat = Angle.normalizedLatitude(this.latitude.add(that.latitude));
89 Angle lon = Angle.normalizedLongitude(this.longitude.add(that.longitude));
91 return new Position(lat, lon, this.elevation + that.elevation);
94 public Position subtract(Position that)
96 Angle lat = Angle.normalizedLatitude(this.latitude.subtract(that.latitude));
97 Angle lon = Angle.normalizedLongitude(this.longitude.subtract(that.longitude));
99 return new Position(lat, lon, this.elevation - that.elevation);
102 public static boolean positionsCrossLongitudeBoundary(Iterable<Position> positions)
104 if (positions == null)
106 String msg = Logging.getMessage("nullValue.PositionsListIsNull");
107 Logging.logger().severe(msg);
108 throw new IllegalArgumentException(msg);
111 Position pos = null;
112 for (Position posNext : positions)
114 if (pos != null)
116 // A segment cross the line if end pos have different longitude signs
117 // and are more than 180 degress longitude apart
118 if (Math.signum(pos.getLongitude().degrees) != Math.signum(posNext.getLongitude().degrees))
120 if (Math.abs(pos.getLongitude().degrees - posNext.getLongitude().degrees) > 180)
121 return true;
124 pos = posNext;
127 return false;
131 * Obtains the elevation of this position
133 * @return this position's elevation
135 public final double getElevation()
137 return this.elevation;
140 public String toString()
142 return "(" + this.latitude.toString() + ", " + this.longitude.toString() + ", " + this.elevation + ")";
145 public static boolean positionsCrossLongitudeBoundary(Position p1, Position p2)
147 if (p1 == null || p2 == null)
149 String msg = Logging.getMessage("nullValue.PositionsListIsNull");
150 Logging.logger().severe(msg);
151 throw new IllegalArgumentException(msg);
154 // A segment cross the line if end pos have different longitude signs
155 // and are more than 180 degress longitude apart
156 if (Math.signum(p1.getLongitude().degrees) != Math.signum(p2.getLongitude().degrees))
158 if (Math.abs(p1.getLongitude().degrees - p2.getLongitude().degrees) > 180)
159 return true;
162 return false;