diff --git a/MapControl/Shared/BoundingBoxTileSource.cs b/MapControl/Shared/BoundingBoxTileSource.cs deleted file mode 100644 index 588c862f..00000000 --- a/MapControl/Shared/BoundingBoxTileSource.cs +++ /dev/null @@ -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; - } - - /// - /// Gets the bounding box in meters of a standard Web Mercator tile, - /// specified by zoom level and grid column and row indices. - /// - 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); - } - } -} diff --git a/MapControl/Shared/TileSource.cs b/MapControl/Shared/TileSource.cs index e77fc241..9cf96877 100644 --- a/MapControl/Shared/TileSource.cs +++ b/MapControl/Shared/TileSource.cs @@ -1,5 +1,4 @@ using System; -using System.Text; using System.Threading.Tasks; #if WPF using System.Windows.Media; @@ -62,67 +61,7 @@ namespace MapControl /// public static TileSource Parse(string uriTemplate) { - return new UriTileSource { UriTemplate = uriTemplate }; - } - } - - public class UriTileSource : TileSource - { - private string uriTemplate; - - /// - /// Gets or sets the template string for tile request Uris. - /// - 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); + return new UriTileSource(uriTemplate); } } } diff --git a/MapControl/Shared/UriTileSource.cs b/MapControl/Shared/UriTileSource.cs new file mode 100644 index 00000000..bd1bf24e --- /dev/null +++ b/MapControl/Shared/UriTileSource.cs @@ -0,0 +1,49 @@ +using System; + +namespace MapControl +{ + public class UriTileSource : TileSource + { + private readonly string uriFormat; + + public UriTileSource(string uriTemplate) + { + 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(string uriTemplate) : UriTileSource(uriTemplate) + { + public override Uri GetUri(int zoomLevel, int column, int row) + { + return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row); + } + } +} diff --git a/MapControl/Shared/WmtsCapabilities.cs b/MapControl/Shared/WmtsCapabilities.cs index f11baf30..11b3e09f 100644 --- a/MapControl/Shared/WmtsCapabilities.cs +++ b/MapControl/Shared/WmtsCapabilities.cs @@ -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 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) diff --git a/MapControl/Shared/WmtsTileSource.cs b/MapControl/Shared/WmtsTileSource.cs index 3fb372c0..6e8d6b0c 100644 --- a/MapControl/Shared/WmtsTileSource.cs +++ b/MapControl/Shared/WmtsTileSource.cs @@ -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 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 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; } } }