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

245 lines
8.5 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2025-09-19 18:49:12 +02:00
using System.Collections.Generic;
using System.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
using Avalonia.Media;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
2025-11-13 23:55:47 +01:00
/// Displays a Web Mercator tile pyramid.
2012-05-04 12:52:20 +02:00
/// </summary>
2025-11-14 23:59:24 +01:00
public class MapTileLayer : TilePyramidLayer
2012-04-25 22:02:53 +02:00
{
2025-12-03 08:21:15 +01:00
private const int tileSize = 256;
2025-11-14 15:02:48 +01:00
2025-12-05 14:44:33 +01:00
private static readonly Point mapTopLeft = new Point(-180d * MapProjection.Wgs84MeterPerDegree,
180d * MapProjection.Wgs84MeterPerDegree);
2025-11-14 15:02:48 +01:00
public static readonly DependencyProperty TileSourceProperty =
DependencyPropertyHelper.Register<MapTileLayer, TileSource>(nameof(TileSource), null,
(layer, oldValue, newValue) => layer.UpdateTileCollection(true));
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty MinZoomLevelProperty =
DependencyPropertyHelper.Register<MapTileLayer, int>(nameof(MinZoomLevel), 0);
public static readonly DependencyProperty MaxZoomLevelProperty =
DependencyPropertyHelper.Register<MapTileLayer, int>(nameof(MaxZoomLevel), 19);
public static readonly DependencyProperty ZoomLevelOffsetProperty =
DependencyPropertyHelper.Register<MapTileLayer, double>(nameof(ZoomLevelOffset), 0d);
/// <summary>
2017-07-17 21:31:09 +02:00
/// A default MapTileLayer using OpenStreetMap data.
/// </summary>
2025-12-05 14:44:33 +01:00
public static MapTileLayer OpenStreetMapTileLayer => new MapTileLayer
{
2025-11-13 15:32:01 +01:00
TileSource = TileSource.Parse("https://tile.openstreetmap.org/{z}/{x}/{y}.png"),
2022-08-06 10:40:59 +02:00
SourceName = "OpenStreetMap",
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)"
};
2025-11-14 15:02:48 +01:00
public MapTileLayer()
{
2025-11-19 23:34:39 +01:00
this.SetRenderTransform(new MatrixTransform());
2025-11-14 15:02:48 +01:00
}
2025-09-21 10:31:22 +02:00
public override IReadOnlyCollection<string> SupportedCrsIds { get; } = [WebMercatorProjection.DefaultCrsId];
2025-09-19 18:49:12 +02:00
public TileMatrix TileMatrix { get; private set; }
2025-11-21 16:37:06 +01:00
public ICollection<ImageTile> Tiles { get; private set; } = [];
2020-03-23 17:13:50 +01:00
/// <summary>
/// Provides the ImagesSource or image request Uri for map tiles.
/// </summary>
public TileSource TileSource
{
get => (TileSource)GetValue(TileSourceProperty);
set => SetValue(TileSourceProperty, value);
}
/// <summary>
/// Minimum zoom level supported by the MapTileLayer. Default value is 0.
/// </summary>
public int MinZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (int)GetValue(MinZoomLevelProperty);
set => SetValue(MinZoomLevelProperty, value);
}
/// <summary>
2020-10-25 16:09:09 +01:00
/// Maximum zoom level supported by the MapTileLayer. Default value is 19.
/// </summary>
public int MaxZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (int)GetValue(MaxZoomLevelProperty);
set => SetValue(MaxZoomLevelProperty, value);
}
2022-04-24 19:34:02 +02:00
/// <summary>
/// Optional offset between the map zoom level and the topmost tile zoom level.
/// Default value is 0.
/// </summary>
public double ZoomLevelOffset
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(ZoomLevelOffsetProperty);
set => SetValue(ZoomLevelOffsetProperty, value);
2022-04-24 19:34:02 +02:00
}
2020-10-29 21:46:41 +01:00
protected override Size MeasureOverride(Size availableSize)
{
foreach (var tile in Tiles)
{
tile.Image.Measure(availableSize);
}
return new Size();
}
protected override Size ArrangeOverride(Size finalSize)
{
2025-11-19 17:49:49 +01:00
foreach (var tile in Tiles)
2020-10-29 21:46:41 +01:00
{
2025-11-19 17:49:49 +01:00
// Arrange tiles relative to TileMatrix.XMin/YMin.
//
2025-12-03 08:21:15 +01:00
var tileSize = MapTileLayer.tileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel);
var x = tileSize * tile.X - MapTileLayer.tileSize * TileMatrix.XMin;
var y = tileSize * tile.Y - MapTileLayer.tileSize * TileMatrix.YMin;
2020-10-29 21:46:41 +01:00
2025-11-19 17:49:49 +01:00
tile.Image.Width = tileSize;
tile.Image.Height = tileSize;
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
2020-10-29 21:46:41 +01:00
}
return finalSize;
}
2025-11-23 17:29:30 +01:00
protected override void UpdateRenderTransform()
{
if (TileMatrix != null)
{
// Tile matrix origin in pixels.
//
2025-12-03 08:21:15 +01:00
var tileMatrixOrigin = new Point(tileSize * TileMatrix.XMin, tileSize * TileMatrix.YMin);
2025-11-23 17:29:30 +01:00
var tileMatrixScale = MapBase.ZoomLevelToScale(TileMatrix.ZoomLevel);
((MatrixTransform)RenderTransform).Matrix =
2025-12-03 08:21:15 +01:00
ParentMap.ViewTransform.GetTileLayerTransform(tileMatrixScale, mapTopLeft, tileMatrixOrigin);
2025-11-23 17:29:30 +01:00
}
}
protected override void UpdateTileCollection()
{
UpdateTileCollection(false);
}
private void UpdateTileCollection(bool tileSourceChanged)
{
if (TileSource == null || ParentMap == null || !SupportedCrsIds.Contains(ParentMap.MapProjection.CrsId))
{
2025-08-19 23:21:51 +02:00
CancelLoadTiles();
2025-11-19 17:49:49 +01:00
Children.Clear();
Tiles.Clear();
TileMatrix = null;
}
else if (SetTileMatrix() || tileSourceChanged)
2020-10-26 21:59:51 +01:00
{
if (tileSourceChanged)
{
Tiles.Clear();
}
2025-10-30 19:49:06 +01:00
UpdateRenderTransform();
UpdateTiles();
BeginLoadTiles(Tiles, TileSource, SourceName);
2022-11-25 19:05:48 +01:00
}
2020-03-20 18:12:56 +01:00
}
private bool SetTileMatrix()
{
2025-11-28 21:59:25 +01:00
// Add 0.001 to avoid floating point precision.
2022-11-30 22:18:45 +01:00
//
var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel - ZoomLevelOffset + 0.001);
2024-08-29 21:35:58 +02:00
var tileMatrixScale = MapBase.ZoomLevelToScale(tileMatrixZoomLevel);
2025-11-21 15:52:38 +01:00
// Tile matrix bounds in pixels.
2020-03-20 18:12:56 +01:00
//
2025-12-03 08:21:15 +01:00
var bounds = ParentMap.ViewTransform.GetTileMatrixBounds(tileMatrixScale, mapTopLeft, ParentMap.ActualWidth, ParentMap.ActualHeight);
2022-11-30 22:18:45 +01:00
// Tile X and Y bounds.
2020-03-20 18:12:56 +01:00
//
2025-12-03 08:21:15 +01:00
var xMin = (int)Math.Floor(bounds.X / tileSize);
var yMin = (int)Math.Floor(bounds.Y / tileSize);
var xMax = (int)Math.Floor((bounds.X + bounds.Width) / tileSize);
var yMax = (int)Math.Floor((bounds.Y + bounds.Height) / tileSize);
if (TileMatrix != null &&
TileMatrix.ZoomLevel == tileMatrixZoomLevel &&
TileMatrix.XMin == xMin && TileMatrix.YMin == yMin &&
TileMatrix.XMax == xMax && TileMatrix.YMax == yMax)
2020-03-20 18:12:56 +01:00
{
return false;
}
TileMatrix = new TileMatrix(tileMatrixZoomLevel, xMin, yMin, xMax, yMax);
2020-03-20 18:12:56 +01:00
return true;
}
private void UpdateTiles()
{
2025-11-13 15:32:01 +01:00
var tiles = new ImageTileList();
2025-11-19 17:49:49 +01:00
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
if (maxZoomLevel >= MinZoomLevel)
{
2025-11-27 20:06:48 +01:00
var minZoomLevel = maxZoomLevel;
if (IsBaseMapLayer)
{
var bgLevels = Math.Max(MaxBackgroundLevels, 0);
minZoomLevel = Math.Max(TileMatrix.ZoomLevel - bgLevels, MinZoomLevel);
}
2025-11-19 17:49:49 +01:00
for (var zoomLevel = minZoomLevel; zoomLevel <= maxZoomLevel; zoomLevel++)
2020-10-29 21:46:41 +01:00
{
2025-11-19 17:49:49 +01:00
var tileCount = 1 << zoomLevel; // per row and column
// Right-shift divides with rounding down also negative values, https://stackoverflow.com/q/55196178
//
var shift = TileMatrix.ZoomLevel - zoomLevel;
var xMin = TileMatrix.XMin >> shift; // may be < 0
var xMax = TileMatrix.XMax >> shift; // may be >= tileCount
var yMin = Math.Max(TileMatrix.YMin >> shift, 0);
var yMax = Math.Min(TileMatrix.YMax >> shift, tileCount - 1);
tiles.FillMatrix(Tiles, zoomLevel, xMin, yMin, xMax, yMax, tileCount);
2012-04-25 22:02:53 +02:00
}
}
2021-11-13 00:28:44 +01:00
2022-08-22 21:13:45 +02:00
Tiles = tiles;
2025-11-19 17:49:49 +01:00
2021-11-13 00:28:44 +01:00
Children.Clear();
2022-08-22 21:13:45 +02:00
foreach (var tile in tiles)
2021-11-13 00:28:44 +01:00
{
Children.Add(tile.Image);
}
2020-03-23 17:13:50 +01:00
}
2012-04-25 22:02:53 +02:00
}
2012-06-24 23:42:11 +02:00
}