geography
Class Point

java.lang.Object
  |
  +--geography.Point

public class Point
extends java.lang.Object

A point on the surface of the earth. Represented by a longitude and a latitude. We always write the longitude first, since (longitude,latitude) is rather like (x,y). These measurements are in degrees, not radians.


Field Summary
protected static double GLOBE_RADIUS_EQUATOR
          Radius of the earth, in meters, at the equator.
protected static double GLOBE_RADIUS_POLES
          Radius of the earth, in meters, at the poles.
protected  double latitude
          Latitude of this point, in degrees.
protected  double longitude
          Longitude of this point, in degrees.
 
Constructor Summary
Point(double longitude, double latitude)
           
Point(java.lang.String longitude, java.lang.String latitude)
           
 
Method Summary
protected static double degrees(double radians)
           
 double directionTo(geography.Point p)
          The direction that you have to go from this point to get to p.
 double distanceTo(geography.Point p)
          Distance from this point to another point, using the Haversine formula.
 boolean equals(java.lang.Object p)
           
protected static double globeRadiusOfCurvature(double lat)
          Computes the earth's radius of curvature at a particular latitude, assuming that the earth is a squashed sphere with elliptical cross-section.
 int hashCode()
           
static void main(java.lang.String[] args)
           
protected static double radians(double degrees)
           
protected static double square(double d)
           
static void test(double long1, double lat1, double long2, double lat2)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

GLOBE_RADIUS_EQUATOR

protected static final double GLOBE_RADIUS_EQUATOR
Radius of the earth, in meters, at the equator.

See Also:
Constant Field Values

GLOBE_RADIUS_POLES

protected static final double GLOBE_RADIUS_POLES
Radius of the earth, in meters, at the poles.

See Also:
Constant Field Values

longitude

protected double longitude
Longitude of this point, in degrees.


latitude

protected double latitude
Latitude of this point, in degrees.

Constructor Detail

Point

public Point(double longitude,
             double latitude)

Point

public Point(java.lang.String longitude,
             java.lang.String latitude)
      throws java.lang.NumberFormatException
Method Detail

equals

public boolean equals(java.lang.Object p)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

distanceTo

public double distanceTo(geography.Point p)
Distance from this point to another point, using the Haversine formula. We take the squashed shape of the earth into account (approximately). Q: Why can't we just use the Pythagorean theorem? We could just return sqrt(dx*dx + dy*dy), where dx is the change in latitude and dy is the change in longitude. A: That doesn't take the curvature of the earth into account. Q: But if we're just driving short distances, isn't the curvature of the earth too slight to matter? A: True, but we don't want our code to mysteriously break if we start using it to drive around the world. Anyway, your formula is still more complicated than you think, since you have to find dx in meters. That's harder than you thought: 1 degree of longitude is a long way at the equator, but isn't a lot near the poles.


directionTo

public double directionTo(geography.Point p)
The direction that you have to go from this point to get to p. Answer is returned in degrees, between -180.0 and 180.0. Here -180, -90, 0, 90, and 180 correspond to west, south, east, north, and west again -- just like theta in polar coordinates. It would be tempting to just return atan2(dy,dx); see the comment in distanceTo() for why we don't do that.


radians

protected static final double radians(double degrees)

degrees

protected static final double degrees(double radians)

square

protected static final double square(double d)

globeRadiusOfCurvature

protected static final double globeRadiusOfCurvature(double lat)
Computes the earth's radius of curvature at a particular latitude, assuming that the earth is a squashed sphere with elliptical cross-section. Since we supposedly have latitude and longitude to lots of decimal places, I decided to worry about the squashing, just for fun. The radius of curvature at a latitude is NOT the same as the actual radius. The actual radius is smaller at the poles than at the equator, but the earth is less curved there, as if it were the surface of a *bigger* sphere! The actual radius could be computed by return Math.sqrt(square(GLOBE_RADIUS_EQUATOR*Math.cos(lat)) + square(GLOBE_RADIUS_POLES*Math.sin(lat))); The radius of curvature depends not only on the latitude you're at but also on the direction you are traveling. But I'll use the approximate formula recommended at http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1 which ignores the direction. There is a whole range of possible answers depending on direction; the formula returns the geometric mean of the max and min of that range.

Parameters:
lat - - latitude in radians. This is the angle that a point at this latitude makes with the horizontal.

test

public static void test(double long1,
                        double lat1,
                        double long2,
                        double lat2)

main

public static void main(java.lang.String[] args)