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

118 lines
3.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2012-04-25 22:02:53 +02:00
using System.Globalization;
2017-10-08 17:35:07 +02:00
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.UI.Xaml.Media;
#else
using System.ComponentModel;
2017-10-08 17:35:07 +02:00
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>
#if !WINDOWS_UWP
[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
{
get { return uriFormat; }
set
{
uriFormat = value;
2020-10-23 23:35:48 +02:00
if (Subdomains == 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; }
2017-10-08 17:35:07 +02:00
/// <summary>
2017-10-09 19:17:04 +02:00
/// Loads a tile ImageSource asynchronously from GetUri(x, y, zoomLevel).
2017-10-08 17:35:07 +02:00
/// </summary>
2020-09-25 15:02:41 +02:00
public virtual Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
2017-10-08 17:35:07 +02:00
{
var uri = GetUri(x, y, zoomLevel);
2020-09-25 15:02:41 +02:00
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
2017-10-08 17:35:07 +02:00
}
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;
2020-10-23 23:35:48 +02:00
if (UriFormat != null)
{
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-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
}
2020-10-23 23:35:48 +02:00
public class BoundingBoxTileSource : TileSource
{
public override Uri GetUri(int x, int y, int zoomLevel)
{
2020-10-23 23:35:48 +02:00
Uri uri = null;
if (UriFormat != null)
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
var west = MapProjection.Wgs84MetersPerDegree * (x * tileSize - 180d);
var east = MapProjection.Wgs84MetersPerDegree * ((x + 1) * tileSize - 180d);
var south = MapProjection.Wgs84MetersPerDegree * (180d - (y + 1) * tileSize);
var north = MapProjection.Wgs84MetersPerDegree * (180d - y * tileSize);
uri = new Uri(UriFormat
.Replace("{west}", west.ToString(CultureInfo.InvariantCulture))
.Replace("{south}", south.ToString(CultureInfo.InvariantCulture))
.Replace("{east}", east.ToString(CultureInfo.InvariantCulture))
.Replace("{north}", north.ToString(CultureInfo.InvariantCulture)));
}
return uri;
2012-04-25 22:02:53 +02:00
}
}
}