IMapLayer.SupportedMapProjections

This commit is contained in:
ClemensFischer 2025-09-19 18:49:12 +02:00
parent 046cebb341
commit b5dac4a854
8 changed files with 144 additions and 103 deletions

View file

@ -22,6 +22,7 @@ namespace MapControl
{ {
Brush MapBackground { get; } Brush MapBackground { get; }
Brush MapForeground { get; } Brush MapForeground { get; }
IReadOnlyCollection<string> SupportedMapProjections { get; }
} }
public partial class MapBase public partial class MapBase

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WPF #if WPF
@ -126,6 +127,8 @@ namespace MapControl
/// </summary> /// </summary>
public double LoadingProgress => (double)GetValue(LoadingProgressProperty); public double LoadingProgress => (double)GetValue(LoadingProgressProperty);
public abstract IReadOnlyCollection<string> SupportedMapProjections { get; }
protected override void SetParentMap(MapBase map) protected override void SetParentMap(MapBase map)
{ {
if (map != null) if (map != null)
@ -183,7 +186,11 @@ namespace MapControl
ImageSource image = null; ImageSource image = null;
BoundingBox boundingBox = null; BoundingBox boundingBox = null;
if (ParentMap != null && ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d) if (ParentMap != null &&
ParentMap.ActualWidth > 0d &&
ParentMap.ActualHeight > 0d &&
SupportedMapProjections != null &&
SupportedMapProjections.Contains(ParentMap.MapProjection.CrsId))
{ {
var width = ParentMap.ActualWidth * RelativeImageSize; var width = ParentMap.ActualWidth * RelativeImageSize;
var height = ParentMap.ActualHeight * RelativeImageSize; var height = ParentMap.ActualHeight * RelativeImageSize;

View file

@ -1,4 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
#if WPF #if WPF
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
@ -33,22 +36,24 @@ namespace MapControl
private const int TileSize = 256; private const int TileSize = 256;
private static readonly Point MapTopLeft = new Point( private static readonly Point MapTopLeft = new(-180d * MapProjection.Wgs84MeterPerDegree,
-180d * MapProjection.Wgs84MeterPerDegree, 180d * MapProjection.Wgs84MeterPerDegree); 180d * MapProjection.Wgs84MeterPerDegree);
/// <summary> /// <summary>
/// A default MapTileLayer using OpenStreetMap data. /// A default MapTileLayer using OpenStreetMap data.
/// </summary> /// </summary>
public static MapTileLayer OpenStreetMapTileLayer => new MapTileLayer public static MapTileLayer OpenStreetMapTileLayer => new()
{ {
TileSource = new TileSource { UriTemplate = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" }, TileSource = new TileSource { UriTemplate = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" },
SourceName = "OpenStreetMap", SourceName = "OpenStreetMap",
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)" Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)"
}; };
public override IReadOnlyCollection<string> SupportedMapProjections { get; } = ["EPSG:3857"];
public TileMatrix TileMatrix { get; private set; } public TileMatrix TileMatrix { get; private set; }
public TileCollection Tiles { get; private set; } = new TileCollection(); public TileCollection Tiles { get; private set; } = [];
/// <summary> /// <summary>
/// Minimum zoom level supported by the MapTileLayer. Default value is 0. /// Minimum zoom level supported by the MapTileLayer. Default value is 0.
@ -113,7 +118,7 @@ namespace MapControl
protected override void UpdateTileLayerAsync(bool resetTiles) protected override void UpdateTileLayerAsync(bool resetTiles)
{ {
if (ParentMap == null || ParentMap.MapProjection.Type != MapProjectionType.WebMercator) if (ParentMap == null || !SupportedMapProjections.Contains(ParentMap.MapProjection.CrsId))
{ {
TileMatrix = null; TileMatrix = null;
Children.Clear(); Children.Clear();

View file

@ -189,6 +189,8 @@ namespace MapControl
} }
} }
public abstract IReadOnlyCollection<string> SupportedMapProjections { get; }
protected bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this; protected bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this;
protected void BeginLoadTiles(IEnumerable<Tile> tiles, string cacheName) protected void BeginLoadTiles(IEnumerable<Tile> tiles, string cacheName)

View file

@ -16,6 +16,7 @@ using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
#elif AVALONIA #elif AVALONIA
using Avalonia; using Avalonia;
using Avalonia.Interactivity;
#endif #endif
namespace MapControl namespace MapControl
@ -26,27 +27,24 @@ namespace MapControl
public partial class WmsImageLayer : MapImageLayer public partial class WmsImageLayer : MapImageLayer
{ {
private static ILogger logger; private static ILogger logger;
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger<GroundOverlay>(); private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmsImageLayer));
public static readonly DependencyProperty ServiceUriProperty = public static readonly DependencyProperty ServiceUriProperty =
DependencyPropertyHelper.Register<WmsImageLayer, Uri>(nameof(ServiceUri), null, DependencyPropertyHelper.Register<WmsImageLayer, Uri>(nameof(ServiceUri), null,
async (layer, oldValue, newValue) => await layer.UpdateImageAsync()); async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
public static readonly DependencyProperty WmsStylesProperty = public static readonly DependencyProperty RequestStylesProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(WmsStyles), "", DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(RequestStyles), "",
async (layer, oldValue, newValue) => await layer.UpdateImageAsync()); async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
public static readonly DependencyProperty WmsLayersProperty = public static readonly DependencyProperty RequestLayersProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(WmsLayers), null, DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(RequestLayers), null,
async (layer, oldValue, newValue) => async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
public WmsImageLayer()
{ {
// Ignore property change from GetImageAsync when WmsLayers was null. Loaded += OnLoaded;
//
if (oldValue != null)
{
await layer.UpdateImageAsync();
} }
});
/// <summary> /// <summary>
/// The base request URL. /// The base request URL.
@ -60,42 +58,30 @@ namespace MapControl
/// <summary> /// <summary>
/// Comma-separated sequence of requested WMS Styles. Default is an empty string. /// Comma-separated sequence of requested WMS Styles. Default is an empty string.
/// </summary> /// </summary>
public string WmsStyles public string RequestStyles
{ {
get => (string)GetValue(WmsStylesProperty); get => (string)GetValue(RequestStylesProperty);
set => SetValue(WmsStylesProperty, value); set => SetValue(RequestStylesProperty, value);
} }
/// <summary> /// <summary>
/// Comma-separated sequence of WMS Layer names to be displayed. If not set, the first Layer is displayed. /// Comma-separated sequence of WMS Layer names to be displayed. If not set, the default Layer is displayed.
/// </summary> /// </summary>
public string WmsLayers public string RequestLayers
{ {
get => (string)GetValue(WmsLayersProperty); get => (string)GetValue(RequestLayersProperty);
set => SetValue(WmsLayersProperty, value); set => SetValue(RequestLayersProperty, value);
} }
/// <summary> /// <summary>
/// Gets a list of all layer names returned by a GetCapabilities response. /// Gets a collection of all Layer names available in a WMS.
/// </summary> /// </summary>
public async Task<IEnumerable<string>> GetLayerNamesAsync() public IReadOnlyCollection<string> AvailableLayers { get; private set; }
{
IEnumerable<string> layerNames = null;
var capabilities = await GetCapabilitiesAsync(); /// <summary>
/// Gets a collection of all CRSs supported by a WMS.
if (capabilities != null) /// </summary>
{ public override IReadOnlyCollection<string> SupportedMapProjections => mapProjections;
var ns = capabilities.Name.Namespace;
layerNames = capabilities
.Descendants(ns + "Layer")
.Select(e => e.Element(ns + "Name")?.Value)
.Where(n => !string.IsNullOrEmpty(n));
}
return layerNames;
}
/// <summary> /// <summary>
/// Loads an XElement from the URL returned by GetCapabilitiesRequestUri(). /// Loads an XElement from the URL returned by GetCapabilitiesRequestUri().
@ -167,16 +153,6 @@ namespace MapControl
try try
{ {
if (ServiceUri != null && ParentMap?.MapProjection != null)
{
if (WmsLayers == null &&
ServiceUri.OriginalString.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0)
{
// Get first Layer from a GetCapabilities response.
//
WmsLayers = (await GetLayerNamesAsync())?.FirstOrDefault() ?? "";
}
if (boundingBox.West >= -180d && boundingBox.East <= 180d || if (boundingBox.West >= -180d && boundingBox.East <= 180d ||
ParentMap.MapProjection.Type > MapProjectionType.NormalCylindrical) ParentMap.MapProjection.Type > MapProjectionType.NormalCylindrical)
{ {
@ -211,7 +187,6 @@ namespace MapControl
} }
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Logger?.LogError(ex, "GetImageAsync"); Logger?.LogError(ex, "GetImageAsync");
@ -251,8 +226,8 @@ namespace MapControl
{ "SERVICE", "WMS" }, { "SERVICE", "WMS" },
{ "VERSION", "1.3.0" }, { "VERSION", "1.3.0" },
{ "REQUEST", "GetMap" }, { "REQUEST", "GetMap" },
{ "LAYERS", WmsLayers ?? "" }, { "LAYERS", RequestLayers ?? "" },
{ "STYLES", WmsStyles ?? "" }, { "STYLES", RequestStyles ?? "" },
{ "FORMAT", "image/png" }, { "FORMAT", "image/png" },
{ "CRS", GetCrsValue() }, { "CRS", GetCrsValue() },
{ "BBOX", GetBboxValue(boundingBox, bbox.Value) }, { "BBOX", GetBboxValue(boundingBox, bbox.Value) },
@ -289,8 +264,8 @@ namespace MapControl
{ "SERVICE", "WMS" }, { "SERVICE", "WMS" },
{ "VERSION", "1.3.0" }, { "VERSION", "1.3.0" },
{ "REQUEST", "GetFeatureInfo" }, { "REQUEST", "GetFeatureInfo" },
{ "LAYERS", WmsLayers ?? "" }, { "LAYERS", RequestLayers ?? "" },
{ "STYLES", WmsStyles ?? "" }, { "STYLES", RequestStyles ?? "" },
{ "FORMAT", "image/png" }, { "FORMAT", "image/png" },
{ "INFO_FORMAT", format }, { "INFO_FORMAT", format },
{ "CRS", GetCrsValue() }, { "CRS", GetCrsValue() },
@ -312,25 +287,25 @@ namespace MapControl
protected virtual string GetCrsValue() protected virtual string GetCrsValue()
{ {
var projection = ParentMap.MapProjection; var projection = ParentMap.MapProjection;
var crsId = projection.CrsId; var crs = projection.CrsId;
if (crsId.StartsWith("AUTO2:") || crsId.StartsWith("AUTO:")) if (crs.StartsWith("AUTO2:") || crs.StartsWith("AUTO:"))
{ {
crsId = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crsId, projection.Center.Longitude, projection.Center.Latitude); crs = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crs, projection.Center.Longitude, projection.Center.Latitude);
} }
return crsId; return crs;
} }
protected virtual string GetBboxValue(BoundingBox boundingBox, Rect mapBoundingBox) protected virtual string GetBboxValue(BoundingBox boundingBox, Rect mapBoundingBox)
{ {
var crsId = ParentMap.MapProjection.CrsId; var crs = ParentMap.MapProjection.CrsId;
string format; string format;
double x1, y1, x2, y2; double x1, y1, x2, y2;
if (crsId == "CRS:84" || crsId == "EPSG:4326") if (crs == "CRS:84" || crs == "EPSG:4326")
{ {
format = crsId == "CRS:84" ? "{0:F8},{1:F8},{2:F8},{3:F8}" : "{1:F8},{0:F8},{3:F8},{2:F8}"; format = crs == "CRS:84" ? "{0:F8},{1:F8},{2:F8},{3:F8}" : "{1:F8},{0:F8},{3:F8},{2:F8}";
x1 = boundingBox.West; x1 = boundingBox.West;
y1 = boundingBox.South; y1 = boundingBox.South;
x2 = boundingBox.East; x2 = boundingBox.East;
@ -366,5 +341,48 @@ namespace MapControl
return uri.Replace(" ", "%20"); return uri.Replace(" ", "%20");
} }
private List<string> mapProjections;
private async void OnLoaded(object sender, RoutedEventArgs e)
{
Loaded -= OnLoaded;
if (AvailableLayers == null && ServiceUri != null)
{
var capabilities = await GetCapabilitiesAsync();
if (capabilities != null)
{
var ns = capabilities.Name.Namespace;
var capability = capabilities.Element(ns + "Capability");
mapProjections = capability
.Element(ns + "Layer")
.Descendants(ns + "CRS")
.Select(e => e.Value)
.ToList();
var layerNames = capability
.Descendants(ns + "Layer")
.Select(e => e.Element(ns + "Name")?.Value)
.Where(n => !string.IsNullOrEmpty(n))
.ToList();
AvailableLayers = layerNames;
if (layerNames.Count > 0 &&
RequestLayers == null &&
ServiceUri.OriginalString.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0)
{
RequestLayers = layerNames[0];
}
else
{
await UpdateImageAsync();
}
}
}
}
} }
} }

View file

@ -23,7 +23,7 @@ namespace MapControl
public partial class WmtsTileLayer : MapTileLayerBase public partial class WmtsTileLayer : MapTileLayerBase
{ {
private static ILogger logger; private static ILogger logger;
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger<GroundOverlay>(); private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmtsTileLayer));
public static readonly DependencyProperty CapabilitiesUriProperty = public static readonly DependencyProperty CapabilitiesUriProperty =
DependencyPropertyHelper.Register<WmtsTileLayer, Uri>(nameof(CapabilitiesUri), null, DependencyPropertyHelper.Register<WmtsTileLayer, Uri>(nameof(CapabilitiesUri), null,
@ -68,11 +68,19 @@ namespace MapControl
set => SetValue(PreferredTileMatrixSetsProperty, value); set => SetValue(PreferredTileMatrixSetsProperty, value);
} }
public IEnumerable<WmtsTileMatrixLayer> ChildLayers => Children.Cast<WmtsTileMatrixLayer>(); /// <summary>
/// Gets a dictionary of all tile matrix sets supported by a WMTS, with their CRS as dictionary key.
/// </summary>
public Dictionary<string, WmtsTileMatrixSet> TileMatrixSets { get; } = []; public Dictionary<string, WmtsTileMatrixSet> TileMatrixSets { get; } = [];
protected virtual WmtsTileSource CreateTileSource(string uriTemplate) => new WmtsTileSource { UriTemplate = uriTemplate }; /// <summary>
/// Gets a collection of all CRSs supported by a WMTS.
/// </summary>
public override IReadOnlyCollection<string> SupportedMapProjections => TileMatrixSets.Keys;
protected IEnumerable<WmtsTileMatrixLayer> ChildLayers => Children.Cast<WmtsTileMatrixLayer>();
protected virtual WmtsTileSource CreateTileSource(string uriTemplate) => new() { UriTemplate = uriTemplate };
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
@ -192,10 +200,10 @@ namespace MapControl
var capabilities = await WmtsCapabilities.ReadCapabilitiesAsync(CapabilitiesUri, Layer); var capabilities = await WmtsCapabilities.ReadCapabilitiesAsync(CapabilitiesUri, Layer);
foreach (var tileMatrixSet in capabilities.TileMatrixSets foreach (var tileMatrixSet in capabilities.TileMatrixSets
.Where(s => !TileMatrixSets.ContainsKey(s.SupportedCrs) || .Where(s => !TileMatrixSets.ContainsKey(s.SupportedMapProjection) ||
PreferredTileMatrixSets != null && PreferredTileMatrixSets.Contains(s.Identifier))) PreferredTileMatrixSets != null && PreferredTileMatrixSets.Contains(s.Identifier)))
{ {
TileMatrixSets[tileMatrixSet.SupportedCrs] = tileMatrixSet; TileMatrixSets[tileMatrixSet.SupportedMapProjection] = tileMatrixSet;
} }
Layer = capabilities.Layer; Layer = capabilities.Layer;

View file

@ -6,16 +6,16 @@ namespace MapControl
{ {
public class WmtsTileMatrixSet public class WmtsTileMatrixSet
{ {
public WmtsTileMatrixSet(string identifier, string supportedCrs, IEnumerable<WmtsTileMatrix> tileMatrixes) public WmtsTileMatrixSet(string identifier, string supportedMapProjection, IEnumerable<WmtsTileMatrix> tileMatrixes)
{ {
if (string.IsNullOrEmpty(identifier)) if (string.IsNullOrEmpty(identifier))
{ {
throw new ArgumentException($"The {nameof(identifier)} argument must not be null or empty.", nameof(identifier)); throw new ArgumentException($"The {nameof(identifier)} argument must not be null or empty.", nameof(identifier));
} }
if (string.IsNullOrEmpty(supportedCrs)) if (string.IsNullOrEmpty(supportedMapProjection))
{ {
throw new ArgumentException($"The {nameof(supportedCrs)} argument must not be null or empty.", nameof(supportedCrs)); throw new ArgumentException($"The {nameof(supportedMapProjection)} argument must not be null or empty.", nameof(supportedMapProjection));
} }
if (tileMatrixes == null || !tileMatrixes.Any()) if (tileMatrixes == null || !tileMatrixes.Any())
@ -24,12 +24,12 @@ namespace MapControl
} }
Identifier = identifier; Identifier = identifier;
SupportedCrs = supportedCrs; SupportedMapProjection = supportedMapProjection;
TileMatrixes = tileMatrixes.OrderBy(m => m.Scale).ToList(); TileMatrixes = tileMatrixes.OrderBy(m => m.Scale).ToList();
} }
public string Identifier { get; } public string Identifier { get; }
public string SupportedCrs { get; } public string SupportedMapProjection { get; }
public IList<WmtsTileMatrix> TileMatrixes { get; } public IList<WmtsTileMatrix> TileMatrixes { get; }
} }
} }

