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

124 lines
3.7 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2025-11-21 16:37:06 +01:00
using System.Collections.Generic;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
2020-03-20 18:12:56 +01:00
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
2020-03-20 18:12:56 +01:00
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
public partial class WmtsTileMatrixLayer : Panel
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
public WmtsTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel)
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
this.SetRenderTransform(new MatrixTransform());
WmtsTileMatrix = wmtsTileMatrix;
TileMatrix = new TileMatrix(zoomLevel, 1, 1, 0, 0);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public WmtsTileMatrix WmtsTileMatrix { get; }
2021-11-13 00:28:27 +01:00
2026-04-13 17:14:49 +02:00
public TileMatrix TileMatrix { get; private set; }
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public IEnumerable<ImageTile> Tiles { get; private set; } = [];
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public void UpdateRenderTransform(ViewTransform viewTransform)
{
// Tile matrix origin in pixels.
//
var tileMatrixOrigin = new Point(WmtsTileMatrix.TileWidth * TileMatrix.XMin, WmtsTileMatrix.TileHeight * TileMatrix.YMin);
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
((MatrixTransform)RenderTransform).Matrix =
viewTransform.GetTileLayerTransform(WmtsTileMatrix.Scale, WmtsTileMatrix.TopLeft, tileMatrixOrigin);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public bool UpdateTiles(ViewTransform viewTransform, double viewWidth, double viewHeight)
{
// Tile matrix bounds in pixels.
//
var bounds = viewTransform.GetTileMatrixBounds(WmtsTileMatrix.Scale, WmtsTileMatrix.TopLeft, viewWidth, viewHeight);
// Tile X and Y bounds.
//
var xMin = (int)Math.Floor(bounds.X / WmtsTileMatrix.TileWidth);
var yMin = (int)Math.Floor(bounds.Y / WmtsTileMatrix.TileHeight);
var xMax = (int)Math.Floor((bounds.X + bounds.Width) / WmtsTileMatrix.TileWidth);
var yMax = (int)Math.Floor((bounds.Y + bounds.Height) / WmtsTileMatrix.TileHeight);
if (!WmtsTileMatrix.HasFullHorizontalCoverage)
2020-03-22 18:33:34 +01:00
{
2026-04-13 17:14:49 +02:00
// Set X range limits.
2020-03-21 08:17:15 +01:00
//
2026-04-13 17:14:49 +02:00
xMin = Math.Max(xMin, 0);
xMax = Math.Min(Math.Max(xMax, 0), WmtsTileMatrix.MatrixWidth - 1);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
// Set Y range limits.
//
yMin = Math.Max(yMin, 0);
yMax = Math.Min(Math.Max(yMax, 0), WmtsTileMatrix.MatrixHeight - 1);
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
if (TileMatrix.XMin == xMin && TileMatrix.YMin == yMin &&
TileMatrix.XMax == xMax && TileMatrix.YMax == yMax)
{
// No change of the TileMatrix and the Tiles collection.
//
return false;
}
2025-11-19 17:49:49 +01:00
2026-04-13 17:14:49 +02:00
TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax);
Tiles = new ImageTileList(Tiles, TileMatrix, WmtsTileMatrix.MatrixWidth);
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
Children.Clear();
2022-11-25 19:05:48 +01:00
2026-04-13 17:14:49 +02:00
foreach (var tile in Tiles)
{
Children.Add(tile.Image);
2020-03-20 18:12:56 +01:00
}
2026-04-13 17:14:49 +02:00
return true;
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
protected override Size MeasureOverride(Size availableSize)
{
foreach (var tile in Tiles)
{
tile.Image.Measure(availableSize);
2020-03-20 18:12:56 +01:00
}
2026-04-13 17:14:49 +02:00
return new Size();
}
protected override Size ArrangeOverride(Size finalSize)
{
foreach (var tile in Tiles)
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
// Arrange tiles relative to TileMatrix.XMin/YMin.
//
var width = WmtsTileMatrix.TileWidth;
var height = WmtsTileMatrix.TileHeight;
var x = width * (tile.X - TileMatrix.XMin);
var y = height * (tile.Y - TileMatrix.YMin);
tile.Image.Width = width;
tile.Image.Height = height;
tile.Image.Arrange(new Rect(x, y, width, height));
2020-03-20 18:12:56 +01:00
}
2026-04-13 17:14:49 +02:00
return finalSize;
2020-03-20 18:12:56 +01:00
}
}