一直在做的一个项目涉及到GIS的部分,因为是给台湾的客户做的,所以要涉及到TWD67、TWD97坐标与WGS84坐标的转换。网上关于地理坐标转换的资料已经不少,其中一部分也已经涉及到经纬度坐标与UTM坐标的转换,但多数的都是6度分带和3度分带,也就是我们国家(大陆)所采用的分带方式。而台湾因为自身岛屿形状的特征(成细长型),所以采用了2度分带。关于这方面的转换算法就比较少了。现在我把自己的算法贴出来,希望能够对相关开发人员有所帮助。
public class Coordinate {
private static double DEG_RAD =
0.01745329251994329572e0;
private static double TWD97_A =
6378137.0e0;
private static double TWD97_ECC =
0.00669438002290e0;
private static double TWD97_ECC2 =
0.00673949677556e0;
private static double TWD97_TM2 =
0.9999e0;
public double mercator(double y, double a,
double ecc) {
if (y == 0.0) {
return
0.0;
} else {
return
a
*
((1.0 - ecc / 4.0 - 3.0 * ecc * ecc / 64.0 - 5.0 * ecc
*
ecc * ecc / 256.0)
*
y
-
(3.0 * ecc / 8.0 + 3.0 * ecc * ecc / 32.0 + 45.0
*
ecc * ecc * ecc / 1024.0)
*
Math.sin(2.0 * y)
+
(15.0 * ecc * ecc / 256.0 + 45.0 * ecc * ecc
*
ecc / 1024.0) * Math.sin(4.0 * y) - (35.0
*
ecc * ecc * ecc / 3072.0)
*
Math.sin(6.0 * y));
}
}
//WGS84经纬度坐标转换为TWD97 Tm2坐标
public HanLonLat toTwd97TM2(double a, double ecc,
double ecc2, double lat,
double lon,
double scale, double x, double y) {
double x0, y0, x1, y1, m0,
m1;
double n, t, c, A;
x0 = x * DEG_RAD;
y0 = y * DEG_RAD;
x1 = lon * DEG_RAD;
y1 = lat * DEG_RAD;
m0 = mercator(y1, a,
ecc);
m1 = mercator(y0, a, ecc);
n = a / Math.sqrt(1 - ecc *
Math.pow(Math.sin(y0), 2.0));
t = Math.pow(Math.tan(y0),
2.0);
c = ecc2 *
Math.pow(Math.cos(y0), 2.0);
A = (x0 - x1) *
Math.cos(y0);
x = scale
*
n
*
(A + (1.0 - t + c) * A * A * A / 6.0 + (5.0 - 18.0 * t + t
*
t + 72.0 * c - 58.0 * ecc2)
*
Math.pow(A, 5.0) / 120.0);
y = scale
*
(m1 - m0 + n
*
Math.tan(y0)
*
(A * A / 2.0 + (5.0 - t + 9.0 * c + 4 * c * c)
*
Math.pow(A, 4.0) / 24.0 + (61.0 - 58.0 * t
+
t * t + 600.0 * c - 330.0 * ecc2)
*
Math.pow(A, 6.0) / 720.0));
x += 250000;
HanLonLat objHanLonLat = new
HanLonLat();
objHanLonLat.setLon(x);
objHanLonLat.setLat(y);
return objHanLonLat;
}
//WGS84经纬度坐标转换为TWD97 Tm2坐标
——方法调用
public HanLonLat
getTwd97TM2LocationFromTwd97(double x, double y) {
HanLonLat objHanLonLat =
this.toTwd97TM2(TWD97_A, TWD97_ECC, TWD97_ECC2, 0,
121,
TWD97_TM2, x, y);//121为台湾岛的中央经线
return objHanLonLat;
}
////WGS84经纬度坐标转换为TWD67 Tm2坐标
public HanLonLat
getTwd67TM2LocationFromTwd97(double x, double y) {
double A = 0.00001549;
double B = 0.000006521;
HanLonLat loncation_97 =
this.toTwd97TM2(TWD97_A, TWD97_ECC, TWD97_ECC2, 0,
121,
TWD97_TM2, x, y);
double X_97 =
loncation_97.getLon();
double Y_97 =
loncation_97.getLat();
double X_67 = X_97 - 807.8 -
A * X_97 - B * Y_97;
double Y_67 = Y_97 + 248.6 - A
* Y_97 - B * X_97;
HanLonLat objHanLonLat = new
HanLonLat();
objHanLonLat.setLon(X_67);
objHanLonLat.setLat(Y_67);
return objHanLonLat;
}
}
//自己定义的一个经纬度类
public class HanLonLat {
private double lon;
private double lat;
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
}