Worldwind public release 0.2.1
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Position.java
blob40e5b79dbdf3f26ab91bf74f6904a951972d1100
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.*;
11 /**
12 * @author tag
13 * @version $Id: Position.java 2233 2007-07-06 23:56:34Z 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 = WorldWind.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
38 WorldWind.logger().log(java.util.logging.Level.FINE, 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 = WorldWind.retrieveErrMsg("nullValue.LatLonIsNull");
52 WorldWind.logger().log(java.util.logging.Level.FINE, 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 Position add(Position that)
83 Angle lat = Angle.normalizedLatitude(this.latitude.add(that.latitude));
84 Angle lon = Angle.normalizedLongitude(this.longitude.add(that.longitude));
86 return new Position(lat, lon, this.elevation + that.elevation);
89 public Position subtract(Position that)
91 Angle lat = Angle.normalizedLatitude(this.latitude.subtract(that.latitude));
92 Angle lon = Angle.normalizedLongitude(this.longitude.subtract(that.longitude));
94 return new Position(lat, lon, this.elevation - that.elevation);
97 public static boolean positionsCrossLongitudeBoundary(Iterable<Position> positions)
99 if (positions == null)
101 String msg = WorldWind.retrieveErrMsg("nullValue.PositionsListIsNull");
102 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
103 throw new IllegalArgumentException(msg);
106 Position pos = null;
107 for (Position posNext : positions)
109 if (pos != null)
111 // A segment cross the line if end pos have different longitude signs
112 // and are more than 180 degress longitude apart
113 if (Math.signum(pos.getLongitude().degrees) != Math.signum(posNext.getLongitude().degrees))
115 if (Math.abs(pos.getLongitude().degrees - posNext.getLongitude().degrees) > 180)
116 return true;
119 pos = posNext;
122 return false;
126 * Obtains the elevation of this position
128 * @return this position's elevation
130 public final double getElevation()
132 return this.elevation;
135 public String toString()
137 return "(" + this.latitude.toString() + ", " + this.longitude.toString() + ", " + this.elevation;