Update BoundingBoxTileSource.cs

This commit is contained in:
ClemensFischer 2024-04-23 20:18:13 +02:00
parent 1b8a89a504
commit 4a44702652

View file

@ -4,39 +4,61 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks;
using System.Windows.Media;
namespace MapControl namespace MapControl
{ {
public class BoundingBoxTileSource : TileSource public class BoundingBoxTileSource : TileSource
{ {
public override Uri GetUri(int column, int row, int zoomLevel) public override Uri GetUri(int column, int row, int zoomLevel)
{
GetBounds(column, row, zoomLevel, out double west, out double south, out double east, out double north);
return GetUri(west, south, east, north);
}
public override Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
{
GetBounds(column, row, zoomLevel, out double west, out double south, out double east, out double north);
return LoadImageAsync(west, south, east, north);
}
protected virtual Uri GetUri(double west, double south, double east, double north)
{ {
Uri uri = null; Uri uri = null;
if (UriTemplate != null) if (UriTemplate != null)
{ {
var tileSize = 360d / (1 << zoomLevel); // tile size in degrees var w = west.ToString("F2", CultureInfo.InvariantCulture);
var west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d); var e = east.ToString("F2", CultureInfo.InvariantCulture);
var east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d); var s = south.ToString("F2", CultureInfo.InvariantCulture);
var south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize); var n = north.ToString("F2", CultureInfo.InvariantCulture);
var north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
if (UriTemplate.Contains("{bbox}")) uri = UriTemplate.Contains("{bbox}")
{ ? new Uri(UriTemplate.Replace("{bbox}", $"{w},{s},{e},{n}"))
uri = new Uri(UriTemplate.Replace("{bbox}", : new Uri(UriTemplate.Replace("{west}", w).Replace("{south}", s).Replace("{east}", e).Replace("{north}", n));
string.Format(CultureInfo.InvariantCulture, "{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north)));
}
else
{
uri = new Uri(UriTemplate
.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; return uri;
} }
protected virtual Task<ImageSource> LoadImageAsync(double west, double south, double east, double north)
{
var uri = GetUri(west, south, east, north);
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
protected void GetBounds(int column, int row, int zoomLevel, out double west, out double south, out double east, out double north)
{
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);
}
} }
} }