gnss_coordinate_convert.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <gnss_coordinate_convert.h>
  2. void gps2xy(double J4, double K4, double *x, double *y)
  3. {
  4. int L4 = (int)((K4 - 1.5) / 3) + 1;
  5. double M4 = K4 - (L4 * 3);
  6. double N4 = sin(J4 * 3.1415926536 / 180);
  7. double O4 = cos(J4 * 3.1415926536 / 180);
  8. double P4 = tan(J4 * 3.1415926536 / 180);
  9. double Q4 = 111134.8611 * J4 - N4 * O4 * (32005.7799 + 133.9238 * N4 * N4 + 0.6973 * N4 * N4 * N4 * N4 + 0.0039 * N4 * N4 * N4 * N4 * N4 * N4);
  10. double R4 = sqrt(0.006738525414683) * O4;
  11. double S4 = sqrt(1 + R4 * R4);
  12. double T4 = 6399698.901783 / S4;
  13. double U4 = (T4 / S4) / S4;
  14. double V4 = O4 * M4 * 3.1415926536 / 180;
  15. double W4 = 0.5 + (5 - P4 * P4 + 9 * R4 * R4 + 4 * R4 * R4 * R4 * R4) * V4 * V4 / 24;
  16. double X4 = V4 * V4 * V4 * V4 / 720 * (61 + (P4 * P4 - 58) * P4 * P4);
  17. double Y4 = 1 + V4 * V4 * (1 - P4 * P4 + R4 * R4) / 6;
  18. double Z4 = V4 * V4 * V4 * V4 * (5 - 18 * P4 * P4 * P4 * P4 * P4 * P4 + 14 * R4 * R4 - 58 * R4 * R4 * P4 * P4) / 120;
  19. *y = Q4 + T4 * P4 * V4 * V4 * (W4 + X4);
  20. *x = 500000 + T4 * V4 * (Y4 + Z4);
  21. }
  22. /*
  23. //高斯投影由经纬度(Unit:DD)反算大地坐标(含带号,Unit:Metres)
  24. void GaussProjCal(double longitude, double latitude, double *X, double *Y)
  25. {
  26. int ProjNo = 0; int ZoneWide; ////带宽
  27. double longitude1, latitude1, longitude0, latitude0, X0, Y0, xval, yval;
  28. double a, f, e2, ee, NN, T, C, A, M, iPI;
  29. iPI = 0.0174532925199433; ////3.1415926535898/180.0;
  30. ZoneWide = 6; ////6度带宽
  31. a = 6378245.0; f = 1.0 / 298.3; //54年北京坐标系参数
  32. ////a=6378140.0; f=1/298.257; //80年西安坐标系参数
  33. ProjNo = (int)(longitude / ZoneWide);
  34. longitude0 = ProjNo * ZoneWide + ZoneWide / 2;
  35. longitude0 = longitude0 * iPI;
  36. latitude0 = 0;
  37. longitude1 = longitude * iPI; //经度转换为弧度
  38. latitude1 = latitude * iPI; //纬度转换为弧度
  39. e2 = 2 * f - f * f;
  40. ee = e2 * (1.0 - e2);
  41. NN = a / sqrt(1.0 - e2 * sin(latitude1)*sin(latitude1));
  42. T = tan(latitude1)*tan(latitude1);
  43. C = ee * cos(latitude1)*cos(latitude1);
  44. A = (longitude1 - longitude0)*cos(latitude1);
  45. M = a * ((1 - e2 / 4 - 3 * e2*e2 / 64 - 5 * e2*e2*e2 / 256)*latitude1 - (3 * e2 / 8 + 3 * e2*e2 / 32 + 45 * e2*e2
  46. *e2 / 1024)*sin(2 * latitude1)
  47. + (15 * e2*e2 / 256 + 45 * e2*e2*e2 / 1024)*sin(4 * latitude1) - (35 * e2*e2*e2 / 3072)*sin(6 * latitude1));
  48. xval = NN * (A + (1 - T + C)*A*A*A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * ee)*A*A*A*A*A / 120);
  49. yval = M + NN * tan(latitude1)*(A*A / 2 + (5 - T + 9 * C + 4 * C*C)*A*A*A*A / 24
  50. + (61 - 58 * T + T * T + 600 * C - 330 * ee)*A*A*A*A*A*A / 720);
  51. X0 = 1000000L * (ProjNo + 1) + 500000L;
  52. Y0 = 0;
  53. xval = xval + X0; yval = yval + Y0;
  54. *X = xval;
  55. *Y = yval;
  56. }
  57. */
  58. //=======================zhaobo0904
  59. #define PI 3.14159265358979
  60. void GaussProjCal(double lon, double lat, double *X, double *Y)
  61. {
  62. // 1975 年国际椭球体长半轴 a, 第一离心率 e2, 第二离心率 ep2
  63. double a = 6378140.0;
  64. double e2 = 0.006694384999588;
  65. double ep2 = e2/(1-e2);
  66. // 原点所在经度
  67. double lon_origin = 6.0*int(lon/6) + 3.0;
  68. lon_origin *= PI / 180.0;
  69. double k0 = 0.9996;
  70. // 角度转弧度
  71. double lat1 = lat * PI / 180.0;
  72. double lon1 = lon * PI / 180.0;
  73. // 经线在该点处的曲率半径,
  74. double N = a / sqrt(1 - e2*sin(lat1)*sin(lat1));
  75. // 赤道到该点的经线长度近似值 M, 使用泰勒展开逐项积分然后取前四项.
  76. // 这个近似值是将 N 作为纬度 \phi 的函数展开为泰勒计数, 然后在区间 [0, lat1] 上积分得到的.
  77. // 首先计算前四项的系数 a1~a4.
  78. double a1 = 1 - e2/4 - (3*e2*e2)/64 - (5*e2*e2*e2)/256;
  79. double a2 = (3*e2)/8 + (3*e2*e2)/32 + (45*e2*e2*e2)/1024;
  80. double a3 = (15*e2*e2)/256 + (45*e2*e2*e2)/1024;
  81. double a4 = (35*e2*e2*e2)/3072;
  82. double M = a * (a1*lat1 - a2*sin(2*lat1) + a3*sin(4*lat1) - a4*sin(6*lat1));
  83. // 辅助量
  84. double T = tan(lat1)*tan(lat1);
  85. double C = ep2*cos(lat1)*cos(lat1);
  86. double A = (lon1 - lon_origin)*cos(lat1);
  87. *X = 500000.0 + k0 * N * (A + (1 - T + C)*(A*A*A)/6 + (5 - 18*T + T*T + 72*C - 58*ep2)*(A*A*A*A*A)/120);
  88. *Y = M + N * tan(lat1) * ((A*A)/2 +
  89. (5 - T + 9*C + 4*C*C)*(A*A*A*A)/24 +
  90. (61 - 58*T + T*T + 600*C - 330*ep2)*(A*A*A*A*A*A)/720);
  91. *Y *= k0;
  92. return;
  93. }
  94. //==========================================================
  95. //高斯投影由大地坐标(Unit:Metres)反算经纬度(Unit:DD)
  96. void GaussProjInvCal(double X, double Y, double *longitude, double *latitude)
  97. {
  98. int ProjNo; int ZoneWide; ////带宽
  99. double longitude1, latitude1, longitude0, latitude0, X0, Y0, xval, yval;
  100. double e1, e2, f, a, ee, NN, T, C, M, D, R, u, fai, iPI;
  101. iPI = 0.0174532925199433; ////3.1415926535898/180.0;
  102. a = 6378245.0; f = 1.0 / 298.3; //54年北京坐标系参数
  103. ////a=6378140.0; f=1/298.257; //80年西安坐标系参数
  104. ZoneWide = 6; ////6度带宽
  105. ProjNo = (int)(X / 1000000L); //查找带号
  106. longitude0 = (ProjNo - 1) * ZoneWide + ZoneWide / 2;
  107. longitude0 = longitude0 * iPI; //中央经线
  108. X0 = ProjNo * 1000000L + 500000L;
  109. Y0 = 0;
  110. xval = X - X0; yval = Y - Y0; //带内大地坐标
  111. e2 = 2 * f - f * f;
  112. e1 = (1.0 - sqrt(1 - e2)) / (1.0 + sqrt(1 - e2));
  113. ee = e2 / (1 - e2);
  114. M = yval;
  115. u = M / (a*(1 - e2 / 4 - 3 * e2*e2 / 64 - 5 * e2*e2*e2 / 256));
  116. fai = u + (3 * e1 / 2 - 27 * e1*e1*e1 / 32)*sin(2 * u) + (21 * e1*e1 / 16 - 55 * e1*e1*e1*e1 / 32)*sin(
  117. 4 * u)
  118. + (151 * e1*e1*e1 / 96)*sin(6 * u) + (1097 * e1*e1*e1*e1 / 512)*sin(8 * u);
  119. C = ee * cos(fai)*cos(fai);
  120. T = tan(fai)*tan(fai);
  121. NN = a / sqrt(1.0 - e2 * sin(fai)*sin(fai));
  122. R = a * (1 - e2) / sqrt((1 - e2 * sin(fai)*sin(fai))*(1 - e2 * sin(fai)*sin(fai))*(1 - e2 * sin
  123. (fai)*sin(fai)));
  124. D = xval / NN;
  125. //计算经度(Longitude) 纬度(Latitude)
  126. longitude1 = longitude0 + (D - (1 + 2 * T + C)*D*D*D / 6 + (5 - 2 * C + 28 * T - 3 * C*C + 8 * ee + 24 * T*T)*D
  127. *D*D*D*D / 120) / cos(fai);
  128. latitude1 = fai - (NN*tan(fai) / R)*(D*D / 2 - (5 + 3 * T + 10 * C - 4 * C*C - 9 * ee)*D*D*D*D / 24
  129. + (61 + 90 * T + 298 * C + 45 * T*T - 256 * ee - 3 * C*C)*D*D*D*D*D*D / 720);
  130. //转换为度 DD
  131. *longitude = longitude1 / iPI;
  132. *latitude = latitude1 / iPI;
  133. }