From 733c3d12663f1796c8d65ce457dcb9488e65d640 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 28 Nov 2025 21:59:25 +0100 Subject: [PATCH] Updated WmtsTileLayer --- MapControl/Shared/MapTileLayer.cs | 2 +- MapControl/Shared/WmtsTileLayer.cs | 63 ++++++++++++++++-------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 6144b204..35f3b369 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -148,7 +148,7 @@ namespace MapControl private bool SetTileMatrix() { - // Add 0.001 to avoid rounding issues. + // Add 0.001 to avoid floating point precision. // var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel - ZoomLevelOffset + 0.001); var tileMatrixScale = MapBase.ZoomLevelToScale(tileMatrixZoomLevel); diff --git a/MapControl/Shared/WmtsTileLayer.cs b/MapControl/Shared/WmtsTileLayer.cs index 58fe25e2..fd8d7fe1 100644 --- a/MapControl/Shared/WmtsTileLayer.cs +++ b/MapControl/Shared/WmtsTileLayer.cs @@ -148,45 +148,50 @@ namespace MapControl // var maxScale = 1.001 * ParentMap.ViewTransform.Scale; var tileMatrixes = tileMatrixSet.TileMatrixes.Where(matrix => matrix.Scale <= maxScale).ToList(); - var childLayers = ChildLayers.Where(layer => tileMatrixes.Contains(layer.WmtsTileMatrix)).ToList(); + + if (tileMatrixes.Count == 0) + { + Children.Clear(); + return false; + } + + var maxLayers = Math.Max(MaxBackgroundLevels, 0) + 1; + + if (!IsBaseMapLayer) + { + // Show only the last layer. + // + tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - 1, 1); + } + else if (tileMatrixes.Count > maxLayers) + { + // Show not more than MaxBackgroundLevels + 1 layers. + // + tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - maxLayers, maxLayers); + } + + // Get reusable layers. + // + var layers = ChildLayers.Where(layer => tileMatrixes.Contains(layer.WmtsTileMatrix)).ToList(); var tilesChanged = false; Children.Clear(); - if (tileMatrixes.Count > 0) + foreach (var tileMatrix in tileMatrixes) { - var maxLayers = Math.Max(MaxBackgroundLevels, 0) + 1; + // Pass index of tileMatrix in tileMatrixSet.TileMatrixes as zoom level to WmtsTileMatrixLayer ctor. + // + var layer = layers.FirstOrDefault(layer => layer.WmtsTileMatrix == tileMatrix) ?? + new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.TileMatrixes.IndexOf(tileMatrix)); - if (!IsBaseMapLayer) + if (layer.UpdateTiles(ParentMap.ViewTransform, ParentMap.ActualWidth, ParentMap.ActualHeight)) { - // Show only the last layer. - // - tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - 1, 1); - } - else if (tileMatrixes.Count > maxLayers) - { - // Show not more than MaxBackgroundLevels + 1 layers. - // - tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - maxLayers, maxLayers); + tilesChanged = true; } - foreach (var tileMatrix in tileMatrixes) - { - // Reuse existing WmtsTileMatrixLayer or create a new one with the - // index of tileMatrix in tileMatrixSet.TileMatrixes as zoom level. - // - var layer = childLayers.FirstOrDefault(layer => layer.WmtsTileMatrix == tileMatrix) ?? - new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.TileMatrixes.IndexOf(tileMatrix)); + layer.UpdateRenderTransform(ParentMap.ViewTransform); - if (layer.UpdateTiles(ParentMap.ViewTransform, ParentMap.ActualWidth, ParentMap.ActualHeight)) - { - tilesChanged = true; - } - - layer.UpdateRenderTransform(ParentMap.ViewTransform); - - Children.Add(layer); - } + Children.Add(layer); } return tilesChanged;