高斯坐标正反算c语言
时间: 2023-07-09 12:14:14 浏览: 124
高斯投影是地图投影的一种常见形式,可以实现地球表面的三维坐标转换为二维平面坐标和反向转换。以下是一个简单的高斯坐标正反算的C语言示例代码:
```
#include <stdio.h>
#include <math.h>
#define pi 3.1415926535898
#define a 6378137.0000 // 长半轴
#define b 6356752.3142 // 短半轴
#define e2 0.0066943799901 // 第一偏心率平方
#define e12 0.0067394967423 // 第二偏心率平方
void GaussForword(double L, double B, double L0, double &X, double &Y) {
double l, N, t, t2, l3, l4, l5, l6, l7, l8, l9, C, A, M, a0, a2, a4, a6, a8, a10;
int i;
L -= L0;
l = L * pi / 180;
B = B * pi / 180;
t = tan(B);
t2 = t * t;
l3 = l * l * l;
l4 = l * l * l * l;
l5 = l * l * l * l * l;
l6 = l * l * l * l * l * l;
l7 = l * l * l * l * l * l * l;
l8 = l * l * l * l * l * l * l * l;
l9 = l * l * l * l * l * l * l * l * l;
C = e12 * cos(B) * cos(B);
A = (a / sqrt(1 - e2 * sin(B) * sin(B))) * (1 + 0.75 * C + 45 / 64 * C * C + 175 / 256 * C * C * C + 11025 / 16384 * C * C * C * C + 43659 / 65536 * C * C * C * C * C);
N = a / sqrt(1 - e2 * sin(B) * sin(B));
M = a * (1 - e2) / pow(1 - e2 * sin(B) * sin(B), 1.5);
a0 = 1 + 3 * t2 / 4 + 45 * t2 * t2 / 64 + 175 * t2 * t2 * t2 / 256 + 11025 * t2 * t2 * t2 * t2 / 16384;
a2 = -3 * t / 2 + 15 * t * t / 16 + 525 * t * t * t / 512 - 2205 * t * t * t * t / 2048 - 72765 * t * t * t * t * t / 65536;
a4 = 15 * t2 / 16 - 75 * t2 * t2 / 64 - 3655 * t2 * t2 * t2 / 2048 + 33953 * t2 * t2 * t2 * t2 / 4096;
a6 = -35 * t2 * t / 48 + 175 * t2 * t2 / 384 + 3675 * t2 * t2 * t2 / 8192;
a8 = 315 * t2 * t2 / 512 - 6925 * t2 * t2 * t2 / 12288;
a10 = -693 * t2 * t2 * t / 1280;
X = A * (B - a0 * sin(2 * B) / 2 + a2 * sin(4 * B) / 4 - a4 * sin(6 * B) / 6 + a6 * sin(8 * B) / 8 - a8 * sin(10 * B) / 10 + a10 * sin(12 * B) / 12) + N * tan(B) * (l * l / 2 + (5 - t2 + 9 * C + 4 * C * C) * l4 / 24 + (61 - 58 * t2 + t2 * t2 + 600 * C - 330 * e12) * l6 / 720 + (1385 - 3111 * t2 + 543 * t2 * t2 - t2 * t2 * t2) * l8 / 40320);
Y = M * (l - l3 / 6 * (1 - t2 + C) + l5 / 120 * (5 - 18 * t2 + t2 * t2 + 72 * C - 58 * e12) - l7 / 5040 * (61 - 479 * t2 + 179 * t2 * t2 - t2 * t2 * t2) + l9 / 362880 * (1385 - 14287 * t2 + 4823 * t2 * t2 - 776 * t2 * t2 * t2 + 47 * t2 * t2 * t2 * t2));
}
void GaussInverse(double X, double Y, double L0, double &L, double &B) {
double l, N, t, t2, l3, l5, l7, l9, C, C1, R, D, E, F, G, H, I, J, K, L, M, P, Q;
int i;
X -= 500000;
Y -= 0;
L0 *= pi / 180;
C = e12 * cos(B) * cos(B);
N = a / sqrt(1 - e2 * sin(B) * sin(B));
M = a * (1 - e2) / pow(1 - e2 * sin(B) * sin(B), 1.5);
l = X / (N * cos(B)) + pow(l, 3) / (6 * pow(N, 3) * cos(B)) * (-1 - 2 * t2 - C) + pow(l, 5) / (120 * pow(N, 5) * cos(B)) * (5 + 28 * t2 + 24 * t2 * t2 + 6 * C + 8 * C * t2) - pow(l, 7) / (5040 * pow(N, 7) * cos(B)) * (-61 - 90 * t2 - 45 * t2 * t2 - 107 * C + 162 * C * t2) - pow(l, 9) / (362880 * pow(N, 9) * cos(B)) * (1385 + 3633 * t2 + 4095 * t2 * t2 + 1574 * t2 * t2 * t2);
l3 = l * l * l;
l5 = l * l * l * l * l;
l7 = l * l * l * l * l * l * l;
l9 = l * l * l * l * l * l * l * l * l;
C1 = e12 * cos(B) * cos(B);
R = a * (1 - e2) / pow(1 - e2 * sin(B) * sin(B), 1.5);
D = Y / R;
D = D - (1 + 2 * t2 + C) * l * l / 2 + (5 + 28 * t2 + 24 * t2 * t2 + 6 * C + 8 * C * t2) * l5 / 120 - (61 + 90 * t2 + 45 * t2 * t2 + 107 * C + 162 * C * t2) * l7 / 5040 + (1385 + 3633 * t2 + 4095 * t2 * t2 + 1574 * t2 * t2 * t2) * l9 / 362880;
B = B - D;
t = tan(B);
t2 = t * t;
C = e12 * cos(B) * cos(B);
N = a / sqrt(1 - e2 * sin(B) * sin(B));
M = a * (1 - e2) / pow(1 - e2 * sin(B) * sin(B), 1.5);
E = l;
F = t / (2 * N * N) * E * E;
G = t / (24 * N * N * N * N) * (5 + 3 * t2 + 10 * C - 4 * C * C - 9 * e12) * E * E * E * E;
H = t / (720 * N * N * N * N * N * N) * (61 + 90 * t2 + 298 * C + 45 * t2 * t2 - 252 * e12 - 3 * C * C) * E * E * E * E * E * E;
I = t / (40320 * N * N * N * N * N * N * N * N) * (1385 + 3633 * t2 + 4095 * t2 * t2 + 1574 * t2 * t2 * t2) * E * E * E * E * E * E * E * E;
B = B - F + G - H + I;
L = L0 + 1 / cos(B) * (E + (1 - t2 + C) * E * E * E / 6 + (5 - 18 * t2 + t2 * t2 + 72 * C - 58 * e12) * E * E * E * E * E / 120);
L *= 180 / pi;
B *= 180 / pi;
}
```
这段代码定义了两个函数`GaussForword()`和`GaussInverse()`,分别代表高斯正算和反算。其中,`L`和`B`分别代表经度和纬度,`L0`代表中央经线,`X`和`Y`则是高斯坐标系下的坐标值。在使用时,只需要将需要转换的坐标值作为参数传入即可得出转换结果。需要注意的是,这段代码仅仅是一个简单的示例,并不保证在所有情况下都能得到正确的结果,具体使用时需要根据实际情况做出相应的调整和改进。
阅读全文