Compare commits

...

2 commits

Author SHA1 Message Date
ClemensFischer e14c9e4102 Updated UriTileSource 2025-12-02 16:41:20 +01:00
ClemensFischer 1222a4a8c2 Updated TileSources 2025-12-02 15:29:45 +01:00
5 changed files with 66 additions and 147 deletions

View file

@ -1,62 +0,0 @@
using System;
using System.Globalization;
using System.Text;
namespace MapControl
{
public class BoundingBoxTileSource : UriTileSource
{
public override Uri GetUri(int zoomLevel, int column, int row)
{
GetTileBounds(zoomLevel, column, row, out double west, out double south, out double east, out double north);
return GetUri(west, south, east, north);
}
protected virtual Uri GetUri(double west, double south, double east, double north)
{
Uri uri = null;
if (UriTemplate != null)
{
var w = west.ToString("F2", CultureInfo.InvariantCulture);
var e = east.ToString("F2", CultureInfo.InvariantCulture);
var s = south.ToString("F2", CultureInfo.InvariantCulture);
var n = north.ToString("F2", CultureInfo.InvariantCulture);
if (UriTemplate.Contains("{bbox}"))
{
uri = new Uri(UriTemplate.Replace("{bbox}", $"{w},{s},{e},{n}"));
}
else
{
var uriBuilder = new StringBuilder(UriTemplate);
uriBuilder.Replace("{west}", w);
uriBuilder.Replace("{south}", s);
uriBuilder.Replace("{east}", e);
uriBuilder.Replace("{north}", n);
uri = new Uri(uriBuilder.ToString());
}
}
return uri;
}
/// <summary>
/// Gets the bounding box in meters of a standard Web Mercator tile,
/// specified by zoom level and grid column and row indices.
/// </summary>
public static void GetTileBounds(int zoomLevel, int column, int row,
out double west, out double south, out double east, out double north)
{
var tileSize = 360d / (1 << zoomLevel); // tile size in degrees
west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d);
east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d);
south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize);
north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Text;
using System.Threading.Tasks;
#if WPF
using System.Windows.Media;
@ -65,64 +64,4 @@ namespace MapControl
return new UriTileSource { UriTemplate = uriTemplate };
}
}
public class UriTileSource : TileSource
{
private string uriTemplate;
/// <summary>
/// Gets or sets the template string for tile request Uris.
/// </summary>
public string UriTemplate
{
get => uriTemplate;
set
{
uriTemplate = value;
if (uriTemplate != null && uriTemplate.Contains("{s}") && Subdomains == null)
{
Subdomains = ["a", "b", "c"]; // default OpenStreetMap subdomains
}
}
}
public string[] Subdomains { get; set; }
public override Uri GetUri(int zoomLevel, int column, int row)
{
Uri uri = null;
if (UriTemplate != null)
{
var uriBuilder = new StringBuilder(UriTemplate);
uriBuilder.Replace("{z}", zoomLevel.ToString());
uriBuilder.Replace("{x}", column.ToString());
uriBuilder.Replace("{y}", row.ToString());
if (Subdomains != null && Subdomains.Length > 0)
{
uriBuilder.Replace("{s}", Subdomains[(column + row) % Subdomains.Length]);
}
uri = new Uri(uriBuilder.ToString(), UriKind.RelativeOrAbsolute);
}
return uri;
}
public override string ToString()
{
return UriTemplate;
}
}
public class TmsTileSource : UriTileSource
{
public override Uri GetUri(int zoomLevel, int column, int row)
{
return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row);
}
}
}

View file

@ -0,0 +1,55 @@
using System;
namespace MapControl
{
public class UriTileSource : TileSource
{
private string uriTemplate;
private string uriFormat;
public string UriTemplate
{
get => uriTemplate;
set
{
uriTemplate = value;
uriFormat = uriTemplate
.Replace("{z}", "{0}")
.Replace("{x}", "{1}")
.Replace("{y}", "{2}")
.Replace("{s}", "{3}");
if (Subdomains == null && uriTemplate.Contains("{s}"))
{
Subdomains = ["a", "b", "c"]; // default OpenStreetMap subdomains
}
}
}
public string[] Subdomains { get; set; }
public override Uri GetUri(int zoomLevel, int column, int row)
{
Uri uri = null;
if (uriFormat != null)
{
var uriString = Subdomains?.Length > 0
? string.Format(uriFormat, zoomLevel, column, row, Subdomains[(column + row) % Subdomains.Length])
: string.Format(uriFormat, zoomLevel, column, row);
uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
}
return uri;
}
}
public class TmsTileSource : UriTileSource
{
public override Uri GetUri(int zoomLevel, int column, int row)
{
return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row);
}
}
}

View file

@ -17,7 +17,7 @@ namespace MapControl
{
public string Identifier => identifier;
public string SupportedCrsId => supportedCrsId;
public string UriTemplate => uriTemplate;
public string UriTemplate { get; } = uriTemplate.Replace("{TileMatrixSet}", identifier);
public List<WmtsTileMatrix> TileMatrixes { get; } = tileMatrixes.OrderBy(m => m.Scale).ToList();
}
@ -236,7 +236,7 @@ namespace MapControl
throw new ArgumentException($"No TileMatrix elements found in TileMatrixSet \"{identifier}\".");
}
return new WmtsTileMatrixSet(identifier, supportedCrs, uriTemplate.Replace("{TileMatrixSet}", identifier), tileMatrixes);
return new WmtsTileMatrixSet(identifier, supportedCrs, uriTemplate, tileMatrixes);
}
public static WmtsTileMatrix ReadTileMatrix(XElement tileMatrixElement, string supportedCrs)

View file

@ -1,35 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace MapControl
{
public class WmtsTileSource : UriTileSource
public class WmtsTileSource(WmtsTileMatrixSet tileMatrixSet) : TileSource
{
private readonly List<WmtsTileMatrix> tileMatrixes;
private readonly string uriFormat = tileMatrixSet.UriTemplate
.Replace("{TileMatrix}", "{0}")
.Replace("{TileCol}", "{1}")
.Replace("{TileRow}", "{2}");
public WmtsTileSource(WmtsTileMatrixSet tileMatrixSet)
{
UriTemplate = tileMatrixSet.UriTemplate;
tileMatrixes = tileMatrixSet.TileMatrixes;
}
private readonly List<WmtsTileMatrix> tileMatrixes = tileMatrixSet.TileMatrixes;
public override Uri GetUri(int zoomLevel, int column, int row)
{
Uri uri = null;
if (zoomLevel < tileMatrixes.Count)
{
var uriBuilder = new StringBuilder(UriTemplate);
uriBuilder.Replace("{TileMatrix}", tileMatrixes[zoomLevel].Identifier);
uriBuilder.Replace("{TileCol}", column.ToString());
uriBuilder.Replace("{TileRow}", row.ToString());
uri = new Uri(uriBuilder.ToString());
}
return uri;
return zoomLevel < tileMatrixes.Count
? new Uri(string.Format(uriFormat, tileMatrixes[zoomLevel].Identifier, column, row))
: null;
}
}
}