1. #region BLH2XYZ
    2. private const double a = 6378137;
    3. private const double f = 1 / 298.257222101;
    4. private static void BLH2XYZ(double B, double L, double H, out double X, out double Y, out double Z)
    5. {
    6. // 三个输入B、L、H,三个输出X、Y、Z
    7. // 角度转弧度
    8. B = B * Math.PI / 180;
    9. L = L * Math.PI / 180;
    10. // 计算相关参数
    11. double e2 = GetE2();
    12. double N = GetN(B);
    13. // 转化计算
    14. X = (N + H) * Math.Cos(B) * Math.Cos(L);
    15. Y = (N + H) * Math.Cos(B) * Math.Sin(L);
    16. Z = (N * (1 - e2) + H) * Math.Sin(B);
    17. }
    18. private static double GetE2()
    19. {
    20. double e2 = 2 * f - f * f;
    21. return e2;
    22. }
    23. private static double GetN(double B)
    24. {
    25. double e2 = GetE2();
    26. double sinB = Math.Sin(B);
    27. double N = a / Math.Sqrt(1 - e2 * sinB * sinB);
    28. return N;
    29. }
    30. #endregion


    选的好像是WGS84(参考椭球体)的长短轴,扁率

    1. #region WGS2UTM
    2. static double pi = Math.PI;
    3. static double sm_a = 6378137.0;
    4. static double sm_b = 6356752.314;
    5. //static double sm_EccSquared = 6.69437999013e-03;
    6. static double UTMScaleFactor = 0.9996;
    7. //得到的结果是:x坐标,y坐标,区域编号
    8. public static double[] LatLonToUTM(double lat, double lon)
    9. {
    10. double zone = Math.Floor((lon + 180.0) / 6) + 1;
    11. double cm = UTMCentralMeridian(zone);
    12. double[] xy = new double[2];
    13. MapLatLonToXY(lat / 180.0 * pi, lon / 180 * pi, cm, out xy);
    14. /* Adjust easting and northing for UTM system. */
    15. xy[0] = xy[0] * UTMScaleFactor + 500000.0;
    16. xy[1] = xy[1] * UTMScaleFactor;
    17. if (xy[1] < 0.0)
    18. {
    19. xy[1] = xy[1] + 10000000.0;
    20. }
    21. return new double[] { xy[0], xy[1], zone };
    22. }
    23. public static double UTMCentralMeridian(double zone)
    24. {
    25. double cmeridian;
    26. double deg = -183.0 + (zone * 6.0);
    27. cmeridian = deg / 180.0 * pi;
    28. return cmeridian;
    29. }
    30. internal static void MapLatLonToXY(double phi, double lambda, double lambda0, out double[] xy)
    31. {
    32. double N, nu2, ep2, t, t2, l;
    33. double l3coef, l4coef, l5coef, l6coef, l7coef, l8coef;
    34. double tmp;
    35. /* Precalculate ep2 */
    36. ep2 = (Math.Pow(sm_a, 2.0) - Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0);
    37. /* Precalculate nu2 */
    38. nu2 = ep2 * Math.Pow(Math.Cos(phi), 2.0);
    39. /* Precalculate N */
    40. N = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nu2));
    41. /* Precalculate t */
    42. t = Math.Tan(phi);
    43. t2 = t * t;
    44. tmp = (t2 * t2 * t2) - Math.Pow(t, 6.0);
    45. /* Precalculate l */
    46. l = lambda - lambda0;
    47. /* Precalculate coefficients for l**n in the equations below
    48. so a normal human being can read the expressions for easting
    49. and northing
    50. -- l**1 and l**2 have coefficients of 1.0 */
    51. l3coef = 1.0 - t2 + nu2;
    52. l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2);
    53. l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
    54. - 58.0 * t2 * nu2;
    55. l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
    56. - 330.0 * t2 * nu2;
    57. l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
    58. l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);
    59. /* Calculate easting (x) */
    60. xy = new double[2];
    61. xy[0] = N * Math.Cos(phi) * l
    62. + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0))
    63. + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0))
    64. + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0));
    65. /* Calculate northing (y) */
    66. xy[1] = ArcLengthOfMeridian(phi)
    67. + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0))
    68. + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0))
    69. + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0))
    70. + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0));
    71. return;
    72. }
    73. internal static double ArcLengthOfMeridian(double phi)
    74. {
    75. double alpha, beta, gamma, delta, epsilon, n;
    76. double result;
    77. /* Precalculate n */
    78. n = (sm_a - sm_b) / (sm_a + sm_b);
    79. /* Precalculate alpha */
    80. alpha = ((sm_a + sm_b) / 2.0)
    81. * (1.0 + (Math.Pow(n, 2.0) / 4.0) + (Math.Pow(n, 4.0) / 64.0));
    82. /* Precalculate beta */
    83. beta = (-3.0 * n / 2.0) + (9.0 * Math.Pow(n, 3.0) / 16.0)
    84. + (-3.0 * Math.Pow(n, 5.0) / 32.0);
    85. /* Precalculate gamma */
    86. gamma = (15.0 * Math.Pow(n, 2.0) / 16.0)
    87. + (-15.0 * Math.Pow(n, 4.0) / 32.0);
    88. /* Precalculate delta */
    89. delta = (-35.0 * Math.Pow(n, 3.0) / 48.0)
    90. + (105.0 * Math.Pow(n, 5.0) / 256.0);
    91. /* Precalculate epsilon */
    92. epsilon = (315.0 * Math.Pow(n, 4.0) / 512.0);
    93. /* Now calculate the sum of the series and return */
    94. result = alpha
    95. * (phi + (beta * Math.Sin(2.0 * phi))
    96. + (gamma * Math.Sin(4.0 * phi))
    97. + (delta * Math.Sin(6.0 * phi))
    98. + (epsilon * Math.Sin(8.0 * phi)));
    99. return result;
    100. }
    101. #endregion