From 24dabf046da999ddd1f7266482816784e7f386e7 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Wed, 19 Nov 2025 17:49:49 +0100 Subject: [PATCH] Minor improvements --- MapControl/Shared/MapPanel.cs | 4 +- MapControl/Shared/MapTileLayer.cs | 74 +++++++++++------------- MapControl/Shared/WmtsTileMatrixLayer.cs | 6 +- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index e2e69c9c..ca7348ba 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -177,7 +177,7 @@ namespace MapControl { availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); - foreach (var element in Children.OfType()) + foreach (var element in Children.Cast()) { element.Measure(availableSize); } @@ -189,7 +189,7 @@ namespace MapControl { if (parentMap != null) { - foreach (var element in Children.OfType()) + foreach (var element in Children.Cast()) { ArrangeChildElement(element, finalSize); } diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 7f7b98e2..f3961ac5 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -99,20 +99,17 @@ namespace MapControl 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. - // - var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); - var x = tileSize * tile.X - TileSize * TileMatrix.XMin; - var y = tileSize * tile.Y - TileSize * TileMatrix.YMin; + // Arrange tiles relative to TileMatrix.XMin/YMin. + // + var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); + var x = tileSize * tile.X - TileSize * TileMatrix.XMin; + var y = tileSize * tile.Y - TileSize * TileMatrix.YMin; - tile.Image.Width = tileSize; - tile.Image.Height = tileSize; - tile.Image.Arrange(new Rect(x, y, tileSize, tileSize)); - } + tile.Image.Width = tileSize; + tile.Image.Height = tileSize; + tile.Image.Arrange(new Rect(x, y, tileSize, tileSize)); } return finalSize; @@ -122,9 +119,10 @@ namespace MapControl { if (ParentMap == null || !SupportedCrsIds.Contains(ParentMap.MapProjection.CrsId)) { - TileMatrix = null; - Children.Clear(); CancelLoadTiles(); + Children.Clear(); + Tiles.Clear(); + TileMatrix = null; } else if (SetTileMatrix() || reset) { @@ -183,39 +181,37 @@ namespace MapControl { 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) - { - 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.FillMatrix(Tiles, zoomLevel, xMin, yMin, xMax, yMax, tileCount); } } Tiles = tiles; + Children.Clear(); foreach (var tile in tiles) diff --git a/MapControl/Shared/WmtsTileMatrixLayer.cs b/MapControl/Shared/WmtsTileMatrixLayer.cs index d2279e0a..9b750f8b 100644 --- a/MapControl/Shared/WmtsTileMatrixLayer.cs +++ b/MapControl/Shared/WmtsTileMatrixLayer.cs @@ -23,11 +23,11 @@ namespace MapControl { // 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()); - WmtsTileMatrix = tileMatrix; + WmtsTileMatrix = wmtsTileMatrix; TileMatrix = new TileMatrix(zoomLevel, 1, 1, 0, 0); } @@ -87,8 +87,8 @@ namespace MapControl var tiles = new ImageTileList(); tiles.FillMatrix(Tiles, TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax, WmtsTileMatrix.MatrixWidth); - Tiles = tiles; + Children.Clear(); foreach (var tile in tiles)