dcb
2025-06-18 4c4d0f591f94428ed7e5d2f4ae5df5c5087d8c26
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.se.nsl.utils;
 
import org.gdal.gdal.gdal;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
 
public class CoordinateTransformer {
 
//    static {
//        try {
//            gdal.AllRegister();
//        } catch (Exception e) {
//            System.err.println("GDAL驱动注册失败: " + e.getMessage());
//        }
//    }
 
    public static double[] transform(int sourceEPSG, int targetEPSG,
                                     double x, double y, double z) {
        SpatialReference sourceSRS = null;
        SpatialReference targetSRS = null;
        CoordinateTransformation ct = null;
 
        try {
            // 创建源坐标系
            sourceSRS = new SpatialReference();
            int sourceResult = sourceSRS.ImportFromEPSG(sourceEPSG);
            if (sourceResult != 0) {
                throw new IllegalArgumentException("无效的源EPSG代码: " + sourceEPSG);
            }
 
            // 创建目标坐标系
            targetSRS = new SpatialReference();
            int targetResult = targetSRS.ImportFromEPSG(targetEPSG);
            if (targetResult != 0) {
                throw new IllegalArgumentException("无效的目标EPSG代码: " + targetEPSG);
            }
 
            sourceSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
            targetSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
 
            // 创建坐标转换对象
            ct = new CoordinateTransformation(sourceSRS, targetSRS);
            double[] result = ct.TransformPoint(x, y, z);
 
            if (result == null || Double.isNaN(result[0]) || Double.isNaN(result[1])) {
                throw new IllegalArgumentException("坐标转换失败,可能是坐标系不兼容");
            }
 
            return result;
        } finally {
            // 确保资源被释放
            if (ct != null) ct.delete();
            if (targetSRS != null) targetSRS.delete();
            if (sourceSRS != null) sourceSRS.delete();
        }
    }
 
    public static double[] transform(int sourceEPSG, int targetEPSG, double x, double y) {
        return transform(sourceEPSG, targetEPSG, x, y, 0.0);
    }
 
    public static double[] transform(String sourceWKT, String targetWKT,
                                     double x, double y, double z) {
        SpatialReference sourceSRS = null;
        SpatialReference targetSRS = null;
        CoordinateTransformation ct = null;
 
        try {
            // 创建源坐标系
            sourceSRS = new SpatialReference();
            int sourceResult = sourceSRS.ImportFromWkt(sourceWKT);
            if (sourceResult != 0) {
                throw new IllegalArgumentException("无效的源WKT定义");
            }
 
            // 创建目标坐标系
            targetSRS = new SpatialReference();
            int targetResult = targetSRS.ImportFromWkt(targetWKT);
            if (targetResult != 0) {
                throw new IllegalArgumentException("无效的目标WKT定义");
            }
 
            // 创建坐标转换对象
            ct = new CoordinateTransformation(sourceSRS, targetSRS);
            double[] result = ct.TransformPoint(x, y, z);
 
            if (result == null || Double.isNaN(result[0]) || Double.isNaN(result[1])) {
                throw new IllegalArgumentException("坐标转换失败,可能是坐标系不兼容");
            }
 
            return result;
        } finally {
            // 确保资源被释放
            if (ct != null) ct.delete();
            if (targetSRS != null) targetSRS.delete();
            if (sourceSRS != null) sourceSRS.delete();
        }
    }
 
    public static void main(String[] args) {
        try {
            double[] result1 = transform(4326, 32650, 116.4074, 39.9042); // 北京
            System.out.printf("WGS84 -> UTM 50N: (%.6f, %.6f, %.2f)%n",
                    result1[0], result1[1], result1[2]);
 
            double[] result2 = transform(4326, 3857, 116.4074, 39.9042); // 北京
            System.out.printf("WGS84 -> Web Mercator: (%.6f, %.6f, %.2f)%n",
                    result2[0], result2[1], result2[2]);
 
            // 使用适合NAD83 California zone 5的坐标
            double[] result3 = transform(4326, 26945, -122.4194, 37.7749); // 旧金山
            System.out.printf("WGS84 -> NAD83 California zone 5: (%.6f, %.6f, %.2f)%n",
                    result3[0], result3[1], result3[2]);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}