mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-06 06:55:04 +00:00
WPF specific file names
This commit is contained in:
parent
172a388841
commit
21d7938d33
4 changed files with 95 additions and 55 deletions
181
MapControl/WPF/BitmapTileMatrixLayer.WPF.cs
Normal file
181
MapControl/WPF/BitmapTileMatrixLayer.WPF.cs
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
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 ImageBrush imageBrush = new ImageBrush
|
||||
{
|
||||
ViewportUnits = BrushMappingMode.Absolute,
|
||||
Transform = new MatrixTransform()
|
||||
};
|
||||
|
||||
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.DrawRectangle(imageBrush, null, new Rect(RenderSize));
|
||||
}
|
||||
|
||||
public void UpdateRenderTransform(ViewTransform viewTransform)
|
||||
{
|
||||
// Tile matrix origin in pixels.
|
||||
//
|
||||
var tileMatrixOrigin = new Point(WmtsTileMatrix.TileWidth * TileMatrix.XMin, WmtsTileMatrix.TileHeight * TileMatrix.YMin);
|
||||
|
||||
((MatrixTransform)imageBrush.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);
|
||||
|
||||
CreateBitmap();
|
||||
CreateTiles();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CreateBitmap()
|
||||
{
|
||||
var width = WmtsTileMatrix.TileWidth * TileMatrix.Width;
|
||||
var height = WmtsTileMatrix.TileHeight * TileMatrix.Height;
|
||||
|
||||
imageBrush.ImageSource = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
|
||||
imageBrush.Viewport = new Rect(0, 0, width, height);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
((WriteableBitmap)imageBrush.ImageSource).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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue