mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Updated TileSources
This commit is contained in:
parent
44d48eadde
commit
1222a4a8c2
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#if WPF
|
#if WPF
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
|
|
@ -62,67 +61,7 @@ namespace MapControl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static TileSource Parse(string uriTemplate)
|
public static TileSource Parse(string uriTemplate)
|
||||||
{
|
{
|
||||||
return new UriTileSource { UriTemplate = uriTemplate };
|
return new UriTileSource(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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
MapControl/Shared/UriTileSource.cs
Normal file
49
MapControl/Shared/UriTileSource.cs
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public string Identifier => identifier;
|
public string Identifier => identifier;
|
||||||
public string SupportedCrsId => supportedCrsId;
|
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();
|
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}\".");
|
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)
|
public static WmtsTileMatrix ReadTileMatrix(XElement tileMatrixElement, string supportedCrs)
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,22 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MapControl
|
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)
|
private readonly List<WmtsTileMatrix> tileMatrixes = tileMatrixSet.TileMatrixes;
|
||||||
{
|
|
||||||
UriTemplate = tileMatrixSet.UriTemplate;
|
|
||||||
tileMatrixes = tileMatrixSet.TileMatrixes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Uri GetUri(int zoomLevel, int column, int row)
|
public override Uri GetUri(int zoomLevel, int column, int row)
|
||||||
{
|
{
|
||||||
Uri uri = null;
|
return zoomLevel < tileMatrixes.Count
|
||||||
|
? new Uri(string.Format(uriFormat, tileMatrixes[zoomLevel].Identifier, column, row))
|
||||||
if (zoomLevel < tileMatrixes.Count)
|
: null;
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue