zhongrj
2024-12-10 a9a2d0f1d8d271985869dd2b26ac29217191ab51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.springblade.common.utils;
 
import java.math.BigDecimal;
 
public class PositionUtil {
 
    /**
     * 度分秒转经纬度
     * @param lng
     * @return
     */
    public static Double tranformPos(String lng){
        String[] lntArr = lng
            .trim()
            .replace("°", ";")
            .replace("˚", ";")
            .replace("′", ";")
            .replace("'", ";")
            .replace("″", ";")
            .replace("\"", "")
            .split(";");
        double degrees = Double.parseDouble(lntArr[0]);
        double minutes = Double.parseDouble(lntArr[1])/60;
        double seconds = Double.parseDouble(lntArr[2])/3600;
        double decimal = degrees + minutes + seconds;
        BigDecimal bd = new BigDecimal(decimal);
        double roundedNumber = bd.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue();
        // 返回
        return roundedNumber;
    }
 
    /**
     * 度分秒转经纬度
     *
     * @param dms 116°25'7.85"
     * @return 116.418847
     */
    public static Double dfm2LatLng(String dms) {
        Double flag = 0.0;
        if (dms == null) return flag;
        try {
            dms = dms.replace(" ", "");
            String[] str2 = dms.split("°");
            if (str2.length < 2) return flag;
            int d = Integer.parseInt(str2[0]);
            String[] str3 = str2[1].split("\'");
            if (str3.length < 2) return flag;
            int f = Integer.parseInt(str3[0]);
            String str4 = str3[1].substring(0, str3[1].length() - 1);
            double m = Double.parseDouble(str4);
 
            double fen = f + (m / 60);
            double du = (fen / 60) + Math.abs(d);
            if (d < 0) du = -du;
            return Double.parseDouble(String.format("%.7f", du));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
 
    // 引用形式的描述信息
    public static Double DMSToDecimal(String dms) {
        String[] parts = dms.split("[°'\"\\s]");
        double degrees = Double.parseDouble(parts[0]);
        double minutes = Double.parseDouble(parts[1]);
        double seconds = Double.parseDouble(parts[2]);
        double decimal = degrees + minutes/60 + seconds/3600;
        return decimal;
    }
 
    public static void main(String[] args) {
        String lng = "27°16′48″";
        Double aDouble = tranformPos(lng);
        System.out.println("aDouble = " + aDouble);
    }
 
}