2026-02-17 17:45:31 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2026-02-15 00:09:21 +01:00
|
|
|
|
#if WPF
|
2026-02-12 18:57:19 +01:00
|
|
|
|
using System.Windows.Media;
|
2026-02-15 00:09:21 +01:00
|
|
|
|
#elif UWP
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using ImageSource=Avalonia.Media.IImage;
|
|
|
|
|
|
#endif
|
2026-02-12 18:57:19 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl.MapsforgeTiles
|
|
|
|
|
|
{
|
2026-02-17 17:45:31 +01:00
|
|
|
|
public partial class MapsforgeTileSource(string theme, int cacheCapacity = 200, float textScale = 1f) : TileSource
|
2026-02-12 18:57:19 +01:00
|
|
|
|
{
|
2026-02-17 17:45:31 +01:00
|
|
|
|
private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger<MapsforgeTileSource>();
|
|
|
|
|
|
|
|
|
|
|
|
private readonly TileRenderer renderer = new(theme, cacheCapacity, textScale);
|
2026-02-12 18:57:19 +01:00
|
|
|
|
|
|
|
|
|
|
public static void Initialize(string mapFilePath, float dpiScale)
|
|
|
|
|
|
{
|
2026-02-17 17:45:31 +01:00
|
|
|
|
List<string> mapFiles;
|
|
|
|
|
|
|
|
|
|
|
|
if (mapFilePath.EndsWith(".map"))
|
|
|
|
|
|
{
|
|
|
|
|
|
mapFiles = [mapFilePath];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
mapFiles = [.. Directory.EnumerateFiles(mapFilePath, "*.map")];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var mapFile in mapFiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogInformation("Loading {mapFile}", mapFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TileRenderer.Initialize(mapFiles, dpiScale);
|
2026-02-12 18:57:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
|
|
|
|
|
{
|
2026-02-17 17:45:31 +01:00
|
|
|
|
ImageSource image = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var pixels = renderer.RenderTile(zoomLevel, column, row);
|
|
|
|
|
|
|
|
|
|
|
|
if (pixels != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
image = CreateImage(pixels);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "LoadImageAsync");
|
|
|
|
|
|
}
|
2026-02-12 18:57:19 +01:00
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(image);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|