wgs84转CGCS2000 c++代码
时间: 2023-11-04 19:57:26 浏览: 198
C++写太阳/月球坐标计算,在地固坐标系(WGS-84)下,ITRF下,CGCS2000下
5星 · 资源好评率100%
您可以使用以下的 C 代码将 WGS84 坐标转换为 CGCS2000 坐标:
```c
#include <stdio.h>
#include <math.h>
const double a = 6378137.0; // WGS84 椭球长半轴
const double f = 1 / 298.257223563; // WGS84 椭球扁率
const double b = a * (1 - f); // WGS84 椭球短半轴
const double e2 = (pow(a,2) - pow(b, 2)) / pow(a, 2); // WGS84 椭球第一偏心率的平方
const double a_cgcs = 6378137.0; // CGCS2000 椭球长半轴
const double f_cgcs = 1 / 298.257222101; // CGCS2000 椭球扁率
const double b_cgcs = a_cgcs * (1 - f_cgcs); // CGCS2000 椭球短半轴
const double e2_cgcs = (pow(a_cgcs, 2) - pow(b_cgcs, 2)) / pow(a_cgcs,2); // CGCS2000 椭球第一偏心率的平方
double deg2rad(double degree) {
return degree * M_PI / 180.0;
}
double rad2deg(double radian) {
return radian * 180.0 / M_PI;
}
void wgs84ToCgcs2000(double lon_wgs84, double lat_wgs84, double height_wgs84,
double *lon_cgcs, double *lat_cgcs, double *height_cgcs) {
double lon_rad = deg2rad(lon_wgs84);
double lat_rad = deg2rad(lat_wgs84);
double N = a_cgcs / sqrt(1 - e2_cgcs * pow(sin(lat_rad), 2));
double X = (N + height_wgs84) * cos(lat_rad) * cos(lon_rad);
double Y = (N + height_wgs84) * cos(lat_rad) * sin(lon_rad);
double Z = (N * (1 - e2_cgcs) + height_wgs84) * sin(lat_rad);
double p = sqrt(pow(X, 2) + pow(Y, 2));
if (p < 1e-10) {
*lon_cgcs = 0;
*lat_cgcs = 0;
*height_cgcs = Z;
} else {
double theta = atan(Z / (p * (1 - e2_cgcs)));
double sin_theta = sin(theta);
double cos_theta = cos(theta);
*lon_cgcs = atan(Y / X);
*lat_cgcs = atan((Z + b_cgcs * e2_cgcs * pow(sin_theta, 3)) /
(p - a_cgcs * e2_cgcs * pow(cos_theta, 3)));
*height_cgcs = p / cos(*lat_cgcs) - a_cgcs / sqrt(1 - e2_cgcs * pow(sin(*lat_cgcs), 2));
}
*lon_cgcs = rad2deg(*lon_cgcs);
*lat_cgcs = rad2deg(*lat_cgcs);
}
int main() {
double lon_wgs84 = 116.3976;
double lat_wgs84 = 39.9037;
double height_wgs84 = 0.0;
double lon_cgcs, lat_cgcs, height_cgcs;
wgs84ToCgcs2000(lon_wgs84, lat_wgs84, height_wgs84, &lon_cgcs, &lat_cgcs, &height_cgcs);
printf("CGCS2000 坐标:\n");
printf("经度:%f\n", lon_cgcs);
printf("纬度:%f\n", lat_cgcs);
printf("高程:%f\n", height_cgcs);
return 0;
}
```
阅读全文