From 9b9490e9cbdee120cfe5b4ed3bf6a290ce627e25 Mon Sep 17 00:00:00 2001 From: pauldendulk Date: Fri, 5 Jul 2024 19:08:26 +0200 Subject: [PATCH] Add ILocalTileSource --- BruTile.MbTiles/MbTilesTileSource.cs | 2 +- BruTile/FileSystem/FileTileSource.cs | 23 +++++++------------ BruTile/ILocalTileSource.cs | 10 ++++++++ BruTile/ITileProvider.cs | 12 ++++++++++ .../FileSystem/FileTileSourceTests.cs | 5 ++-- 5 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 BruTile/ILocalTileSource.cs create mode 100644 BruTile/ITileProvider.cs diff --git a/BruTile.MbTiles/MbTilesTileSource.cs b/BruTile.MbTiles/MbTilesTileSource.cs index 62b1a34b..82d4b9ba 100644 --- a/BruTile.MbTiles/MbTilesTileSource.cs +++ b/BruTile.MbTiles/MbTilesTileSource.cs @@ -18,7 +18,7 @@ namespace BruTile.MbTiles; /// An implementation for MapBox Tiles files /// /// -public class MbTilesTileSource : ITileSource +public class MbTilesTileSource : ILocalTileSource { public MbTilesType Type { get; } public string Version { get; } diff --git a/BruTile/FileSystem/FileTileSource.cs b/BruTile/FileSystem/FileTileSource.cs index d6965e4f..16a441e9 100644 --- a/BruTile/FileSystem/FileTileSource.cs +++ b/BruTile/FileSystem/FileTileSource.cs @@ -1,30 +1,23 @@ // Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information. using System; -using System.IO; using System.Threading.Tasks; using BruTile.Cache; namespace BruTile.FileSystem; -public class FileTileSource +public class FileTileSource(ITileSchema tileSchema, string directory, string format, TimeSpan? cacheExpireTime = null, + string? name = null, Attribution? attribution = null) : ILocalTileSource { - private readonly FileCache _fileCache; + private readonly FileCache _fileCache = new(directory, format, cacheExpireTime ?? TimeSpan.Zero); - public FileTileSource(string directory, string format, TimeSpan cacheExpireTime) - { - _fileCache = new FileCache(directory, format, cacheExpireTime); - } - - public FileTileSource(FileCache fileCache) - { - _fileCache = fileCache; - } + public ITileSchema Schema { get; } = tileSchema; + public string Name { get; } = name ?? directory; + public Attribution Attribution { get; } = attribution ?? new Attribution(); - public Task GetTileAsync(TileInfo tileInfo) + public Task GetTileAsync(TileInfo tileInfo) { - var bytes = _fileCache.Find(tileInfo.Index) - ?? throw new FileNotFoundException("The tile was not found at it's expected location"); + var bytes = _fileCache.Find(tileInfo.Index); return Task.FromResult(bytes); } } diff --git a/BruTile/ILocalTileSource.cs b/BruTile/ILocalTileSource.cs new file mode 100644 index 00000000..8fd9806d --- /dev/null +++ b/BruTile/ILocalTileSource.cs @@ -0,0 +1,10 @@ +// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information. + +using System.Threading.Tasks; + +namespace BruTile; + +public interface ILocalTileSource : ITileSource +{ + Task GetTileAsync(TileInfo tileInfo); +} diff --git a/BruTile/ITileProvider.cs b/BruTile/ITileProvider.cs new file mode 100644 index 00000000..1834675d --- /dev/null +++ b/BruTile/ITileProvider.cs @@ -0,0 +1,12 @@ +// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information. + +using System.Threading.Tasks; +using System; + +namespace BruTile; + +[Obsolete("Use ILocalTileSource or IHttpTileSource instead", true)] +public interface ITileProvider +{ + Task GetTileAsync(TileInfo tileInfo); +} \ No newline at end of file diff --git a/Tests/BruTile.Tests/FileSystem/FileTileSourceTests.cs b/Tests/BruTile.Tests/FileSystem/FileTileSourceTests.cs index f9dfc1ce..f7975a1c 100644 --- a/Tests/BruTile.Tests/FileSystem/FileTileSourceTests.cs +++ b/Tests/BruTile.Tests/FileSystem/FileTileSourceTests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using BruTile.Cache; using BruTile.FileSystem; +using BruTile.Predefined; using NUnit.Framework; namespace BruTile.Tests.FileSystem; @@ -17,13 +18,13 @@ public async Task GetTileWhenTilePresentShouldReturnTile() // Arrange var tileCache = new FileCache(".\\FileCacheTest", "png", new TimeSpan(long.MaxValue)); tileCache.Add(new TileIndex(4, 5, 8), new byte[243]); - var fileTileSource = new FileTileSource(".\\FileCacheTest", "png", new TimeSpan(long.MaxValue)); + var fileTileSource = new FileTileSource(new GlobalSphericalMercator(), ".\\FileCacheTest", "png", new TimeSpan(long.MaxValue)); // Act var tile = await fileTileSource.GetTileAsync(new TileInfo { Index = new TileIndex(4, 5, 8) }) .ConfigureAwait(false); // Assert - Assert.AreEqual(tile.Length, 243); + Assert.AreEqual(tile?.Length, 243); } }