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

255 lines
8.4 KiB
C#
Raw Normal View History

// 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;
2012-04-25 22:02:53 +02:00
using System.Collections.Generic;
using System.Linq;
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
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Media;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
/// 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
{
2020-03-22 18:33:34 +01:00
public const int TileSize = 256;
2020-03-24 16:13:25 +01:00
2020-04-17 14:40:14 +02:00
public 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
/// <summary>
2017-07-17 21:31:09 +02:00
/// A default MapTileLayer using OpenStreetMap data.
/// </summary>
public static MapTileLayer OpenStreetMapTileLayer
{
get
{
return new MapTileLayer
{
TileSource = new TileSource { UriFormat = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" },
SourceName = "OpenStreetMap",
2020-10-25 16:09:09 +01:00
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)"
};
}
}
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
2020-10-25 16:09:09 +01:00
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(19));
public MapTileLayer()
: this(new TileImageLoader())
2012-04-25 22:02:53 +02:00
{
}
public MapTileLayer(ITileImageLoader tileImageLoader)
2020-03-20 18:12:56 +01:00
: base(tileImageLoader)
{
2017-09-05 20:57:17 +02:00
}
public TileMatrix TileMatrix { get; private set; }
2021-11-13 00:28:44 +01:00
public IReadOnlyCollection<Tile> Tiles { get; private set; } = new List<Tile>();
2020-03-23 17:13:50 +01:00
/// <summary>
/// Minimum zoom level supported by the MapTileLayer. Default value is 0.
/// </summary>
public int MinZoomLevel
{
get { return (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
{
get { return (int)GetValue(MaxZoomLevelProperty); }
set { SetValue(MaxZoomLevelProperty, value); }
}
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)
{
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()
{
2020-10-29 21:46:41 +01:00
var update = false;
2022-03-05 18:40:57 +01:00
if (ParentMap == null || ParentMap.MapProjection.Type != MapProjectionType.WebMercator)
{
2021-11-13 16:45:41 +01:00
update = TileMatrix != null;
TileMatrix = null;
}
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)
{
Tiles = new List<Tile>(); // clear all
}
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)
{
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
}
2020-03-20 18:12:56 +01:00
protected override void SetRenderTransform()
{
// tile matrix origin in pixels
2020-03-20 18:12:56 +01:00
//
var tileMatrixOrigin = new Point(TileSize * TileMatrix.XMin, TileSize * TileMatrix.YMin);
2020-03-20 18:12:56 +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);
}
private bool SetTileMatrix()
{
var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel + 0.001); // avoid rounding issues
2020-03-20 18:12:56 +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);
2020-03-22 18:33:34 +01:00
// tile column and row index 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);
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;
}
2021-07-05 00:03:44 +02:00
private void UpdateTiles()
{
2021-11-13 00:28:44 +01:00
var newTiles = new List<Tile>();
2020-10-29 21:46:41 +01:00
int maxZoomLevel;
2021-11-13 00:28:44 +01:00
if (TileSource != null &&
TileMatrix != null &&
(maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel)) >= MinZoomLevel)
2012-04-25 22:02:53 +02:00
{
2021-11-11 22:27:57 +01:00
var minZoomLevel = IsBaseMapLayer
2021-11-10 22:31:39 +01:00
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
: maxZoomLevel;
2020-10-29 21:46:41 +01:00
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
{
var tileSize = 1 << (TileMatrix.ZoomLevel - z);
var x1 = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative
var x2 = TileMatrix.XMax / tileSize;
var y1 = Math.Max(TileMatrix.YMin / tileSize, 0);
var y2 = Math.Min(TileMatrix.YMax / tileSize, (1 << z) - 1);
for (var y = y1; y <= y2; y++)
{
for (var x = x1; x <= x2; x++)
{
2021-11-13 00:28:44 +01:00
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
2020-10-29 21:46:41 +01:00
if (tile == null)
{
tile = new Tile(z, x, y);
2021-11-13 00:28:44 +01:00
var equivalentTile = Tiles.FirstOrDefault(
2020-10-29 21:46:41 +01:00
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && !t.Pending);
2020-10-29 21:46:41 +01:00
if (equivalentTile != null)
{
tile.SetImage(equivalentTile.Image.Source, false); // no fade-in animation
}
}
2020-10-29 21:46:41 +01:00
2021-11-13 00:28:44 +01:00
newTiles.Add(tile);
}
2012-04-25 22:02:53 +02:00
}
}
}
2021-11-13 00:28:44 +01:00
Tiles = newTiles;
Children.Clear();
foreach (var tile in Tiles)
{
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
}