49 lines
1.8 KiB
Java
49 lines
1.8 KiB
Java
|
|
package zutil.converters;
|
|||
|
|
|
|||
|
|
public class WGS84Converter {
|
|||
|
|
public static void main(String[] args){
|
|||
|
|
System.out.println(toWGS84Decimal("N 59<35> 47' 43\"")+" "+toWGS84Decimal(" E 17<31> 42' 55\""));
|
|||
|
|
System.out.println(toWGS84Decimal("55<EFBFBD> 0' 0\"")+" "+toWGS84Decimal("68<EFBFBD> 59' 59,999\""));
|
|||
|
|
System.out.println(toWGS84Decimal("55<EFBFBD> 0.001'")+" "+toWGS84Decimal("68<EFBFBD> 59.999'"));
|
|||
|
|
System.out.println(toWGS84Decimal("3444.0000S")+" "+toWGS84Decimal("13521.0000E"));
|
|||
|
|
System.out.println(toWGS84Decimal("-44.0001")+" "+toWGS84Decimal("521.0001"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Converts an WGS84 coordinate to an WGS84 decimal coordinate
|
|||
|
|
*
|
|||
|
|
* @param coordinate is the coordinate to convert
|
|||
|
|
* @return the new coordinate in decimal degrees, returns 0 if conversions fails
|
|||
|
|
*/
|
|||
|
|
public static float toWGS84Decimal(String coordinate){
|
|||
|
|
float deg=0, min=0, sec=0, neg=1;
|
|||
|
|
coordinate = coordinate.trim().replaceAll(",", ".").toUpperCase();
|
|||
|
|
if(coordinate.contains("S") || coordinate.contains("W"))
|
|||
|
|
neg = -1;
|
|||
|
|
|
|||
|
|
// 55<35> 0' 68<36> 59,999 or 55<35> 0' 0" 68<36> 59' 59,999"
|
|||
|
|
if(coordinate.matches("[NSWE ]? ?[0-9]{1,3}<7D> [0-9]{1,2}.?[0-9]*'[ 0-9.\\\"]*")){
|
|||
|
|
coordinate = coordinate.replaceAll("[NSEW<45>'\\\"]", "").trim();
|
|||
|
|
String[] tmp = coordinate.split(" ");
|
|||
|
|
deg = Float.parseFloat(tmp[0]);
|
|||
|
|
min = Float.parseFloat(tmp[1]);
|
|||
|
|
if(tmp.length > 2){
|
|||
|
|
sec = Float.parseFloat(tmp[2]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 3444.0000S 13521.0000E
|
|||
|
|
else if(coordinate.matches("[0-9]{4,5}.[0-9]*[NSEW]{1}")){
|
|||
|
|
coordinate = coordinate.replaceAll("[NS EW]", "");
|
|||
|
|
float tmpf = Float.parseFloat(coordinate);
|
|||
|
|
deg = (int)(tmpf/100);
|
|||
|
|
min = tmpf-(deg*100);
|
|||
|
|
}
|
|||
|
|
// 55.0 68.99999
|
|||
|
|
else if(coordinate.matches("\\-?[0-9]{2,3}.[0-9]*")){
|
|||
|
|
return Float.parseFloat(coordinate);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return neg*(deg + min/60 + sec/3600);
|
|||
|
|
}
|
|||
|
|
}
|