Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / LatLon.java
blob46998b4b88adb971f7990a18b1408cf2328a5b9f
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 * Represents a point on the two-dimensional surface of a globe. Latitude is the degrees North and ranges between [-90,
13 * 90], while longitude refers to degrees East, and ranges between (-180, 180].
14 * <p/>
15 * Instances of <code>LatLon</code> are immutable.
17 * @author Tom Gaskins
18 * @version $Id: LatLon.java 1749 2007-05-06 19:48:14Z tgaskins $
20 public class LatLon
22 private final Angle latitude;
23 private final Angle longitude;
25 /**
26 * Factor method for obtaining a new <code>LatLon</code> from two angles expressed in radians.
28 * @param latitude in radians
29 * @param longitude in radians
30 * @return a new <code>LatLon</code> from the given angles, which are expressed as radians
32 public static LatLon fromRadians(double latitude, double longitude)
34 return new LatLon(Math.toDegrees(latitude), Math.toDegrees(longitude));
37 /**
38 * Factory method for obtaining a new <code>LatLon</code> from two angles expressed in degrees.
40 * @param latitude in degrees
41 * @param longitude in degrees
42 * @return a new <code>LatLon</code> from the given angles, which are expressed as degrees
44 public static LatLon fromDegrees(double latitude, double longitude)
46 return new LatLon(latitude, longitude);
49 private LatLon(double latitude, double longitude)
51 this.latitude = Angle.fromDegrees(latitude);
52 this.longitude = Angle.fromDegrees(longitude);
55 /**
56 * Contructs a new <code>LatLon</code> from two angles. Neither angle may be null.
58 * @param latitude
59 * @param longitude
60 * @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null
62 public LatLon(Angle latitude, Angle longitude)
64 if (latitude == null || longitude == null)
66 String message = WorldWind.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
67 WorldWind.logger().log(java.util.logging.Level.FINE, message);
68 throw new IllegalArgumentException(message);
71 this.latitude = latitude;
72 this.longitude = longitude;
75 /**
76 * Obtains the latitude of this <code>LatLon</code>.
78 * @return this <code>LatLon</code>'s latitude
80 public final Angle getLatitude()
82 return this.latitude;
85 /**
86 * Obtains the longitude of this <code>LatLon</code>.
88 * @return this <code>LatLon</code>'s longitude
90 public final Angle getLongitude()
92 return this.longitude;
95 public static LatLon interpolate(double t, LatLon begin, LatLon end)
97 if (begin == null || end == null)
99 String message = WorldWind.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
100 WorldWind.logger().log(java.util.logging.Level.FINE, message);
101 throw new IllegalArgumentException(message);
104 if (t < 0)
105 return begin;
106 else if (t > 1)
107 return end;
108 Quaternion beginQuat = Quaternion.EulerToQuaternion(begin.getLongitude().getRadians(),
109 begin.getLatitude().getRadians(), 0);
110 Quaternion endQuat = Quaternion.EulerToQuaternion(end.getLongitude().getRadians(),
111 end.getLatitude().getRadians(), 0);
112 Quaternion q = Quaternion.Slerp(beginQuat, endQuat, t);
113 Point v = Quaternion.QuaternionToEuler(q);
114 if (Double.isNaN(v.x()) || Double.isNaN(v.y()))
115 return null;
116 return LatLon.fromRadians(v.y(), v.x());
119 @Override
120 public String toString()
122 return "(" + this.latitude.toString() + ", " + this.longitude.toString() + ")";
125 @Override
126 public boolean equals(Object o)
128 if (this == o)
129 return true;
130 if (o == null || getClass() != o.getClass())
131 return false;
133 final gov.nasa.worldwind.geom.LatLon latLon = (gov.nasa.worldwind.geom.LatLon) o;
135 if (!latitude.equals(latLon.latitude))
136 return false;
137 //noinspection RedundantIfStatement
138 if (!longitude.equals(latLon.longitude))
139 return false;
141 return true;
144 @Override
145 public int hashCode()
147 int result;
148 result = latitude.hashCode();
149 result = 29 * result + longitude.hashCode();
150 return result;