CoordinateConversion.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ProjectBase.Data.Logs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SIASUN.Autopilot.GNSS
  9. {
  10. /// <summary>
  11. /// WGS84和平面坐标系转换
  12. /// </summary>
  13. public class CoordinateConversion
  14. {
  15. // minLongitude, maxLatitude
  16. private WGS84Points point0;
  17. // maxLongitude, minLatitude
  18. private WGS84Points point1;
  19. //// minLongitude, maxLatitude
  20. //private WGS84Points point2;
  21. //// 显示区域的宽度
  22. //private double screenx;
  23. //// 显示区域的高度
  24. //private double screeny;
  25. private PointF imagePoint0;
  26. private PointF imagePoint1;
  27. // x轴比例因子
  28. private double scalex;
  29. // y轴比例因子
  30. private double scaley;
  31. //public CoordinateConversion(double imageWidth,double imageHeight, WGS84Points point0, WGS84Points point1, WGS84Points point2)
  32. //{
  33. // this.screenx = imageWidth;
  34. // this.screeny = imageHeight;
  35. // this.point0 = point0;
  36. // this.point1 = point1;
  37. // this.point2 = point2;
  38. //}
  39. public CoordinateConversion(PointF imagePoint0, PointF imagePoint1, WGS84Points point0, WGS84Points point1)
  40. {
  41. this.imagePoint0 = imagePoint0;
  42. this.imagePoint1 = imagePoint1;
  43. this.point0 = point0;
  44. this.point1 = point1;
  45. }
  46. /// <summary>
  47. /// 获取WGS84转换到屏幕坐标的x轴比例因子
  48. /// </summary>
  49. private void GetScaleX()
  50. {
  51. try
  52. {
  53. double lon = 0.0;
  54. float x = 0.0F;
  55. lon = point0.x - point1.x;
  56. x = imagePoint0.X - imagePoint1.X;
  57. //scalex = ((point1.x - point0.x) * 3600) / screenx;
  58. scalex = (lon * 3600) / x;
  59. }
  60. catch(Exception ex)
  61. {
  62. LogHelper.log.Error("计算x比例因子出现错误", ex);
  63. }
  64. }
  65. /// <summary>
  66. /// 获取WGS84转换到屏幕坐标的Y轴比例因子
  67. /// </summary>
  68. private void GetScaleY()
  69. {
  70. try
  71. {
  72. double lat = 0.0;
  73. float y = 0.0F;
  74. lat = point0.y - point1.y;
  75. y = imagePoint0.Y - imagePoint1.Y;
  76. scaley = (lat * 3600) / y;
  77. //scaley = ((point0.y - point2.y) * 3600) / screeny;
  78. }
  79. catch (Exception ex)
  80. {
  81. LogHelper.log.Error("计算y比例因子出现错误", ex);
  82. }
  83. }
  84. /// <summary>
  85. /// 从WGS84转换成屏幕坐标
  86. /// </summary>
  87. /// <param name="lon"></param>
  88. /// <param name="lat"></param>
  89. /// <param name="x"></param>
  90. /// <param name="y"></param>
  91. public void GetWGS84ToGauss(double lon, double lat, out double x, out double y)
  92. {
  93. x = 0.0;
  94. y = 0.0;
  95. try
  96. {
  97. GetScaleX();
  98. GetScaleY();
  99. //x = (lon - point0.x) * 3600 / scalex;
  100. //y = (point0.y - lat) * 3600 / scaley;
  101. x = imagePoint0.X -( (point0.x - lon) * 3600 / scalex);
  102. y = imagePoint0.Y - ((point0.y - lat) * 3600 / scaley);
  103. }
  104. catch (Exception ex)
  105. {
  106. LogHelper.log.Error("从WGS84转换成屏幕坐标出现错误", ex);
  107. }
  108. }
  109. /// <summary>
  110. /// 从屏幕坐标转换成WGS84
  111. /// </summary>
  112. /// <param name="lon"></param>
  113. /// <param name="lat"></param>
  114. /// <param name="x"></param>
  115. /// <param name="y"></param>
  116. public void GetGaussToWGS84(double x, double y, out double lon, out double lat)
  117. {
  118. lon = 0.0;
  119. lat = 0.0;
  120. try
  121. {
  122. GetScaleX();
  123. GetScaleY();
  124. //lon = x * scalex /3600 + point0.x;
  125. //lat = point0.y - y * scaley / 3600;
  126. lon = point0.x - ((imagePoint0.X - x) * scalex / 3600);
  127. lat = point0.y - ((imagePoint0.Y - y) * scaley / 3600);
  128. }
  129. catch (Exception ex)
  130. {
  131. LogHelper.log.Error("从屏幕坐标转换成WGS84出现错误", ex);
  132. }
  133. }
  134. }
  135. }