public TileRange DownloadTiles(TileRange tiles, ImageryProvider provider) { if (provider is ITileGenerator generator) { // download tiles using (TimeSpanBlock timer = new TimeSpanBlock("Tile generation", _logger)) { // Max download threads defined in provider var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.TilesInfo.ToList(); _logger?.LogInformation($"Generating {range.Count} tiles with {provider.Name} generator..."); Parallel.ForEach(range, parallelOptions, tile => { var contentbytes = generator.GenerateTile(tile.X, tile.Y, tile.Zoom); tiles.Add(new MapTile(contentbytes, provider.TileSize, null, tile)); } ); } } else { // download tiles Stopwatch swDownload = Stopwatch.StartNew(); _logger?.LogTrace("Starting images download"); // Max download threads defined in provider var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.TilesInfo.ToList(); _logger?.LogInformation($"Downloading {range.Count} tiles..."); Parallel.ForEach(range, parallelOptions, tile => { Uri tileUri = BuildUri(provider, tile.X, tile.Y, tile.Zoom); var contentBytes = cache.GetTile(tileUri, provider, tile); tiles.Add(new MapTile(contentBytes, provider.TileSize, tileUri, tile)); } ); swDownload.Stop(); _logger?.LogInformation($"DownloadImages done in : {swDownload.Elapsed:g}"); } return(tiles); }
public TileRange DownloadTiles(TileRange tiles, ImageryProvider provider) { Stopwatch sw = Stopwatch.StartNew(); int intervalMs = 1000; if (provider is ITileGenerator generator) { // download tiles using (TimeSpanBlock timer = new TimeSpanBlock("Tile generation", _logger)) { // Max download threads defined in provider var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.TilesInfo.ToList(); _logger?.LogInformation($"Generating {range.Count} tiles with {provider.Name} generator..."); Parallel.ForEach(range, parallelOptions, tile => { var contentbytes = generator.GenerateTile(tile.X, tile.Y, tile.Zoom); tiles.Add(new MapTile(contentbytes, provider.TileSize, null, tile)); } ); } } else { // download tiles Stopwatch swDownload = Stopwatch.StartNew(); _logger?.LogTrace("Starting images download"); // Max download threads defined in provider var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.TilesInfo.ToList(); int numTilesDownloaded = 0; _logger?.LogInformation($"Downloading {range.Count} tiles..."); try { Parallel.ForEach(range, parallelOptions, (tile, state) => { Uri tileUri = BuildUri(provider, tile.X, tile.Y, tile.Zoom); var contentBytes = cache.GetTile(tileUri, provider, tile); tiles.Add(new MapTile(contentBytes, provider.TileSize, tileUri, tile)); Interlocked.Increment(ref numTilesDownloaded); if (sw.ElapsedMilliseconds > intervalMs) { _logger.LogInformation($"{numTilesDownloaded:N0}/{range.Count:N0} tiles downloaded..."); sw.Restart(); } } ); } catch (AggregateException ex) { throw ex.GetInnerMostException(); } catch (Exception) { throw; } swDownload.Stop(); _logger?.LogInformation($"DownloadImages done in : {swDownload.Elapsed:g}"); } return(tiles); }