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

243 lines
8.3 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 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.Diagnostics;
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;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Windows.Media;
using System.Windows.Media.Imaging;
#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>
public partial class TileSource
2012-04-25 22:02:53 +02:00
{
private Func<int, int, int, Uri> getUri;
private string uriFormat = string.Empty;
public TileSource()
{
}
protected TileSource(string uriFormat)
{
this.uriFormat = uriFormat;
}
2017-08-04 21:38:58 +02:00
/// <summary>
/// Gets or sets the format string to produce tile Uris.
/// </summary>
public string UriFormat
{
get { return uriFormat; }
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException("The value of the UriFormat property must not be null or empty.");
}
uriFormat = value;
2013-01-29 17:55:53 +01:00
if (uriFormat.Contains("{x}") && uriFormat.Contains("{y}") && uriFormat.Contains("{z}"))
{
2013-01-29 17:55:53 +01:00
if (uriFormat.Contains("{c}"))
{
getUri = GetOpenStreetMapUri;
}
else if (uriFormat.Contains("{i}"))
{
getUri = GetGoogleMapsUri;
}
else if (uriFormat.Contains("{n}"))
{
getUri = GetMapQuestUri;
}
else
{
getUri = GetBasicUri;
}
}
else if (uriFormat.Contains("{x}") && uriFormat.Contains("{v}") && uriFormat.Contains("{z}"))
{
getUri = GetTmsUri;
}
2013-01-29 17:55:53 +01:00
else if (uriFormat.Contains("{q}")) // {i} is optional
{
2013-01-29 17:55:53 +01:00
getUri = GetQuadKeyUri;
}
else if (uriFormat.Contains("{W}") && uriFormat.Contains("{S}") && uriFormat.Contains("{E}") && uriFormat.Contains("{N}"))
{
2013-01-29 17:55:53 +01:00
getUri = GetBoundingBoxUri;
}
else if (uriFormat.Contains("{w}") && uriFormat.Contains("{s}") && uriFormat.Contains("{e}") && uriFormat.Contains("{n}"))
{
getUri = GetLatLonBoundingBoxUri;
}
}
2013-01-29 17:55:53 +01:00
}
2017-08-04 21:38:58 +02:00
/// <summary>
/// Gets the map tile Uri.
/// </summary>
2013-01-29 17:55:53 +01:00
public virtual Uri GetUri(int x, int y, int zoomLevel)
{
return getUri?.Invoke(x, y, zoomLevel);
}
2017-10-08 17:35:07 +02:00
/// <summary>
/// Load a tile ImageSource asynchronously from GetUri(x, y, zoomLevel)
/// </summary>
public virtual async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
{
ImageSource imageSource = null;
var uri = GetUri(x, y, zoomLevel);
if (uri != null)
{
try
{
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
{
imageSource = await LoadLocalImageAsync(uri);
}
else if (uri.Scheme == "http")
{
imageSource = await LoadHttpImageAsync(uri);
}
else
{
imageSource = new BitmapImage(uri);
}
}
catch (Exception ex)
{
Debug.WriteLine("TileSource: {0}: {1}", uri, ex.Message);
}
}
return imageSource;
}
private Uri GetBasicUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
return new Uri(uriFormat
.Replace("{x}", x.ToString())
.Replace("{y}", y.ToString())
.Replace("{z}", zoomLevel.ToString()),
UriKind.RelativeOrAbsolute);
2012-04-25 22:02:53 +02:00
}
private Uri GetOpenStreetMapUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
var hostIndex = (x + y) % 3;
2012-04-25 22:02:53 +02:00
return new Uri(uriFormat
.Replace("{c}", "abc".Substring(hostIndex, 1))
.Replace("{x}", x.ToString())
.Replace("{y}", y.ToString())
.Replace("{z}", zoomLevel.ToString()),
UriKind.RelativeOrAbsolute);
2012-04-25 22:02:53 +02:00
}
private Uri GetGoogleMapsUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
var hostIndex = (x + y) % 4;
2012-04-25 22:02:53 +02:00
return new Uri(uriFormat
.Replace("{i}", hostIndex.ToString())
.Replace("{x}", x.ToString())
.Replace("{y}", y.ToString())
.Replace("{z}", zoomLevel.ToString()),
UriKind.RelativeOrAbsolute);
2012-04-25 22:02:53 +02:00
}
private Uri GetMapQuestUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
var hostIndex = (x + y) % 4 + 1;
2012-04-25 22:02:53 +02:00
return new Uri(uriFormat
.Replace("{n}", hostIndex.ToString())
.Replace("{x}", x.ToString())
.Replace("{y}", y.ToString())
.Replace("{z}", zoomLevel.ToString()),
UriKind.RelativeOrAbsolute);
2012-04-25 22:02:53 +02:00
}
private Uri GetTmsUri(int x, int y, int zoomLevel)
{
y = (1 << zoomLevel) - 1 - y;
return new Uri(uriFormat
.Replace("{x}", x.ToString())
.Replace("{v}", y.ToString())
.Replace("{z}", zoomLevel.ToString()),
UriKind.RelativeOrAbsolute);
}
private Uri GetQuadKeyUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
if (zoomLevel < 1)
{
return null;
}
var quadkey = new char[zoomLevel];
2012-04-25 22:02:53 +02:00
for (var z = zoomLevel - 1; z >= 0; z--, x /= 2, y /= 2)
2012-04-25 22:02:53 +02:00
{
quadkey[z] = (char)('0' + 2 * (y % 2) + (x % 2));
2012-04-25 22:02:53 +02:00
}
return new Uri(uriFormat
.Replace("{i}", new string(quadkey, zoomLevel - 1, 1))
.Replace("{q}", new string(quadkey)),
UriKind.RelativeOrAbsolute);
2012-04-25 22:02:53 +02:00
}
private Uri GetBoundingBoxUri(int x, int y, int zoomLevel)
2012-04-25 22:02:53 +02:00
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
var west = MapProjection.MetersPerDegree * (x * tileSize - 180d);
var east = MapProjection.MetersPerDegree * ((x + 1) * tileSize - 180d);
var south = MapProjection.MetersPerDegree * (180d - (y + 1) * tileSize);
var north = MapProjection.MetersPerDegree * (180d - y * tileSize);
return new Uri(uriFormat
.Replace("{W}", west.ToString(CultureInfo.InvariantCulture))
.Replace("{S}", south.ToString(CultureInfo.InvariantCulture))
.Replace("{E}", east.ToString(CultureInfo.InvariantCulture))
.Replace("{N}", north.ToString(CultureInfo.InvariantCulture))
2017-08-04 21:38:58 +02:00
.Replace("{X}", MapProjection.TileSize.ToString())
.Replace("{Y}", MapProjection.TileSize.ToString()));
}
private Uri GetLatLonBoundingBoxUri(int x, int y, int zoomLevel)
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
var west = x * tileSize - 180d;
var east = (x + 1) * tileSize - 180d;
var south = WebMercatorProjection.YToLatitude(180d - (y + 1) * tileSize);
var north = WebMercatorProjection.YToLatitude(180d - y * tileSize);
return new Uri(uriFormat
.Replace("{w}", west.ToString(CultureInfo.InvariantCulture))
.Replace("{s}", south.ToString(CultureInfo.InvariantCulture))
.Replace("{e}", east.ToString(CultureInfo.InvariantCulture))
.Replace("{n}", north.ToString(CultureInfo.InvariantCulture))
2017-08-04 21:38:58 +02:00
.Replace("{X}", MapProjection.TileSize.ToString())
.Replace("{Y}", MapProjection.TileSize.ToString()));
2012-04-25 22:02:53 +02:00
}
}
}