2018-08-15 23:29:03 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2018-08-15 23:29:03 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-07-18 21:09:54 +02:00
|
|
|
|
using System.Collections.Generic;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
namespace MapControl.MBTiles
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-06-19 11:19:20 +02:00
|
|
|
|
public sealed class MBTileSource : TileSource, IDisposable
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
public MBTileData TileData { get; }
|
2019-07-18 21:09:54 +02:00
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
public string Description { get; }
|
2019-11-11 20:11:35 +01:00
|
|
|
|
public string Format { get; }
|
2019-07-18 21:09:54 +02:00
|
|
|
|
public int? MinZoom { get; }
|
|
|
|
|
|
public int? MaxZoom { get; }
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
private MBTileSource(MBTileData tiledata, IDictionary<string, string> metadata)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
TileData = tiledata;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2019-07-17 17:56:46 +02:00
|
|
|
|
string s;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
int minZoom;
|
|
|
|
|
|
int maxZoom;
|
|
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
if (metadata.TryGetValue("name", out s))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
Name = s;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
if (metadata.TryGetValue("description", out s))
|
2019-07-17 17:56:46 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
Description = s;
|
2019-07-17 17:56:46 +02:00
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
if (metadata.TryGetValue("format", out s))
|
|
|
|
|
|
{
|
|
|
|
|
|
Format = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (metadata.TryGetValue("minzoom", out s) && int.TryParse(s, out minZoom))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
MinZoom = minZoom;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
if (metadata.TryGetValue("maxzoom", out s) && int.TryParse(s, out maxZoom))
|
2019-07-17 17:56:46 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
MaxZoom = maxZoom;
|
2019-07-17 17:56:46 +02:00
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-18 21:09:54 +02:00
|
|
|
|
public static async Task<MBTileSource> CreateAsync(string file)
|
|
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
var tiledata = await MBTileData.CreateAsync(file);
|
|
|
|
|
|
var metadata = await tiledata.ReadMetadataAsync();
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2019-11-11 20:11:35 +01:00
|
|
|
|
return new MBTileSource(tiledata, metadata);
|
2019-07-18 21:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 23:29:03 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
TileData.Dispose();
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
2019-11-11 20:11:35 +01:00
|
|
|
|
var buffer = await TileData.ReadImageBufferAsync(x, y, zoomLevel);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2018-08-16 19:45:50 +02:00
|
|
|
|
return buffer != null ? await ImageLoader.LoadImageAsync(buffer) : null;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|