DrawDtoLineHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. namespace XdCxRhDW.Core.Api
  9. {
  10. public class DrawDtoLineHelper
  11. {
  12. private const string XdtsDll = @"Api\时差线\Positioning.dll";
  13. [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOA", CallingConvention = CallingConvention.Cdecl)]//星地时差线
  14. public extern static void CurveByTwoTDOA(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] ref_pos, double[] Zone,
  15. double target_dto, double ref_dto, out IntPtr LOP_Value, ref int LOP_Len);
  16. private const string exeName = "XingDiSCX.exe";
  17. private static double[] zone = new double[] { -85, 85, -180, 180 }; //定位区域
  18. /// <summary>
  19. /// 星地时差线
  20. /// </summary>
  21. /// <param name="opt"></param>
  22. /// <returns></returns>
  23. /// <exception cref="Exception"></exception>
  24. public static IEnumerable<(double lon, double lat)> DtoLineXd(DtoLineXdOption opt)
  25. {
  26. List<DtoLinePoint> list = new List<DtoLinePoint>();
  27. string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线\\DtoLine.dat");
  28. string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线");
  29. if (string.IsNullOrWhiteSpace(exePath))
  30. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  31. if (!Directory.Exists(exePath))
  32. throw new Exception($"路径[{exePath}]不存在");
  33. var exeFile = Path.Combine(exePath, exeName);
  34. if (!File.Exists(exeFile))
  35. throw new Exception($"文件[{exeFile}]不存在");
  36. List<string> arguments = new List<string>();
  37. arguments.Add(file);
  38. arguments.Add($"{opt.MsEph[0]}");
  39. arguments.Add($"{opt.MsEph[1]}");
  40. arguments.Add($"{opt.MsEph[2]}");
  41. arguments.Add($"{opt.MsAnt[0]}");
  42. arguments.Add($"{opt.MsAnt[1]}");
  43. arguments.Add($"{opt.CDBAnt[0]}");
  44. arguments.Add($"{opt.CDBAnt[1]}");
  45. arguments.Add($"{opt.RefGeod[0]}");
  46. arguments.Add($"{opt.RefGeod[1]}");
  47. arguments.Add($"{opt.xdDto}");
  48. arguments.Add($"{opt.RefDto}");
  49. arguments.Add($"{opt.PosLon}");
  50. arguments.Add($"{opt.PosLat}");
  51. Process p = new Process();
  52. p.StartInfo.WorkingDirectory = exePath;
  53. p.StartInfo.FileName = exeFile;
  54. p.StartInfo.Arguments = string.Join(" ", arguments);
  55. p.StartInfo.CreateNoWindow = true;
  56. p.StartInfo.RedirectStandardError = true;
  57. p.StartInfo.RedirectStandardOutput = true;
  58. p.StartInfo.UseShellExecute = false;
  59. p.Start();
  60. var succeed = p.WaitForExit(10000);
  61. if (!succeed)
  62. {
  63. throw new Exception($"进程[{exeName}]超时未完成!");
  64. }
  65. string result = p.StandardOutput.ReadToEnd();
  66. if (string.IsNullOrWhiteSpace(result))
  67. {
  68. throw new Exception("计算时差线出现未知错误!");
  69. }
  70. var array = result.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  71. if (array[0] == "1")
  72. {
  73. throw new Exception(array[1]);
  74. }
  75. if (File.Exists(file))
  76. {
  77. var dtostr = File.ReadAllText(file);
  78. list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DtoLinePoint>>(dtostr);
  79. }
  80. var Lines = list.Select(s => (s.Lon, s.Lat));
  81. return Lines;
  82. }
  83. /// <summary>
  84. /// 星地双星时差线
  85. /// </summary>
  86. /// <param name="opt"></param>
  87. /// <returns></returns>
  88. public static IEnumerable<(double lon, double lat)> DtoLineXDTwoStart(DtoLineTwoStartOption opt)
  89. {
  90. List<DtoLinePoint> list = new List<DtoLinePoint>();
  91. IntPtr LOP_ValuePtr;
  92. int LOP_Len = 0;
  93. CurveByTwoTDOA(
  94. opt.MsEph,
  95. opt.NsEph,
  96. opt.MsAnt,
  97. opt.NsAnt,
  98. opt.RefGeod,
  99. zone,
  100. opt.TargetDto * 1e-6,
  101. opt.RefDto * 1e-6, out LOP_ValuePtr, ref LOP_Len);
  102. double[] LOP_Value = new double[LOP_Len * 3];
  103. if (LOP_Len > 0)
  104. {
  105. Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
  106. list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len);
  107. }
  108. var Lines = list.Select(p => (p.Lon, p.Lat));
  109. return Lines;
  110. }
  111. private static IEnumerable<(double lon, double lat)> ParseResult(IntPtr LOP_ValuePtr, int LOP_Len)
  112. {
  113. List<DtoLinePoint> list = new List<DtoLinePoint>();
  114. double[] LOP_Value = new double[LOP_Len * 3];
  115. if (LOP_Len > 0)
  116. {
  117. Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
  118. for (int idx = 0; idx < LOP_Len; ++idx)
  119. {
  120. if (LOP_Value[3 * idx + 1] != -1)
  121. {
  122. list.Add(new DtoLinePoint()
  123. {
  124. Lon = LOP_Value[3 * idx + 1],
  125. Lat = LOP_Value[3 * idx]
  126. });
  127. }
  128. }
  129. for (int idx = 0; idx < LOP_Len; ++idx)
  130. {
  131. if (LOP_Value[3 * idx + 2] != -1)
  132. {
  133. list.Add(new DtoLinePoint()
  134. {
  135. Lon = LOP_Value[3 * idx + 2],
  136. Lat = LOP_Value[3 * idx]
  137. });
  138. }
  139. }
  140. }
  141. var Lines = list.Select(p => (p.Lon, p.Lat));
  142. return Lines;
  143. }
  144. }
  145. }