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

104 lines
3.2 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
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))]
#endif
public class TileSource
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;
if (Subdomains == null && uriTemplate != null && uriTemplate.Contains("{s}"))
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
}
}
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; }
2020-10-23 23:35:48 +02:00
/// <summary>
/// Gets the image Uri for the specified tile indices and zoom level.
/// </summary>
2022-11-22 19:15:34 +01:00
public virtual Uri GetUri(int column, int row, int zoomLevel)
{
2020-10-23 23:35:48 +02:00
Uri uri = null;
2022-11-22 19:15:34 +01:00
if (UriTemplate != null && column >= 0 && row >= 0 && zoomLevel >= 0)
{
var uriString = UriTemplate
2022-11-22 19:15:34 +01:00
.Replace("{x}", column.ToString())
.Replace("{y}", row.ToString())
2020-10-23 23:35:48 +02:00
.Replace("{z}", zoomLevel.ToString());
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>
2022-11-22 19:15:34 +01:00
/// Loads a tile ImageSource asynchronously from GetUri(column, row, zoomLevel).
2023-08-12 17:36:37 +02:00
/// This method is called by a TileImageLoader that does not perform caching.
2020-10-25 17:35:33 +01:00
/// </summary>
2022-11-22 19:15:34 +01:00
public virtual Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
2020-10-25 17:35:33 +01:00
{
2022-11-22 19:15:34 +01:00
var uri = GetUri(column, row, zoomLevel);
2020-10-25 17:35:33 +01:00
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
2024-04-11 14:57:54 +02:00
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
{
2022-11-22 19:15:34 +01:00
public override Uri GetUri(int column, int row, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
2022-11-22 19:15:34 +01:00
return base.GetUri(column, (1 << zoomLevel) - 1 - row, zoomLevel);
}
2020-10-23 23:35:48 +02:00
}
2012-04-25 22:02:53 +02:00
}