DrawDtoLineHelper.cs 12 KB

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