package org.oneliveweb.geocode;
import java.util.Map;
import org.dom4j.Element;
//Copyright 2003 Princeton Board of Trustees.
//All rights reserved.
/**
* The Position class represents coordinates given in
* latitude/longitude pairs.
*/
public class Position {
private Double latitude;
private Double longitude;
private Double accuracy;
protected Element result;
public Element getResult()
{
return result;
}
public void setResult(Element inResult)
{
result = inResult;
}
private final double RADIUS_EARTH = 6400000;
/**
* Constructs a new Position with the position indicated by
* the arguments. The arguments can be null if the values are
* not known.
*
* @param latitude
* a Double
* @param longitude
* a Double
*/
public Position(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
/**
* Constructs a new Position that represents the same
* position as the argument; in other words, the newly created position is a
* copy of the argument position.
*
* @param p
* a Position
*/
public Position(Position p) {
longitude = p.longitude;
latitude = p.latitude;
}
public Position(Map inValue)
{
Double lat = (Double)inValue.get("lat");
latitude = lat;
Double lon = (Double)inValue.get("lon");
longitude = lon;
}
/**
* Returns the latitude.
*
* @return the latitude
*/
public double getLatitude() {
return latitude;
}
/**
* Returns the longitude.
*
* @return the longitude
*/
public double getLongitude() {
return longitude;
}
/**
* Sets the latitude.
*
* @param latitude
* a Double
*/
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
/**
* Sets the longitude.
*
* @param longitude
* a Double
*/
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
/**
* Finds the distance in meters between two positions on earth.
*
* @param position
* a Position
*
* @return the distance between two positions
*/
public double distanceTo(Position position) {
double x_A = RADIUS_EARTH
* Math.cos(Math.toRadians(latitude.doubleValue()))
* Math.cos(Math.toRadians(longitude.doubleValue()));
double y_A = RADIUS_EARTH
* Math.cos(Math.toRadians(latitude.doubleValue()))
* Math.sin(Math.toRadians(longitude.doubleValue()));
double z_A = RADIUS_EARTH
* Math.sin(Math.toRadians(latitude.doubleValue()));
double x_B = RADIUS_EARTH
* Math
.cos(Math.toRadians(position.getLatitude()
))
* Math.cos(Math
.toRadians(position.getLongitude()));
double y_B = RADIUS_EARTH
* Math
.cos(Math.toRadians(position.getLatitude()
))
* Math.sin(Math
.toRadians(position.getLongitude()));
double z_B = RADIUS_EARTH
* Math
.sin(Math.toRadians(position.getLatitude()
));
double distance = Math.sqrt((x_A - x_B) * (x_A - x_B) + (y_A - y_B)
* (y_A - y_B) + (z_A - z_B) * (z_A - z_B));
return distance;
}
/**
* Finds the square of the distance between two positions, treating them as
* points on a flat plane.
*
* @param position
* a Position
*
* @return the distance between two positions treated as points on a flat
* plane
*/
public double coordinateDistanceTo(Position position) {
double x_1 = latitude.doubleValue();
double y_1 = longitude.doubleValue();
double x_2 = position.getLatitude();
double y_2 = position.getLongitude();
double x_diff = x_1 - x_2;
double y_diff = y_1 - y_2;
if (y_diff > 180)
y_diff = 360 - y_diff;
return (x_diff) * (x_diff) + (y_diff) * (y_diff);
}
/**
* Returns true if both latitude and longitude are not null and false
* otherwise.
*
* @return whether the position is defined
*/
public boolean isDefined() {
if (latitude == null || longitude == null)
return false;
return true;
}
/**
* Returns a string representation of this Position. This is
* for debugging purposes only.
*
* @return a string representation of this Position
*/
public String toString() {
return "{lat: "+ latitude + ", lng: " + longitude + "}";
}
public Double getAccuracy() {
return accuracy;
}
public void setAccuracy(Double accuracy) {
this.accuracy = accuracy;
}
public String getFormatedAddress()
{
if( result == null)
{
return null;
}
return result.elementTextTrim("formatted_address");
}
}