2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2025-08-19 23:20:11 +02:00
|
|
|
|
using System.Threading;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows.Media;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// Provides the download Uri or ImageSource of map tiles.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if UWP || WINUI
|
2024-04-11 15:41:05 +02:00
|
|
|
|
[Windows.Foundation.Metadata.CreateFromString(MethodName = "Parse")]
|
2024-04-11 14:57:54 +02:00
|
|
|
|
#else
|
2021-06-14 21:41:37 +02:00
|
|
|
|
[System.ComponentModel.TypeConverter(typeof(TileSourceConverter))]
|
2018-08-08 23:31:52 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
public class TileSource
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
private string uriTemplate;
|
2017-10-09 19:17:04 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2022-08-23 17:20:16 +02:00
|
|
|
|
/// Gets or sets the template string for tile request Uris.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2022-08-23 17:20:16 +02:00
|
|
|
|
public string UriTemplate
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
get => uriTemplate;
|
2012-07-20 21:57:29 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
uriTemplate = value;
|
2013-01-28 23:49:22 +01:00
|
|
|
|
|
2024-07-14 09:37:07 +02:00
|
|
|
|
if (uriTemplate != null && uriTemplate.Contains("{s}") && Subdomains == null)
|
2017-10-09 19:17:04 +02:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
Subdomains = new string[] { "a", "b", "c" }; // default OpenStreetMap subdomains
|
2017-10-09 19:17:04 +02:00
|
|
|
|
}
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-01-29 17:55:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2020-10-23 23:35:48 +02:00
|
|
|
|
/// Gets or sets an array of request subdomain names that are replaced for the {s} format specifier.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2020-10-23 23:35:48 +02:00
|
|
|
|
public string[] Subdomains { get; set; }
|
2012-07-20 21:57:29 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the image Uri for the specified tile indices and zoom level.
|
|
|
|
|
|
/// </summary>
|
2025-09-10 22:19:20 +02:00
|
|
|
|
public virtual Uri GetUri(int zoomLevel, int column, int row)
|
2013-04-21 23:56:08 +02:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
Uri uri = null;
|
2013-04-21 23:56:08 +02:00
|
|
|
|
|
2025-08-03 17:29:37 +02:00
|
|
|
|
if (UriTemplate != null)
|
2013-11-17 16:52:03 +01:00
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
var uriString = UriTemplate
|
2025-09-10 22:19:20 +02:00
|
|
|
|
.Replace("{z}", zoomLevel.ToString())
|
2022-11-22 19:15:34 +01:00
|
|
|
|
.Replace("{x}", column.ToString())
|
2025-09-10 22:19:20 +02:00
|
|
|
|
.Replace("{y}", row.ToString());
|
2013-11-17 16:52:03 +01:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
if (Subdomains != null && Subdomains.Length > 0)
|
|
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
uriString = uriString.Replace("{s}", Subdomains[(column + row) % Subdomains.Length]);
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
return uri;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2020-10-25 17:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-10 22:19:20 +02:00
|
|
|
|
/// Loads a tile ImageSource asynchronously from GetUri(zoomLevel, column, row).
|
2025-01-17 07:05:03 +01:00
|
|
|
|
/// This method is called by TileImageLoader when caching is disabled.
|
2020-10-25 17:35:33 +01:00
|
|
|
|
/// </summary>
|
2025-09-10 22:19:20 +02:00
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
2020-10-25 17:35:33 +01:00
|
|
|
|
{
|
2025-09-10 22:19:20 +02:00
|
|
|
|
var uri = GetUri(zoomLevel, column, row);
|
2020-10-25 17:35:33 +01:00
|
|
|
|
|
2025-08-21 14:25:58 +02:00
|
|
|
|
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
|
2020-10-25 17:35:33 +01:00
|
|
|
|
}
|
2024-04-11 14:57:54 +02:00
|
|
|
|
|
2025-01-17 07:05:03 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads a tile ImageSource asynchronously from an encoded frame buffer in a byte array.
|
|
|
|
|
|
/// This method is called by TileImageLoader when caching is enabled.
|
|
|
|
|
|
/// </summary>
|
2025-01-28 17:52:40 +01:00
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ImageLoader.LoadImageAsync(buffer);
|
|
|
|
|
|
}
|
2025-01-17 07:05:03 +01:00
|
|
|
|
|
2025-08-14 13:11:25 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return UriTemplate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-11 15:59:07 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a TileSource instance from an Uri template string.
|
|
|
|
|
|
/// </summary>
|
2024-04-11 14:57:54 +02:00
|
|
|
|
public static TileSource Parse(string uriTemplate)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new TileSource { UriTemplate = uriTemplate };
|
|
|
|
|
|
}
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
public class TmsTileSource : TileSource
|
|
|
|
|
|
{
|
2025-09-10 22:19:20 +02:00
|
|
|
|
public override Uri GetUri(int zoomLevel, int column, int row)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2025-09-10 22:19:20 +02:00
|
|
|
|
return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|