Skip to content

Commit

Permalink
FlightPlanner : Inject Custom Map tiles and reload map into custom (#…
Browse files Browse the repository at this point in the history
…3322)

* Inject Custom Map tiles and reload map

* Fix destination index
  • Loading branch information
Godeffroy authored Jul 30, 2024
1 parent eefd216 commit 4b59c8e
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 5 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.

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

0 comments on commit 4b59c8e

Please sign in to comment.