/**
* GeoDress - A program for reverse geocoding
* Copyright (C) 2010 Stefan T.
*
* See COPYING for Details.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package geodress.model;
import geodress.exceptions.ParameterOutOfRangeException;
import java.text.DecimalFormat;
/**
* Represents geographic coordinates with latitude and longitude.
*
* @author Stefan T.
*/
public class Coordinate {
/** the geographic latitude in degrees (-90 <= x <= 90) */
private double latitude;
/** the geographic longitude in degrees (-180 <= x <= 180) */
private double longitude;
/**
* Constructs a new coordinate.
*
* @param latitude
* the latitude
* @param longitude
* the longitude
* @throws ParameterOutOfRangeException
* thrown if latitude or longitude are invalid
*/
public Coordinate(double latitude, double longitude)
throws ParameterOutOfRangeException {
setLatitude(latitude);
setLongitude(longitude);
}
/**
* Constructs a new coordinate.
*
* @param latitude
* the latitude as a string
* @param longitude
* the longitude as a string
* @throws ParameterOutOfRangeException
* thrown if latitude or longitude are invalid or if a value was
* given that is not a number
*/
public Coordinate(String latitude, String longitude)
throws ParameterOutOfRangeException {
try {
setLatitude(Double.valueOf(latitude));
setLongitude(Double.valueOf(longitude));
} catch (NumberFormatException nfe) {
throw new ParameterOutOfRangeException(
"the given values where not a number");
}
}
/**
* Gets the latitude.
*
* @return the latitude in the format <i>N 48° 51,928'</i>
*/
public String getLatitudeAsString() {
DecimalFormat df1 = new DecimalFormat("00");
DecimalFormat df2 = new DecimalFormat("00.000");
String prefix = "N";
if (getLatitude() < 0)
prefix = "S";
int degree = (int) Math.abs(getLatitude());
double minutes = (double) Math
.round((Math.abs(getLatitude()) - degree) * 60 * 1000) / 1000;
return prefix + " " + df1.format(degree) + "\u00B0 "
+ df2.format(minutes) + "\u0027";
}
/**
* Gets the latitude.
*
* @return the latitude in degrees
*/
public double getLatitude() {
return latitude;
}
/**
* Sets a latitude.
*
* @param latitude
* the new latitude in degrees
* @throws ParameterOutOfRangeException
* thrown if the latitude is invalid
*/
public void setLatitude(double latitude)
throws ParameterOutOfRangeException {
if (-90 <= latitude && latitude <= 90) {
this.latitude = latitude;
} else {
throw new ParameterOutOfRangeException(
"longitude may be min. -90 and max. 90 degrees");
}
}
/**
* Gets the longitude
*
* @return the longitude in the format <i>E 002° 19,273</i>
*/
public String getLongitudeAsString() {
DecimalFormat df1 = new DecimalFormat("000");
DecimalFormat df2 = new DecimalFormat("00.000");
String prefix = "E";
if (getLongitude() < 0)
prefix = "W";
int degree = (int) Math.abs(getLongitude());
double minutes = (double) Math
.round((Math.abs(getLongitude()) - degree) * 60 * 1000) / 1000;
return prefix + " " + df1.format(degree) + "\u00B0 "
+ df2.format(minutes) + "\u0027";
}
/**
* Gets the longitude.
*
* @return the longitude in degrees
*/
public double getLongitude() {
return longitude;
}
/**
* Sets a longitude.
*
* @param longitude
* the new longitude in degrees
* @throws ParameterOutOfRangeException
* thrown if the longitude is invalid
*/
public void setLongitude(double longitude)
throws ParameterOutOfRangeException {
if (-180 <= longitude && longitude <= 180) {
this.longitude = longitude;
} else {
throw new ParameterOutOfRangeException(
"longitude may be min. -180 and max. 180 degrees");
}
}
/**
* Returns the coordinates as a string.
*
* @return a string in the form <i>N 48° 51,928' E 002° 19,273</i>
*/
@Override
public String toString() {
return getLatitudeAsString() + " " + getLongitudeAsString();
}
/**
* Converts a coordinate from format <i>DDD MM SS.SSS</i> to <i>DDD.DDD</i>.
*
* @param degree
* degrees of coordinate
* @param minutes
* minutes of coordinate
* @param seconds
* seconds of coordinate
* @return the coordinate in degrees
*/
public static double convertToDecimal(int degree, int minutes,
double seconds) {
return (((seconds / 60) + minutes) / 60) + degree;
}
}