TileCollection

This commit is contained in:
ClemensFischer 2025-08-22 16:10:03 +02:00
parent 2d97f1f2c7
commit 6742149599
3 changed files with 27 additions and 36 deletions

View file

@ -199,24 +199,17 @@ namespace MapControl
{ {
var numTiles = 1 << z; var numTiles = 1 << z;
var tileSize = 1 << (TileMatrix.ZoomLevel - z); var tileSize = 1 << (TileMatrix.ZoomLevel - z);
var x1 = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative var xMin = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative
var x2 = TileMatrix.XMax / tileSize; // may be greater than numTiles-1 var xMax = TileMatrix.XMax / tileSize; // may be greater than numTiles-1
var y1 = Math.Max(TileMatrix.YMin / tileSize, 0); var yMin = Math.Max(TileMatrix.YMin / tileSize, 0);
var y2 = Math.Min(TileMatrix.YMax / tileSize, numTiles - 1); var yMax = Math.Min(TileMatrix.YMax / tileSize, numTiles - 1);
for (var y = y1; y <= y2; y++) tiles.FillMatrix(Tiles, z, xMin, yMin, xMax, yMax, numTiles);
{
for (var x = x1; x <= x2; x++)
{
tiles.Add(Tiles.GetTile(z, x, y, numTiles));
}
}
} }
} }
} }
Tiles = tiles; Tiles = tiles;
Children.Clear(); Children.Clear();
foreach (var tile in tiles) foreach (var tile in tiles)

View file

@ -6,17 +6,21 @@ namespace MapControl
public class TileCollection : List<Tile> public class TileCollection : List<Tile>
{ {
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
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); for (var y = yMin; y <= yMax; y++)
{
for (var x = xMin; x <= xMax; x++)
{
var tile = source.FirstOrDefault(t => t.ZoomLevel == zoomLevel && t.X == x && t.Y == y);
if (tile == null) if (tile == null)
{ {
tile = new Tile(zoomLevel, x, y, columnCount); tile = new Tile(zoomLevel, x, y, columnCount);
var equivalentTile = this.FirstOrDefault( var equivalentTile = source.FirstOrDefault(
t => t.Image.Source != null && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row); t => t.Image.Source != null && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row);
if (equivalentTile != null) if (equivalentTile != null)
@ -26,7 +30,9 @@ namespace MapControl
} }
} }
return tile; Add(tile);
}
}
} }
} }
} }

View file

@ -86,17 +86,9 @@ namespace MapControl
TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax); TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax);
var tiles = new TileCollection(); var tiles = new TileCollection();
tiles.FillMatrix(Tiles, TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax, WmtsTileMatrix.MatrixWidth);
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 = tiles; Tiles = tiles;
Children.Clear(); Children.Clear();
foreach (var tile in tiles) foreach (var tile in tiles)