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

93 lines
2.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 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;
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
2017-10-08 17:35:07 +02:00
using Windows.UI.Xaml.Media;
#else
using System.Windows.Media;
#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>
2021-11-17 23:17:11 +01:00
#if !UWP
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
{
2017-10-09 19:17:04 +02:00
private string uriFormat;
2017-08-04 21:38:58 +02:00
/// <summary>
2020-10-23 23:35:48 +02:00
/// Gets or sets the format string to produce tile request Uris.
2017-08-04 21:38:58 +02:00
/// </summary>
public string UriFormat
{
2022-08-06 10:40:59 +02:00
get => uriFormat;
set
{
2022-08-19 08:38:48 +02:00
uriFormat = value;
2020-10-24 10:23:00 +02:00
if (Subdomains == null && uriFormat != null && uriFormat.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>
public virtual Uri GetUri(int x, int y, int zoomLevel)
{
2020-10-23 23:35:48 +02:00
Uri uri = null;
2022-08-19 08:38:48 +02:00
if (UriFormat != null && x >= 0 && y >= 0 && zoomLevel >= 0)
{
2020-10-23 23:35:48 +02:00
var uriString = UriFormat
.Replace("{x}", x.ToString())
.Replace("{y}", y.ToString())
.Replace("{z}", zoomLevel.ToString());
2020-10-23 23:35:48 +02:00
if (Subdomains != null && Subdomains.Length > 0)
{
uriString = uriString.Replace("{s}", Subdomains[(x + y) % Subdomains.Length]);
}
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>
/// Loads a tile ImageSource asynchronously from GetUri(x, y, zoomLevel).
/// </summary>
public virtual Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
{
var uri = GetUri(x, y, zoomLevel);
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
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 x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
2020-10-23 23:35:48 +02:00
return base.GetUri(x, (1 << zoomLevel) - 1 - y, zoomLevel);
}
2020-10-23 23:35:48 +02:00
}
2012-04-25 22:02:53 +02:00
}