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

63 lines
2.3 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2020-10-25 16:09:09 +01:00
using System.Globalization;
2025-10-29 18:59:19 +01:00
using System.Text;
2020-10-25 16:09:09 +01:00
namespace MapControl
{
2025-11-13 15:32:01 +01:00
public class BoundingBoxTileSource : UriTileSource
2020-10-25 16:09:09 +01:00
{
public override Uri GetUri(int zoomLevel, int column, int row)
2024-04-23 20:18:13 +02:00
{
GetTileBounds(zoomLevel, column, row, out double west, out double south, out double east, out double north);
2024-04-23 20:18:13 +02:00
return GetUri(west, south, east, north);
}
protected virtual Uri GetUri(double west, double south, double east, double north)
2020-10-25 16:09:09 +01:00
{
Uri uri = null;
if (UriTemplate != null)
2020-10-25 16:09:09 +01:00
{
2024-04-23 20:18:13 +02:00
var w = west.ToString("F2", CultureInfo.InvariantCulture);
var e = east.ToString("F2", CultureInfo.InvariantCulture);
var s = south.ToString("F2", CultureInfo.InvariantCulture);
var n = north.ToString("F2", CultureInfo.InvariantCulture);
2025-10-29 18:59:19 +01:00
if (UriTemplate.Contains("{bbox}"))
{
uri = new Uri(UriTemplate.Replace("{bbox}", $"{w},{s},{e},{n}"));
}
else
{
var uriBuilder = new StringBuilder(UriTemplate);
uriBuilder.Replace("{west}", w);
uriBuilder.Replace("{south}", s);
uriBuilder.Replace("{east}", e);
uriBuilder.Replace("{north}", n);
uri = new Uri(uriBuilder.ToString());
}
2020-10-25 16:09:09 +01:00
}
return uri;
}
2024-04-23 20:18:13 +02:00
2024-05-18 11:39:01 +02:00
/// <summary>
2024-05-18 11:42:40 +02:00
/// Gets the bounding box in meters of a standard Web Mercator tile,
/// specified by zoom level and grid column and row indices.
2024-05-18 11:39:01 +02:00
/// </summary>
public static void GetTileBounds(int zoomLevel, int column, int row,
2024-05-18 11:39:01 +02:00
out double west, out double south, out double east, out double north)
2024-04-23 20:18:13 +02:00
{
var tileSize = 360d / (1 << zoomLevel); // tile size in degrees
west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d);
east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d);
south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize);
north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
}
2020-10-25 16:09:09 +01:00
}
}