2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2016-02-23 20:07:30 +01:00
|
|
|
|
// © 2016 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;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2012-07-20 21:57:29 +02:00
|
|
|
|
/// Provides the URI of a map tile.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public partial class TileSource
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
public const int TileSize = 256;
|
2014-07-22 20:02:30 +02:00
|
|
|
|
public const double MetersPerDegree = 6378137d * Math.PI / 180d; // WGS 84 semi major axis
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Func<int, int, int, Uri> getUri;
|
|
|
|
|
|
private string uriFormat = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
public TileSource()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-19 21:50:23 +02:00
|
|
|
|
protected TileSource(string uriFormat)
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
this.uriFormat = uriFormat;
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
public string UriFormat
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return uriFormat; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
if (string.IsNullOrEmpty(value))
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
throw new ArgumentException("The value of the UriFormat property must not be null or empty.");
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-28 23:49:22 +01:00
|
|
|
|
uriFormat = value;
|
|
|
|
|
|
|
2013-01-29 17:55:53 +01:00
|
|
|
|
if (uriFormat.Contains("{x}") && uriFormat.Contains("{y}") && uriFormat.Contains("{z}"))
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
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;
|
|
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
getUri = GetBasicUri;
|
|
|
|
|
|
}
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-01-29 17:55:53 +01:00
|
|
|
|
else if (uriFormat.Contains("{q}")) // {i} is optional
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2013-01-29 17:55:53 +01:00
|
|
|
|
getUri = GetQuadKeyUri;
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
else if (uriFormat.Contains("{W}") && uriFormat.Contains("{S}") && uriFormat.Contains("{E}") && uriFormat.Contains("{N}"))
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2013-01-29 17:55:53 +01:00
|
|
|
|
getUri = GetBoundingBoxUri;
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
else if (uriFormat.Contains("{w}") && uriFormat.Contains("{s}") && uriFormat.Contains("{e}") && uriFormat.Contains("{n}"))
|
|
|
|
|
|
{
|
|
|
|
|
|
getUri = GetLatLonBoundingBoxUri;
|
|
|
|
|
|
}
|
2013-04-21 23:56:08 +02:00
|
|
|
|
else if (uriFormat.Contains("{x}") && uriFormat.Contains("{v}") && uriFormat.Contains("{z}"))
|
|
|
|
|
|
{
|
|
|
|
|
|
getUri = GetTmsUri;
|
|
|
|
|
|
}
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-01-29 17:55:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual Uri GetUri(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
2016-08-08 20:36:02 +02:00
|
|
|
|
return getUri?.Invoke(x, y, zoomLevel);
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-07 18:12:25 +02:00
|
|
|
|
private Uri GetBasicUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{y}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString()),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Uri GetOpenStreetMapUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2013-11-12 21:14:53 +01:00
|
|
|
|
var hostIndex = (x + y) % 3;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
|
|
|
|
|
.Replace("{c}", "abc".Substring(hostIndex, 1))
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{y}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString()),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Uri GetGoogleMapsUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2013-11-12 21:14:53 +01:00
|
|
|
|
var hostIndex = (x + y) % 4;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
|
|
|
|
|
.Replace("{i}", hostIndex.ToString())
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{y}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString()),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Uri GetMapQuestUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2013-11-12 21:14:53 +01:00
|
|
|
|
var hostIndex = (x + y) % 4 + 1;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
|
|
|
|
|
.Replace("{n}", hostIndex.ToString())
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{y}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString()),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-21 23:56:08 +02:00
|
|
|
|
private Uri GetTmsUri(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
y = (1 << zoomLevel) - 1 - y;
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{v}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString()),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2013-04-21 23:56:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Uri GetQuadKeyUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2013-11-17 16:52:03 +01:00
|
|
|
|
if (zoomLevel < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-19 21:50:23 +02:00
|
|
|
|
var quadkey = new char[zoomLevel];
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
for (var z = zoomLevel - 1; z >= 0; z--, x /= 2, y /= 2)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
quadkey[z] = (char)('0' + 2 * (y % 2) + (x % 2));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
return new Uri(uriFormat
|
2015-11-28 21:09:25 +01:00
|
|
|
|
.Replace("{i}", new string(quadkey, zoomLevel - 1, 1))
|
2015-08-09 20:04:44 +02:00
|
|
|
|
.Replace("{q}", new string(quadkey)),
|
2015-06-09 20:09:57 +02:00
|
|
|
|
UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-20 21:57:29 +02:00
|
|
|
|
private Uri GetBoundingBoxUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
var tileSize = 360d / (double)(1 << zoomLevel); // tile width in degrees
|
|
|
|
|
|
var west = MetersPerDegree * ((double)x * tileSize - 180d);
|
|
|
|
|
|
var east = MetersPerDegree * ((double)(x + 1) * tileSize - 180d);
|
|
|
|
|
|
var south = MetersPerDegree * (180d - (double)(y + 1) * tileSize);
|
|
|
|
|
|
var north = MetersPerDegree * (180d - (double)y * tileSize);
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
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))
|
2015-11-11 19:48:50 +01:00
|
|
|
|
.Replace("{X}", TileSize.ToString())
|
|
|
|
|
|
.Replace("{Y}", TileSize.ToString()));
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Uri GetLatLonBoundingBoxUri(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
var tileSize = 360d / (double)(1 << zoomLevel); // tile width in degrees
|
|
|
|
|
|
var west = (double)x * tileSize - 180d;
|
|
|
|
|
|
var east = (double)(x + 1) * tileSize - 180d;
|
|
|
|
|
|
var south = MercatorTransform.YToLatitude(180d - (double)(y + 1) * tileSize);
|
|
|
|
|
|
var north = MercatorTransform.YToLatitude(180d - (double)y * tileSize);
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
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))
|
2015-11-11 19:48:50 +01:00
|
|
|
|
.Replace("{X}", TileSize.ToString())
|
|
|
|
|
|
.Replace("{Y}", TileSize.ToString()));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|