public MBTilesTileSource(TileSetConfiguration configuration) { this.configuration = configuration; this.contentType = Utils.GetContentType(this.configuration.Format); // TODO: from db metadata if (this.configuration.UseCoordinatesCache) { var filePath = GetLocalFilePath(this.configuration.Source); if (File.Exists(filePath)) { // TODO: not the best placement in constructor Task.Run(() => { var connectionString = GetMBTilesConnectionString(this.configuration.Source); var db = new MBTilesRepository(connectionString); db.ReadTileCoordinatesAsync(this.tileKeys).Wait(); // TODO: check presence of rowid this.isTileKeysReady = true; }); } } }
async Task <byte[]> ITileSource.GetTileAsync(int x, int y, int z) { if (!this.configuration.Tms) { y = Utils.FromTmsY(y, z); } var connectionString = GetMBTilesConnectionString(this.configuration.Source); var db = new MBTilesRepository(connectionString); // TODO: if database contents were changed, coordinates cache should be invalidated if (this.configuration.UseCoordinatesCache) { var key = MBTilesRepository.CreateTileCoordinatesKey(z, x, y); if (tileKeys.ContainsKey(key)) { // Get rowid from cache, read table record by rowid (very fast, compared to selecting by three columns) return(await db.ReadTileAsync(tileKeys[key])); } else { if (this.isTileKeysReady) { // Assuming there is no tile in database, if it not exists in cache return(null); } else { // While cache is not ready, allow simple database read return(await db.ReadTileAsync(x, y, z)); } } } else { return(await db.ReadTileAsync(x, y, z)); } }