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.EnumerateRange().ToList(); _logger?.LogInformation($"Generating {range.Count} tiles with {provider.Name} generator..."); Parallel.ForEach(range, parallelOptions, tileInfo => { var contentbytes = generator.GenerateTile(tileInfo.X, tileInfo.Y, tileInfo.Zoom); tiles.Add(new MapTile(contentbytes, provider.TileSize, null, tileInfo)); } ); } } 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.EnumerateRange().ToList(); _logger?.LogInformation($"Downloading {range.Count} tiles..."); Parallel.ForEach(range, parallelOptions, tileInfo => { Uri tileUri = BuildUri(provider, tileInfo.X, tileInfo.Y, tileInfo.Zoom); var contentBytes = cache.GetOrCreate(tileUri, entry => { entry.SetSlidingExpiration(TimeSpan.FromMinutes(options.ImageryCacheExpirationMinutes)); return(_httpClient.GetByteArrayAsync(tileUri).Result); }); //var contentBytes = _httpClient.GetByteArrayAsync(tileUri).Result; tiles.Add(new MapTile(contentBytes, provider.TileSize, tileUri, tileInfo)); } ); swDownload.Stop(); _logger?.LogInformation($"DownloadImages done in : {swDownload.Elapsed:g}"); } return(tiles); }
public TileRange DownloadTiles(TileRange tiles, ImageryProvider provider) { // downdload tiles Stopwatch swDownload = Stopwatch.StartNew(); _logger?.LogTrace("Starting images download"); // 2 max download threads var options = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.EnumerateRange().ToList(); _logger?.LogInformation($"Downloading {range.Count} tiles..."); Parallel.ForEach(range, options, tileInfo => { Uri tileUri = BuildUri(provider, tileInfo.X, tileInfo.Y, tileInfo.Zoom); var contentbytes = _httpClient.GetByteArrayAsync(tileUri).Result; tiles.Add(new MapTile(contentbytes, provider.TileSize, tileUri, tileInfo)); //Interlocked.Increment(ref i); //_logger?.LogInformation($"Downloading {tileUri}"); } ); swDownload.Stop(); _logger?.LogInformation($"DownloadImages done in : {swDownload.Elapsed:g}"); return(tiles); }
public TileRange DownloadTiles(BoundingBox bbox, ImageryProvider provider, int minTilesPerImage = 4) { TileRange tiles = new TileRange(provider); BoundingBox mapBbox = null; PointInt topLeft = new PointInt(); PointInt bottomRight = new PointInt(); // optimal zoom calculation (maybe there's a direct way) // calculate the size of the full bbox at increasing zoom levels // until the full image would be greater than a tile int zoom = 0; do { zoom++; // coords are pixels in global map image (see TileUtils.MapSize(zoom)) topLeft = TileUtils.LatLongToPixelXY(bbox.yMax, bbox.xMin, zoom); bottomRight = TileUtils.LatLongToPixelXY(bbox.yMin, bbox.xMax, zoom); mapBbox = new BoundingBox(topLeft.X, bottomRight.X, topLeft.Y, bottomRight.Y); }while (zoom < provider.MaxZoom && (mapBbox.Width < provider.TileSize * minTilesPerImage && mapBbox.Height < provider.TileSize * minTilesPerImage)); // now we have the minimum zoom without image // we can know which tiles are needed tiles.Start = new MapTileInfo(TileUtils.PixelXYToTileXY(topLeft.X, topLeft.Y), zoom); tiles.End = new MapTileInfo(TileUtils.PixelXYToTileXY(bottomRight.X, bottomRight.Y), zoom); tiles.AreaOfInterest = mapBbox; // downdload tiles Stopwatch swDownload = Stopwatch.StartNew(); _logger?.LogTrace("Starting images download"); // 2 max download threads var options = new ParallelOptions() { MaxDegreeOfParallelism = provider.MaxDegreeOfParallelism }; var range = tiles.EnumerateRange().ToList(); _logger.LogInformation($"Downloading {range.Count} tiles..."); Parallel.ForEach(range, options, tileInfo => { Uri tileUri = BuildUri(provider, tileInfo.X, tileInfo.Y, tileInfo.Zoom); _logger?.LogInformation($"Downloading {tileUri}"); var contentbytes = _httpClient.GetByteArrayAsync(tileUri).Result; tiles.Add(new MapTile(contentbytes, provider.TileSize, tileUri, tileInfo)); } ); swDownload.Stop(); _logger?.LogInformation($"DownloadImages done in : {swDownload.Elapsed:g}"); return(tiles); }