C++实现经纬度坐标转utm坐标,详细代码是什么
时间: 2023-03-26 13:01:19 浏览: 412
以下是 C 语言实现经纬度坐标转 UTM 坐标的示例代码:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define a 6378137.
#define b 6356752.314245
#define f (a - b) / a
#define e sqrt(2 * f - f * f)
double rad(double d) {
return d * PI / 180.;
}
void LLtoUTM(double lat, double lon, int *zone, double *x, double *y) {
double lon, x, y, N, T, C, A, M;
int zoneNumber;
zoneNumber = (int) ((lon + 180.) / 6.) + 1;
if (lat >= 56. && lat < 64. && lon >= 3. && lon < 12.) {
zoneNumber = 32;
}
if (lat >= 72. && lat < 84.) {
if (lon >= . && lon < 9.) {
zoneNumber = 31;
} else if (lon >= 9. && lon < 21.) {
zoneNumber = 33;
} else if (lon >= 21. && lon < 33.) {
zoneNumber = 35;
} else if (lon >= 33. && lon < 42.) {
zoneNumber = 37;
}
}
lon = zoneNumber * 6 - 183;
x = 500000.;
y = (lat < .) ? 10000000. : .;
N = a / sqrt(1 - e * e * sin(rad(lat)) * sin(rad(lat)));
T = tan(rad(lat)) * tan(rad(lat));
C = e * e * cos(rad(lat)) * cos(rad(lat)) / (1 - e * e);
A = cos(rad(lat)) * (rad(lon) - rad(lon));
M = a * ((1 - e * e / 4 - 3 * e * e * e * e / 64 - 5 * e * e * e * e * e * e / 256) * rad(lat)
- (3 * e * e / 8 + 3 * e * e * e * e / 32 + 45 * e * e * e * e * e * e / 1024) * sin(2 * rad(lat))
+ (15 * e * e * e * e / 256 + 45 * e * e * e * e * e * e / 1024) * sin(4 * rad(lat))
- (35 * e * e * e * e * e * e / 3072) * sin(6 * rad(lat)));
*x = x + N * (A + (1 - T + C) * A * A * A / 6
+ (5 - 18 * T + T * T + 72 * C - 58 * e * e) * A * A * A * A * A / 120);
*y = y + M + N * tan(rad(lat)) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
+ (61 - 58 * T + T * T + 600 * C - 330 * e * e) * A * A * A * A * A * A / 720);
*zone = zoneNumber;
}
int main() {
double lat = 39.9087;
double lon = 116.3975;
int zone;
double x, y;
LLtoUTM(lat, lon, &zone, &x, &y);
printf("经度: %f\n", lon);
printf("纬度: %f\n", lat);
printf("UTM 坐标: %d %f %f\n", zone, x, y);
return ;
}
```
该代码实现了经纬度坐标转 UTM 坐标的功能,其中包括了一些常量和函数,如 `PI`、`a`、`b`、`f`、`e` 和 `rad`。`LLtoUTM` 函数接受经纬度坐标和指向 UTM 坐标的指针,计算出 UTM 坐标和所在的 UTM 带号。在 `main` 函数中,我们将北京市的经纬度坐标传递给 `LLtoUTM` 函数,并输出计算结果。
阅读全文