Skip to content

Commit

Permalink
Add ILocalTileSource
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldendulk committed Jul 5, 2024
1 parent 0a0ab05 commit 9b9490e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion BruTile.MbTiles/MbTilesTileSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace BruTile.MbTiles;
/// An <see cref="ITileSource"/> implementation for MapBox Tiles files
/// </summary>
/// <seealso href="https://www.mapbox.com/developers/mbtiles/"/>
public class MbTilesTileSource : ITileSource
public class MbTilesTileSource : ILocalTileSource
{
public MbTilesType Type { get; }
public string Version { get; }
Expand Down
23 changes: 8 additions & 15 deletions BruTile/FileSystem/FileTileSource.cs
Original file line number Diff line number Diff line change
@@ -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<byte[]> GetTileAsync(TileInfo tileInfo)
public Task<byte[]?> 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);
}
}
10 changes: 10 additions & 0 deletions BruTile/ILocalTileSource.cs
Original file line number Diff line number Diff line change
@@ -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<byte[]?> GetTileAsync(TileInfo tileInfo);
}
12 changes: 12 additions & 0 deletions BruTile/ITileProvider.cs
Original file line number Diff line number Diff line change
@@ -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<byte[]?> GetTileAsync(TileInfo tileInfo);
}
5 changes: 3 additions & 2 deletions Tests/BruTile.Tests/FileSystem/FileTileSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using BruTile.Cache;
using BruTile.FileSystem;
using BruTile.Predefined;
using NUnit.Framework;

namespace BruTile.Tests.FileSystem;
Expand All @@ -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);
}
}

0 comments on commit 9b9490e

Please sign in to comment.