XAML-Map-Control/MapControl/Shared/TileSource.cs

106 lines
2.9 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2025-10-29 18:59:19 +01:00
using System.Text;
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;
2025-11-13 13:36:28 +01:00
#elif AVALONIA
using ImageSource = Avalonia.Media.IImage;
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))]
#endif
2025-11-13 13:36:28 +01:00
public class TileSource : ITileSource
2012-04-25 22:02:53 +02:00
{
private string uriTemplate;
2017-10-09 19:17:04 +02:00
2017-08-04 21:38:58 +02:00
/// <summary>
/// Gets or sets the template string for tile request Uris.
2017-08-04 21:38:58 +02:00
/// </summary>
public string UriTemplate
{
get => uriTemplate;
set
{
uriTemplate = value;
2024-07-14 09:37:07 +02:00
if (uriTemplate != null && uriTemplate.Contains("{s}") && Subdomains == null)
2017-10-09 19:17:04 +02:00
{
2025-11-13 13:36:28 +01:00
Subdomains = ["a", "b", "c"]; // default OpenStreetMap subdomains
2017-10-09 19:17:04 +02:00
}
}
2013-01-29 17:55:53 +01:00
}
2020-10-23 23:35:48 +02:00
public string[] Subdomains { get; set; }
2025-11-13 13:36:28 +01:00
public bool Cacheable => UriTemplate != null && UriTemplate.StartsWith("http");
public virtual Uri GetUri(int zoomLevel, int column, int row)
{
2020-10-23 23:35:48 +02:00
Uri uri = null;
2025-08-03 17:29:37 +02:00
if (UriTemplate != null)
{
2025-10-29 18:59:19 +01:00
var uriBuilder = new StringBuilder(UriTemplate);
uriBuilder.Replace("{z}", zoomLevel.ToString());
uriBuilder.Replace("{x}", column.ToString());
uriBuilder.Replace("{y}", row.ToString());
2020-10-23 23:35:48 +02:00
if (Subdomains != null && Subdomains.Length > 0)
{
2025-10-29 18:59:19 +01:00
uriBuilder.Replace("{s}", Subdomains[(column + row) % Subdomains.Length]);
2020-10-23 23:35:48 +02:00
}
2012-04-25 22:02:53 +02:00
2025-10-29 18:59:19 +01:00
uri = new Uri(uriBuilder.ToString(), 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
public virtual Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
2020-10-25 17:35:33 +01:00
{
var uri = GetUri(zoomLevel, column, row);
2020-10-25 17:35:33 +01: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-28 17:52:40 +01:00
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer)
{
return ImageLoader.LoadImageAsync(buffer);
}
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
{
public override Uri GetUri(int zoomLevel, int column, int row)
2012-04-25 22:02:53 +02:00
{
return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row);
}
2020-10-23 23:35:48 +02:00
}
2012-04-25 22:02:53 +02:00
}