Cleaner code, mainly WmsImageLayer

This commit is contained in:
Clemens 2022-08-22 21:13:45 +02:00
parent 4b4eecf810
commit c46b6c1afa
4 changed files with 137 additions and 159 deletions

View file

@ -204,12 +204,13 @@ namespace MapControl
private void UpdateTiles() private void UpdateTiles()
{ {
var newTiles = new List<Tile>(); var tiles = new List<Tile>();
int maxZoomLevel;
if (TileSource != null && if (TileSource != null && TileMatrix != null)
TileMatrix != null && {
(maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel)) >= MinZoomLevel) var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
if (maxZoomLevel >= MinZoomLevel)
{ {
var minZoomLevel = IsBaseMapLayer var minZoomLevel = IsBaseMapLayer
? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel) ? Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel)
@ -242,17 +243,18 @@ namespace MapControl
} }
} }
newTiles.Add(tile); tiles.Add(tile);
}
} }
} }
} }
} }
Tiles = newTiles; Tiles = tiles;
Children.Clear(); Children.Clear();
foreach (var tile in Tiles) foreach (var tile in tiles)
{ {
Children.Add(tile.Image); Children.Add(tile.Image);
} }

View file

@ -45,7 +45,7 @@ namespace MapControl
nameof(Description), typeof(string), typeof(MapTileLayerBase), new PropertyMetadata(null)); nameof(Description), typeof(string), typeof(MapTileLayerBase), new PropertyMetadata(null));
public static readonly DependencyProperty MaxBackgroundLevelsProperty = DependencyProperty.Register( public static readonly DependencyProperty MaxBackgroundLevelsProperty = DependencyProperty.Register(
nameof(MaxBackgroundLevels), typeof(int), typeof(MapTileLayerBase), new PropertyMetadata(8)); nameof(MaxBackgroundLevels), typeof(int), typeof(MapTileLayerBase), new PropertyMetadata(5));
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register( public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayerBase), nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayerBase),
@ -111,7 +111,7 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Maximum number of background tile levels. Default value is 8. /// Maximum number of background tile levels. Default value is 5.
/// Only effective in a MapTileLayer or WmtsTileLayer that is the MapLayer of its ParentMap. /// Only effective in a MapTileLayer or WmtsTileLayer that is the MapLayer of its ParentMap.
/// </summary> /// </summary>
public int MaxBackgroundLevels public int MaxBackgroundLevels

View file

