Skip to content

Commit

Permalink
FlightPlanner :
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)
Use Parallel.For instead of For to get tiles faster !
  • Loading branch information
Godeffroy committed Jun 10, 2024
1 parent 33aa9ec commit 982232d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 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 @@ -11,6 +11,7 @@ namespace GMap.NET
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using System.Drawing;
using System.Threading.Tasks;

/// <summary>
/// form helping to prefetch tiles on local db
Expand Down Expand Up @@ -185,15 +186,21 @@ 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)
{
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
{
if (GMaps.Instance.CheckImageExist(pr, p, zoom, out ex))
return true;
else
img = GMaps.Instance.GetImageFrom(pr, p, zoom, out ex);
}

Expand Down Expand Up @@ -228,6 +235,7 @@ void worker_DoWork(object sender, DoWorkEventArgs e)

int countOk = 0;
int retryCount = 0;
int count = 0;

if(Shuffle)
{
Expand All @@ -239,10 +247,10 @@ void worker_DoWork(object sender, DoWorkEventArgs e)
CachedTiles.Clear();
}

for(int i = 0; i < all; i++)
Parallel.For(0, all, (i, state)=>
{
if(worker.CancellationPending)
break;
state.Break();

GPoint p = list[i];
{
Expand All @@ -264,22 +272,22 @@ void worker_DoWork(object sender, DoWorkEventArgs e)
{
i--;
System.Threading.Thread.Sleep(1111);
continue;
return;
}
else
{
retryCount = 0;
}
}
}

worker.ReportProgress((int)((i + 1) * 100 / all), i + 1);
count++;
worker.ReportProgress((int)((count + 1) * 100 / all), count + 1);

if (sleep > 0)
{
System.Threading.Thread.Sleep(sleep);
}
}
});

e.Result = countOk;

Expand All @@ -295,8 +303,11 @@ 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 Down Expand Up @@ -324,6 +335,10 @@ void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
}
}
}
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 @@ -5016,7 +5016,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 @@ -5031,6 +5052,10 @@ public void prefetchToolStripMenuItem_Click(object sender, EventArgs e)

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

0 comments on commit 982232d

Please sign in to comment.