Minor improvements

This commit is contained in:
ClemensFischer 2025-11-19 17:49:49 +01:00
parent e0eb50b7da
commit 24dabf046d
3 changed files with 40 additions and 44 deletions

View file

@ -177,7 +177,7 @@ namespace MapControl
{ {
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (var element in Children.OfType<FrameworkElement>()) foreach (var element in Children.Cast<FrameworkElement>())
{ {
element.Measure(availableSize); element.Measure(availableSize);
} }
@ -189,7 +189,7 @@ namespace MapControl
{ {
if (parentMap != null) if (parentMap != null)
{ {
foreach (var element in Children.OfType<FrameworkElement>()) foreach (var element in Children.Cast<FrameworkElement>())
{ {
ArrangeChildElement(element, finalSize); ArrangeChildElement(element, finalSize);
} }

View file

@ -99,20 +99,17 @@ namespace MapControl
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
if (TileMatrix != null) foreach (var tile in Tiles)
{ {
foreach (var tile in Tiles) // Arrange tiles relative to TileMatrix.XMin/YMin.
{ //
// Arrange tiles relative to TileMatrix.XMin/YMin. var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel);
// var x = tileSize * tile.X - TileSize * TileMatrix.XMin;
var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); var y = tileSize * tile.Y - TileSize * TileMatrix.YMin;
var x = tileSize * tile.X - TileSize * TileMatrix.XMin;
var y = tileSize * tile.Y - TileSize * TileMatrix.YMin;
tile.Image.Width = tileSize; tile.Image.Width = tileSize;
tile.Image.Height = tileSize; tile.Image.Height = tileSize;
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize)); tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
}
} }
return finalSize; return finalSize;
@ -122,9 +119,10 @@ namespace MapControl
{ {
if (ParentMap == null || !SupportedCrsIds.Contains(ParentMap.MapProjection.CrsId)) if (ParentMap == null || !SupportedCrsIds.Contains(ParentMap.MapProjection.CrsId))
{ {
TileMatrix = null;
Children.Clear();
CancelLoadTiles(); CancelLoadTiles();
Children.Clear();
Tiles.Clear();
TileMatrix = null;
} }
else if (SetTileMatrix() || reset) else if (SetTileMatrix() || reset)
{ {
@ -183,39 +181,37 @@ namespace MapControl
{ {
var tiles = new ImageTileList(); var tiles = new ImageTileList();
if (TileSource != null && TileMatrix != null) if (reset)
{ {
if (reset) Tiles.Clear();
}
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
if (maxZoomLevel >= MinZoomLevel)
{
var minZoomLevel = IsBaseMapLayer
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
: maxZoomLevel;
for (var zoomLevel = minZoomLevel; zoomLevel <= maxZoomLevel; zoomLevel++)
{ {
Tiles.Clear(); var tileCount = 1 << zoomLevel; // per row and column
}
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel); // Right-shift divides with rounding down also negative values, https://stackoverflow.com/q/55196178
//
var shift = TileMatrix.ZoomLevel - zoomLevel;
var xMin = TileMatrix.XMin >> shift; // may be < 0
var xMax = TileMatrix.XMax >> shift; // may be >= tileCount
var yMin = Math.Max(TileMatrix.YMin >> shift, 0);
var yMax = Math.Min(TileMatrix.YMax >> shift, tileCount - 1);
if (maxZoomLevel >= MinZoomLevel) tiles.FillMatrix(Tiles, zoomLevel, xMin, yMin, xMax, yMax, tileCount);
{
var minZoomLevel = IsBaseMapLayer
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
: maxZoomLevel;
for (var zoomLevel = minZoomLevel; zoomLevel <= maxZoomLevel; zoomLevel++)
{
var tileCount = 1 << zoomLevel; // per row and column
// Right-shift divides with rounding down also negative values, https://stackoverflow.com/q/55196178
//
var shift = TileMatrix.ZoomLevel - zoomLevel;
var xMin = TileMatrix.XMin >> shift; // may be < 0
var xMax = TileMatrix.XMax >> shift; // may be >= tileCount
var yMin = Math.Max(TileMatrix.YMin >> shift, 0);
var yMax = Math.Min(TileMatrix.YMax >> shift, tileCount - 1);
tiles.FillMatrix(Tiles, zoomLevel, xMin, yMin, xMax, yMax, tileCount);
}
} }
} }
Tiles = tiles; Tiles = tiles;
Children.Clear(); Children.Clear();
foreach (var tile in tiles) foreach (var tile in tiles)

View file

@ -23,11 +23,11 @@ namespace MapControl
{ {
// zoomLevel is index of tileMatrix in a WmtsTileMatrixSet.TileMatrixes list. // zoomLevel is index of tileMatrix in a WmtsTileMatrixSet.TileMatrixes list.
// //
public WmtsTileMatrixLayer(WmtsTileMatrix tileMatrix, int zoomLevel) public WmtsTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel)
{ {
MapPanel.SetRenderTransform(this, new MatrixTransform()); MapPanel.SetRenderTransform(this, new MatrixTransform());
WmtsTileMatrix = tileMatrix; WmtsTileMatrix = wmtsTileMatrix;
TileMatrix = new TileMatrix(zoomLevel, 1, 1, 0, 0); TileMatrix = new TileMatrix(zoomLevel, 1, 1, 0, 0);
} }
@ -87,8 +87,8 @@ namespace MapControl
var tiles = new ImageTileList(); var tiles = new ImageTileList();
tiles.FillMatrix(Tiles, TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax, WmtsTileMatrix.MatrixWidth); tiles.FillMatrix(Tiles, TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax, WmtsTileMatrix.MatrixWidth);
Tiles = tiles; Tiles = tiles;
Children.Clear(); Children.Clear();
foreach (var tile in tiles) foreach (var tile in tiles)