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

76 lines
2.8 KiB
C#
Raw Normal View History

2020-10-25 16:09:09 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2020-10-25 16:09:09 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
2024-04-23 20:18:13 +02:00
using System.Threading.Tasks;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows.Media;
2024-05-18 11:42:40 +02:00
#elif UWP
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml.Media;
2024-05-18 11:42:40 +02:00
#endif
2020-10-25 16:09:09 +01:00
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);
}
public override Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
{
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 LoadImageAsync(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
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);
}
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
}
}