XAML-Map-Control/MBTiles/Shared/MBTileLayer.cs

88 lines
2.7 KiB
C#
Raw Normal View History

2021-01-13 21:19:27 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Threading.Tasks;
2021-10-17 12:46:05 +02:00
#if WINUI
using Microsoft.UI.Xaml;
2021-11-17 23:46:48 +01:00
#elif UWP
2017-10-07 17:43:28 +02:00
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace MapControl.MBTiles
2017-10-07 17:43:28 +02:00
{
/// <summary>
/// MapTileLayer that uses an MBTiles SQLite Database. See https://wiki.openstreetmap.org/wiki/MBTiles.
/// </summary>
2017-10-07 17:43:28 +02:00
public class MBTileLayer : MapTileLayer
{
public static readonly DependencyProperty FileProperty = DependencyProperty.Register(
nameof(File), typeof(string), typeof(MBTileLayer),
new PropertyMetadata(null, async (o, e) => await ((MBTileLayer)o).FilePropertyChanged((string)e.NewValue)));
2017-10-07 17:43:28 +02:00
public MBTileLayer()
: this(new TileImageLoader())
{
}
public MBTileLayer(ITileImageLoader tileImageLoader)
: base(tileImageLoader)
{
}
public string File
{
2022-08-06 11:04:49 +02:00
get => (string)GetValue(FileProperty);
set => SetValue(FileProperty, value);
2017-10-07 17:43:28 +02:00
}
2019-11-17 00:10:08 +01:00
/// <summary>
2021-11-10 22:31:06 +01:00
/// May be overridden to create a derived MBTileSource that handles other tile formats than png and jpg.
2019-11-17 00:10:08 +01:00
/// </summary>
protected virtual MBTileSource CreateTileSource(MBTileData tileData)
{
return new MBTileSource(tileData);
}
private async Task FilePropertyChanged(string file)
2017-10-07 17:43:28 +02:00
{
2019-11-14 23:21:08 +01:00
(TileSource as MBTileSource)?.Dispose();
2017-10-07 17:43:28 +02:00
2019-11-14 23:21:08 +01:00
ClearValue(TileSourceProperty);
ClearValue(SourceNameProperty);
ClearValue(DescriptionProperty);
ClearValue(MinZoomLevelProperty);
ClearValue(MaxZoomLevelProperty);
if (file != null)
2017-10-07 17:43:28 +02:00
{
2019-11-17 00:10:08 +01:00
var tileData = await MBTileData.CreateAsync(file);
2020-04-16 23:15:03 +02:00
if (tileData.Metadata.TryGetValue("name", out string sourceName))
2017-10-07 17:43:28 +02:00
{
2020-04-16 23:15:03 +02:00
SourceName = sourceName;
2017-10-07 17:43:28 +02:00
}
2020-04-16 23:15:03 +02:00
if (tileData.Metadata.TryGetValue("description", out string description))
2017-10-07 17:43:28 +02:00
{
2020-04-16 23:15:03 +02:00
Description = description;
2017-10-07 17:43:28 +02:00
}
2020-04-16 23:15:03 +02:00
if (tileData.Metadata.TryGetValue("minzoom", out sourceName) && int.TryParse(sourceName, out int minZoom))
2017-10-07 17:43:28 +02:00
{
2019-11-14 23:21:08 +01:00
MinZoomLevel = minZoom;
2017-10-07 17:43:28 +02:00
}
2020-04-16 23:15:03 +02:00
if (tileData.Metadata.TryGetValue("maxzoom", out sourceName) && int.TryParse(sourceName, out int maxZoom))
2017-10-07 17:43:28 +02:00
{
2019-11-14 23:21:08 +01:00
MaxZoomLevel = maxZoom;
2017-10-07 17:43:28 +02:00
}
2019-11-17 00:10:08 +01:00
TileSource = CreateTileSource(tileData);
2017-10-07 17:43:28 +02:00
}
}
}
}