OutputHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace XdCxRhDW.Api
  7. {
  8. public class OutputHelper
  9. {
  10. public const double WGS84a = 6378137.0;
  11. public const double f = 1.0 / 298.257224;
  12. public static List<DtoLinePoint> WriteDtoLine(double[] LOP_Value, int LOP_Len)
  13. {
  14. List<DtoLinePoint> list = new List<DtoLinePoint>();
  15. if (LOP_Len == 0) return list;
  16. var len = LOP_Len * 2;
  17. Lla[] line = new Lla[len + 1];
  18. double? prevLon = null, prevLat = null;
  19. for (int i = 0; i < LOP_Len; i++)
  20. {
  21. var lon1 = LOP_Value[3 * i + 1];
  22. var lon2 = LOP_Value[3 * i + 2];
  23. var lat = LOP_Value[3 * i];
  24. var p2Idx = len - 1 - i;
  25. if (lon1 != -1 && lon2 != -1)
  26. {
  27. if (prevLon == null)
  28. {
  29. line[i] = new Lla(lon1, lat);
  30. line[p2Idx] = new Lla(lon2, lat);
  31. }
  32. else
  33. {
  34. var disLon1 = DisLon(prevLon.Value, prevLat.Value, lon1, lat);
  35. var disLon2 = DisLon(prevLon.Value, prevLat.Value, lon2, lat);
  36. if (disLon1 < disLon2)
  37. {
  38. line[i] = new Lla(lon1, lat);
  39. line[p2Idx] = new Lla(lon2, lat);
  40. }
  41. else
  42. {
  43. line[i] = new Lla(lon2, lat);
  44. line[p2Idx] = new Lla(lon1, lat);
  45. }
  46. }
  47. prevLon = line[i].Lon;
  48. prevLat = line[i].Lat;
  49. }
  50. }
  51. var firstPoint = line.FirstOrDefault(m => m != null);
  52. if (firstPoint != null)
  53. {
  54. line[line.Length - 1] = new Lla(firstPoint.Lon, firstPoint.Lat);
  55. }
  56. Array.ForEach(line, lla =>
  57. {
  58. if (lla != null)
  59. {
  60. DtoLinePoint dtoLine = new DtoLinePoint();
  61. dtoLine.Lon = lla.Lon;
  62. dtoLine.Lat = lla.Lat;
  63. list.Add(dtoLine);
  64. }
  65. });
  66. return list;
  67. }
  68. public static double DisLon(double lon1, double lat1, double lon2, double lat2)
  69. {
  70. return DistinceLla(new double[] { lon1, lat1, 0 }, new double[] { lon2, lat2, 0 });
  71. }
  72. public static double DistinceLla(double[] lla1, double[] lla2)
  73. {
  74. double[] xyz1 = LLA2XYZ(lla1[0], lla1[1], lla1[2]), xyz2 = LLA2XYZ(lla2[0], lla2[1], lla2[2]);
  75. return Distince(xyz1[0], xyz1[1], xyz1[2], xyz2[0], xyz2[1], xyz2[2]);
  76. }
  77. public static double Distince(double x1, double y1, double z1, double x2, double y2, double z2)
  78. {
  79. double x = (x1 - x2);
  80. double y = (y1 - y2);
  81. double z = (z1 - z2);
  82. double dist = Math.Sqrt(x * x + y * y + z * z);
  83. return dist;
  84. }
  85. public static double[] LLA2XYZ(double lon, double lat, double alt)
  86. {
  87. lat = Math.PI * lat / 180.0;
  88. lon = Math.PI * lon / 180.0;
  89. double cosLat = Math.Cos(lat);
  90. double sinLat = Math.Sin(lat);
  91. double cosLong = Math.Cos(lon);
  92. double sinLong = Math.Sin(lon);
  93. double c = 1 / Math.Sqrt(cosLat * cosLat + (1 - f) * (1 - f) * sinLat * sinLat);
  94. double s = (1 - f) * (1 - f) * c;
  95. double x = (WGS84a * c + alt) * cosLat * cosLong;
  96. double y = (WGS84a * c + alt) * cosLat * sinLong;
  97. double z = (WGS84a * s + alt) * sinLat;
  98. return new double[3] { x, y, z };
  99. }
  100. }
  101. public class Lla
  102. {
  103. public Lla(double lon, double lat)
  104. {
  105. this.Lon = lon;
  106. this.Lat = lat;
  107. }
  108. public double Lon { get; set; }
  109. public double Lat { get; set; }
  110. public double Alt { get; set; } = 0;
  111. }
  112. }