OutputHelper.cs 4.2 KB

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