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

248 lines
8.4 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 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;
2017-08-04 21:38:58 +02:00
#if WINDOWS_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>
/// Fills the map viewport with map tiles from a TileSource.
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
public static readonly Point TileMatrixTopLeft = new Point(
2020-03-24 16:13:25 +01:00
-180d * MapProjection.Wgs84MetersPerDegree, 180d * MapProjection.Wgs84MetersPerDegree);
2020-03-22 18:33:34 +01:00
public static double TileMatrixScale(int zoomLevel)
2020-03-22 18:33:34 +01:00
{
2020-03-24 16:13:25 +01:00
return (TileSize << zoomLevel) / (360d * MapProjection.Wgs84MetersPerDegree);
2020-03-22 18:33:34 +01:00
}
2020-03-20 18:12:56 +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
{
SourceName = "OpenStreetMap",
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)",
TileSource = new TileSource { UriFormat = "https://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" },
MaxZoomLevel = 19
};
}
}
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(18));
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; }
2020-03-23 17:13:50 +01:00
public IReadOnlyCollection<Tile> Tiles { get; private set; } = new List<Tile>();
/// <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>
/// Maximum zoom level supported by the MapTileLayer. Default value is 18.
/// </summary>
public int MaxZoomLevel
{
get { return (int)GetValue(MaxZoomLevelProperty); }
set { SetValue(MaxZoomLevelProperty, value); }
}
2020-03-20 18:12:56 +01:00
protected override void TileSourcePropertyChanged()
{
if (TileMatrix != null)
{
Tiles = new List<Tile>();
UpdateTiles();
}
}
2020-03-20 18:12:56 +01:00
protected override void UpdateTileLayer()
{
2020-03-20 18:12:56 +01:00
UpdateTimer.Stop();
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
{
TileMatrix = null;
2020-03-20 18:12:56 +01:00
UpdateTiles();
}
else if (SetTileMatrix())
{
SetRenderTransform();
2020-03-20 18:12:56 +01:00
UpdateTiles();
}
}
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
((MatrixTransform)RenderTransform).Matrix = ParentMap.ViewTransform.GetTileLayerTransform(
TileMatrixScale(TileMatrix.ZoomLevel), TileMatrixTopLeft, tileMatrixOrigin);
}
private bool SetTileMatrix()
{
var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel + 0.001); // avoid rounding issues
2020-03-20 18:12:56 +01:00
2020-03-22 18:33:34 +01:00
// bounds in tile pixels from viewport size
2020-03-20 18:12:56 +01:00
//
var tileBounds = ParentMap.ViewTransform.GetTileMatrixBounds(
TileMatrixScale(tileMatrixZoomLevel), TileMatrixTopLeft, 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-03-22 18:33:34 +01:00
var xMin = (int)Math.Floor(tileBounds.X / TileSize);
var yMin = (int)Math.Floor(tileBounds.Y / TileSize);
var xMax = (int)Math.Floor((tileBounds.X + tileBounds.Width) / TileSize);
var yMax = (int)Math.Floor((tileBounds.Y + tileBounds.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;
}
2017-07-17 21:31:09 +02:00
private void UpdateTiles()
{
var newTiles = new List<Tile>();
2012-04-25 22:02:53 +02:00
if (ParentMap != null && TileMatrix != null && TileSource != null)
2012-04-25 22:02:53 +02:00
{
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
2017-10-07 17:43:28 +02:00
if (maxZoomLevel >= MinZoomLevel)
2017-10-07 17:43:28 +02:00
{
var minZoomLevel = maxZoomLevel;
2020-03-20 18:12:56 +01:00
if (this == ParentMap.MapLayer) // load background tiles
{
minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel);
}
2012-04-25 22:02:53 +02: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++)
2012-04-25 22:02:53 +02:00
{
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
if (tile == null)
{
tile = new Tile(z, x, y);
var equivalentTile = Tiles.FirstOrDefault(
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && t.Image.Source != null);
if (equivalentTile != null)
{
tile.SetImage(equivalentTile.Image.Source, false); // no fade-in animation
}
}
newTiles.Add(tile);
}
}
2012-04-25 22:02:53 +02:00
}
}
}
Tiles = newTiles;
Children.Clear();
foreach (var tile in Tiles)
{
Children.Add(tile.Image);
}
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
2012-04-25 22:02:53 +02:00
}
2020-03-23 17:13:50 +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)
2020-03-23 17:13:50 +01:00
{
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;
2020-03-23 17:13:50 +01:00
tile.Image.Width = tileSize;
tile.Image.Height = tileSize;
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
}
}
return finalSize;
}
2012-04-25 22:02:53 +02:00
}
2012-06-24 23:42:11 +02:00
}