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

50 lines
1.9 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;
namespace MapControl
{
public class BoundingBoxTileSource : TileSource
{
2022-11-22 19:15:34 +01:00
public override Uri GetUri(int column, int row, int zoomLevel)
2024-04-23 20:18:13 +02:00
{
2024-05-18 11:39:01 +02:00
GetTileBounds(column, row, zoomLevel, 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);
uri = UriTemplate.Contains("{bbox}")
? new Uri(UriTemplate.Replace("{bbox}", $"{w},{s},{e},{n}"))
: new Uri(UriTemplate.Replace("{west}", w).Replace("{south}", s).Replace("{east}", e).Replace("{north}", n));
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 grid column and row indices and zoom level.
2024-05-18 11:39:01 +02:00
/// </summary>
public static void GetTileBounds(int column, int row, int zoomLevel,
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
}
}