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>
|
2025-11-14 15:02:48 +01:00
|
|
|
|
/// Provides the download Uri or ImageSource of map tiles. Used by TileImageLoader.
|
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
|
2025-11-13 17:06:33 +01:00
|
|
|
|
public class TileSource
|
2025-11-13 15:32:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-11-14 15:02:48 +01:00
|
|
|
|
/// Gets an image request Uri for the specified zoom level and tile indices.
|
|
|
|
|
|
/// May return null when the image shall be loaded by
|
|
|
|
|
|
/// the LoadImageAsync(zoomLevel, column, row) method.
|
2025-11-13 15:32:01 +01:00
|
|
|
|
/// </summary>
|
2025-11-14 15:02:48 +01:00
|
|
|
|
public virtual Uri GetUri(int zoomLevel, int column, int row)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-11-13 15:32:01 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-11-13 17:06:33 +01:00
|
|
|
|
/// Loads a tile image without an Uri.
|
2025-11-13 15:32:01 +01:00
|
|
|
|
/// </summary>
|
2025-11-14 15:02:48 +01:00
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads a tile image from an Uri.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ImageLoader.LoadImageAsync(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads a tile image from an encoded frame buffer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ImageLoader.LoadImageAsync(buffer);
|
|
|
|
|
|
}
|
2025-11-13 15:32:01 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a TileSource instance from an Uri template string.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static TileSource Parse(string uriTemplate)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new UriTileSource { UriTemplate = uriTemplate };
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class UriTileSource : 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
|
|
|
|
{
|
2025-11-13 13:36:28 +01:00
|
|
|
|
Subdomains = ["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
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
public string[] Subdomains { get; set; }
|
2012-07-20 21:57:29 +02:00
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
public override 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
|
|
|
|
{
|
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());
|
2013-11-17 16:52:03 +01:00
|
|
|
|
|
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
|
|
|
|
|
2025-08-14 13:11:25 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return UriTemplate;
|
|
|
|
|
|
}
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
public class TmsTileSource : UriTileSource
|
2020-10-23 23:35:48 +02:00
|
|
|
|
{
|
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
|
|
|
|
}
|