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

43 lines
1.7 KiB
C#
Raw Normal View History

2020-10-25 16:09:09 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
2020-10-25 16:09:09 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
namespace MapControl
{
public class BoundingBoxTileSource : TileSource
{
2022-11-22 19:15:34 +01:00
public override Uri GetUri(int column, int row, int zoomLevel)
2020-10-25 16:09:09 +01:00
{
Uri uri = null;
if (UriTemplate != null)
2020-10-25 16:09:09 +01:00
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
2022-11-22 19:15:34 +01:00
var west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d);
var east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d);
var south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize);
var north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
2020-10-25 16:09:09 +01:00
if (UriTemplate.Contains("{bbox}"))
2020-10-25 16:09:09 +01:00
{
uri = new Uri(UriTemplate.Replace("{bbox}",
2021-01-26 00:25:50 +01:00
string.Format(CultureInfo.InvariantCulture, "{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north)));
2020-10-25 16:09:09 +01:00
}
else
{
uri = new Uri(UriTemplate
2020-10-25 16:09:09 +01:00
.Replace("{west}", west.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{south}", south.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{east}", east.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{north}", north.ToString("F2", CultureInfo.InvariantCulture)));
}
}
return uri;
}
}
}