我从GPS接收到的纬度和经度格式如下:
纬度:3328.21103S 经度:07043.64734W
我需要将这些数据转换为:
纬度:-33.4701838 经度:-70.7274557
如何在Java中进行转换?
谢谢。
最好的问候。
我从GPS接收到的纬度和经度格式如下:
纬度:3328.21103S 经度:07043.64734W
我需要将这些数据转换为:
纬度:-33.4701838 经度:-70.7274557
如何在Java中进行转换?
谢谢。
最好的问候。
public double DMSaDecimal(int grados, int minutos, double segundos, String direccion) {
double decimal = Math.signum(grados) * (Math.abs(grados) + (minutos / 60.0) + (segundos / 3600.0));
//reverse for south or west coordinates; north is assumed
if (direccion.equals("S") || direccion.equals("W")) {
decimal *= -1;
}
return decimal;
}
public String[] SeparararDMS(String coordenada, int type) {
String grados = null;
String minutos = null;
String segundos = null;
String direccion = null;
switch (type) {
case 1: // latitude
grados = coordenada.substring(0, 2);
minutos = coordenada.substring(2, 4);
segundos = coordenada.substring(5, coordenada.length() - 1);
break;
case 2: // longitude
grados = coordenada.substring(0, 3);
minutos = coordenada.substring(3, 5);
segundos = coordenada.substring(6, coordenada.length() - 1);
break;
default:
}
double sec = 0;
try {
sec = Double.parseDouble("0."+segundos);
}catch(Exception e) {
}
sec = (sec * 60);
direccion = coordenada.substring(coordenada.length() - 1);
String[] data = {grados, minutos, sec + "", direccion};
return data;
}
使用 arcgis:
public static void convertCoordDMS(){
try{
// create coordinate object for degrees, minutes, seconds format
DMSCoordinate dmsCoord = new DMSCoordinate();
dmsCoord.setString("37 -123 32 34.43"); // put a string value
String dms = dmsCoord.getString(); // get back the normalized DMS string
System.out.println("Input (string): (\"37 -123 32 34.43\")");
System.out.println("Output (normalized string): " + dms + '\n');
// now for decimal degrees
DDCoordinate ddCoord = new DDCoordinate();
// put in the Point from dmsCoord. The Point property is
// “common ground” for all Coordinate objects
IPoint point = null;
ddCoord.setPoint(dmsCoord.getPoint());
// get back the normalized DD string
String dd = ddCoord.getString();
System.out.println("Input (Point): same as DMS");
System.out.println("Output (normalized string): " + dd + '\n');
// and the coordinate as lon lat doubles
double[] x = new double[25];
double[] y = new double[25];
ddCoord.getCoords(x, y);
System.out.println("Input (Point): same as DMS");
System.out.println("Output (Lat Lon): " + x + " " + y);
}
catch (Exception ex){
ex.printStackTrace();
}
}
如果你想了解更多,wikipedia 是一个很好的起点。