mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-01-01 22:30:14 +01:00
175 lines
6.2 KiB
C#
175 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace MapControl
|
|
{
|
|
public class BitmapTile(int zoomLevel, int x, int y, int columnCount, int width, int height)
|
|
: Tile(zoomLevel, x, y, columnCount)
|
|
{
|
|
public event EventHandler Completed;
|
|
|
|
public byte[] PixelBuffer { get; set; }
|
|
|
|
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
|
{
|
|
var image = await loadImageFunc().ConfigureAwait(false);
|
|
|
|
if (image is BitmapSource bitmap)
|
|
{
|
|
if (bitmap.Format != PixelFormats.Pbgra32)
|
|
{
|
|
bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Pbgra32, null, 0d);
|
|
}
|
|
|
|
PixelBuffer = new byte[4 * width * height];
|
|
bitmap.CopyPixels(PixelBuffer, 4 * width, 0);
|
|
}
|
|
|
|
Completed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public class BitmapTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel) : UIElement
|
|
{
|
|
private readonly MatrixTransform transform = new MatrixTransform();
|
|
|
|
private WriteableBitmap bitmap;
|
|
|
|
public WmtsTileMatrix WmtsTileMatrix => wmtsTileMatrix;
|
|
|
|
public TileMatrix TileMatrix { get; private set; } = new TileMatrix(zoomLevel, 1, 1, 0, 0);
|
|
|
|
public IEnumerable<BitmapTile> Tiles { get; private set; } = [];
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
{
|
|
drawingContext.PushTransform(transform);
|
|
drawingContext.DrawImage(bitmap, new Rect(0d, 0d, bitmap.PixelWidth, bitmap.PixelHeight));
|
|
}
|
|
|
|
public void UpdateRenderTransform(ViewTransform viewTransform)
|
|
{
|
|
// Tile matrix origin in pixels.
|
|
//
|
|
var tileMatrixOrigin = new Point(WmtsTileMatrix.TileWidth * TileMatrix.XMin, WmtsTileMatrix.TileHeight * TileMatrix.YMin);
|
|
|
|
transform.Matrix = viewTransform.GetTileLayerTransform(WmtsTileMatrix.Scale, WmtsTileMatrix.TopLeft, tileMatrixOrigin);
|
|
}
|
|
|
|
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)
|
|
{
|
|
// Set X range limits.
|
|
//
|
|
xMin = Math.Max(xMin, 0);
|
|
xMax = Math.Min(Math.Max(xMax, 0), WmtsTileMatrix.MatrixWidth - 1);
|
|
}
|
|
|
|
// Set Y range limits.
|
|
//
|
|
yMin = Math.Max(yMin, 0);
|
|
yMax = Math.Min(Math.Max(yMax, 0), WmtsTileMatrix.MatrixHeight - 1);
|
|
|
|
if (TileMatrix.XMin == xMin && TileMatrix.YMin == yMin &&
|
|
TileMatrix.XMax == xMax && TileMatrix.YMax == yMax)
|
|
{
|
|
// No change of the TileMatrix and the Tiles collection.
|
|
//
|
|
return false;
|
|
}
|
|
|
|
TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax);
|
|
|
|
bitmap = new WriteableBitmap(
|
|
WmtsTileMatrix.TileWidth * TileMatrix.Width,
|
|
WmtsTileMatrix.TileHeight * TileMatrix.Height,
|
|
96, 96, PixelFormats.Pbgra32, null);
|
|
|
|
CreateTiles();
|
|
InvalidateVisual();
|
|
|
|
return true;
|
|
}
|
|
|
|
private void CreateTiles()
|
|
{
|
|
var tiles = new List<BitmapTile>(TileMatrix.Width * TileMatrix.Height);
|
|
|
|
for (var y = TileMatrix.YMin; y <= TileMatrix.YMax; y++)
|
|
{
|
|
for (var x = TileMatrix.XMin; x <= TileMatrix.XMax; x++)
|
|
{
|
|
var tile = Tiles.FirstOrDefault(t => t.X == x && t.Y == y);
|
|
|
|
if (tile == null)
|
|
{
|
|
tile = new BitmapTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth, WmtsTileMatrix.TileWidth, WmtsTileMatrix.TileHeight);
|
|
|
|
var equivalentTile = Tiles.FirstOrDefault(t => t.PixelBuffer != null && t.Column == tile.Column && t.Row == tile.Row);
|
|
|
|
if (equivalentTile != null)
|
|
{
|
|
tile.IsPending = false;
|
|
tile.PixelBuffer = equivalentTile.PixelBuffer;
|
|
}
|
|
else
|
|
{
|
|
tile.Completed += OnTileCompleted;
|
|
}
|
|
}
|
|
|
|
if (tile.PixelBuffer != null)
|
|
{
|
|
CopyTile(tile);
|
|
}
|
|
|
|
tiles.Add(tile);
|
|
}
|
|
}
|
|
|
|
Tiles = tiles;
|
|
}
|
|
|
|
private void CopyTile(BitmapTile tile)
|
|
{
|
|
var width = WmtsTileMatrix.TileWidth;
|
|
var height = WmtsTileMatrix.TileHeight;
|
|
var x = width * (tile.X - TileMatrix.XMin);
|
|
var y = height * (tile.Y - TileMatrix.YMin);
|
|
|
|
bitmap.WritePixels(new Int32Rect(x, y, width, height), tile.PixelBuffer, 4 * WmtsTileMatrix.TileWidth, 0);
|
|
}
|
|
|
|
private void OnTileCompleted(object sender, EventArgs e)
|
|
{
|
|
var tile = (BitmapTile)sender;
|
|
|
|
tile.Completed -= OnTileCompleted;
|
|
|
|
if (tile.X >= TileMatrix.XMin && tile.X <= TileMatrix.XMax &&
|
|
tile.Y >= TileMatrix.YMin && tile.Y <= TileMatrix.YMax &&
|
|
tile.PixelBuffer != null)
|
|
{
|
|
_ = Dispatcher.InvokeAsync(() => CopyTile(tile));
|
|
}
|
|
}
|
|
}
|
|
}
|