Skip to content

Commit

Permalink
Inject Custom Map tiles and reload map
Browse files Browse the repository at this point in the history
  • Loading branch information
Godeffroy committed Jun 10, 2024
1 parent 33aa9ec commit 6ab96d4
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 4 deletions.
20 changes: 20 additions & 0 deletions GCSViews/FlightPlanner.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions GCSViews/FlightPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<int, int>();
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();
}
}
}
59 changes: 55 additions & 4 deletions GCSViews/FlightPlanner.resx
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
<value>2</value>
</data>
<data name="panel5.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 212</value>
<value>3, 278</value>
</data>
<data name="panel5.Size" type="System.Drawing.Size, System.Drawing">
<value>123, 87</value>
Expand Down Expand Up @@ -598,7 +598,7 @@
<value>6</value>
</data>
<data name="panel1.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 305</value>
<value>3, 371</value>
</data>
<data name="panel1.Size" type="System.Drawing.Size, System.Drawing">
<value>123, 89</value>
Expand Down Expand Up @@ -1316,6 +1316,57 @@
<data name="&gt;&gt;panel4.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="progressBarInjectCustomMap.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 97</value>
</data>
<data name="progressBarInjectCustomMap.Size" type="System.Drawing.Size, System.Drawing">
<value>115, 23</value>
</data>
<data name="progressBarInjectCustomMap.TabIndex" type="System.Int32, mscorlib">
<value>48</value>
</data>
<data name="progressBarInjectCustomMap.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="&gt;&gt;progressBarInjectCustomMap.Name" xml:space="preserve">
<value>progressBarInjectCustomMap</value>
</data>
<data name="&gt;&gt;progressBarInjectCustomMap.Type" xml:space="preserve">
<value>System.Windows.Forms.ProgressBar, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;progressBarInjectCustomMap.Parent" xml:space="preserve">
<value>panel3</value>
</data>
<data name="&gt;&gt;progressBarInjectCustomMap.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="BUT_InjectCustomMap.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="BUT_InjectCustomMap.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 68</value>
</data>
<data name="BUT_InjectCustomMap.Size" type="System.Drawing.Size, System.Drawing">
<value>115, 23</value>
</data>
<data name="BUT_InjectCustomMap.TabIndex" type="System.Int32, mscorlib">
<value>46</value>
</data>
<data name="BUT_InjectCustomMap.Text" xml:space="preserve">
<value>Inject Custom Map</value>
</data>
<data name="&gt;&gt;BUT_InjectCustomMap.Name" xml:space="preserve">
<value>BUT_InjectCustomMap</value>
</data>
<data name="&gt;&gt;BUT_InjectCustomMap.Type" xml:space="preserve">
<value>MissionPlanner.Controls.MyButton, MissionPlanner.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;BUT_InjectCustomMap.Parent" xml:space="preserve">
<value>panel3</value>
</data>
<data name="&gt;&gt;BUT_InjectCustomMap.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="chk_grid.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
Expand Down Expand Up @@ -1404,7 +1455,7 @@
<value>3, 64</value>
</data>
<data name="panel3.Size" type="System.Drawing.Size, System.Drawing">
<value>123, 61</value>
<value>123, 127</value>
</data>
<data name="panel3.TabIndex" type="System.Int32, mscorlib">
<value>49</value>
Expand Down Expand Up @@ -1506,7 +1557,7 @@
<value>2</value>
</data>
<data name="panel2.Location" type="System.Drawing.Point, System.Drawing">
<value>3, 131</value>
<value>3, 197</value>
</data>
<data name="panel2.Size" type="System.Drawing.Size, System.Drawing">
<value>123, 75</value>
Expand Down

0 comments on commit 6ab96d4

Please sign in to comment.