如何在Java中将十进制DMS格式的纬度和经度转换为度数

3

我从GPS接收到的纬度和经度格式如下:

纬度:3328.21103S 经度:07043.64734W

我需要将这些数据转换为:

纬度:-33.4701838 经度:-70.7274557

如何在Java中进行转换?

谢谢。

最好的问候。


获取输入字符串的最后一个索引,并使用该索引来确定是否需要负号。 - Brendan Lesniak
2个回答

3
我这样解决的:
1- 将纬度和经度分离为度、分、秒和方向(N、S、W、E)
2- 将度、分、秒和方向发送到DMSaDecimal方法,该方法返回:十进制表示的纬度或经度
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;
}

我认为,不使用英语语言是一种反模式。 - bmoss

0

使用 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 是一个很好的起点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接