View file

@ -28,7 +28,7 @@ namespace ProjectionDemo
new WmsImageLayer new WmsImageLayer
{ {
ServiceUri = new Uri("http://ows.terrestris.de/osm/service"), ServiceUri = new Uri("http://ows.terrestris.de/osm/service"),
WmsLayers = "OSM-WMS" RequestLayers = "OSM-WMS"
}); });
viewModel.Layers.Add( viewModel.Layers.Add(
@ -36,7 +36,7 @@ namespace ProjectionDemo
new WmsImageLayer new WmsImageLayer
{ {
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open"), ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open"),
WmsLayers = "web" RequestLayers = "web"
}); });
viewModel.Layers.Add( viewModel.Layers.Add(
@ -44,7 +44,7 @@ namespace ProjectionDemo
new WmsImageLayer new WmsImageLayer
{ {
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_basemapde"), ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_basemapde"),
WmsLayers = "de_basemapde_web_raster_farbe" RequestLayers = "de_basemapde_web_raster_farbe"
}); });
viewModel.Layers.Add( viewModel.Layers.Add(
@ -52,7 +52,7 @@ namespace ProjectionDemo
new WmsImageLayer new WmsImageLayer
{ {
ServiceUri = new Uri("https://geoportal.wiesbaden.de/cgi-bin/mapserv.fcgi?map=d:/openwimap/umn/map/orthophoto/orthophotos.map"), ServiceUri = new Uri("https://geoportal.wiesbaden.de/cgi-bin/mapserv.fcgi?map=d:/openwimap/umn/map/orthophoto/orthophotos.map"),
WmsLayers = "orthophoto2023" RequestLayers = "orthophoto2023"
}); });
viewModel.CurrentProjection = viewModel.Projections[0]; viewModel.CurrentProjection = viewModel.Projections[0];