Skip to content

Commit

Permalink
FlightPlanner : map prefetch (ArduPilot#3320)
Browse files Browse the repository at this point in the history
When prefetching, do not download tile if file already exist
Add minZoom input in prefetch (like maxZoom)
  • Loading branch information
Godeffroy authored Aug 15, 2024
1 parent 0bb3a2d commit b67ec8d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
25 changes: 25 additions & 0 deletions ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,31 @@ public PureImage GetImageFrom(GMapProvider provider, GPoint pos, int zoom, out E

readonly Exception noDataException = new Exception("No data in local tile cache...");

public bool CheckImageExist(GMapProvider provider, GPoint pos, int zoom, out Exception result)
{
result = null;
try
{
if (Mode != AccessMode.ServerOnly && !provider.BypassCache)
{
if (PrimaryCache != null)
{
return PrimaryCache.CheckImageFromCache(provider.DbId, pos, zoom);
}

if (SecondaryCache != null)
{
return SecondaryCache.CheckImageFromCache(provider.DbId, pos, zoom);
}
}
}
catch (Exception ex)
{
Debug.WriteLine("CheckImageExist: " + ex.ToString());
}
return false;
}

#if !PocketPC
TileHttpHost host;

Expand Down
9 changes: 9 additions & 0 deletions ExtLibs/GMap.NET.Core/GMap.NET/PureImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ public interface PureImageCache
/// <param name="type">provider dbid or null to use all providers</param>
/// <returns>The number of deleted tiles.</returns>
int DeleteOlderThan(DateTime date, int ? type);

/// <summary>
/// check if image exist on db
/// </summary>
/// <param name="type"></param>
/// <param name="pos"></param>
/// <param name="zoom"></param>
/// <returns>True if Image Exist, false otherwise</returns>
bool CheckImageFromCache(int type, GPoint pos, int zoom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,22 @@ bool CacheTiles(int zoom, GPoint p)
foreach(var pr in provider.Overlays)
{
Exception ex;
PureImage img;
PureImage img = null;

// tile number inversion(BottomLeft -> TopLeft)
if(pr.InvertedAxisY)
{
img = GMaps.Instance.GetImageFrom(pr, new GPoint(p.X, maxOfTiles.Height - p.Y), zoom, out ex);
if (GMaps.Instance.CheckImageExist(pr, new GPoint(p.X, maxOfTiles.Height - p.Y), zoom, out ex))
return true;
else
img = GMaps.Instance.GetImageFrom(pr, new GPoint(p.X, maxOfTiles.Height - p.Y), zoom, out ex);
}
else // ok
{
img = GMaps.Instance.GetImageFrom(pr, p, zoom, out ex);
if (GMaps.Instance.CheckImageExist(pr, p, zoom, out ex))
return true;
else
img = GMaps.Instance.GetImageFrom(pr, p, zoom, out ex);
}

if(img != null)
Expand Down Expand Up @@ -295,8 +301,10 @@ void worker_DoWork(object sender, DoWorkEventArgs e)

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
try{
this.label1.Text = "Fetching tile at zoom (" + zoom + "): " + ((int)e.UserState).ToString() + " of " + all + ", complete: " + e.ProgressPercentage.ToString() + "%";
this.progressBarDownload.Value = e.ProgressPercentage;
int percent = Math.Max(0, Math.Min(100, e.ProgressPercentage));
this.progressBarDownload.Value = percent;

if (Overlay != null)
{
Expand All @@ -323,6 +331,10 @@ void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
Overlay.Markers.Add(m);
}
}
}
catch (Exception)
{
}
}

private void Prefetch_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
Expand Down
31 changes: 31 additions & 0 deletions ExtLibs/Maps/MyImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,37 @@ int PureImageCache.DeleteOlderThan(DateTime date, int? type)
return affectedRows;
}

public bool CheckImageFromCache(int type, GPoint pos, int zoom)
{
bool ret = false;
if (Created)
{
try
{
string file = CacheLocation + Path.DirectorySeparatorChar + GMapProviders.TryGetProvider(type).Name +
Path.DirectorySeparatorChar + zoom + Path.DirectorySeparatorChar + pos.Y +
Path.DirectorySeparatorChar + pos.X + ".jpg";
if (File.Exists(file))
{
ret = true;
}
else
{
ret = false;
}
}
catch (Exception ex)
{
#if MONO
Console.WriteLine("CheckImageFromCache: " + ex.ToString());
#endif
Debug.WriteLine("CheckImageFromCache: " + ex.ToString());
ret = false;
}
}
return ret;
}

#endregion
}
}
27 changes: 26 additions & 1 deletion GCSViews/FlightPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5033,7 +5033,28 @@ public void prefetchToolStripMenuItem_Click(object sender, EventArgs e)

maxzoom = Math.Min(maxzoom, MainMap.MaxZoom);

for (int i = 1; i <= maxzoom; i++)
string minzoomstring = "1";
if (InputBox.Show("min zoom", "Enter the min zoom to prefetch to.", ref minzoomstring) !=
DialogResult.OK)
return;

int minzoom = 20;
if (!int.TryParse(minzoomstring, out minzoom))
{
CustomMessageBox.Show(Strings.InvalidNumberEntered, Strings.ERROR);
return;
}
minzoom = Math.Max(minzoom, MainMap.MinZoom);

if (minzoom > maxzoom)
{
CustomMessageBox.Show(Strings.InvalidNumberEntered, Strings.ERROR);
return;
}

for (int i = minzoom; i <= maxzoom; i++)
{
try
{
TilePrefetcher obj = new TilePrefetcher();
ThemeManager.ApplyThemeTo(obj);
Expand All @@ -5048,6 +5069,10 @@ public void prefetchToolStripMenuItem_Click(object sender, EventArgs e)

obj.Dispose();
}
catch
{
}
}
}
else
{
Expand Down

0 comments on commit b67ec8d

Please sign in to comment.