Compare commits

...

3 commits

Author SHA1 Message Date
ClemensFischer f382c59d19 Updated DrawingTile 2025-12-05 15:43:22 +01:00
ClemensFischer 21d7938d33 WPF specific file names 2025-12-05 15:07:44 +01:00
ClemensFischer 172a388841 Constants naming 2025-12-05 15:07:01 +01:00
6 changed files with 112 additions and 80 deletions

View file

@ -23,9 +23,9 @@ namespace MapControl.Caching
/// </summary> /// </summary>
public sealed class FileDbCache : IDistributedCache, IDisposable public sealed class FileDbCache : IDistributedCache, IDisposable
{ {
private const string keyField = "Key"; private const string KeyField = "Key";
private const string valueField = "Value"; private const string ValueField = "Value";
private const string expiresField = "Expires"; private const string ExpiresField = "Expires";
private readonly FileDb fileDb = new FileDb { AutoFlush = true }; private readonly FileDb fileDb = new FileDb { AutoFlush = true };
private readonly Timer timer; private readonly Timer timer;
@ -71,9 +71,9 @@ namespace MapControl.Caching
fileDb.Create(path, new Field[] fileDb.Create(path, new Field[]
{ {
new Field(keyField, DataTypeEnum.String) { IsPrimaryKey = true }, new Field(KeyField, DataTypeEnum.String) { IsPrimaryKey = true },
new Field(valueField, DataTypeEnum.Byte) { IsArray = true }, new Field(ValueField, DataTypeEnum.Byte) { IsArray = true },
new Field(expiresField, DataTypeEnum.DateTime) new Field(ExpiresField, DataTypeEnum.DateTime)
}); });
logger?.LogInformation("Created database {path}", path); logger?.LogInformation("Created database {path}", path);
@ -99,7 +99,7 @@ namespace MapControl.Caching
{ {
try try
{ {
var record = fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false); var record = fileDb.GetRecordByKey(key, new string[] { ValueField, ExpiresField }, false);
if (record != null && (DateTime)record[1] > DateTime.UtcNow) if (record != null && (DateTime)record[1] > DateTime.UtcNow)
{ {
@ -130,8 +130,8 @@ namespace MapControl.Caching
var fieldValues = new FieldValues(3) var fieldValues = new FieldValues(3)
{ {
{ valueField, value }, { ValueField, value },
{ expiresField, expiration } { ExpiresField, expiration }
}; };
try try
@ -142,7 +142,7 @@ namespace MapControl.Caching
} }
else else
{ {
fieldValues.Add(keyField, key); fieldValues.Add(KeyField, key);
fileDb.AddRecord(fieldValues); fileDb.AddRecord(fieldValues);
} }
} }
@ -193,7 +193,7 @@ namespace MapControl.Caching
public void DeleteExpiredItems() public void DeleteExpiredItems()
{ {
var deletedItemsCount = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThanOrEqual)); var deletedItemsCount = fileDb.DeleteRecords(new FilterExpression(ExpiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThanOrEqual));
if (deletedItemsCount > 0) if (deletedItemsCount > 0)
{ {

View file

@ -36,7 +36,7 @@ namespace MapControl
if (equivalentTile != null) if (equivalentTile != null)
{ {
tile.IsPending = false; tile.IsPending = false;
tile.Image.Source = equivalentTile.Image.Source; // no opacity animation tile.Image.Source = equivalentTile.Image.Source; // no Opacity animation
} }
} }

View file

@ -24,9 +24,9 @@ namespace MapControl
/// </summary> /// </summary>
public class MapTileLayer : TilePyramidLayer public class MapTileLayer : TilePyramidLayer
{ {
private const int tileSize = 256; private const int TileSize = 256;
private static readonly Point mapTopLeft = new Point(-180d * MapProjection.Wgs84MeterPerDegree, private static readonly Point MapTopLeft = new Point(-180d * MapProjection.Wgs84MeterPerDegree,
180d * MapProjection.Wgs84MeterPerDegree); 180d * MapProjection.Wgs84MeterPerDegree);
public static readonly DependencyProperty TileSourceProperty = public static readonly DependencyProperty TileSourceProperty =
@ -116,9 +116,9 @@ namespace MapControl
{ {
// Arrange tiles relative to TileMatrix.XMin/YMin. // Arrange tiles relative to TileMatrix.XMin/YMin.
// //
var tileSize = MapTileLayer.tileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel);
var x = tileSize * tile.X - MapTileLayer.tileSize * TileMatrix.XMin; var x = tileSize * tile.X - TileSize * TileMatrix.XMin;
var y = tileSize * tile.Y - MapTileLayer.tileSize * TileMatrix.YMin; var y = tileSize * tile.Y - TileSize * TileMatrix.YMin;
tile.Image.Width = tileSize; tile.Image.Width = tileSize;
tile.Image.Height = tileSize; tile.Image.Height = tileSize;
@ -134,11 +134,11 @@ namespace MapControl
{ {
// Tile matrix origin in pixels. // Tile matrix origin in pixels.
// //
var tileMatrixOrigin = new Point(tileSize * TileMatrix.XMin, tileSize * TileMatrix.YMin); var tileMatrixOrigin = new Point(TileSize * TileMatrix.XMin, TileSize * TileMatrix.YMin);
var tileMatrixScale = MapBase.ZoomLevelToScale(TileMatrix.ZoomLevel); var tileMatrixScale = MapBase.ZoomLevelToScale(TileMatrix.ZoomLevel);
((MatrixTransform)RenderTransform).Matrix = ((MatrixTransform)RenderTransform).Matrix =
ParentMap.ViewTransform.GetTileLayerTransform(tileMatrixScale, mapTopLeft, tileMatrixOrigin); ParentMap.ViewTransform.GetTileLayerTransform(tileMatrixScale, MapTopLeft, tileMatrixOrigin);
} }
} }
@ -178,14 +178,14 @@ namespace MapControl
// Tile matrix bounds in pixels. // Tile matrix bounds in pixels.
// //
var bounds = ParentMap.ViewTransform.GetTileMatrixBounds(tileMatrixScale, mapTopLeft, ParentMap.ActualWidth, ParentMap.ActualHeight); var bounds = ParentMap.ViewTransform.GetTileMatrixBounds(tileMatrixScale, MapTopLeft, ParentMap.ActualWidth, ParentMap.ActualHeight);
// Tile X and Y bounds. // Tile X and Y bounds.
// //
var xMin = (int)Math.Floor(bounds.X / tileSize); var xMin = (int)Math.Floor(bounds.X / TileSize);
var yMin = (int)Math.Floor(bounds.Y / tileSize); var yMin = (int)Math.Floor(bounds.Y / TileSize);
var xMax = (int)Math.Floor((bounds.X + bounds.Width) / tileSize); var xMax = (int)Math.Floor((bounds.X + bounds.Width) / TileSize);
var yMax = (int)Math.Floor((bounds.Y + bounds.Height) / tileSize); var yMax = (int)Math.Floor((bounds.Y + bounds.Height) / TileSize);
if (TileMatrix != null && if (TileMatrix != null &&
TileMatrix.ZoomLevel == tileMatrixZoomLevel && TileMatrix.ZoomLevel == tileMatrixZoomLevel &&

View file

@ -0,0 +1,78 @@
using System;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace MapControl
{
public class DrawingTile : Tile
{
public DrawingTile(int zoomLevel, int x, int y, int columnCount)
: base(zoomLevel, x, y, columnCount)
{
Drawing.Children.Add(ImageDrawing);
}
public DrawingGroup Drawing { get; } = new DrawingGroup();
public ImageDrawing ImageDrawing { get; } = new ImageDrawing();
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
{
var image = await loadImageFunc().ConfigureAwait(false);
void SetImageSource()
{
ImageDrawing.ImageSource = image;
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
{
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
{
bitmap.DownloadCompleted += BitmapDownloadCompleted;
bitmap.DownloadFailed += BitmapDownloadFailed;
}
else
{
BeginFadeInAnimation();
}
}
}
await Drawing.Dispatcher.InvokeAsync(SetImageSource);
}
private void BeginFadeInAnimation()
{
var fadeInAnimation = new DoubleAnimation
{
From = 0d,
Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop
};
Drawing.BeginAnimation(DrawingGroup.OpacityProperty, fadeInAnimation);
}
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
var bitmap = (BitmapSource)sender;
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
BeginFadeInAnimation();
}
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
var bitmap = (BitmapSource)sender;
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
ImageDrawing.ImageSource = null;
}
}
}

View file

@ -1,68 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MapControl namespace MapControl
{ {
public class ImageDrawingTile : Tile
{
private readonly ImageDrawing imageDrawing = new ImageDrawing();
public ImageDrawingTile(int zoomLevel, int x, int y, int columnCount)
: base(zoomLevel, x, y, columnCount)
{
Drawing.Children.Add(imageDrawing);
}
public DrawingGroup Drawing { get; } = new DrawingGroup();
public ImageSource ImageSource
{
get => imageDrawing.ImageSource;
set => imageDrawing.ImageSource = value;
}
public void SetRect(int xMin, int yMin, int tileWidth, int tileHeight)
{
imageDrawing.Rect = new Rect(tileWidth * (X - xMin), tileHeight * (Y - yMin), tileWidth, tileHeight);
}
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
{
var image = await loadImageFunc().ConfigureAwait(false);
void SetImageSource()
{
imageDrawing.ImageSource = image;
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
{
var fadeInAnimation = new DoubleAnimation
{
From = 0d,
Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop
};
Drawing.BeginAnimation(DrawingGroup.OpacityProperty, fadeInAnimation);
}
}
await Drawing.Dispatcher.InvokeAsync(SetImageSource);
}
}
public class DrawingTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel) : UIElement public class DrawingTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel) : UIElement
{ {
public WmtsTileMatrix WmtsTileMatrix => wmtsTileMatrix; public WmtsTileMatrix WmtsTileMatrix => wmtsTileMatrix;
public TileMatrix TileMatrix { get; private set; } = new TileMatrix(zoomLevel, 1, 1, 0, 0); public TileMatrix TileMatrix { get; private set; } = new TileMatrix(zoomLevel, 1, 1, 0, 0);
public IEnumerable<ImageDrawingTile> Tiles { get; private set; } = []; public IEnumerable<DrawingTile> Tiles { get; private set; } = [];
public DrawingGroup Drawing { get; } = new DrawingGroup { Transform = new MatrixTransform() }; public DrawingGroup Drawing { get; } = new DrawingGroup { Transform = new MatrixTransform() };
@ -125,7 +75,7 @@ namespace MapControl
private void CreateTiles() private void CreateTiles()
{ {
var tileCount = TileMatrix.Width * TileMatrix.Height; var tileCount = TileMatrix.Width * TileMatrix.Height;
var tiles = new List<ImageDrawingTile>(tileCount); var tiles = new List<DrawingTile>(tileCount);
var drawings = new DrawingCollection(tileCount); var drawings = new DrawingCollection(tileCount);
for (var y = TileMatrix.YMin; y <= TileMatrix.YMax; y++) for (var y = TileMatrix.YMin; y <= TileMatrix.YMax; y++)
@ -136,18 +86,22 @@ namespace MapControl
if (tile == null) if (tile == null)
{ {
tile = new ImageDrawingTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth); tile = new DrawingTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth);
var equivalentTile = Tiles.FirstOrDefault(t => t.ImageSource != null && t.Column == tile.Column && t.Row == tile.Row); var equivalentTile = Tiles.FirstOrDefault(t => t.ImageDrawing.ImageSource != null && t.Column == tile.Column && t.Row == tile.Row);
if (equivalentTile != null) if (equivalentTile != null)
{ {
tile.IsPending = false; tile.IsPending = false;
tile.ImageSource = equivalentTile.ImageSource; tile.ImageDrawing.ImageSource = equivalentTile.ImageDrawing.ImageSource; // no Opacity animation
} }
} }
tile.SetRect(TileMatrix.XMin, TileMatrix.YMin, WmtsTileMatrix.TileWidth, WmtsTileMatrix.TileHeight); tile.ImageDrawing.Rect = new Rect(
WmtsTileMatrix.TileWidth * (x - TileMatrix.XMin),
WmtsTileMatrix.TileHeight * (y - TileMatrix.YMin),
WmtsTileMatrix.TileWidth,
WmtsTileMatrix.TileHeight);
tiles.Add(tile); tiles.Add(tile);
drawings.Add(tile.Drawing); drawings.Add(tile.Drawing);