Reputation: 87
I receive the latitude and longitude from a GPS with this format:
Latitude: 3328.21103S Longitude: 07043.64734W
I need convert this data to:
Latitude: -33.4701838 Longitude: -70.7274557
How to convert this data in java?
Thanks .
Best Regards.
Upvotes: 3
Views: 8568
Reputation: 87
I solved this way:
1- Separate lattitude and longitude in degree, minutes, seconds, direction(N,S,W,E)
2- Send degree, minutes, seconds, direction to method DMSaDecimal this return: latitude or longitude in decimal
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;
}
Upvotes: 3
Reputation: 34657
Using 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();
}
}
If you're interested in knowing more, wikipedia is a good place to start.
Upvotes: 0