CtrlPosXd.cs 19 KB

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