@ -37,7 +37,7 @@ namespace MapControl
new PropertyMetadata(null, new PropertyMetadata(null,
async (o, e) => async (o, e) =>
{ {
if (e.OldValue != null) // ignore initial property change from GetImageAsync if (e.OldValue != null) // ignore property change from GetImageAsync
{ {
await ((WmsImageLayer)o).UpdateImageAsync(); await ((WmsImageLayer)o).UpdateImageAsync();
} }
@ -222,7 +222,12 @@ namespace MapControl
/// </summary> /// </summary>
protected virtual string GetCapabilitiesRequestUri() protected virtual string GetCapabilitiesRequestUri()
{ {
return GetRequestUri("GetCapabilities").Replace(" ", "%20"); return GetRequestUri(new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetCapabilities" }
});
} }
/// <summary> /// <summary>
@ -230,40 +235,29 @@ namespace MapControl
/// </summary> /// </summary>
protected virtual string GetMapRequestUri() protected virtual string GetMapRequestUri()
{ {
string uri = null;
var projection = ParentMap?.MapProjection; var projection = ParentMap?.MapProjection;
if (projection != null) if (projection == null)
{ {
uri = GetRequestUri("GetMap"); return null;
if (uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0 && Layers != null)
{
uri += "&LAYERS=" + Layers;
}
if (uri.IndexOf("STYLES=", StringComparison.OrdinalIgnoreCase) < 0 && Styles != null)
{
uri += "&STYLES=" + Styles;
}
if (uri.IndexOf("FORMAT=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "&FORMAT=image/png";
} }
var mapRect = projection.BoundingBoxToRect(BoundingBox); var mapRect = projection.BoundingBoxToRect(BoundingBox);
var viewScale = ParentMap.ViewTransform.Scale; var viewScale = ParentMap.ViewTransform.Scale;
uri += "&" + GetCrsParam(projection); return GetRequestUri(new Dictionary<string, string>
uri += "&" + GetBboxParam(projection, mapRect); {
uri += "&WIDTH=" + (int)Math.Round(viewScale * mapRect.Width); { "SERVICE", "WMS" },
uri += "&HEIGHT=" + (int)Math.Round(viewScale * mapRect.Height); { "VERSION", "1.3.0" },
{ "REQUEST", "GetMap" },
uri = uri.Replace(" ", "%20"); { "LAYERS", Layers ?? "" },
} { "STYLES", Styles ?? "" },
{ "FORMAT", "image/png" },
return uri; { "CRS", GetCrsValue(projection) },
{ "BBOX", GetBboxValue(projection, mapRect) },
{ "WIDTH", Math.Round(viewScale * mapRect.Width).ToString("F0") },
{ "HEIGHT", Math.Round(viewScale * mapRect.Height).ToString("F0") }
});
} }
/// <summary> /// <summary>
@ -271,26 +265,11 @@ namespace MapControl
/// </summary> /// </summary>
protected virtual string GetFeatureInfoRequestUri(Point position, string format) protected virtual string GetFeatureInfoRequestUri(Point position, string format)
{ {
string uri = null;
var projection = ParentMap?.MapProjection; var projection = ParentMap?.MapProjection;
if (projection != null) if (projection == null)
{ {
uri = GetRequestUri("GetFeatureInfo"); return null;
var i = uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase);
if (i >= 0)
{
i += "LAYERS=".Length;
var j = uri.IndexOf('&', i);
var layers = j >= i ? uri.Substring(i, j - i) : uri.Substring(i);
uri += "&QUERY_LAYERS=" + layers;
}
else if (Layers != null)
{
uri += "&LAYERS=" + Layers;
uri += "&QUERY_LAYERS=" + Layers;
} }
var mapRect = projection.BoundingBoxToRect(BoundingBox); var mapRect = projection.BoundingBoxToRect(BoundingBox);
@ -303,50 +282,55 @@ namespace MapControl
var imagePos = transform.Transform(position); var imagePos = transform.Transform(position);
uri += "&" + GetCrsParam(projection); var queryParameters = new Dictionary<string, string>
uri += "&" + GetBboxParam(projection, mapRect);
uri += "&WIDTH=" + (int)Math.Round(viewRect.Width);
uri += "&HEIGHT=" + (int)Math.Round(viewRect.Height);
uri += "&I=" + (int)Math.Round(imagePos.X);
uri += "&J=" + (int)Math.Round(imagePos.Y);
uri += "&INFO_FORMAT=" + format;
uri = uri.Replace(" ", "%20");
}
return uri;
}
protected virtual string GetCrsParam(MapProjection projection)
{ {
return "CRS=" + projection.GetCrsValue(); { "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetFeatureInfo" },
{ "LAYERS", Layers ?? "" },
{ "STYLES", Styles ?? "" },
{ "FORMAT", "image/png" },
{ "INFO_FORMAT", format },
{ "CRS", GetCrsValue(projection) },
{ "BBOX", GetBboxValue(projection, mapRect) },
{ "WIDTH", Math.Round(viewRect.Width).ToString("F0") },
{ "HEIGHT", Math.Round(viewRect.Height).ToString("F0") },
{ "I", Math.Round(imagePos.X).ToString("F0") },
{ "J", Math.Round(imagePos.Y).ToString("F0") }
};
return GetRequestUri(queryParameters) + "&QUERY_LAYERS=" + queryParameters["LAYERS"];
} }
protected virtual string GetBboxParam(MapProjection projection, Rect mapRect) protected virtual string GetCrsValue(MapProjection projection)
{ {
return "BBOX=" + projection.GetBboxValue(mapRect); return projection.GetCrsValue();
} }
protected string GetRequestUri(string request) protected virtual string GetBboxValue(MapProjection projection, Rect mapRect)
{ {
var uri = ServiceUri.ToString(); return projection.GetBboxValue(mapRect);
}
if (!uri.EndsWith("?") && !uri.EndsWith("&")) protected string GetRequestUri(IDictionary<string, string> queryParameters)
{ {
uri += !uri.Contains("?") ? "?" : "&"; var query = ServiceUri.Query;
}
if (uri.IndexOf("SERVICE=", StringComparison.OrdinalIgnoreCase) < 0) if (!string.IsNullOrEmpty(query))
{ {
uri += "SERVICE=WMS&"; foreach (var param in query.Substring(1).Split('&'))
}
if (uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase) < 0)
{ {
uri += "VERSION=1.3.0&"; var pair = param.Split('=');
queryParameters[pair[0].ToUpper()] = pair.Length > 1 ? pair[1] : "";
}
} }
return uri + "REQUEST=" + request; var uri = ServiceUri.GetLeftPart(UriPartial.Path) + "?"
+ string.Join("&", queryParameters.Select(kv => kv.Key + "=" + kv.Value));
Debug.WriteLine(uri);
return uri.Replace(" ", "%20");
} }
} }
} }

