DrawDtoLineHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. using System.Text;
  9. using XdCxRhDW.App.Api;
  10. namespace XdCxRhDW.Core.Api
  11. {
  12. public class DrawDtoLineHelper
  13. {
  14. private const string XdtsDll = @"Api\时差线\Positioning.dll";
  15. [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOA", CallingConvention = CallingConvention.Cdecl)]//高轨双星有参时差线
  16. public extern static void CurveByTwoTDOA(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] ref_pos, double[] Zone,
  17. double target_dto, double ref_dto, out IntPtr LOP_Value, ref int LOP_Len);
  18. /*
  19. 双星时差线-无参
  20. LOP_Value 返回值 结构参考DTO_Plot
  21. LOP_Len*3为LOP_Value的长度
  22. */
  23. [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOAWithNoRef", CallingConvention = CallingConvention.Cdecl)]//高轨双星无参时差线
  24. public extern static void CurveByTwoTDOAWithNoRef(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos,
  25. double[] Zone, double target_dto, out IntPtr LOP_Value, ref int LOP_Len);
  26. [DllImport(XdtsDll, CallingConvention = CallingConvention.Cdecl)]
  27. public static extern void Destory(IntPtr val);
  28. private const string exeName = "XingDiSCX.exe";//星地时差线
  29. private static double[] zone = new double[] { -85, 85, -180, 180 }; //定位区域
  30. private const string locexeName = "locow.exe";
  31. /// <summary>
  32. /// 星地时差线
  33. /// </summary>
  34. /// <param name="opt"></param>
  35. /// <returns></returns>
  36. /// <exception cref="Exception"></exception>
  37. public static IEnumerable<(double lon, double lat)> DtoLineXd(DtoLineXdOption opt)
  38. {
  39. List<DtoLinePoint> list = new List<DtoLinePoint>();
  40. string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线\\DtoLine.dat");
  41. string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线");
  42. if (string.IsNullOrWhiteSpace(exePath))
  43. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  44. if (!Directory.Exists(exePath))
  45. throw new Exception($"路径[{exePath}]不存在");
  46. var exeFile = Path.Combine(exePath, exeName);
  47. if (!File.Exists(exeFile))
  48. throw new Exception($"文件[{exeFile}]不存在");
  49. List<string> arguments = new List<string>();
  50. arguments.Add(file);
  51. arguments.Add($"{opt.MsEph[0]}");
  52. arguments.Add($"{opt.MsEph[1]}");
  53. arguments.Add($"{opt.MsEph[2]}");
  54. arguments.Add($"{opt.MsAnt[0]}");
  55. arguments.Add($"{opt.MsAnt[1]}");
  56. arguments.Add($"{opt.CDBAnt[0]}");
  57. arguments.Add($"{opt.CDBAnt[1]}");
  58. arguments.Add($"{opt.RefGeod[0]}");
  59. arguments.Add($"{opt.RefGeod[1]}");
  60. arguments.Add($"{opt.xdDto}");
  61. arguments.Add($"{opt.RefDto}");
  62. arguments.Add($"{opt.PosLon}");
  63. arguments.Add($"{opt.PosLat}");
  64. Process p = new Process();
  65. p.StartInfo.WorkingDirectory = exePath;
  66. p.StartInfo.FileName = exeFile;
  67. p.StartInfo.Arguments = string.Join(" ", arguments);
  68. p.StartInfo.CreateNoWindow = true;
  69. p.StartInfo.RedirectStandardError = true;
  70. p.StartInfo.RedirectStandardOutput = true;
  71. p.StartInfo.UseShellExecute = false;
  72. p.Start();
  73. var succeed = p.WaitForExit(10000);
  74. if (!succeed)
  75. {
  76. throw new Exception($"进程[{exeName}]超时未完成!");
  77. }
  78. string result = p.StandardOutput.ReadToEnd();
  79. if (string.IsNullOrWhiteSpace(result))
  80. {
  81. throw new Exception("计算时差线出现未知错误!");
  82. }
  83. var array = result.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  84. if (array[0] == "1")
  85. {
  86. throw new Exception(array[1]);
  87. }
  88. if (File.Exists(file))
  89. {
  90. var dtostr = File.ReadAllText(file);
  91. list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DtoLinePoint>>(dtostr);
  92. }
  93. var Lines = list.Select(s => (s.Lon, s.Lat));
  94. return Lines;
  95. }
  96. /// <summary>
  97. /// 高轨双星时差线
  98. /// </summary>
  99. /// <param name="opt"></param>
  100. /// <returns></returns>
  101. public static IEnumerable<(double lon, double lat)> DtoLine2XStart(DtoLineTwoStartOption opt)
  102. {
  103. List<DtoLinePoint> list = new List<DtoLinePoint>();
  104. IntPtr LOP_ValuePtr;
  105. int LOP_Len = 0;
  106. CurveByTwoTDOA(
  107. opt.MsEph,
  108. opt.NsEph,
  109. opt.MsAnt,
  110. opt.NsAnt,
  111. opt.RefGeod,
  112. zone,
  113. opt.TargetDto * 1e-6,
  114. opt.RefDto * 1e-6, out LOP_ValuePtr, ref LOP_Len);
  115. double[] LOP_Value = new double[LOP_Len * 3];
  116. if (LOP_Len > 0)
  117. {
  118. Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
  119. list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len);
  120. }
  121. Destory(LOP_ValuePtr);
  122. var Lines = list.Select(p => (p.Lon, p.Lat));
  123. return Lines;
  124. }
  125. public static IEnumerable<(double lon, double lat)> DtoLine2XNoRefStart(DtoLineTwoStartOption opt)
  126. {
  127. List<DtoLinePoint> list = new List<DtoLinePoint>();
  128. IntPtr LOP_ValuePtr;
  129. int LOP_Len = 0;
  130. CurveByTwoTDOAWithNoRef(
  131. opt.MsEph,
  132. opt.NsEph,
  133. opt.MsAnt,
  134. opt.NsAnt,
  135. zone,
  136. opt.TargetDto * 1e-6, out LOP_ValuePtr, ref LOP_Len);
  137. double[] LOP_Value = new double[LOP_Len * 3];
  138. if (LOP_Len > 0)
  139. {
  140. Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
  141. list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len);
  142. }
  143. Destory(LOP_ValuePtr);
  144. var Lines = list.Select(p => (p.Lon, p.Lat));
  145. return Lines;
  146. }
  147. /// <summary>
  148. /// 高轨双星频差线
  149. /// </summary>
  150. /// <param name="opt"></param>
  151. /// <returns></returns>
  152. /// <exception cref="Exception"></exception>
  153. public static IEnumerable<(double lon, double lat)> DtoLineTwoStart(DtoLineTwoStartOption opt)
  154. {
  155. List<DfoLinePoint> list = new List<DfoLinePoint>();
  156. string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\频差线");
  157. if (string.IsNullOrWhiteSpace(exePath))
  158. throw new Exception($"请先调用SetExePath指定{locexeName}进程所在路径,支持相对路径");
  159. if (!Directory.Exists(exePath))
  160. throw new Exception($"路径[{exePath}]不存在");
  161. var exeFile = Path.Combine(exePath, locexeName);
  162. if (!File.Exists(exeFile))
  163. throw new Exception($"文件[{exeFile}]不存在");
  164. List<string> arguments = new List<string>();
  165. arguments.Add("dtoline");
  166. arguments.Add($"{opt.TargetDto}");
  167. arguments.Add($"--rec1 {opt.MsAnt[0]} {opt.MsAnt[1]} 0");
  168. arguments.Add($"--rec2 {opt.NsAnt[0]} {opt.NsAnt[1]} 0");
  169. arguments.Add($"--eph1 {opt.MsEph[0]} {opt.MsEph[1]} {opt.MsEph[2]}");
  170. arguments.Add($"--eph2 {opt.NsEph[0]} {opt.NsEph[1]} {opt.NsEph[2]}");
  171. arguments.Add($"--refdt {opt.RefDto}");
  172. arguments.Add($"--reflla {opt.RefGeod[0]} {opt.RefGeod[1]} 0");
  173. Process p = new Process();
  174. p.StartInfo.WorkingDirectory = exePath;
  175. p.StartInfo.FileName = exeFile;
  176. p.StartInfo.Arguments = string.Join(" ", arguments);
  177. p.StartInfo.CreateNoWindow = true;
  178. p.StartInfo.RedirectStandardError = true;
  179. p.StartInfo.RedirectStandardOutput = true;
  180. p.StartInfo.UseShellExecute = false;
  181. StringBuilder sb = new StringBuilder();
  182. p.OutputDataReceived += (sender, e) => sb.Append(e.Data);
  183. p.Start();
  184. p.BeginOutputReadLine();
  185. p.WaitForExit();
  186. if (string.IsNullOrWhiteSpace(sb.ToString()))
  187. {
  188. throw new Exception("计算时差线出现未知错误!");
  189. }
  190. var array = sb.ToString().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  191. foreach (var item in array)
  192. {
  193. var strs = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  194. DfoLinePoint point = new DfoLinePoint();
  195. point.Lon = Convert.ToDouble(strs[0]);
  196. point.Lat = Convert.ToDouble(strs[1]);
  197. list.Add(point);
  198. }
  199. var Lines = list.Select(s => (s.Lon, s.Lat));
  200. return Lines;
  201. }
  202. private static IEnumerable<(double lon, double lat)> ParseResult(IntPtr LOP_ValuePtr, int LOP_Len)
  203. {
  204. List<DtoLinePoint> list = new List<DtoLinePoint>();
  205. double[] LOP_Value = new double[LOP_Len * 3];
  206. if (LOP_Len > 0)
  207. {
  208. Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
  209. for (int idx = 0; idx < LOP_Len; ++idx)
  210. {
  211. if (LOP_Value[3 * idx + 1] != -1)
  212. {
  213. list.Add(new DtoLinePoint()
  214. {
  215. Lon = LOP_Value[3 * idx + 1],
  216. Lat = LOP_Value[3 * idx]
  217. });
  218. }
  219. }
  220. for (int idx = 0; idx < LOP_Len; ++idx)
  221. {
  222. if (LOP_Value[3 * idx + 2] != -1)
  223. {
  224. list.Add(new DtoLinePoint()
  225. {
  226. Lon = LOP_Value[3 * idx + 2],
  227. Lat = LOP_Value[3 * idx]
  228. });
  229. }
  230. }
  231. }
  232. var Lines = list.Select(p => (p.Lon, p.Lat));
  233. return Lines;
  234. }
  235. }
  236. }