Compare commits

...

2 commits

Author SHA1 Message Date
ClemensFischer 0f3635b97a Comments 2025-11-27 23:18:57 +01:00
ClemensFischer ec99fc0cf7 Tile layer background levels 2025-11-27 20:06:48 +01:00
3 changed files with 39 additions and 34 deletions

View file

@ -190,9 +190,13 @@ namespace MapControl
if (maxZoomLevel >= MinZoomLevel) if (maxZoomLevel >= MinZoomLevel)
{ {
var minZoomLevel = IsBaseMapLayer var minZoomLevel = maxZoomLevel;
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
: maxZoomLevel; if (IsBaseMapLayer)
{
var bgLevels = Math.Max(MaxBackgroundLevels, 0);
minZoomLevel = Math.Max(TileMatrix.ZoomLevel - bgLevels, MinZoomLevel);
}
for (var zoomLevel = minZoomLevel; zoomLevel <= maxZoomLevel; zoomLevel++) for (var zoomLevel = minZoomLevel; zoomLevel <= maxZoomLevel; zoomLevel++)
{ {

View file

@ -143,47 +143,50 @@ namespace MapControl
private bool UpdateChildLayers(WmtsTileMatrixSet tileMatrixSet) private bool UpdateChildLayers(WmtsTileMatrixSet tileMatrixSet)
{ {
// Multiply scale by 1.001 to avoid rounding issues. // Multiply scale by 1.001 to avoid floating point precision issues
// and get all WmtsTileMatrixes with Scale <= maxScale.
// //
var maxScale = 1.001 * ParentMap.ViewTransform.Scale; var maxScale = 1.001 * ParentMap.ViewTransform.Scale;
var tileMatrixes = tileMatrixSet.TileMatrixes.Where(matrix => matrix.Scale <= maxScale).ToList();
// Show all WmtsTileMatrix layers with Scale <= maxScale, at least the first layer. var childLayers = ChildLayers.Where(layer => tileMatrixes.Contains(layer.WmtsTileMatrix)).ToList();
//
var currentMatrixes = tileMatrixSet.TileMatrixes
.Where((matrix, i) => i == 0 || matrix.Scale <= maxScale)
.ToList();
if (!IsBaseMapLayer)
{
// Show only the last layer.
//
currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - 1).ToList();
}
else if (currentMatrixes.Count > MaxBackgroundLevels + 1)
{
// Show not more than MaxBackgroundLevels + 1 layers.
//
currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - MaxBackgroundLevels - 1).ToList();
}
var currentLayers = ChildLayers.Where(layer => currentMatrixes.Contains(layer.WmtsTileMatrix)).ToList();
var tilesChanged = false; var tilesChanged = false;
Children.Clear(); Children.Clear();
foreach (var tileMatrix in currentMatrixes) if (tileMatrixes.Count > 0)
{ {
var layer = currentLayers.FirstOrDefault(layer => layer.WmtsTileMatrix == tileMatrix) ?? var maxLayers = Math.Max(MaxBackgroundLevels, 0) + 1;
new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.TileMatrixes.IndexOf(tileMatrix));
if (layer.UpdateTiles(ParentMap.ViewTransform, ParentMap.ActualWidth, ParentMap.ActualHeight)) if (!IsBaseMapLayer)
{ {
tilesChanged = true; // 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);
} }
layer.UpdateRenderTransform(ParentMap.ViewTransform); 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));
Children.Add(layer); if (layer.UpdateTiles(ParentMap.ViewTransform, ParentMap.ActualWidth, ParentMap.ActualHeight))
{
tilesChanged = true;
}
layer.UpdateRenderTransform(ParentMap.ViewTransform);
Children.Add(layer);
}
} }
return tilesChanged; return tilesChanged;

View file

@ -22,8 +22,6 @@ namespace MapControl
{ {
public class WmtsTileMatrixLayer : Panel public class WmtsTileMatrixLayer : Panel
{ {
// zoomLevel is index of tileMatrix in a WmtsTileMatrixSet.TileMatrixes list.
//
public WmtsTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel) public WmtsTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel)
{ {
this.SetRenderTransform(new MatrixTransform()); this.SetRenderTransform(new MatrixTransform());