Show LatitudeLongitude.java syntax highlighted
//-----------------------------------------------------------------------------
// GridReferenceConverter.java
//
// (c) 2003 Jonathan Stott
//
// 0.2 - 02 Mar 2004
// - Added exceptions to setLongitude() and setLatitude()
// 0.1 - 11 Nov 2003
// - First version
//-----------------------------------------------------------------------------
package fi.hip.gb.bluetooth.coordconv;
/**
* An object to represent a latitude and longitude pair
*
* @author Jonathan Stott
* @version 0.2
* @since 0.1
*/
public class LatitudeLongitude {
private Float latitude;
private Float longitude;
/**
* Construct a latitude and longitude pair
*
* @param lat the latitude
* @param lng the longitude
*/
public LatitudeLongitude(Float lat, Float lng) {
latitude = lat;
longitude = lng;
}
/**
* Construct a latitude and longitude pair
*
* @param degreesN degrees of latitude
* @param minutesN minutes of latitude
* @param secondsN seconds of latitude
* @param degreesE degrees of longitude
* @param minutesE minutes of longitude
* @param secondsE seconds of longitude
*/
public LatitudeLongitude(int degreesN, int minutesN, Float secondsN,
int degreesE, int minutesE, Float secondsE) {
latitude = new Float(degreesN+((minutesN+(secondsN.floatValue()/60.0))/60.0));
longitude = new Float(degreesE+((minutesE+(secondsE.floatValue()/60.0))/60.0));
}
public static Float parse(String coord) {
try {
return new Float(coord);
} catch(NumberFormatException nfe) {
coord = coord.toLowerCase();
int start = 0;
int end = coord.indexOf("d");
int deg = Integer.parseInt(coord.substring(start, end));
start = end+1;
end = coord.indexOf("m");
int min = Integer.parseInt(coord.substring(start, end));
start = end+1;
end = coord.indexOf("s");
double sec = Double.parseDouble(coord.substring(start, end));
double res = deg+((min+(sec/60.0))/60.0);
if(coord.indexOf("S") != -1 || coord.indexOf("W")!=-1) {
return new Float(-res);
}
return new Float(res);
}
}
/**
* Get the latitude
*
* @return the latitude
*/
public Float getLatitude() {
return latitude;
}
/**
* Get the longitude
*
* @return the longitude
*/
public Float getLongitude() {
return longitude;
}
/**
* Set the latitude
*
* @param d the new value of the latitude
*/
public void setLatitude(Float d) {
if (d.floatValue() > 90 || d.floatValue() < -90) {
throw new IllegalArgumentException("Latitude must be between -90 and " +
"90");
}
latitude = d;
}
/**
* Sets the value of longitude.
*
* @param d the new value of the longitude
*/
public void setLongitude(Float d) {
if (d.floatValue() > 180 || d.floatValue() < -180) {
throw new IllegalArgumentException("Longitude must be between -180 and " +
"180");
}
longitude = d;
}
/**
* Gets a string representation of the latitude in the form
* 52d39m27.2531s
* @return string presentation of the latitude
*/
public String getLatitudeMinSec() {
String[] arr = toStringArray(getLatitude());
return arr[0] + "d" + arr[1] + "m" + arr[2] + "s";
}
/**
* Gets the north or south side of latitude.
* @return S or N depending on the current latitude
*/
public String getLatitudeNS() {
if (getLatitude().floatValue() < 0) {
return "S";
}
return "N";
}
/**
* Prints the value in degrees, minutes and seconds
* @param tude
* @return array of [degree, minutes, seconds]
*/
public String[] toStringArray(Float tude) {
int deg = (int)Math.floor(Math.abs(tude.floatValue()));
int min = (int)Math.floor((Math.abs(tude.floatValue()) - deg) * 60);
float sec = (((Math.abs(tude.floatValue()) - deg) * 60) - min) * 60;
sec = ((int)(sec *100)) / 100.f;
return new String[] {
Integer.toString(deg),
Integer.toString(min),
Float.toString(sec)
};
}
/**
* Get a string representation of the longitude in the form
* 1d43m4.5177s
* @return string presentation of the longitude
*/
public String getLongitudeMinSec() {
String[] arr = toStringArray(getLongitude());
return arr[0] + "d" + arr[1] + "m" + arr[2] + "s";
}
/**
* Gets the east or west side of longitude.
* @return E or W depending on the current longitude
*/
public String getLongitudeEW() {
if (getLongitude().floatValue() < 0) {
return "W";
}
return "E";
}
/**
* Get a string representation of the latitude and longitude in the form
* 52d39m27.2531sN 1d43m4.5177sE
*
* @return string presentation of the latitude and longitude
*/
public String toString() {
return getLatitudeMinSec() + getLatitudeNS()
+ " "
+ getLongitudeMinSec() + getLongitudeEW();
}
}
See more files for this project here
GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.
Project homepage:
http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other
LatitudeLongitude.java