Minor tile layer updates

This commit is contained in:
ClemensF 2020-10-25 16:09:09 +01:00
parent ea992d3d0f
commit ddf77441f2
10 changed files with 124 additions and 86 deletions

View file

@ -0,0 +1,42 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
namespace MapControl
{
public class BoundingBoxTileSource : TileSource
{
public override Uri GetUri(int x, int y, int zoomLevel)
{
Uri uri = null;
if (UriFormat != null)
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
var west = MapProjection.Wgs84MetersPerDegree * (x * tileSize - 180d);
var east = MapProjection.Wgs84MetersPerDegree * ((x + 1) * tileSize - 180d);
var south = MapProjection.Wgs84MetersPerDegree * (180d - (y + 1) * tileSize);
var north = MapProjection.Wgs84MetersPerDegree * (180d - y * tileSize);
if (UriFormat.Contains("{bbox}"))
{
uri = new Uri(UriFormat.Replace("{bbox}", string.Format(CultureInfo.InvariantCulture,
"{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north)));
}
else
{
uri = new Uri(UriFormat
.Replace("{west}", west.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{south}", south.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{east}", east.ToString("F2", CultureInfo.InvariantCulture))
.Replace("{north}", north.ToString("F2", CultureInfo.InvariantCulture)));
}
}
return uri;
}
}
}

View file

@ -216,7 +216,7 @@ namespace MapControl
} }
} }
protected virtual async Task UpdateImageAsync() protected async Task UpdateImageAsync()
{ {
updateTimer.Stop(); updateTimer.Stop();

View file

@ -37,8 +37,7 @@ namespace MapControl
{ {
TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" }, TileSource = new TileSource { UriFormat = "https://{s}.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)"
MaxZoomLevel = 19
}; };
} }
} }
@ -47,7 +46,7 @@ namespace MapControl
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0)); nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register( public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(18)); nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(19));
public MapTileLayer() public MapTileLayer()
: this(new TileImageLoader()) : this(new TileImageLoader())
@ -73,7 +72,7 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Maximum zoom level supported by the MapTileLayer. Default value is 18. /// Maximum zoom level supported by the MapTileLayer. Default value is 19.
/// </summary> /// </summary>
public int MaxZoomLevel public int MaxZoomLevel
{ {
@ -92,8 +91,6 @@ namespace MapControl
protected override void UpdateTileLayer() protected override void UpdateTileLayer()
{ {
UpdateTimer.Stop();
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator) if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
{ {
TileMatrix = null; TileMatrix = null;

View file

@ -39,7 +39,7 @@ namespace MapControl
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),
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).UpdateTimer.Interval = (TimeSpan)e.NewValue)); new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).updateTimer.Interval = (TimeSpan)e.NewValue));
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register( public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false)); nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false));
@ -50,6 +50,7 @@ namespace MapControl
public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register( public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register(
nameof(MapForeground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null)); nameof(MapForeground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null));
private readonly DispatcherTimer updateTimer;
private MapBase parentMap; private MapBase parentMap;
protected MapTileLayerBase(ITileImageLoader tileImageLoader) protected MapTileLayerBase(ITileImageLoader tileImageLoader)
@ -57,16 +58,19 @@ namespace MapControl
RenderTransform = new MatrixTransform(); RenderTransform = new MatrixTransform();
TileImageLoader = tileImageLoader; TileImageLoader = tileImageLoader;
UpdateTimer = new DispatcherTimer { Interval = UpdateInterval }; updateTimer = new DispatcherTimer { Interval = UpdateInterval };
UpdateTimer.Tick += (s, e) => UpdateTileLayer();
updateTimer.Tick += (s, e) =>
{
updateTimer.Stop();
UpdateTileLayer();
};
MapPanel.InitMapElement(this); MapPanel.InitMapElement(this);
} }
public ITileImageLoader TileImageLoader { get; } public ITileImageLoader TileImageLoader { get; }
public DispatcherTimer UpdateTimer { get; }
/// <summary> /// <summary>
/// Provides map tile URIs or images. /// Provides map tile URIs or images.
/// </summary> /// </summary>
@ -145,6 +149,8 @@ namespace MapControl
get { return parentMap; } get { return parentMap; }
set set
{ {
updateTimer.Stop();
if (parentMap != null) if (parentMap != null)
{ {
parentMap.ViewportChanged -= OnViewportChanged; parentMap.ViewportChanged -= OnViewportChanged;
@ -165,20 +171,21 @@ namespace MapControl
{ {
if (Children.Count == 0 || e.ProjectionChanged || Math.Abs(e.LongitudeOffset) > 180d) if (Children.Count == 0 || e.ProjectionChanged || Math.Abs(e.LongitudeOffset) > 180d)
{ {
updateTimer.Stop();
UpdateTileLayer(); // update immediately when projection has changed or center has moved across 180° longitude UpdateTileLayer(); // update immediately when projection has changed or center has moved across 180° longitude
} }
else else
{ {
SetRenderTransform(); SetRenderTransform();
if (UpdateTimer.IsEnabled && !UpdateWhileViewportChanging) if (updateTimer.IsEnabled && !UpdateWhileViewportChanging)
{ {
UpdateTimer.Stop(); // restart updateTimer.Stop(); // restart
} }
if (!UpdateTimer.IsEnabled) if (!updateTimer.IsEnabled)
{ {
UpdateTimer.Start(); updateTimer.Start();
} }
} }
} }

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
@ -89,29 +88,4 @@ namespace MapControl
return base.GetUri(x, (1 << zoomLevel) - 1 - y, zoomLevel); return base.GetUri(x, (1 << zoomLevel) - 1 - y, zoomLevel);
} }
} }
public class BoundingBoxTileSource : TileSource
{
public override Uri GetUri(int x, int y, int zoomLevel)
{
Uri uri = null;
if (UriFormat != null)
{
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
var west = MapProjection.Wgs84MetersPerDegree * (x * tileSize - 180d);
var east = MapProjection.Wgs84MetersPerDegree * ((x + 1) * tileSize - 180d);
var south = MapProjection.Wgs84MetersPerDegree * (180d - (y + 1) * tileSize);
var north = MapProjection.Wgs84MetersPerDegree * (180d - y * tileSize);
uri = new Uri(UriFormat
.Replace("{west}", west.ToString(CultureInfo.InvariantCulture))
.Replace("{south}", south.ToString(CultureInfo.InvariantCulture))
.Replace("{east}", east.ToString(CultureInfo.InvariantCulture))
.Replace("{north}", north.ToString(CultureInfo.InvariantCulture)));
}
return uri;
}
}
} }

