CtrlPosXd.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using DevExpress.Utils.Drawing.Helpers;
  2. using DevExpress.Utils.Extensions;
  3. using DevExpress.XtraEditors;
  4. using DevExpress.XtraExport.Helpers;
  5. using DevExpress.XtraGrid.Views.Grid;
  6. using DevExpress.XtraMap;
  7. using DevExpress.XtraTreeList.Data;
  8. using DxHelper;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Entity;
  14. using System.Drawing;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows.Controls;
  21. using System.Windows.Documents;
  22. using System.Windows.Forms;
  23. using XdCxRhDW.App.Api.时差粗值预测;
  24. using XdCxRhDW.App.Api.时差线;
  25. using XdCxRhDW.App.Api.星历推算;
  26. using XdCxRhDW.App.DTO;
  27. using XdCxRhDW.App.UserControl;
  28. using XzXdDw.App.Api;
  29. using XzXdDw.App.Api.星地GDOP误差椭圆;
  30. using XzXdDw.App.EditForms;
  31. using XzXdDw.App.EFContext;
  32. using XzXdDw.App.Model;
  33. namespace XzXdDw.App.UserControl
  34. {
  35. /// <summary>
  36. /// 星地定位
  37. /// </summary>
  38. public partial class CtrlPosXd : DevExpress.XtraEditors.XtraUserControl
  39. {
  40. DtXDParam dtxdctrl = null;
  41. X2D1GDOPParam x2D1GdopParam = null;
  42. TcpServer tcpServer;
  43. public CtrlPosXd()
  44. {
  45. InitializeComponent();
  46. tcpServer = new TcpServer();
  47. }
  48. private async void CtrlXdPos_Load(object sender, EventArgs e)
  49. {
  50. try
  51. {
  52. gridPos.Init().UseMultiSelect().UseFilter().UseRowNumber().UseExportXlsx()
  53. .AddMenu("删除", SvgHelper.CreateClose(), async () =>
  54. {
  55. try
  56. {
  57. if (XtraMessageBox.Show("确认删除?(Ctrl+A可全选删除)", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  58. {
  59. return;
  60. }
  61. var ids = gridView1.GetSelectedRows();
  62. List<XDPosRes> deletePos = new List<XDPosRes>();
  63. using (RHDWContext db = new RHDWContext())
  64. {
  65. foreach (var id in ids)
  66. {
  67. var item = gridView1.GetRow(id) as XDPosRes;
  68. var delItem = await db.XDPosRes.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  69. if (delItem != null)
  70. {
  71. db.XDPosRes.Remove(delItem);
  72. }
  73. deletePos.Add(item);
  74. }
  75. await db.SaveChangesAsync();
  76. }
  77. gridView1.DeleteSelectedRows();
  78. mapControl1.DelPosItem(deletePos);
  79. }
  80. catch (Exception ex)
  81. {
  82. Serilog.Log.Error("删除结果失败", ex);
  83. XtraMessageBox.Show("删除结果失败");
  84. }
  85. })
  86. .AddMenu("手动定位", SvgHelper.LoadFromFile("Image\\Pos.svg"), XdPos)
  87. .AddMenu("绘制时差线", SvgHelper.LoadFromFile("Image\\DrawLine.svg"), DrawDtoLine)
  88. .AddMenu("加载仿真数据", SvgHelper.LoadFromFile("Image\\LoadData.svg"), LoadSimulationData)
  89. .AddMenu("绘制误差椭圆", SvgHelper.LoadFromFile("Image\\误差椭圆.svg"), () =>
  90. {
  91. var ctrl = new X2D1ErrEllipseParam();
  92. PopupHelper.ShowPopup(ctrl, mapControl1, mapControl1.Width / 4, onHide: hideReason =>
  93. {
  94. if (hideReason == HideReason.Defalut) return;
  95. var model = ctrl.Model;
  96. double[] main_sat = Tle2XYZ.GetXyz(model.TleMain, model.CapTime);
  97. double[] adja_sat = Tle2XYZ.GetXyz(model.TleAdja, model.CapTime);
  98. DrawErrorEllipse2X1D(main_sat, adja_sat, new double[] { model.StationLon, model.StationLat, 0 }, new double[] { model.RefLon, model.RefLat, 0 }, model.DtousErr, model.SatLocErr);
  99. });
  100. })
  101. .AddMenu("停止加载", SvgHelper.LoadFromFile("Image\\Stop.svg"), () => stoped = true);
  102. mapControl1.UseDefalutOptions()
  103. //.UseCluster()//定位点使用内置聚合
  104. .UseClearAll()
  105. .UseDistanceLine()
  106. .UseMarkDot()
  107. //.UseExportImg()
  108. //.UseExportXlsx()
  109. //.UseExportCsv()
  110. .UseDrawRect(rect =>
  111. {
  112. if (dtxdctrl != null)
  113. {
  114. dtxdctrl.rect = rect;
  115. }
  116. });
  117. //MapMenu为地图瓦片上右键菜单
  118. //AddPosMenu为定位点上右键菜单
  119. //RectMenu为框选的区域上右键菜单
  120. mapControl1.AddMapMenu("加载仿真数据", SvgHelper.LoadFromFile("Image\\LoadData.svg"), (lon, lat) => LoadSimulationData())
  121. .AddMapMenu("停止加载", SvgHelper.LoadFromFile("Image\\Stop.svg"), (lon, lat) => stoped = true)
  122. .AddMapMenu("GDOP分析", SvgHelper.LoadFromFile("Image\\GDOP.svg"), (lon, lat) =>
  123. {
  124. if (x2D1GdopParam == null)
  125. {
  126. x2D1GdopParam = new X2D1GDOPParam();
  127. }
  128. x2D1GdopParam.mapControl1 = mapControl1;
  129. PopupHelper.ShowPopup(x2D1GdopParam, mapControl1, mapControl1.Width / 4);
  130. })
  131. .AddPosMenu<XDPosRes>("误差椭圆", SvgHelper.LoadFromFile("Image\\误差椭圆.svg"), item =>
  132. {
  133. var ctrl = new X2D1ErrEllipseParam();
  134. PopupHelper.ShowPopup(ctrl, mapControl1, mapControl1.Width / 4, onHide: hideReason =>
  135. {
  136. if (hideReason == HideReason.Defalut) return;
  137. var model = ctrl.Model;
  138. double[] main_sat = Tle2XYZ.GetXyz(model.TleMain, model.CapTime);
  139. double[] adja_sat = Tle2XYZ.GetXyz(model.TleAdja, model.CapTime);
  140. DrawErrorEllipse2X1D(main_sat, adja_sat, new double[] { model.StationLon, model.StationLat, 0 }, new double[] { model.RefLon, model.RefLat, 0 }, model.DtousErr, model.SatLocErr);
  141. });
  142. })
  143. .AddRectMenu<XDPosRes>("时差初值预估", SvgHelper.LoadFromFile("Image\\初值预估.svg"), items =>
  144. {
  145. var rect = mapControl1.GetCurrentRect();
  146. if (dtxdctrl == null)
  147. {
  148. dtxdctrl = new DtXDParam();
  149. }
  150. dtxdctrl.rect = rect;
  151. PopupHelper.ShowPopup(dtxdctrl, mapControl1, mapControl1.Width / 4);
  152. });
  153. //查询已有的定位记录
  154. using (RHDWContext db = new RHDWContext())
  155. {
  156. List<XDPosRes> list = await db.XDPosRes.ToListAsync();
  157. this.gridPos.DataSource = list;
  158. mapControl1.SetPosDataSource(list);
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. Serilog.Log.Error("查询定位结果失败", ex);
  164. XtraMessageBox.Show("查询定位结果失败");
  165. }
  166. }
  167. bool stoped = false;
  168. private async void LoadSimulationData()
  169. {
  170. SimulationEditor editor = new SimulationEditor("Simulation_Data2023.dat");
  171. if (editor.ShowDialog() != DialogResult.OK)
  172. {
  173. return;
  174. }
  175. string simulationFile = editor.SimulationFile;
  176. stoped = false;
  177. //在列表控件中全选+右键可以删除所有测试结果
  178. using (RHDWContext db = new RHDWContext())
  179. {
  180. var listTx = db.TxInfos.ToList();
  181. var sysSetings = await db.SysSetings.FirstOrDefaultAsync();
  182. var lines = File.ReadAllLines(simulationFile);
  183. Random r = new Random();
  184. foreach (var item in lines)
  185. {
  186. if (stoped) break;
  187. var items = item.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  188. if (items.Length != 13) continue;
  189. var sxDto = Convert.ToDouble(items[0]);
  190. var xdDto = Convert.ToDouble(items[1]);
  191. var mainYbDto = Convert.ToDouble(items[2]);
  192. var adjaYbDto = Convert.ToDouble(items[3]);
  193. var mainX = Convert.ToDouble(items[7]);
  194. var mainY = Convert.ToDouble(items[8]);
  195. var mainZ = Convert.ToDouble(items[9]);
  196. var adjaX = Convert.ToDouble(items[10]);
  197. var adjaY = Convert.ToDouble(items[11]);
  198. var adjaZ = Convert.ToDouble(items[12]);
  199. var cgRes = new CgRes()
  200. {
  201. SigTime = DateTime.Now,
  202. DtoSx = sxDto * 1e6,
  203. DfoSx = 200 + r.Next(1, 5) + Math.Round(r.NextDouble(), 3),
  204. SnrSx = r.Next(18, 24) + Math.Round(r.NextDouble(), 1),
  205. DtoCdb = xdDto * 1e6,
  206. DfoCdb = 600 + r.Next(1, 5) + Math.Round(r.NextDouble(), 3),
  207. SnrCdb = r.Next(22, 32) + Math.Round(r.NextDouble(), 1),
  208. YbMain = mainYbDto * 1e6,
  209. YbAdja = adjaYbDto * 1e6,
  210. MainX = mainX,
  211. MainY = mainY,
  212. MainZ = mainZ,
  213. AdjaX = adjaX,
  214. AdjaY = adjaY,
  215. AdjaZ = adjaZ,
  216. };
  217. var cg = db.CgRes.Add(cgRes);//参估结果入库
  218. await db.SaveChangesAsync();
  219. var res = PosApi.X2D1_POS(cgRes, listTx);
  220. XDPosRes posRes = new XDPosRes()
  221. {
  222. SigTime = cgRes.SigTime,
  223. CgResID = cgRes.ID,
  224. TarName = "未知目标",
  225. TsName = "tar1",
  226. PosLon = res[0],
  227. PosLat = res[1],
  228. MirrLon = res[3],
  229. MirrLat = res[4]
  230. };
  231. db.XDPosRes.Add(posRes);//定位结果入库
  232. await db.SaveChangesAsync();
  233. SendPosData(posRes, sysSetings);
  234. //更新定位结果UI列表和地图
  235. var currentTask = gridView1.GetFocusedRow() as TaskInfo;
  236. var ds = this.gridPos.DataSource as List<XDPosRes>;
  237. ds.Insert(0, posRes);
  238. this.Invoke(new Action(() =>
  239. {
  240. gridView1.RefreshData();
  241. mapControl1.AddPosItem(posRes);
  242. }));
  243. await Task.Delay(200);
  244. }
  245. }
  246. }
  247. private void SendPosData(XDPosRes pos, SysSetings setings)
  248. {
  249. if (setings == null || string.IsNullOrEmpty(setings.ServerIp) || setings.Port < 0 || setings.Port > 65535)
  250. {
  251. return;
  252. }
  253. string json = Newtonsoft.Json.JsonConvert.SerializeObject(pos);
  254. tcpServer.SendData(setings.ServerIp, setings.Port, json);
  255. }
  256. private async void XdPos()
  257. {
  258. try
  259. {
  260. var ids = gridView1.GetSelectedRows();
  261. if (ids.Length <= 0)
  262. {
  263. XtraMessageBox.Show("请选择需要手动定位的数据信息!");
  264. return;
  265. }
  266. var item = gridView1.GetRow(ids[0]) as XDPosRes;
  267. List<TxInfo> listTx = new List<TxInfo>();
  268. CgRes cg;
  269. using (RHDWContext db = new RHDWContext())
  270. {
  271. listTx = db.TxInfos.ToList();
  272. cg = await db.CgRes.Where(m => m.ID == item.CgResID).FirstOrDefaultAsync();
  273. }
  274. X2D1PosParamEditor frm = new X2D1PosParamEditor(item, cg, listTx, () =>
  275. {
  276. gridView1.RefreshData();
  277. if (item.PosLon > 180 || item.PosLon < -180)
  278. {
  279. item.PosLon = 0;
  280. item.PosLat = 0;
  281. }
  282. mapControl1.UpdatePosItem(item);
  283. });
  284. frm.ShowDialog();
  285. }
  286. catch (Exception ex)
  287. {
  288. Serilog.Log.Error("手动定位失败", ex);
  289. XtraMessageBox.Show("手动定位失败");
  290. }
  291. }
  292. private async void DrawDtoLine()
  293. {
  294. try
  295. {
  296. var ids = gridView1.GetSelectedRows();
  297. if (ids.Length <= 0)
  298. {
  299. XtraMessageBox.Show("请选择需要绘制时差线的定位数据信息!");
  300. return;
  301. }
  302. var item = gridView1.GetRow(ids[0]) as XDPosRes;
  303. List<TxInfo> listTx = new List<TxInfo>();
  304. List<XzXdDw.App.Model.SatInfo> listSat = new List<XzXdDw.App.Model.SatInfo>();
  305. CgRes cg;
  306. using (RHDWContext db = new RHDWContext())
  307. {
  308. listTx = db.TxInfos.ToList();
  309. listSat = db.SatInfos.ToList();
  310. cg = await db.CgRes.Where(m => m.ID == item.CgResID).FirstOrDefaultAsync();
  311. }
  312. if (cg == null)
  313. {
  314. XtraMessageBox.Show($"未找到定位相关的计算[{item.CgResID}]信息");
  315. return;
  316. }
  317. var satTx = listTx.Find(p => p.TxType == EnumTxType.MainSat);
  318. var satNTx = listTx.Find(p => p.TxType == EnumTxType.AdjaSat);
  319. var cdbTx = listTx.Find(p => p.TxType == EnumTxType.Cdb);
  320. var refTx = listTx.Find(p => p.TxType == EnumTxType.Ref);
  321. DtoLineXdOption dtoLineXd = new DtoLineXdOption();
  322. dtoLineXd.MsEph = new double[] { cg.MainX, cg.MainY, cg.MainZ, 0, 0, 0 };
  323. dtoLineXd.MsAnt = new double[] { satTx.Lon, satTx.Lat, 0 };
  324. dtoLineXd.CDBAnt = new double[] { cdbTx.Lon, cdbTx.Lat, 0 };
  325. dtoLineXd.RefGeod = new double[] { refTx.Lon, refTx.Lat, 0 };
  326. dtoLineXd.xdDto = cg.DtoCdb;
  327. dtoLineXd.RefDto = cg.YbMain;
  328. dtoLineXd.PosLon = item.PosLon;
  329. dtoLineXd.PosLat = item.PosLat;
  330. var xdDtoLine = DrawDtoLineHelper.DtoLineXd(dtoLineXd);
  331. mapControl1.DrawDtoPonit($"星地[{listSat.FirstOrDefault(m => m.ID == satTx.ID)?.Sat},{listSat.FirstOrDefault(m => m.ID == cdbTx.ID)?.Sat}]时差线", xdDtoLine);
  332. DtoLineTwoStartOption twoStartOption = new DtoLineTwoStartOption();
  333. twoStartOption.MsEph = new double[] { cg.MainX, cg.MainY, cg.MainZ, 0, 0, 0 };
  334. twoStartOption.NsEph = new double[] { cg.AdjaX, cg.AdjaY, cg.AdjaZ, 0, 0, 0 };
  335. twoStartOption.MsAnt = new double[] { satTx.Lon, satTx.Lat, 0 };
  336. twoStartOption.NsAnt = new double[] { satNTx.Lon, satNTx.Lat, 0 };
  337. twoStartOption.RefGeod = new double[] { refTx.Lon, refTx.Lat, 0 };
  338. twoStartOption.TargetDto = cg.DtoSx;
  339. twoStartOption.RefDto = cg.YbMain - cg.YbAdja;
  340. twoStartOption.PosLon = item.PosLon;
  341. twoStartOption.PosLat = item.PosLat;
  342. var tsDtoLine = DrawDtoLineHelper.DtoLineXDTwoStart(twoStartOption);
  343. mapControl1.DrawDtoPonit($"双星[{listSat.FirstOrDefault(m => m.ID == satTx.ID)?.Sat},{listSat.FirstOrDefault(m => m.ID == satNTx.ID)?.Sat}]时差线", tsDtoLine);
  344. }
  345. catch (Exception ex)
  346. {
  347. Serilog.Log.Error("绘制时差线失败", ex);
  348. XtraMessageBox.Show($"绘制时差线失败,失败信息:{ex.Message}");
  349. }
  350. }
  351. private void DrawErrorEllipse2X1D(double[] MsEph, double[] NsEph, double[] CDBAnt, double[] RefGeod, double DtoErr, double EphErr)
  352. {
  353. try
  354. {
  355. var ids = gridView1.GetSelectedRows();
  356. if (ids.Length <= 0)
  357. {
  358. XtraMessageBox.Show("请选择需要绘制误差椭圆线的定位数据信息!");
  359. return;
  360. }
  361. var item = gridView1.GetRow(ids[0]) as XDPosRes;
  362. List<TxInfo> listTx = new List<TxInfo>();
  363. List<XzXdDw.App.Model.SatInfo> listSat = new List<XzXdDw.App.Model.SatInfo>();
  364. using (RHDWContext db = new RHDWContext())
  365. {
  366. listTx = db.TxInfos.ToList();
  367. listSat = db.SatInfos.ToList();
  368. }
  369. var satTx = listTx.Find(p => p.TxType == EnumTxType.MainSat);
  370. var satNTx = listTx.Find(p => p.TxType == EnumTxType.AdjaSat);
  371. XzXdDw.App.Api.低轨GDOP误差椭圆.ErrorEllipseDTO2X1DOption Option = new XzXdDw.App.Api.低轨GDOP误差椭圆.ErrorEllipseDTO2X1DOption();
  372. Option.MsEph = MsEph;
  373. Option.NsEph = NsEph;
  374. Option.CDBAnt = CDBAnt;
  375. Option.RefGeod = RefGeod;
  376. Option.SelectPoint = new double[3] { item.PosLon, item.PosLat, 0 };
  377. Option.DtoErr = DtoErr * 1e-6;
  378. Option.EphErr = EphErr;
  379. var points = XzXdDw.App.Api.低轨GDOP误差椭圆.ErrEllipseHelper.ErrorEllipse2X1D(Option);
  380. mapControl1.DrawDtoPonit($"双星[{listSat.FirstOrDefault(m => m.ID == satTx.ID)?.Sat},{listSat.FirstOrDefault(m => m.ID == satNTx.ID)?.Sat}]误差椭圆线", points);
  381. }
  382. catch (Exception ex)
  383. {
  384. Serilog.Log.Error("绘制误差椭圆线失败", ex);
  385. XtraMessageBox.Show($"绘制误差椭圆线失败,失败信息:{ex.Message}");
  386. }
  387. }
  388. }
  389. }