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

78 lines
2.5 KiB
C#
Raw Normal View History

2021-01-13 21:19:27 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Threading.Tasks;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
2021-11-17 23:46:48 +01:00
#elif UWP
2017-10-07 17:43:28 +02:00
using Windows.UI.Xaml;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
2017-10-07 17:43:28 +02:00
#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
{
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty FileProperty =
2024-05-23 19:21:28 +02:00
DependencyPropertyHelper.Register<MBTileLayer, string>(nameof(File), null,
2024-05-23 18:08:14 +02:00
async (layer, oldValue, newValue) => await layer.FilePropertyChanged(newValue));
2017-10-07 17:43:28 +02:00
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
}
}
}
}