# Module: Services/TileService ## Purpose Orchestrates tile downloading and persistence. Bridges the downloader (Google Maps) with the tile repository (PostgreSQL), handling in-memory caching, entity creation, and metadata mapping. Single ownership point for all tile read/write business logic — both region-batch and single-tile API endpoints route through this service. **csproj**: `SatelliteProvider.Services.TileDownloader/TileService.cs` ## Public Interface ### TileService (implements ITileService) - `DownloadAndStoreTilesAsync(double lat, double lon, double sizeMeters, int zoomLevel, CancellationToken) → Task>`: 1. Queries existing tiles in the region from the repository (filtered to current year's version) 2. Calls `ISatelliteDownloader.GetTilesWithMetadataAsync` with existing tiles to skip 3. Creates `TileEntity` for each newly downloaded tile and inserts via repository (upsert) 4. Returns combined list of existing + new tile metadata - `GetTileAsync(Guid id) → Task`: single tile lookup - `GetTilesByRegionAsync(double lat, double lon, double sizeMeters, int zoomLevel) → Task>`: query tiles in a region - `GetOrDownloadTileAsync(int z, int x, int y, CancellationToken) → Task` (AZ-310): cache → repository → downloader fallback for single Z/X/Y serving - `DownloadAndStoreSingleTileAsync(double latitude, double longitude, int zoomLevel, CancellationToken) → Task` (AZ-311): download one tile by lat/lon, persist, return metadata ## Internal Logic - Version is `DateTime.UtcNow.Year` — tiles are considered fresh for the current calendar year - `MapToMetadata(TileEntity) → TileMetadata`: entity-to-DTO mapping (static helper) - Tile size hardcoded to 256 pixels, image type "jpg" - `MapsVersion` formatted as `"downloaded_{date}"` - `IMemoryCache` keyed by `(z, x, y)` with 1h absolute / 30min sliding expiration; populated on first hit and on downloader fallback ## Dependencies - `ISatelliteDownloader` (resolved via DI; concrete is `GoogleMapsDownloaderV2`) - `ITileRepository` - `IMemoryCache` (registered by `AddTileDownloader()`) - `SatelliteProvider.Common.DTO` — GeoPoint, TileMetadata, TileBytes - `SatelliteProvider.DataAccess.Models` — TileEntity ## Consumers - `RegionService.ProcessRegionAsync` — downloads and retrieves tiles for a region ## Data Models Transforms between `TileEntity` (persistence) and `TileMetadata` (DTO). ## Configuration None directly; relies on `GoogleMapsDownloaderV2`'s configuration. ## External Integrations Indirect: Google Maps (via downloader), PostgreSQL (via repository). ## Security None. ## Tests No dedicated tests.