2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-07-05 00:03:44 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2014-07-01 18:57:44 +02:00
|
|
|
|
using Windows.Foundation;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2020-04-19 10:53:25 +02:00
|
|
|
|
/// Displays web mercator map tiles.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2020-03-20 18:12:56 +01:00
|
|
|
|
public class MapTileLayer : MapTileLayerBase
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2022-04-24 19:34:02 +02:00
|
|
|
|
private const int TileSize = 256;
|
2020-03-24 16:13:25 +01:00
|
|
|
|
|
2022-04-24 19:34:02 +02:00
|
|
|
|
private static readonly Point MapTopLeft = new Point(
|
2022-03-02 22:03:18 +01:00
|
|
|
|
-180d * MapProjection.Wgs84MeterPerDegree, 180d * MapProjection.Wgs84MeterPerDegree);
|
2020-03-22 18:33:34 +01:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// A default MapTileLayer using OpenStreetMap data.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2022-08-06 10:40:59 +02:00
|
|
|
|
public static MapTileLayer OpenStreetMapTileLayer => new MapTileLayer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
TileSource = new TileSource { UriTemplate = "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)"
|
|
|
|
|
|
};
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
|
2020-10-25 16:09:09 +01:00
|
|
|
|
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(19));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2022-04-24 19:34:02 +02:00
|
|
|
|
public static readonly DependencyProperty ZoomLevelOffsetProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(ZoomLevelOffset), typeof(double), typeof(MapTileLayer), new PropertyMetadata(0d));
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public MapTileLayer()
|
2014-10-19 21:50:23 +02:00
|
|
|
|
: this(new TileImageLoader())
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public MapTileLayer(ITileImageLoader tileImageLoader)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
: base(tileImageLoader)
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public TileMatrix TileMatrix { get; private set; }
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
public TileCollection Tiles { get; private set; } = new TileCollection();
|
2020-03-23 17:13:50 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <summary>
|
2019-07-13 00:08:56 +02:00
|
|
|
|
/// Minimum zoom level supported by the MapTileLayer. Default value is 0.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MinZoomLevel
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (int)GetValue(MinZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(MinZoomLevelProperty, value);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-25 16:09:09 +01:00
|
|
|
|
/// Maximum zoom level supported by the MapTileLayer. Default value is 19.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MaxZoomLevel
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (int)GetValue(MaxZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(MaxZoomLevelProperty, value);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TileMatrix != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
// arrange tiles relative to XMin/YMin
|
|
|
|
|
|
//
|
2020-10-29 21:46:41 +01:00
|
|
|
|
var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel);
|
|
|
|
|
|
var x = tileSize * tile.X - TileSize * TileMatrix.XMin;
|
|
|
|
|
|
var y = tileSize * tile.Y - TileSize * TileMatrix.YMin;
|
|
|
|
|
|
|
|
|
|
|
|
tile.Image.Width = tileSize;
|
|
|
|
|
|
tile.Image.Height = tileSize;
|
|
|
|
|
|
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
protected override Task UpdateTileLayer()
|
2017-07-18 18:20:57 +02:00
|
|
|
|
{
|
2020-10-29 21:46:41 +01:00
|
|
|
|
var update = false;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2022-03-05 18:40:57 +01:00
|
|
|
|
if (ParentMap == null || ParentMap.MapProjection.Type != MapProjectionType.WebMercator)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2021-11-13 16:45:41 +01:00
|
|
|
|
update = TileMatrix != null;
|
2020-03-26 19:08:20 +01:00
|
|
|
|
TileMatrix = null;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
2020-10-26 21:59:51 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-07-05 00:03:44 +02:00
|
|
|
|
if (TileSource != TileImageLoader.TileSource)
|
2020-10-26 21:59:51 +01:00
|
|
|
|
{
|
2021-11-13 16:45:41 +01:00
|
|
|
|
if (Tiles.Count > 0)
|
|
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
Tiles = new TileCollection(); // clear all
|
2021-11-13 16:45:41 +01:00
|
|
|
|
}
|
2020-10-29 21:46:41 +01:00
|
|
|
|
update = true;
|
2020-10-26 21:59:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SetTileMatrix())
|
|
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
2020-10-29 21:46:41 +01:00
|
|
|
|
update = true;
|
2020-10-26 21:59:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 21:46:41 +01:00
|
|
|
|
if (update)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2021-07-05 00:03:44 +02:00
|
|
|
|
UpdateTiles();
|
2020-10-29 21:46:41 +01:00
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
return TileImageLoader.LoadTiles(Tiles, TileSource, SourceName);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2021-07-05 00:03:44 +02:00
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
protected override void SetRenderTransform()
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
// tile matrix origin in pixels
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileMatrixOrigin = new Point(TileSize * TileMatrix.XMin, TileSize * TileMatrix.YMin);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-28 21:53:38 +01:00
|
|
|
|
var tileMatrixScale = ViewTransform.ZoomLevelToScale(TileMatrix.ZoomLevel);
|
|
|
|
|
|
|
2020-04-17 14:40:14 +02:00
|
|
|
|
((MatrixTransform)RenderTransform).Matrix =
|
|
|
|
|
|
ParentMap.ViewTransform.GetTileLayerTransform(tileMatrixScale, MapTopLeft, tileMatrixOrigin);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
private bool SetTileMatrix()
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2022-04-24 19:34:02 +02:00
|
|
|
|
var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel - ZoomLevelOffset + 0.001); // avoid rounding issues
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-28 21:53:38 +01:00
|
|
|
|
var tileMatrixScale = ViewTransform.ZoomLevelToScale(tileMatrixZoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
// bounds in tile pixels from view size
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-04-17 14:40:14 +02:00
|
|
|
|
var bounds = ParentMap.ViewTransform.GetTileMatrixBounds(tileMatrixScale, MapTopLeft, ParentMap.RenderSize);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
// tile X and Y bounds
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-04-17 14:40:14 +02: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);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
TileMatrix = new TileMatrix(tileMatrixZoomLevel, xMin, yMin, xMax, yMax);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
private void UpdateTiles()
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var tiles = new TileCollection();
|
2020-10-29 21:46:41 +01:00
|
|
|
|
|
2022-08-22 21:13:45 +02:00
|
|
|
|
if (TileSource != null && TileMatrix != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2022-08-22 21:13:45 +02:00
|
|
|
|
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2022-08-22 21:13:45 +02:00
|
|
|
|
if (maxZoomLevel >= MinZoomLevel)
|
2020-10-29 21:46:41 +01:00
|
|
|
|
{
|
2022-08-22 21:13:45 +02:00
|
|
|
|
var minZoomLevel = IsBaseMapLayer
|
|
|
|
|
|
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
|
|
|
|
|
|
: maxZoomLevel;
|
2020-10-29 21:46:41 +01:00
|
|
|
|
|
2022-08-22 21:13:45 +02:00
|
|
|
|
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
2020-10-29 21:46:41 +01:00
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var numTiles = 1 << z;
|
2022-08-22 21:13:45 +02:00
|
|
|
|
var tileSize = 1 << (TileMatrix.ZoomLevel - z);
|
|
|
|
|
|
var x1 = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var x2 = TileMatrix.XMax / tileSize; // may be greater than numTiles-1
|
2022-08-22 21:13:45 +02:00
|
|
|
|
var y1 = Math.Max(TileMatrix.YMin / tileSize, 0);
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var y2 = Math.Min(TileMatrix.YMax / tileSize, numTiles - 1);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2022-08-22 21:13:45 +02:00
|
|
|
|
for (var y = y1; y <= y2; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var x = x1; x <= x2; x++)
|
2020-10-29 21:46:41 +01:00
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
tiles.Add(Tiles.GetTile(z, x, y, numTiles));
|
2022-08-22 21:13:45 +02:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-18 18:20:57 +02:00
|
|
|
|
}
|
2021-11-13 00:28:44 +01:00
|
|
|
|
|
2022-08-22 21:13:45 +02:00
|
|
|
|
Tiles = tiles;
|
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
|
|
|
|
}
|