View file

@ -133,44 +133,36 @@ namespace MapControl
if (resourceUrls.Any()) if (resourceUrls.Any())
{ {
var urlTemplates var urlTemplates = resourceUrls.Contains(formatPng) ? resourceUrls[formatPng]
= resourceUrls.Contains(formatPng) ? resourceUrls[formatPng]
: resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg] : resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg]
: resourceUrls.First(); : resourceUrls.First();
urlTemplate = urlTemplates.First().Replace("{Style}", style); urlTemplate = urlTemplates.First().Replace("{Style}", style);
} }
else if (capabilitiesUrl != null) else if (capabilitiesUrl != null &&
{ capabilitiesUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) >= 0)
const string requestParam = "Request=GetCapabilities";
var requestIndex = capabilitiesUrl.IndexOf(requestParam, StringComparison.OrdinalIgnoreCase);
if (requestIndex > 0)
{ {
var formats = layerElement.Descendants(ns + "Format").Select(e => e.Value).ToList(); var formats = layerElement.Descendants(ns + "Format").Select(e => e.Value).ToList();
var format = formatPng;
if (formats.Count == 0) if (formats.Count > 0)
{ {
throw new ArgumentException($"No Format element found in Layer \"{layerIdentifier}\"."); format = formats.Contains(formatPng) ? formatPng
}
var format
= formats.Contains(formatPng) ? formatPng
: formats.Contains(formatJpg) ? formatJpg : formats.Contains(formatJpg) ? formatJpg
: formats[0]; : formats[0];
}
urlTemplate = capabilitiesUrl.Substring(0, requestIndex) urlTemplate = capabilitiesUrl.Split('?')[0]
+ "Request=GetTile" + "?Service=WMTS"
+ capabilitiesUrl.Substring(requestIndex + requestParam.Length) + "&Request=GetTile"
+ "&Version=1.0.0" + "&Version=1.0.0"
+ "&Layer=" + layerIdentifier + "&Layer=" + layerIdentifier
+ "&Format=" + format
+ "&Style=" + style + "&Style=" + style
+ "&Format=" + format
+ "&TileMatrixSet={TileMatrixSet}" + "&TileMatrixSet={TileMatrixSet}"
+ "&TileMatrix={TileMatrix}" + "&TileMatrix={TileMatrix}"
+ "&TileCol={TileCol}" + "&TileRow={TileRow}"
+ "&TileRow={TileRow}"; + "&TileCol={TileCol}";
}
} }
if (string.IsNullOrEmpty(urlTemplate)) if (string.IsNullOrEmpty(urlTemplate))