CtrlPosXd.cs 19 KB

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