2020-03-20 18:12:56 +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-03-20 18:12:56 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
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;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public class WmtsTileMatrixLayer : Panel
|
|
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// zoomLevel is index of tileMatrix in a WmtsTileMatrixSet.TileMatrixes list.
|
|
|
|
|
|
//
|
2020-03-20 18:12:56 +01:00
|
|
|
|
public WmtsTileMatrixLayer(WmtsTileMatrix tileMatrix, int zoomLevel)
|
|
|
|
|
|
{
|
2024-05-22 11:47:57 +02:00
|
|
|
|
MapPanel.SetRenderTransform(this, new MatrixTransform());
|
|
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
WmtsTileMatrix = tileMatrix;
|
|
|
|
|
|
TileMatrix = new TileMatrix(zoomLevel, 1, 1, 0, 0);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
public WmtsTileMatrix WmtsTileMatrix { get; }
|
2021-11-13 00:28:27 +01:00
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
public TileMatrix TileMatrix { get; private set; }
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
public TileCollection Tiles { get; private set; } = new TileCollection();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public void SetRenderTransform(ViewTransform viewTransform)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Tile matrix origin in pixels.
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2022-11-25 19:05:48 +01:00
|
|
|
|
var tileMatrixOrigin = new Point(WmtsTileMatrix.TileWidth * TileMatrix.XMin, WmtsTileMatrix.TileHeight * TileMatrix.YMin);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-22 18:33:34 +01:00
|
|
|
|
((MatrixTransform)RenderTransform).Matrix =
|
2022-11-25 19:05:48 +01:00
|
|
|
|
viewTransform.GetTileLayerTransform(WmtsTileMatrix.Scale, WmtsTileMatrix.TopLeft, tileMatrixOrigin);
|
2020-03-22 18:33:34 +01:00
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
public bool UpdateTiles(ViewTransform viewTransform, Size viewSize)
|
2020-03-22 18:33:34 +01:00
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Bounds in tile pixels from view size.
|
2020-03-21 08:17:15 +01:00
|
|
|
|
//
|
2022-11-25 19:05:48 +01:00
|
|
|
|
var bounds = viewTransform.GetTileMatrixBounds(WmtsTileMatrix.Scale, WmtsTileMatrix.TopLeft, viewSize);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Tile X and Y bounds.
|
2020-03-22 18:33:34 +01:00
|
|
|
|
//
|
2022-11-25 19:05:48 +01:00
|
|
|
|
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);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Total tile matrix width in meters.
|
2022-11-22 19:15:34 +01:00
|
|
|
|
//
|
2022-11-25 19:05:48 +01:00
|
|
|
|
var totalWidth = WmtsTileMatrix.MatrixWidth * WmtsTileMatrix.TileWidth / WmtsTileMatrix.Scale;
|
2022-11-22 19:15:34 +01:00
|
|
|
|
|
|
|
|
|
|
if (Math.Abs(totalWidth - 360d * MapProjection.Wgs84MeterPerDegree) > 1d)
|
|
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// No full longitudinal coverage, restrict x index.
|
2022-11-22 19:15:34 +01:00
|
|
|
|
//
|
|
|
|
|
|
xMin = Math.Max(xMin, 0);
|
2022-11-25 19:05:48 +01:00
|
|
|
|
xMax = Math.Min(Math.Max(xMax, 0), WmtsTileMatrix.MatrixWidth - 1);
|
2022-11-22 19:15:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
yMin = Math.Max(yMin, 0);
|
2022-11-25 19:05:48 +01:00
|
|
|
|
yMax = Math.Min(Math.Max(yMax, 0), WmtsTileMatrix.MatrixHeight - 1);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
if (TileMatrix.XMin == xMin && TileMatrix.YMin == yMin &&
|
|
|
|
|
|
TileMatrix.XMax == xMax && TileMatrix.YMax == yMax)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// No change of the TileMatrix and the Tiles collection.
|
2022-11-25 19:05:48 +01:00
|
|
|
|
//
|
2020-03-20 18:12:56 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var tiles = new TileCollection();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
for (var y = yMin; y <= yMax; y++)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-11-25 19:05:48 +01:00
|
|
|
|
for (var x = xMin; x <= xMax; x++)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-11-25 19:05:48 +01:00
|
|
|
|
tiles.Add(Tiles.GetTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
Tiles = tiles;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
Children.Clear();
|
|
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
foreach (var tile in tiles)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
Children.Add(tile.Image);
|
|
|
|
|
|
}
|
2022-11-25 19:05:48 +01:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.Image.Measure(availableSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Arrange tiles relative to XMin/YMin.
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2022-11-25 19:05:48 +01:00
|
|
|
|
var x = WmtsTileMatrix.TileWidth * (tile.X - TileMatrix.XMin);
|
|
|
|
|
|
var y = WmtsTileMatrix.TileHeight * (tile.Y - TileMatrix.YMin);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
tile.Image.Width = WmtsTileMatrix.TileWidth;
|
|
|
|
|
|
tile.Image.Height = WmtsTileMatrix.TileHeight;
|
|
|
|
|
|
tile.Image.Arrange(new Rect(x, y, WmtsTileMatrix.TileWidth, WmtsTileMatrix.TileHeight));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|