From 6742149599d0545dfe619e0a3e7f43c470eb298c Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 22 Aug 2025 16:10:03 +0200 Subject: [PATCH] TileCollection --- MapControl/Shared/MapTileLayer.cs | 17 ++++------- MapControl/Shared/TileCollection.cs | 36 ++++++++++++++---------- MapControl/Shared/WmtsTileMatrixLayer.cs | 10 +------ 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 0e912e89..173b0abb 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -199,24 +199,17 @@ namespace MapControl { var numTiles = 1 << z; var tileSize = 1 << (TileMatrix.ZoomLevel - z); - var x1 = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative - var x2 = TileMatrix.XMax / tileSize; // may be greater than numTiles-1 - var y1 = Math.Max(TileMatrix.YMin / tileSize, 0); - var y2 = Math.Min(TileMatrix.YMax / tileSize, numTiles - 1); + var xMin = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative + var xMax = TileMatrix.XMax / tileSize; // may be greater than numTiles-1 + var yMin = Math.Max(TileMatrix.YMin / tileSize, 0); + var yMax = Math.Min(TileMatrix.YMax / tileSize, numTiles - 1); - for (var y = y1; y <= y2; y++) - { - for (var x = x1; x <= x2; x++) - { - tiles.Add(Tiles.GetTile(z, x, y, numTiles)); - } - } + tiles.FillMatrix(Tiles, z, xMin, yMin, xMax, yMax, numTiles); } } } Tiles = tiles; - Children.Clear(); foreach (var tile in tiles) diff --git a/MapControl/Shared/TileCollection.cs b/MapControl/Shared/TileCollection.cs index e65a38a7..d13d4935 100644 --- a/MapControl/Shared/TileCollection.cs +++ b/MapControl/Shared/TileCollection.cs @@ -6,27 +6,33 @@ namespace MapControl public class TileCollection : List { /// - /// Get a matching Tile from a TileCollection or create a new one. + /// Adds existing Tiles from the source collection or newly created Tiles to fill the specified tile matrix. /// - public Tile GetTile(int zoomLevel, int x, int y, int columnCount) + public void FillMatrix(TileCollection source, int zoomLevel, int xMin, int yMin, int xMax, int yMax, int columnCount) { - var tile = this.FirstOrDefault(t => t.ZoomLevel == zoomLevel && t.X == x && t.Y == y); - - if (tile == null) + for (var y = yMin; y <= yMax; y++) { - tile = new Tile(zoomLevel, x, y, columnCount); - - var equivalentTile = this.FirstOrDefault( - t => t.Image.Source != null && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row); - - if (equivalentTile != null) + for (var x = xMin; x <= xMax; x++) { - tile.IsPending = false; - tile.Image.Source = equivalentTile.Image.Source; // no opacity animation + var tile = source.FirstOrDefault(t => t.ZoomLevel == zoomLevel && t.X == x && t.Y == y); + + if (tile == null) + { + tile = new Tile(zoomLevel, x, y, columnCount); + + var equivalentTile = source.FirstOrDefault( + t => t.Image.Source != null && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row); + + if (equivalentTile != null) + { + tile.IsPending = false; + tile.Image.Source = equivalentTile.Image.Source; // no opacity animation + } + } + + Add(tile); } } - - return tile; } } } diff --git a/MapControl/Shared/WmtsTileMatrixLayer.cs b/MapControl/Shared/WmtsTileMatrixLayer.cs index 971e6e5b..e9199302 100644 --- a/MapControl/Shared/WmtsTileMatrixLayer.cs +++ b/MapControl/Shared/WmtsTileMatrixLayer.cs @@ -86,17 +86,9 @@ namespace MapControl TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax); var tiles = new TileCollection(); - - for (var y = yMin; y <= yMax; y++) - { - for (var x = xMin; x <= xMax; x++) - { - tiles.Add(Tiles.GetTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth)); - } - } + tiles.FillMatrix(Tiles, TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax, WmtsTileMatrix.MatrixWidth); Tiles = tiles; - Children.Clear(); foreach (var tile in tiles)