Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlightPlanner : Inject Custom Map tiles and reload map into custom #3322

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

118 changes: 117 additions & 1 deletion GCSViews/FlightPlanner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DotSpatial.Data;
using DotSpatial.Data;
using DotSpatial.Projections;
using GeoUtility.GeoSystem;
using GeoUtility.GeoSystem.Base;
Expand Down 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 @@ -3392,6 +3393,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 @@ -8030,5 +8032,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, 0, files, 0, files_jpg.Length);
Array.Copy(files_jpeg, 0, files, files_jpg.Length, files_jpeg.Length);
Array.Copy(files_png, 0, files, files_jpg.Length + files_jpeg.Length, 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
Loading