From b67ec8d23519d7b7fd9195d4c3e9ac08e6ec0b64 Mon Sep 17 00:00:00 2001 From: Geoffroy Arenou Date: Thu, 15 Aug 2024 03:07:40 +0200 Subject: [PATCH] FlightPlanner : map prefetch (#3320) When prefetching, do not download tile if file already exist Add minZoom input in prefetch (like maxZoom) --- ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs | 25 +++++++++++++++ .../GMap.NET.Core/GMap.NET/PureImageCache.cs | 9 ++++++ .../GMap.NET.WindowsForms/TilePrefetcher.cs | 20 +++++++++--- ExtLibs/Maps/MyImageCache.cs | 31 +++++++++++++++++++ GCSViews/FlightPlanner.cs | 27 +++++++++++++++- 5 files changed, 107 insertions(+), 5 deletions(-) diff --git a/ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs b/ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs index 86f179612c..c349e635a2 100644 --- a/ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs +++ b/ExtLibs/GMap.NET.Core/GMap.NET/GMaps.cs @@ -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; diff --git a/ExtLibs/GMap.NET.Core/GMap.NET/PureImageCache.cs b/ExtLibs/GMap.NET.Core/GMap.NET/PureImageCache.cs index 540b5b3fe6..e84d8a7cf9 100644 --- a/ExtLibs/GMap.NET.Core/GMap.NET/PureImageCache.cs +++ b/ExtLibs/GMap.NET.Core/GMap.NET/PureImageCache.cs @@ -35,5 +35,14 @@ public interface PureImageCache /// provider dbid or null to use all providers /// The number of deleted tiles. int DeleteOlderThan(DateTime date, int ? type); + + /// + /// check if image exist on db + /// + /// + /// + /// + /// True if Image Exist, false otherwise + bool CheckImageFromCache(int type, GPoint pos, int zoom); } } diff --git a/ExtLibs/GMap.NET.WindowsForms/GMap.NET.WindowsForms/TilePrefetcher.cs b/ExtLibs/GMap.NET.WindowsForms/GMap.NET.WindowsForms/TilePrefetcher.cs index 9eefa5a169..e301fb551a 100644 --- a/ExtLibs/GMap.NET.WindowsForms/GMap.NET.WindowsForms/TilePrefetcher.cs +++ b/ExtLibs/GMap.NET.WindowsForms/GMap.NET.WindowsForms/TilePrefetcher.cs @@ -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) @@ -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) { @@ -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) diff --git a/ExtLibs/Maps/MyImageCache.cs b/ExtLibs/Maps/MyImageCache.cs index 6049e41d59..5404ddec05 100644 --- a/ExtLibs/Maps/MyImageCache.cs +++ b/ExtLibs/Maps/MyImageCache.cs @@ -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 } } \ No newline at end of file diff --git a/GCSViews/FlightPlanner.cs b/GCSViews/FlightPlanner.cs index 01cbcd1ccb..8fa04b5247 100644 --- a/GCSViews/FlightPlanner.cs +++ b/GCSViews/FlightPlanner.cs @@ -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); @@ -5048,6 +5069,10 @@ public void prefetchToolStripMenuItem_Click(object sender, EventArgs e) obj.Dispose(); } + catch + { + } + } } else {