mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 09:26:39 +00:00
tiels are cached and reused properly
This commit is contained in:
@@ -84,40 +84,6 @@ public class GoogleMapsDownloaderV2
|
||||
var response = await httpClient.GetAsync(url, token);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
_logger.LogInformation("=== HTTP Response Headers from Google Maps ===");
|
||||
_logger.LogInformation("Status Code: {StatusCode}", response.StatusCode);
|
||||
|
||||
foreach (var header in response.Headers)
|
||||
{
|
||||
_logger.LogInformation("Header: {Key} = {Value}", header.Key, string.Join(", ", header.Value));
|
||||
}
|
||||
|
||||
foreach (var header in response.Content.Headers)
|
||||
{
|
||||
_logger.LogInformation("Content Header: {Key} = {Value}", header.Key, string.Join(", ", header.Value));
|
||||
}
|
||||
|
||||
if (response.Headers.ETag != null)
|
||||
{
|
||||
_logger.LogInformation("*** ETag Found: {ETag}", response.Headers.ETag.Tag);
|
||||
}
|
||||
|
||||
if (response.Content.Headers.LastModified.HasValue)
|
||||
{
|
||||
_logger.LogInformation("*** Last-Modified Found: {LastModified}", response.Content.Headers.LastModified.Value);
|
||||
}
|
||||
|
||||
if (response.Headers.CacheControl != null)
|
||||
{
|
||||
_logger.LogInformation("*** Cache-Control: MaxAge={MaxAge}, Public={Public}, Private={Private}, MustRevalidate={MustRevalidate}",
|
||||
response.Headers.CacheControl.MaxAge,
|
||||
response.Headers.CacheControl.Public,
|
||||
response.Headers.CacheControl.Private,
|
||||
response.Headers.CacheControl.MustRevalidate);
|
||||
}
|
||||
|
||||
_logger.LogInformation("=== End of Headers ===");
|
||||
|
||||
var imageBytes = await response.Content.ReadAsByteArrayAsync(token);
|
||||
await File.WriteAllBytesAsync(filePath, imageBytes, token);
|
||||
|
||||
|
||||
@@ -29,15 +29,34 @@ public class TileService : ITileService
|
||||
int zoomLevel,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existingTiles = await _tileRepository.GetTilesByRegionAsync(latitude, longitude, sizeMeters, zoomLevel);
|
||||
var existingTilesList = existingTiles.ToList();
|
||||
var currentVersion = DateTime.UtcNow.Year;
|
||||
|
||||
_logger.LogInformation("Found {Count} existing tiles for region", existingTilesList.Count);
|
||||
var existingTiles = await _tileRepository.GetTilesByRegionAsync(latitude, longitude, sizeMeters, zoomLevel);
|
||||
var existingTilesList = existingTiles.Where(t => t.Version == currentVersion).ToList();
|
||||
|
||||
_logger.LogInformation("Found {Count} existing tiles for region (version {Version})", existingTilesList.Count, currentVersion);
|
||||
|
||||
if (existingTilesList.Any())
|
||||
{
|
||||
_logger.LogInformation("Existing tiles in DB:");
|
||||
foreach (var et in existingTilesList)
|
||||
{
|
||||
_logger.LogInformation(" DB Tile: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}", et.Latitude, et.Longitude, et.ZoomLevel);
|
||||
}
|
||||
}
|
||||
|
||||
var centerPoint = new GeoPoint(latitude, longitude);
|
||||
var downloadedTiles = await _downloader.GetTilesWithMetadataAsync(centerPoint, sizeMeters / 2, zoomLevel, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Downloaded {Count} tiles from Google:", downloadedTiles.Count);
|
||||
foreach (var dt in downloadedTiles)
|
||||
{
|
||||
_logger.LogInformation(" Downloaded: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}", dt.CenterLatitude, dt.CenterLongitude, dt.ZoomLevel);
|
||||
}
|
||||
|
||||
var result = new List<TileMetadata>();
|
||||
int reusedCount = 0;
|
||||
int downloadedCount = 0;
|
||||
|
||||
foreach (var downloadedTile in downloadedTiles)
|
||||
{
|
||||
@@ -48,11 +67,30 @@ public class TileService : ITileService
|
||||
|
||||
if (existingTile != null)
|
||||
{
|
||||
_logger.LogDebug("Reusing existing tile at ({Lat}, {Lon})", downloadedTile.CenterLatitude, downloadedTile.CenterLongitude);
|
||||
reusedCount++;
|
||||
_logger.LogInformation("REUSED tile at ({Lat:F12}, {Lon:F12}) - matched DB tile ID {Id}", downloadedTile.CenterLatitude, downloadedTile.CenterLongitude, existingTile.Id);
|
||||
result.Add(MapToMetadata(existingTile));
|
||||
}
|
||||
else
|
||||
{
|
||||
downloadedCount++;
|
||||
_logger.LogInformation("NEW tile needed at ({Lat:F12}, {Lon:F12}) - no match in DB", downloadedTile.CenterLatitude, downloadedTile.CenterLongitude);
|
||||
|
||||
var closestTile = existingTilesList
|
||||
.Select(t => new {
|
||||
Tile = t,
|
||||
LatDiff = Math.Abs(t.Latitude - downloadedTile.CenterLatitude),
|
||||
LonDiff = Math.Abs(t.Longitude - downloadedTile.CenterLongitude)
|
||||
})
|
||||
.OrderBy(x => x.LatDiff + x.LonDiff)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (closestTile != null)
|
||||
{
|
||||
_logger.LogInformation(" Closest DB tile: Lat={Lat:F12} (diff={LatDiff:F12}), Lon={Lon:F12} (diff={LonDiff:F12})",
|
||||
closestTile.Tile.Latitude, closestTile.LatDiff, closestTile.Tile.Longitude, closestTile.LonDiff);
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var tileEntity = new TileEntity
|
||||
{
|
||||
@@ -64,16 +102,19 @@ public class TileService : ITileService
|
||||
TileSizePixels = 256,
|
||||
ImageType = "jpg",
|
||||
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
|
||||
Version = currentVersion,
|
||||
FilePath = downloadedTile.FilePath,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
};
|
||||
|
||||
await _tileRepository.InsertAsync(tileEntity);
|
||||
_logger.LogInformation("Saved new tile {Id} at ({Lat}, {Lon})", tileEntity.Id, tileEntity.Latitude, tileEntity.Longitude);
|
||||
_logger.LogInformation("Saved new tile {Id} at ({Lat:F12}, {Lon:F12}) version {Version}", tileEntity.Id, tileEntity.Latitude, tileEntity.Longitude, currentVersion);
|
||||
result.Add(MapToMetadata(tileEntity));
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Tile processing summary: {Reused} reused, {Downloaded} new", reusedCount, downloadedCount);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -106,6 +147,7 @@ public class TileService : ITileService
|
||||
TileSizePixels = entity.TileSizePixels,
|
||||
ImageType = entity.ImageType,
|
||||
MapsVersion = entity.MapsVersion,
|
||||
Version = entity.Version,
|
||||
FilePath = entity.FilePath,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt
|
||||
|
||||
Reference in New Issue
Block a user