download 1 tile, first integration test

This commit is contained in:
Anton Martynenko
2025-10-28 12:04:09 +01:00
parent f676e510cd
commit d361fe70ab
12 changed files with 507 additions and 7 deletions
@@ -11,17 +11,17 @@ using SixLabors.ImageSharp;
namespace SatelliteProvider.Services;
public record DownloadedTileInfo(int X, int Y, int ZoomLevel, double Latitude, double Longitude, string FilePath, double TileSizeMeters);
public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions<MapConfig> mapConfig, IHttpClientFactory httpClientFactory)
public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions<MapConfig> mapConfig, IOptions<StorageConfig> storageConfig, IHttpClientFactory httpClientFactory)
: ISatelliteDownloader
{
private const string TILE_URL_TEMPLATE = "https://mt{0}.google.com/vt/lyrs=s&x={1}&y={2}&z={3}&token={4}";
private const string USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36";
private const int NUM_SERVERS = 4;
private const int TILE_SIZE_PIXELS = 256;
private readonly string _apiKey = mapConfig.Value.ApiKey;
private readonly string _satDirectory = Path.Combine(Directory.GetCurrentDirectory(), "maps");
private readonly string _tilesDirectory = storageConfig.Value.TilesDirectory;
private record SessionResponse(string Session);
@@ -45,6 +45,11 @@ public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions
}
public async Task GetTiles(GeoPoint centerGeoPoint, double radiusM, int zoomLevel, CancellationToken token = default)
{
await GetTilesWithMetadataAsync(centerGeoPoint, radiusM, zoomLevel, token);
}
public async Task<List<DownloadedTileInfo>> GetTilesWithMetadataAsync(GeoPoint centerGeoPoint, double radiusM, int zoomLevel, CancellationToken token = default)
{
var (latMin, latMax, lonMin, lonMax) = GeoUtils.GetBoundingBox(centerGeoPoint, radiusM);
@@ -52,6 +57,7 @@ public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions
var (xMax, yMax) = GeoUtils.WorldToTilePos(new GeoPoint(latMin, lonMax), zoomLevel);
var tilesToDownload = new ConcurrentQueue<SatTile>();
var downloadedTiles = new ConcurrentBag<DownloadedTileInfo>();
var server = 0;
var sessionToken = await GetSessionToken();
@@ -69,13 +75,14 @@ public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions
for (int i = 0; i < NUM_SERVERS; i++)
{
downloadTasks.Add(Task.Run(() => DownloadTilesWorker(tilesToDownload, token), token));
downloadTasks.Add(Task.Run(() => DownloadTilesWorker(tilesToDownload, downloadedTiles, zoomLevel, token), token));
}
await Task.WhenAll(downloadTasks);
return downloadedTiles.ToList();
}
private async Task DownloadTilesWorker(ConcurrentQueue<SatTile> tilesToDownload, CancellationToken token)
private async Task DownloadTilesWorker(ConcurrentQueue<SatTile> tilesToDownload, ConcurrentBag<DownloadedTileInfo> downloadedTiles, int zoomLevel, CancellationToken token)
{
using var httpClient = httpClientFactory.CreateClient();
@@ -84,12 +91,33 @@ public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions
if (token.IsCancellationRequested) break;
try
{
Directory.CreateDirectory(_tilesDirectory);
var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
var fileName = $"tile_{tileInfo.Zoom}_{tileInfo.X}_{tileInfo.Y}_{timestamp}.jpg";
var filePath = Path.Combine(_tilesDirectory, fileName);
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(USER_AGENT);
var response = await httpClient.GetAsync(tileInfo.Url, token);
response.EnsureSuccessStatusCode();
var tileData = await response.Content.ReadAsByteArrayAsync(token);
using var tileImage = Image.Load(tileData);
await tileImage.SaveAsync(Path.Combine(_satDirectory, tileInfo.FileName), token);
await tileImage.SaveAsync(filePath, token);
var tileCenter = GeoUtils.TileToWorldPos(tileInfo.X, tileInfo.Y, tileInfo.Zoom);
var tileSizeMeters = CalculateTileSizeInMeters(tileInfo.Zoom, tileCenter.Lat);
var downloadedTile = new DownloadedTileInfo(
tileInfo.X,
tileInfo.Y,
tileInfo.Zoom,
tileCenter.Lat,
tileCenter.Lon,
filePath,
tileSizeMeters
);
downloadedTiles.Add(downloadedTile);
}
catch (HttpRequestException requestException)
{
@@ -101,4 +129,12 @@ public class GoogleMapsDownloader(ILogger<GoogleMapsDownloader> logger, IOptions
}
}
}
private static double CalculateTileSizeInMeters(int zoomLevel, double latitude)
{
const double EARTH_CIRCUMFERENCE_METERS = 40075016.686;
var latRad = latitude * Math.PI / 180.0;
var metersPerPixel = (EARTH_CIRCUMFERENCE_METERS * Math.Cos(latRad)) / (Math.Pow(2, zoomLevel) * TILE_SIZE_PIXELS);
return metersPerPixel * TILE_SIZE_PIXELS;
}
}