c实现utm转wsg84
时间: 2023-11-02 20:05:18 浏览: 136
以下是一个简单的C语言代码示例,用于将UTM坐标转换为WGS84经纬度坐标。
```
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
double deg2rad(double deg) {
return (deg * PI / 180);
}
double rad2deg(double rad) {
return (rad * 180 / PI);
}
void utm2wgs84(double utm_x, double utm_y, int zone, int southhemi, double *lat, double *lon) {
double k0 = 0.9996; // UTM缩放因子
double a = 6378137.0; // WGS84椭球体长半轴
double f = 1/298.257223563; // WGS84椭球体扁率
double b = a * (1 - f); // WGS84椭球体短半轴
double e = sqrt(1 - (b/a)*(b/a)); // WGS84椭球体第一偏心率
double e2 = e*e; // WGS84椭球体第二偏心率
double x = utm_x - 500000.0; // 计算相对于中央经线的偏移量
double y = utm_y;
if (southhemi) {
y -= 10000000.0; // 南半球情况下,将y坐标减去10000000
}
double s = zone * 6.0 - 183.0; // 计算中央经线的经度
double lat1 = y / (k0 * a);
double M = lat1 / (1 - e2/4 - 3*e2*e2/64 - 5*e2*e2*e2/256); // 计算子午线弧长
double mu = M / (a * (1 - e2/4 - 3*e2*e2/64 - 5*e2*e2*e2/256)); // 计算子午线曲率半径
double phi1 = lat1 + (3*e2/8 - 27*e2*e2/128) * sin(2*lat1) + (21*e2*e2/256 - 55*e2*e2*e2/1280) * sin(4*lat1) + (151*e2*e2*e2/6144) * sin(6*lat1);
double tan_phi1 = tan(phi1);
double cos_phi1 = cos(phi1);
double sin_phi1 = sin(phi1);
double N1 = a / sqrt(1 - e2*sin_phi1*sin_phi1);
double T1 = tan_phi1*tan_phi1;
double C1 = e2*cos_phi1*cos_phi1/(1 - e2);
double R1 = a*(1 - e2)/pow(1 - e2*sin_phi1*sin_phi1, 1.5);
double D = x / (N1*k0);
double lat = phi1 - (N1*tan_phi1/R1) * (D*D/2 - (5 + 3*T1 + 10*C1 - 4*C1*C1 - 9*e2)*D*D*D*D/24 + (61 + 90*T1 + 298*C1 + 45*T1*T1 - 252*e2 - 3*C1*C1)*D*D*D*D*D*D/720);
double lon = s + (1/(cos_phi1*N1*k0)) * (D - (1 + 2*T1 + C1)*D*D*D/6 + (5 - 2*C1 + 28*T1 - 3*C1*C1 + 8*e2 + 24*T1*T1)*D*D*D*D*D/120);
*lat = rad2deg(lat);
*lon = rad2deg(lon);
}
int main() {
double x = 398462;
double y = 3987679;
int zone = 50;
int southhemi = 0;
double lat, lon;
utm2wgs84(x, y, zone, southhemi, &lat, &lon);
printf("WGS84坐标:(%f, %f)\n", lat, lon);
return 0;
}
```
注意,该代码仅适用于UTM投影下的WGS84椭球体。如果您需要将其他椭球体或其他投影系统下的坐标转换为WGS84坐标,请使用适当的算法和参数。
阅读全文