2018-08-15 23:29:03 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2018-08-15 23:29:03 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-11-10 22:31:06 +01:00
|
|
|
|
using System.Diagnostics;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows.Media;
|
2021-11-17 23:46:48 +01:00
|
|
|
|
#elif UWP
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2024-05-27 17:30:58 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using ImageSource = Avalonia.Media.IImage;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
namespace MapControl.MBTiles
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-11-17 00:10:08 +01:00
|
|
|
|
public class MBTileSource : TileSource, IDisposable
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
public MBTileData TileData { get; }
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2019-11-14 23:21:08 +01:00
|
|
|
|
public MBTileSource(MBTileData tiledata)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2021-11-10 22:31:06 +01:00
|
|
|
|
var format = tiledata.Metadata["format"];
|
|
|
|
|
|
|
|
|
|
|
|
if (format == "png" || format == "jpg")
|
|
|
|
|
|
{
|
|
|
|
|
|
TileData = tiledata;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine($"MBTileSource: unsupported format '{format}'");
|
|
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 23:29:03 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2019-11-17 00:10:08 +01:00
|
|
|
|
Dispose(true);
|
2021-11-10 22:31:06 +01:00
|
|
|
|
GC.SuppressFinalize(this);
|
2019-11-17 00:10:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
|
{
|
2021-11-10 22:31:06 +01:00
|
|
|
|
if (disposing && TileData != null)
|
2019-11-17 00:10:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
TileData.Dispose();
|
|
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
2021-11-10 22:31:06 +01:00
|
|
|
|
ImageSource image = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (TileData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var buffer = await TileData.ReadImageBufferAsync(x, y, zoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer != null)
|
|
|
|
|
|
{
|
2021-11-14 18:37:07 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
image = await ImageLoader.LoadImageAsync(buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine($"MBTileSource : {ex.Message}");
|
|
|
|
|
|
}
|
2021-11-10 22:31:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2021-11-10 22:31:06 +01:00
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|