View file

@ -90,8 +90,6 @@ namespace MapControl
protected override void UpdateTileLayer() protected override void UpdateTileLayer()
{ {
UpdateTimer.Stop();
if (ParentMap == null || if (ParentMap == null ||
!TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out WmtsTileMatrixSet tileMatrixSet)) !TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out WmtsTileMatrixSet tileMatrixSet))
{ {

View file

@ -59,6 +59,9 @@
<Compile Include="..\Shared\BoundingBox.cs"> <Compile Include="..\Shared\BoundingBox.cs">
<Link>BoundingBox.cs</Link> <Link>BoundingBox.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\BoundingBoxTileSource.cs">
<Link>BoundingBoxTileSource.cs</Link>
</Compile>
<Compile Include="..\Shared\CenteredBoundingBox.cs"> <Compile Include="..\Shared\CenteredBoundingBox.cs">
<Link>CenteredBoundingBox.cs</Link> <Link>CenteredBoundingBox.cs</Link>
</Compile> </Compile>

View file

@ -26,18 +26,25 @@ namespace ViewModel
{ {
TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" }, TileSource = new TileSource { UriFormat = "https://{s}.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)"
MaxZoomLevel = 19
} }
}, },
{ {
"OpenStreetMap German", "OpenStreetMap German",
new MapTileLayer new MapTileLayer
{ {
TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png" }, TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" },
SourceName = "OpenStreetMap German", SourceName = "OpenStreetMap German",
Description = "© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", Description = "© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"
MaxZoomLevel = 19 }
},
{
"OpenStreetMap French",
new MapTileLayer
{
TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png" },
SourceName = "OpenStreetMap French",
Description = "© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"
} }
}, },
{ {
@ -46,10 +53,19 @@ namespace ViewModel
{ {
TileSource = new TileSource { UriFormat = "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png" }, TileSource = new TileSource { UriFormat = "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png" },
SourceName = "OpenTopoMap", SourceName = "OpenTopoMap",
Description = "© [OpenTopoMap](https://opentopomap.org/)\n© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", Description = "© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)",
MaxZoomLevel = 17 MaxZoomLevel = 17
} }
}, },
{
"Hike & Bike",
new MapTileLayer
{
TileSource = new TileSource { UriFormat = "https://tiles.wmflabs.org/hikebike/{z}/{x}/{y}.png" },
SourceName = "HikeBike",
Description = "© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"
}
},
{ {
"Seamarks", "Seamarks",
new MapTileLayer new MapTileLayer
@ -60,35 +76,6 @@ namespace ViewModel
MaxZoomLevel = 18 MaxZoomLevel = 18
} }
}, },
{
"TopPlusOpen WMTS",
new WmtsTileLayer
{
SourceName = "TopPlusOpen",
Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)",
CapabilitiesUri = new Uri("https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml")
}
},
{
"TopPlusOpen WMS",
new WmsImageLayer
{
Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)",
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open")
}
},
{
"OpenStreetMap WMS",
new WmsImageLayer
{
Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/)\nData © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)",
ServiceUri = new Uri("http://ows.terrestris.de/osm/service")
}
},
{
"SevenCs ChartServer WMS",
new ChartServerLayer()
},
{ {
"Bing Maps Road", "Bing Maps Road",
new BingMapsTileLayer new BingMapsTileLayer
@ -120,6 +107,35 @@ namespace ViewModel
MapBackground = new SolidColorBrush(Colors.Black) MapBackground = new SolidColorBrush(Colors.Black)
} }
}, },
{
"TopPlusOpen WMTS",
new WmtsTileLayer
{
SourceName = "TopPlusOpen",
Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)",
CapabilitiesUri = new Uri("https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml")
}
},
{
"TopPlusOpen WMS",
new WmsImageLayer
{
Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)",
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open")
}
},
{
"OpenStreetMap WMS",
new WmsImageLayer
{
Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)",
ServiceUri = new Uri("http://ows.terrestris.de/osm/service")
}
},
{
"SevenCs ChartServer WMS",
new ChartServerLayer()
},
}; };
private string currentMapLayerName = "OpenStreetMap"; private string currentMapLayerName = "OpenStreetMap";
@ -149,7 +165,9 @@ namespace ViewModel
{ {
"OpenStreetMap", "OpenStreetMap",
"OpenStreetMap German", "OpenStreetMap German",
"OpenStreetMap French",
"OpenTopoMap", "OpenTopoMap",
"Hike & Bike",
"TopPlusOpen WMTS", "TopPlusOpen WMTS",
"TopPlusOpen WMS", "TopPlusOpen WMS",
"OpenStreetMap WMS", "OpenStreetMap WMS",

View file

@ -174,8 +174,8 @@
</map:Pushpin> </map:Pushpin>
</map:Map> </map:Map>
<Border HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="#7FFFFFFF"> <Border HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="#AFFFFFFF">
<TextBlock Margin="2" FontSize="10" map:HyperlinkText.InlinesSource="{Binding MapLayer.Description, ElementName=map}"/> <TextBlock Margin="4,2" FontSize="10" map:HyperlinkText.InlinesSource="{Binding MapLayer.Description, ElementName=map}"/>
</Border> </Border>
<Grid Grid.Row="1"> <Grid Grid.Row="1">

View file

@ -21,8 +21,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\Shared\MapLayers.cs" Link="MapLayers.cs" /> <Compile Include="..\Shared\*.cs" />
<Compile Include="..\Shared\MapViewModel.cs" Link="MapViewModel.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>