From 6ab96d4e023c136fda340e4727879fe5c4a62ef8 Mon Sep 17 00:00:00 2001 From: Geoffroy Arenou Date: Mon, 10 Jun 2024 16:16:06 +0200 Subject: [PATCH] Inject Custom Map tiles and reload map --- GCSViews/FlightPlanner.Designer.cs | 20 +++++ GCSViews/FlightPlanner.cs | 116 +++++++++++++++++++++++++++++ GCSViews/FlightPlanner.resx | 59 ++++++++++++++- 3 files changed, 191 insertions(+), 4 deletions(-) diff --git a/GCSViews/FlightPlanner.Designer.cs b/GCSViews/FlightPlanner.Designer.cs index 7f7b1a070b..9dcfcd221f 100644 --- a/GCSViews/FlightPlanner.Designer.cs +++ b/GCSViews/FlightPlanner.Designer.cs @@ -112,6 +112,8 @@ private void InitializeComponent() this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.panel4 = new System.Windows.Forms.Panel(); this.panel3 = new System.Windows.Forms.Panel(); + this.progressBarInjectCustomMap = new System.Windows.Forms.ProgressBar(); + this.BUT_InjectCustomMap = new MissionPlanner.Controls.MyButton(); this.chk_grid = new System.Windows.Forms.CheckBox(); this.comboBoxMapType = new System.Windows.Forms.ComboBox(); this.lnk_kml = new System.Windows.Forms.LinkLabel(); @@ -694,6 +696,9 @@ private void InitializeComponent() // // panel3 // + this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panel3.Controls.Add(this.progressBarInjectCustomMap); + this.panel3.Controls.Add(this.BUT_InjectCustomMap); this.panel3.Controls.Add(this.chk_grid); this.panel3.Controls.Add(this.lbl_status); this.panel3.Controls.Add(this.comboBoxMapType); @@ -701,6 +706,19 @@ private void InitializeComponent() resources.ApplyResources(this.panel3, "panel3"); this.panel3.Name = "panel3"; // + // progressBarInjectCustomMap + // + resources.ApplyResources(this.progressBarInjectCustomMap, "progressBarInjectCustomMap"); + this.progressBarInjectCustomMap.Name = "progressBarInjectCustomMap"; + this.progressBarInjectCustomMap.Step = 1; + // + // BUT_InjectCustomMap + // + resources.ApplyResources(this.BUT_InjectCustomMap, "BUT_InjectCustomMap"); + this.BUT_InjectCustomMap.Name = "BUT_InjectCustomMap"; + this.BUT_InjectCustomMap.UseVisualStyleBackColor = true; + this.BUT_InjectCustomMap.Click += new System.EventHandler(this.BUT_InjectCustomMap_Click); + // // chk_grid // resources.ApplyResources(this.chk_grid, "chk_grid"); @@ -1697,5 +1715,7 @@ private void InitializeComponent() private DataGridViewTextBoxColumn AZ; private DataGridViewTextBoxColumn TagData; private ToolStripMenuItem gDALOpacityToolStripMenuItem; + private MyButton BUT_InjectCustomMap; + private ProgressBar progressBarInjectCustomMap; } } \ No newline at end of file diff --git a/GCSViews/FlightPlanner.cs b/GCSViews/FlightPlanner.cs index 60f6c1898e..408bf53859 100644 --- a/GCSViews/FlightPlanner.cs +++ b/GCSViews/FlightPlanner.cs @@ -51,6 +51,7 @@ using Resources = MissionPlanner.Properties.Resources; using Newtonsoft.Json; using MissionPlanner.ArduPilot.Mavlink; +using System.Drawing.Imaging; using SharpKml.Engine; namespace MissionPlanner.GCSViews @@ -3384,6 +3385,7 @@ private void FillCommand(int rowIndex, MAVLink.MAV_CMD cmd, double p1, double p2 public void FlightPlanner_FormClosing(object sender, FormClosingEventArgs e) { timer1.Stop(); + stopInjectCustomMap = true; } public void FlightPlanner_Load(object sender, EventArgs e) @@ -8004,5 +8006,119 @@ private void gDALOpacityToolStripMenuItem_Click(object sender, EventArgs e) if (InputBox.Show("Opacity 0.0-1.0", "Enter opacity (0.0-1.0)", ref ans) == DialogResult.OK) GDAL.GDALProvider.Instance.opacity = double.Parse(InputBox.value); } + + private static bool stopInjectCustomMap = false; + private void BUT_InjectCustomMap_Click(object sender, EventArgs e) + { + var map = new GMapControl(); + var tilesCount = new Dictionary(); + try + { + if (BUT_InjectCustomMap.Text == Strings.Cancel) + { + stopInjectCustomMap = true; + return; + } + stopInjectCustomMap = false; + + map.MapProvider = GoogleSatelliteMapProvider.Instance; + + map.CacheLocation = Settings.GetDataDirectory() + + "gmapcache" + Path.DirectorySeparatorChar; + + var fbd = new FolderBrowserDialog(); + fbd.SelectedPath = @"C:\"; + + if (fbd.ShowDialog() != DialogResult.OK) + { + map.Dispose(); + return; + } + + if (fbd.SelectedPath != "") + { + BUT_InjectCustomMap.Text = Strings.Cancel; + progressBarInjectCustomMap.Value = 0; + progressBarInjectCustomMap.Visible = true; + + var files_jpg = Directory.GetFiles(fbd.SelectedPath, "*.jpg", SearchOption.AllDirectories); + var files_jpeg = Directory.GetFiles(fbd.SelectedPath, "*.jpeg", SearchOption.AllDirectories); + var files_png = Directory.GetFiles(fbd.SelectedPath, "*.png", SearchOption.AllDirectories); + string[] files = new string[files_jpg.Length + files_jpeg.Length + files_png.Length]; + Array.Copy(files_jpg, files, files_jpg.Length); + Array.Copy(files_jpeg, files, files_jpeg.Length); + Array.Copy(files_png, files, files_png.Length); + + progressBarInjectCustomMap.Maximum = files.Length + 1; + + foreach (var file in files) + { + if(stopInjectCustomMap) + { + log.Info("Stop inject Custom Map"); + break; + } + log.Info(DateTime.Now.Millisecond + " Doing " + file); + var reg = new Regex(@"\\Z*([0-9]+)\\([0-9]+)\\([0-9]+)\."); + + var mat = reg.Match(file); + + if (mat.Success == false) + continue; + + var zoom = int.Parse(mat.Groups[1].Value); + var pnt = new GPoint(int.Parse(mat.Groups[3].Value), int.Parse(mat.Groups[2].Value)); + var tile = new MemoryStream(); + var Img = Image.FromFile(file); + Img.Save(tile, ImageFormat.Jpeg); + + tile.Seek(0, SeekOrigin.Begin); + log.Info(pnt.X + " " + pnt.Y); + + Application.DoEvents(); + + GMaps.Instance.PrimaryCache.PutImageToCache(tile.ToArray(), Custom.Instance.DbId, pnt, zoom); + + Application.DoEvents(); + if (progressBarInjectCustomMap.Value < progressBarInjectCustomMap.Maximum) + progressBarInjectCustomMap.Value++; + if (tilesCount.ContainsKey(zoom)) + tilesCount[zoom]++; + else + tilesCount.Add(zoom, 1); + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + BUT_InjectCustomMap.Text = rm.GetString("BUT_InjectCustomMap.Text"); + progressBarInjectCustomMap.Visible = false; + progressBarInjectCustomMap.Value = 0; + int index = comboBoxMapType.FindString("Custom"); + if (index != -1) + { + comboBoxMapType.SelectedIndex = index; + + //Clear memory cache and force map reload + GMaps.Instance.MemoryCache.Clear(); + MainMap.Core.ReloadMap(); + FlightData.mymap.Core.ReloadMap(); + MainMap.Refresh(); + FlightData.mymap.Refresh(); + } + string results = ""; + int count = 0; + var tilesCountOrdered = tilesCount.OrderBy(x => x.Key); + foreach (var item in tilesCountOrdered) + { + results += Environment.NewLine + "Zoom " + item.Key + " : " + item.Value; + count += item.Value; + } + results += Environment.NewLine + Environment.NewLine + count + " tile" + (count > 1 ? "s" : "") + " loaded !"; + CustomMessageBox.Show("Number of tiles loaded per zoom : " + Environment.NewLine + results, "Injecting Custom Map Results"); + map.Dispose(); + } } } \ No newline at end of file diff --git a/GCSViews/FlightPlanner.resx b/GCSViews/FlightPlanner.resx index 4b2dd335ae..2e36709f98 100644 --- a/GCSViews/FlightPlanner.resx +++ b/GCSViews/FlightPlanner.resx @@ -394,7 +394,7 @@ 2 - 3, 212 + 3, 278 123, 87 @@ -598,7 +598,7 @@ 6 - 3, 305 + 3, 371 123, 89 @@ -1316,6 +1316,57 @@ 0 + + 3, 97 + + + 115, 23 + + + 48 + + + False + + + progressBarInjectCustomMap + + + System.Windows.Forms.ProgressBar, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + panel3 + + + 0 + + + NoControl + + + 3, 68 + + + 115, 23 + + + 46 + + + Inject Custom Map + + + BUT_InjectCustomMap + + + MissionPlanner.Controls.MyButton, MissionPlanner.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + + + panel3 + + + 1 + True @@ -1404,7 +1455,7 @@ 3, 64 - 123, 61 + 123, 127 49 @@ -1506,7 +1557,7 @@ 2 - 3, 131 + 3, 197 123, 75