Program.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GpsConvert
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //double[] utm = LatLonToUtm(30.65156708, 103.6880587);
  13. double[] utm = LatLonToUtm(31.108761,121.314085);
  14. Console.WriteLine($"X:{utm[0]},Y:{utm[1]},区域:{utm[2]}");
  15. var ss= Wgs84Tosh(31.0158865505,121.2238203740);
  16. Console.WriteLine($"X:{ss[0]},Y:{ss[1]}");
  17. Console.ReadLine();
  18. }
  19. static double pi = Math.PI;
  20. static double sm_a = 6378137.0;
  21. static double sm_b = 6356752.314;
  22. //static double sm_EccSquared = 6.69437999013e-03;
  23. static double UTMScaleFactor = 0.9996;
  24. //得到的结果是:x坐标,y坐标,区域编号
  25. public static double[] LatLonToUtm (double lat, double lon)
  26. {
  27. double zone = Math.Floor((lon + 180.0) / 6) + 1;
  28. double cm = UtmCentralMeridian(zone);
  29. MapLatLonToXy(lat / 180.0 * pi, lon / 180 * pi, cm, out var xy);
  30. /* Adjust easting and northing for UTM system. */
  31. xy[0] = xy[0] * UTMScaleFactor + 500000.0;
  32. xy[1] = xy[1] * UTMScaleFactor;
  33. if (xy[1] < 0.0)
  34. {
  35. xy[1] = xy[1] + 10000000.0;
  36. }
  37. return new double[] { xy[0], xy[1], zone };
  38. }
  39. public static double UtmCentralMeridian (double zone)
  40. {
  41. double cmeridian;
  42. double deg = -183.0 + (zone * 6.0);
  43. cmeridian = deg / 180.0 * pi;
  44. return cmeridian;
  45. }
  46. internal static void MapLatLonToXy (double phi, double lambda, double lambda0, out double[] xy)
  47. {
  48. /* Precalculate ep2 */
  49. var ep2 = (Math.Pow(sm_a, 2.0) - Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0);
  50. /* Precalculate nu2 */
  51. var nu2 = ep2 * Math.Pow(Math.Cos(phi), 2.0);
  52. /* Precalculate N */
  53. var N = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nu2));
  54. /* Precalculate t */
  55. var t = Math.Tan (phi);
  56. var t2 = t * t;
  57. /* Precalculate l */
  58. var l = lambda - lambda0;
  59. /* Precalculate coefficients for l**n in the equations below
  60. so a normal human being can read the expressions for easting
  61. and northing
  62. -- l**1 and l**2 have coefficients of 1.0 */
  63. var l3coef = 1.0 - t2 + nu2;
  64. var l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2);
  65. var l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
  66. - 58.0 * t2 * nu2;
  67. var l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
  68. - 330.0 * t2 * nu2;
  69. var l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
  70. var l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);
  71. /* Calculate easting (x) */
  72. xy = new double[2];
  73. xy[0] = N * Math.Cos (phi) * l
  74. + (N / 6.0 * Math.Pow (Math.Cos (phi), 3.0) * l3coef * Math.Pow (l, 3.0))
  75. + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0))
  76. + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0));
  77. /* Calculate northing (y) */
  78. xy[1] = ArcLengthOfMeridian (phi)
  79. + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0))
  80. + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0))
  81. + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0))
  82. + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0));
  83. return;
  84. }
  85. internal static double ArcLengthOfMeridian(double phi)
  86. {
  87. double alpha, beta, gamma, delta, epsilon, n;
  88. double result;
  89. /* Precalculate n */
  90. n = (sm_a - sm_b) / (sm_a + sm_b);
  91. /* Precalculate alpha */
  92. alpha = ((sm_a + sm_b) / 2.0)
  93. * (1.0 + (Math.Pow(n, 2.0) / 4.0) + (Math.Pow(n, 4.0) / 64.0));
  94. /* Precalculate beta */
  95. beta = (-3.0 * n / 2.0) + (9.0 * Math.Pow(n, 3.0) / 16.0)
  96. + (-3.0 * Math.Pow(n, 5.0) / 32.0);
  97. /* Precalculate gamma */
  98. gamma = (15.0 * Math.Pow(n, 2.0) / 16.0)
  99. + (-15.0 * Math.Pow(n, 4.0) / 32.0);
  100. /* Precalculate delta */
  101. delta = (-35.0 * Math.Pow(n, 3.0) / 48.0)
  102. + (105.0 * Math.Pow(n, 5.0) / 256.0);
  103. /* Precalculate epsilon */
  104. epsilon = (315.0 * Math.Pow(n, 4.0) / 512.0);
  105. /* Now calculate the sum of the series and return */
  106. result = alpha
  107. * (phi + (beta * Math.Sin (2.0 * phi))
  108. + (gamma * Math.Sin(4.0 * phi))
  109. + (delta * Math.Sin(6.0 * phi))
  110. + (epsilon * Math.Sin(8.0 * phi)));
  111. return result;
  112. }
  113. // 经纬度坐标转换为上海地方坐标
  114. public static double[] Wgs84Tosh(double lat, double lon)
  115. {
  116. double xx, yy, r2d, tolat, tolon, rearth, PI;
  117. PI = 3.141592653589793;
  118. r2d = 57.2957795131;
  119. tolat = (31 + (14.0 + 7.55996 / 60.0) / 60.0) / r2d;
  120. tolon = (121.0 + (28.0 + 1.80651 / 60.0) / 60) / r2d;
  121. rearth = 6371006.84;
  122. double hor, frlat, frlon, gcdist, clatf, clatt, slatf, slatt, gcbrg;
  123. double dlon, cdlon, sdlon, sdist, cdist, sbrg, cbrg, temp;
  124. double intlat, intlon;
  125. intlat = lat;
  126. intlon = lon;
  127. frlat = lat / r2d;
  128. frlon = lon / r2d;
  129. clatt = Math.Cos(frlat);
  130. clatf = Math.Cos(tolat);
  131. slatt = Math.Sin(frlat);
  132. slatf = Math.Sin(tolat);
  133. dlon = frlon - tolon;
  134. cdlon = Math.Cos(dlon);
  135. sdlon = Math.Sin(dlon);
  136. cdist = slatf * slatt + clatf * clatt * cdlon;
  137. temp = (clatt * sdlon) * (clatt * sdlon) + (clatf * slatt - slatf * clatt * cdlon) * (clatf * slatt - slatf * clatt * cdlon);
  138. sdist = Math.Sqrt(Math.Abs(temp));
  139. if ((Math.Abs(sdist) > 1e-7) || (Math.Abs(cdist) > 1e-7))
  140. gcdist = Math.Atan2(sdist, cdist);
  141. else
  142. gcdist = 0;
  143. sbrg = sdlon * clatt;
  144. cbrg = (clatf * slatt - slatf * clatt * cdlon);
  145. if ((Math.Abs(sbrg) > 1e-7) || (Math.Abs(cbrg) > 1e-7))
  146. {
  147. temp = Math.Atan2(sbrg, cbrg);
  148. while (temp < 0)
  149. {
  150. temp = temp + 2 * PI;
  151. gcbrg = temp;
  152. }
  153. }
  154. else
  155. gcbrg = 0;
  156. hor = gcdist * rearth;
  157. xx = hor * Math.Sin(temp);
  158. yy = hor * Math.Cos(temp);
  159. double[] res = new double[2];
  160. res[0] = xx;
  161. res[1] = yy;
  162. return res;
  163. }
  164. }
  165. }