Added WorldMercatorProjection, fixed bounding box problems when changing projection.

This commit is contained in:
ClemensF 2017-10-27 17:15:18 +02:00
parent 2f123886ff
commit 156ebfe177
21 changed files with 586 additions and 469 deletions

View file

@ -4,19 +4,18 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
#endif
@ -26,7 +25,7 @@ namespace MapControl
/// Map image layer. Fills the entire viewport with a map image, e.g. provided by a Web Map Service (WMS).
/// The image must be provided by the abstract UpdateImage(BoundingBox) method.
/// </summary>
public abstract partial class MapImageLayer : MapPanel, IMapLayer
public abstract class MapImageLayer : MapPanel, IMapLayer
{
public static readonly DependencyProperty MinLatitudeProperty = DependencyProperty.Register(
nameof(MinLatitude), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
@ -64,7 +63,6 @@ namespace MapControl
private readonly DispatcherTimer updateTimer;
private BoundingBox boundingBox;
private int topImageIndex;
private bool updateInProgress;
public MapImageLayer()
@ -73,7 +71,7 @@ namespace MapControl
Children.Add(new Image { Opacity = 0d, Stretch = Stretch.Fill });
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
updateTimer.Tick += (s, e) => UpdateImage();
updateTimer.Tick += async (s, e) => await UpdateImage();
}
/// <summary>
@ -180,34 +178,26 @@ namespace MapControl
set { SetValue(MapBackgroundProperty, value); }
}
/// <summary>
/// Returns an ImageSource for the specified bounding box.
/// </summary>
protected abstract Task<ImageSource> GetImage(BoundingBox boundingBox);
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
base.OnViewportChanged(e);
if (e.ProjectionChanged)
{
UpdateImage((BitmapSource)null);
UpdateImage();
ClearImages();
base.OnViewportChanged(e);
var task = UpdateImage();
}
else
{
if (Math.Abs(e.LongitudeOffset) > 180d && boundingBox != null && boundingBox.HasValidBounds)
{
var offset = 360d * Math.Sign(e.LongitudeOffset);
AdjustBoundingBox(e.LongitudeOffset);
boundingBox.West += offset;
boundingBox.East += offset;
foreach (UIElement element in Children)
{
var bbox = GetBoundingBox(element);
if (bbox != null && bbox.HasValidBounds)
{
SetBoundingBox(element, new BoundingBox(bbox.South, bbox.West + offset, bbox.North, bbox.East + offset));
}
}
}
base.OnViewportChanged(e);
if (updateTimer.IsEnabled && !UpdateWhileViewportChanging)
{
@ -221,7 +211,7 @@ namespace MapControl
}
}
protected virtual void UpdateImage()
protected virtual async Task UpdateImage()
{
updateTimer.Stop();
@ -233,104 +223,124 @@ namespace MapControl
{
updateInProgress = true;
var width = ParentMap.RenderSize.Width * RelativeImageSize;
var height = ParentMap.RenderSize.Height * RelativeImageSize;
var x = (ParentMap.RenderSize.Width - width) / 2d;
var y = (ParentMap.RenderSize.Height - height) / 2d;
var rect = new Rect(x, y, width, height);
boundingBox = ParentMap.MapProjection.ViewportRectToBoundingBox(rect);
if (boundingBox != null && boundingBox.HasValidBounds)
{
if (!double.IsNaN(MinLatitude) && boundingBox.South < MinLatitude)
{
boundingBox.South = MinLatitude;
}
if (!double.IsNaN(MinLongitude) && boundingBox.West < MinLongitude)
{
boundingBox.West = MinLongitude;
}
if (!double.IsNaN(MaxLatitude) && boundingBox.North > MaxLatitude)
{
boundingBox.North = MaxLatitude;
}
if (!double.IsNaN(MaxLongitude) && boundingBox.East > MaxLongitude)
{
boundingBox.East = MaxLongitude;
}
if (!double.IsNaN(MaxBoundingBoxWidth) && boundingBox.Width > MaxBoundingBoxWidth)
{
var d = (boundingBox.Width - MaxBoundingBoxWidth) / 2d;
boundingBox.West += d;
boundingBox.East -= d;
}
}
ImageSource imageSource = null;
try
if (UpdateBoundingBox())
{
imageSource = GetImage(boundingBox);
}
catch (Exception ex)
{
Debug.WriteLine("MapImageLayer: " + ex.Message);
try
{
imageSource = await GetImage(boundingBox);
}
catch (Exception ex)
{
Debug.WriteLine("MapImageLayer: " + ex.Message);
}
}
UpdateImage(imageSource);
SwapImages(imageSource);
updateInProgress = false;
}
}
/// <summary>
/// Returns an ImageSource for the specified bounding box.
/// </summary>
protected abstract ImageSource GetImage(BoundingBox boundingBox);
private void SetTopImage(ImageSource imageSource)
private bool UpdateBoundingBox()
{
topImageIndex = (topImageIndex + 1) % 2;
var topImage = (Image)Children[topImageIndex];
var width = ParentMap.RenderSize.Width * RelativeImageSize;
var height = ParentMap.RenderSize.Height * RelativeImageSize;
var x = (ParentMap.RenderSize.Width - width) / 2d;
var y = (ParentMap.RenderSize.Height - height) / 2d;
var rect = new Rect(x, y, width, height);
boundingBox = ParentMap.MapProjection.ViewportRectToBoundingBox(rect);
if (boundingBox == null || !boundingBox.HasValidBounds)
{
return false;
}
if (!double.IsNaN(MinLatitude) && boundingBox.South < MinLatitude)
{
boundingBox.South = MinLatitude;
}
if (!double.IsNaN(MinLongitude) && boundingBox.West < MinLongitude)
{
boundingBox.West = MinLongitude;
}
if (!double.IsNaN(MaxLatitude) && boundingBox.North > MaxLatitude)
{
boundingBox.North = MaxLatitude;
}
if (!double.IsNaN(MaxLongitude) && boundingBox.East > MaxLongitude)
{
boundingBox.East = MaxLongitude;
}
if (!double.IsNaN(MaxBoundingBoxWidth) && boundingBox.Width > MaxBoundingBoxWidth)
{
var d = (boundingBox.Width - MaxBoundingBoxWidth) / 2d;
boundingBox.West += d;
boundingBox.East -= d;
}
return true;
}
private void AdjustBoundingBox(double longitudeOffset)
{
if (Math.Abs(longitudeOffset) > 180d && boundingBox != null && boundingBox.HasValidBounds)
{
var offset = 360d * Math.Sign(longitudeOffset);
boundingBox.West += offset;
boundingBox.East += offset;
foreach (UIElement element in Children)
{
var bbox = GetBoundingBox(element);
if (bbox != null && bbox.HasValidBounds)
{
SetBoundingBox(element, new BoundingBox(bbox.South, bbox.West + offset, bbox.North, bbox.East + offset));
}
}
}
}
private void ClearImages()
{
foreach (UIElement element in Children)
{
element.ClearValue(BoundingBoxProperty);
element.ClearValue(Image.SourceProperty);
}
}
private void SwapImages(ImageSource imageSource)
{
var topImage = (Image)Children[0];
var bottomImage = (Image)Children[1];
Children.RemoveAt(0);
Children.Insert(1, topImage);
topImage.Source = imageSource;
SetBoundingBox(topImage, boundingBox?.Clone());
}
private void SwapImages()
{
var topImage = (Image)Children[topImageIndex];
var bottomImage = (Image)Children[(topImageIndex + 1) % 2];
Canvas.SetZIndex(topImage, 1);
Canvas.SetZIndex(bottomImage, 0);
if (topImage.Source != null)
topImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
topImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
To = 1d,
Duration = Tile.FadeDuration
});
To = 1d,
Duration = Tile.FadeDuration
});
bottomImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
To = 0d,
BeginTime = Tile.FadeDuration,
Duration = TimeSpan.Zero
});
}
else
bottomImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
topImage.Opacity = 0d;
bottomImage.Opacity = 0d;
bottomImage.Source = null;
}
updateInProgress = false;
To = 0d,
BeginTime = Tile.FadeDuration,
Duration = TimeSpan.Zero
});
}
}
}

View file

@ -19,10 +19,14 @@ namespace MapControl
/// </summary>
public abstract class MapProjection
{
public const int TileSize = 256;
public const double Wgs84EquatorialRadius = 6378137d;
public const double Wgs84Flattening = 1d / 298.257223563;
public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening);
public const double MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
public const int TileSize = 256;
/// <summary>
/// Gets the scaling factor from cartesian map coordinates in degrees to viewport coordinates for the specified zoom level.
/// </summary>

View file

@ -9,11 +9,6 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.Web.Http;
#else
using System.Net.Http;
#endif
namespace MapControl
{
@ -139,14 +134,13 @@ namespace MapControl
}
}
private static DateTime GetExpiration(HttpResponseMessage response)
private static DateTime GetExpiration(TimeSpan? maxAge)
{
var expiration = DefaultCacheExpiration;
var headers = response.Headers;
if (headers.CacheControl != null && headers.CacheControl.MaxAge.HasValue)
if (maxAge.HasValue)
{
expiration = headers.CacheControl.MaxAge.Value;
expiration = maxAge.Value;
if (expiration < MinimumCacheExpiration)
{

View file

@ -8,10 +8,8 @@ using System.Globalization;
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Windows.Media;
using System.Windows.Media.Imaging;
#endif
namespace MapControl
@ -117,18 +115,7 @@ namespace MapControl
{
try
{
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
{
imageSource = await LoadLocalImageAsync(uri);
}
else if (uri.Scheme == "http")
{
imageSource = await LoadHttpImageAsync(uri);
}
else
{
imageSource = new BitmapImage(uri);
}
imageSource = await ImageLoader.LoadImageAsync(uri, true);
}
catch (Exception ex)
{

View file

@ -12,7 +12,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Web Mercator Projection.
/// Transforms map coordinates according to the Web (or Pseudo) Mercator Projection, EPSG:3857.
/// Longitude values are transformed linearly to X values in meters, by multiplying with MetersPerDegree.
/// Latitude values in the interval [-MaxLatitude .. MaxLatitude] are transformed to Y values in meters
/// in the interval [-R*pi .. R*pi], R=Wgs84EquatorialRadius.
@ -70,8 +70,6 @@ namespace MapControl
public static double LatitudeToY(double latitude)
{
var lat = latitude * Math.PI / 180d;
return latitude <= -90d ? double.NegativeInfinity
: latitude >= 90d ? double.PositiveInfinity
: Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d;
@ -79,7 +77,7 @@ namespace MapControl
public static double YToLatitude(double y)
{
return Math.Atan(Math.Exp(y * Math.PI / 180d)) / Math.PI * 360d - 90d;
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) / Math.PI * 360d;
}
}
}

View file

@ -11,12 +11,10 @@ using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Xml;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
#endif
namespace MapControl
@ -25,27 +23,27 @@ namespace MapControl
{
public static readonly DependencyProperty ServerUriProperty = DependencyProperty.Register(
nameof(ServerUri), typeof(Uri), typeof(WmsImageLayer),
new PropertyMetadata(null, (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
public static readonly DependencyProperty VersionProperty = DependencyProperty.Register(
nameof(Version), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata("1.3.0", (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata("1.3.0", async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
public static readonly DependencyProperty LayersProperty = DependencyProperty.Register(
nameof(Layers), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata(string.Empty, (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
public static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
nameof(Styles), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata(string.Empty, (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register(
nameof(Format), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata("image/png", (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
public static readonly DependencyProperty TransparentProperty = DependencyProperty.Register(
nameof(Transparent), typeof(bool), typeof(WmsImageLayer),
new PropertyMetadata(false, (o, e) => ((WmsImageLayer)o).UpdateImage()));
new PropertyMetadata(false, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
private string layers = string.Empty;
@ -85,25 +83,32 @@ namespace MapControl
set { SetValue(TransparentProperty, value); }
}
protected override ImageSource GetImage(BoundingBox boundingBox)
protected override async Task<ImageSource> GetImage(BoundingBox boundingBox)
{
if (ServerUri == null)
ImageSource imageSource = null;
if (ServerUri != null)
{
return null;
var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox, Version);
if (!string.IsNullOrEmpty(projectionParameters))
{
var uri = GetRequestUri("GetMap"
+ "&LAYERS=" + Layers + "&STYLES=" + Styles + "&FORMAT=" + Format
+ "&TRANSPARENT=" + (Transparent ? "TRUE" : "FALSE") + "&" + projectionParameters);
try
{
imageSource = await ImageLoader.LoadImageAsync(uri, false);
}
catch (Exception ex)
{
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
}
}
}
var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox, Version);
if (string.IsNullOrEmpty(projectionParameters))
{
return null;
}
var uri = GetRequestUri("GetMap"
+ "&LAYERS=" + Layers + "&STYLES=" + Styles + "&FORMAT=" + Format
+ "&TRANSPARENT=" + (Transparent ? "TRUE" : "FALSE") + "&" + projectionParameters);
return new BitmapImage(uri);
return imageSource;
}
public async Task<IList<string>> GetLayerNamesAsync()

View file

@ -0,0 +1,116 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if WINDOWS_UWP
using Windows.Foundation;
#else
using System.Windows;
#endif
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the "World Mercator" Projection, EPSG:3395.
/// Longitude values are transformed linearly to X values in meters, by multiplying with MetersPerDegree.
/// Latitude values are transformed according to the elliptical versions of the Mercator equations,
/// as shown in "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44.
/// </summary>
public class WorldMercatorProjection : MapProjection
{
public static double MinLatitudeDelta = 1d / Wgs84EquatorialRadius; // corresponds to 1 meter
public static int MaxIterations = 10;
public WorldMercatorProjection()
: this("EPSG:3395")
{
}
public WorldMercatorProjection(string crsId)
{
CrsId = crsId;
LongitudeScale = MetersPerDegree;
MaxLatitude = YToLatitude(180d);
}
public override double GetViewportScale(double zoomLevel)
{
return DegreesToViewportScale(zoomLevel) / MetersPerDegree;
}
public override Point GetMapScale(Location location)
{
var lat = location.Latitude * Math.PI / 180d;
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
var scale = ViewportScale * Math.Sqrt(1d - eSinLat * eSinLat) / Math.Cos(lat);
return new Point(scale, scale);
}
public override Point LocationToPoint(Location location)
{
return new Point(
MetersPerDegree * location.Longitude,
MetersPerDegree * LatitudeToY(location.Latitude));
}
public override Location PointToLocation(Point point)
{
return new Location(
YToLatitude(point.Y / MetersPerDegree),
point.X / MetersPerDegree);
}
public override Location TranslateLocation(Location location, Point translation)
{
var scaleX = MetersPerDegree * ViewportScale;
var scaleY = scaleX / Math.Cos(location.Latitude * Math.PI / 180d);
return new Location(
location.Latitude - translation.Y / scaleY,
location.Longitude + translation.X / scaleX);
}
public static double LatitudeToY(double latitude)
{
if (latitude <= -90d)
{
return double.NegativeInfinity;
}
if (latitude >= 90d)
{
return double.PositiveInfinity;
}
var lat = latitude * Math.PI / 180d;
return Math.Log(Math.Tan(lat / 2d + Math.PI / 4d) * ConformalFactor(lat)) / Math.PI * 180d;
}
public static double YToLatitude(double y)
{
var t = Math.Exp(-y * Math.PI / 180d);
var lat = Math.PI / 2d - 2d * Math.Atan(t);
var latDelta = 1d;
for (int i = 0; i < MaxIterations && latDelta > MinLatitudeDelta; i++)
{
var newLat = Math.PI / 2d - 2d * Math.Atan(t * ConformalFactor(lat));
latDelta = Math.Abs(newLat - lat);
lat = newLat;
}
return lat / Math.PI * 180d;
}
private static double ConformalFactor(double lat)
{
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
return Math.Pow((1d - eSinLat) / (1d + eSinLat), Wgs84Eccentricity / 2d);
}
}
}

View file

@ -0,0 +1,110 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Web.Http;
using Windows.Web.Http.Headers;
namespace MapControl
{
public static class ImageLoader
{
/// <summary>
/// The HttpClient instance used when image data is downloaded from a web resource.
/// </summary>
public static HttpClient HttpClient { get; set; } = new HttpClient();
public static async Task<ImageSource> LoadImageAsync(Uri uri, bool isTileImage)
{
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
{
return await LoadLocalImageAsync(uri);
}
if (uri.Scheme == "http")
{
return await LoadHttpImageAsync(uri, isTileImage);
}
return new BitmapImage(uri);
}
public static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
if (!await Task.Run(() => File.Exists(path)))
{
return null;
}
var file = await StorageFile.GetFileFromPathAsync(path);
using (var stream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(stream);
return bitmapImage;
}
}
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri, bool isTileImage)
{
using (var response = await HttpClient.GetAsync(uri))
{
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (!isTileImage || IsTileAvailable(response.Headers))
{
using (var stream = new InMemoryRandomAccessStream())
{
await response.Content.WriteToStreamAsync(stream);
stream.Seek(0);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(stream);
return bitmapImage;
}
}
return null;
}
}
public static async Task<bool> LoadHttpTileImageAsync(Uri uri, Func<IBuffer, TimeSpan?, Task> tileCallback)
{
using (var response = await HttpClient.GetAsync(uri))
{
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (IsTileAvailable(response.Headers))
{
var buffer = await response.Content.ReadAsBufferAsync();
await tileCallback(buffer, response.Headers.CacheControl?.MaxAge);
}
return response.IsSuccessStatusCode;
}
}
private static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
{
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
}
}
}

View file

@ -136,13 +136,15 @@
<Compile Include="..\Shared\WmsImageLayer.cs">
<Link>WmsImageLayer.cs</Link>
</Compile>
<Compile Include="..\Shared\WorldMercatorProjection.cs">
<Link>WorldMercatorProjection.cs</Link>
</Compile>
<Compile Include="Extensions.UWP.cs" />
<Compile Include="ImageCache.UWP.cs" />
<Compile Include="ImageFileCache.UWP.cs" />
<Compile Include="Map.UWP.cs" />
<Compile Include="MapBase.UWP.cs" />
<Compile Include="MapGraticule.UWP.cs" />
<Compile Include="MapImageLayer.UWP.cs" />
<Compile Include="MapOverlay.UWP.cs" />
<Compile Include="MapPanel.UWP.cs" />
<Compile Include="MapPath.UWP.cs" />
@ -151,7 +153,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tile.UWP.cs" />
<Compile Include="TileImageLoader.UWP.cs" />
<Compile Include="TileSource.UWP.cs" />
<Compile Include="ImageLoader.UWP.cs" />
<EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" />
</ItemGroup>
<ItemGroup>

View file

@ -1,52 +0,0 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace MapControl
{
public partial class MapImageLayer
{
protected void UpdateImage(ImageSource imageSource)
{
SetTopImage(imageSource);
var bitmapImage = imageSource as BitmapImage;
if (bitmapImage != null)
{
bitmapImage.ImageOpened += BitmapImageOpened;
bitmapImage.ImageFailed += BitmapImageFailed;
}
else
{
SwapImages();
}
}
private void BitmapImageOpened(object sender, RoutedEventArgs e)
{
var bitmapImage = (BitmapImage)sender;
bitmapImage.ImageOpened -= BitmapImageOpened;
bitmapImage.ImageFailed -= BitmapImageFailed;
SwapImages();
}
private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)
{
var bitmapImage = (BitmapImage)sender;
bitmapImage.ImageOpened -= BitmapImageOpened;
bitmapImage.ImageFailed -= BitmapImageFailed;
((Image)Children[topImageIndex]).Source = null;
SwapImages();
}
}
}

View file

@ -36,50 +36,30 @@ namespace MapControl
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
var cacheItem = await Cache.GetAsync(cacheKey);
var buffer = cacheItem?.Buffer;
var cacheBuffer = cacheItem?.Buffer;
var loaded = false;
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
if (cacheBuffer == null || cacheItem.Expiration < DateTime.UtcNow)
{
loaded = await DownloadTileImageAsync(tile, uri, cacheKey);
}
if (!loaded && buffer != null) // keep expired image if download failed
{
await SetTileImageAsync(tile, buffer);
}
}
private async Task<bool> DownloadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
var success = false;
try
{
using (var response = await TileSource.HttpClient.GetAsync(uri))
try
{
success = response.IsSuccessStatusCode;
if (!success)
loaded = await ImageLoader.LoadHttpTileImageAsync(uri, async (buffer, maxAge) =>
{
Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (TileSource.TileAvailable(response.Headers))
{
var buffer = await response.Content.ReadAsBufferAsync();
await SetTileImageAsync(tile, buffer); // create BitmapImage before caching
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response));
}
await Cache.SetAsync(cacheKey, buffer, GetExpiration(maxAge));
});
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
}
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
}
return success;
if (!loaded && cacheBuffer != null) // keep expired image if download failed
{
await SetTileImageAsync(tile, cacheBuffer);
}
}
private async Task SetTileImageAsync(Tile tile, IBuffer buffer)

View file

@ -25,14 +25,14 @@ namespace MapControl
/// <summary>
/// Check HTTP response headers for tile availability, e.g. X-VE-Tile-Info=no-tile
/// </summary>
public static bool TileAvailable(HttpResponseHeaderCollection responseHeaders)
public static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
{
string tileInfo;
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out tileInfo) || tileInfo != "no-tile";
}
protected async Task<ImageSource> LoadLocalImageAsync(Uri uri)
protected static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
@ -52,7 +52,7 @@ namespace MapControl
}
}
protected async Task<ImageSource> LoadHttpImageAsync(Uri uri)
protected static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
{
using (var response = await HttpClient.GetAsync(uri))
{
@ -60,7 +60,7 @@ namespace MapControl
{
Debug.WriteLine("TileSource: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (TileAvailable(response.Headers))
else if (IsTileAvailable(response.Headers))
{
using (var stream = new InMemoryRandomAccessStream())
{

View file

@ -0,0 +1,110 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl
{
public static class ImageLoader
{
/// <summary>
/// The HttpClient instance used when image data is downloaded from a web resource.
/// </summary>
public static HttpClient HttpClient { get; set; } = new HttpClient();
public static async Task<ImageSource> LoadImageAsync(Uri uri, bool isTileImage)
{
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
{
return await LoadLocalImageAsync(uri);
}
if (uri.Scheme == "http")
{
return await LoadHttpImageAsync(uri, isTileImage);
}
return new BitmapImage(uri);
}
public static Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
return Task.Run(() =>
{
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
if (!File.Exists(path))
{
return null;
}
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return (ImageSource)BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
});
}
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri, bool isTileImage)
{
using (var response = await HttpClient.GetAsync(uri))
{
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (!isTileImage || IsTileAvailable(response.Headers))
{
using (var stream = new MemoryStream())
{
await response.Content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
return null;
}
}
public static async Task<bool> LoadHttpTileImageAsync(Uri uri, Func<MemoryStream, TimeSpan?, Task> tileCallback)
{
using (var response = await HttpClient.GetAsync(uri))
{
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (IsTileAvailable(response.Headers))
{
var stream = new MemoryStream();
await response.Content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
await tileCallback(stream, response.Headers.CacheControl?.MaxAge);
}
return response.IsSuccessStatusCode;
}
}
private static bool IsTileAvailable(HttpResponseHeaders responseHeaders)
{
IEnumerable<string> tileInfo;
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
}
}
}

View file

@ -155,17 +155,19 @@
<Compile Include="..\Shared\WmsImageLayer.cs">
<Link>WmsImageLayer.cs</Link>
</Compile>
<Compile Include="..\Shared\WorldMercatorProjection.cs">
<Link>WorldMercatorProjection.cs</Link>
</Compile>
<Compile Include="ImageFileCache.WPF.cs" />
<Compile Include="Map.WPF.cs" />
<Compile Include="MapBase.WPF.cs" />
<Compile Include="MapGraticule.WPF.cs" />
<Compile Include="MapImageLayer.WPF.cs" />
<Compile Include="MapOverlay.WPF.cs" />
<Compile Include="MapPanel.WPF.cs" />
<Compile Include="MapPath.WPF.cs" />
<Compile Include="MapPolyline.WPF.cs" />
<Compile Include="TileImageLoader.WPF.cs" />
<Compile Include="TileSource.WPF.cs" />
<Compile Include="ImageLoader.WPF.cs" />
<Compile Include="TypeConverters.WPF.cs" />
<Compile Include="MatrixEx.WPF.cs" />
<Compile Include="Properties\AssemblyInfo.cs">

View file

@ -42,55 +42,59 @@ namespace MapControl
if (projection != null && !double.IsNaN(projection.LongitudeScale))
{
var bounds = projection.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize));
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
var latLabelStart = Math.Ceiling(bounds.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(bounds.West / lineDistance) * lineDistance;
var latLabels = new List<Label>((int)((bounds.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((bounds.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pen = new Pen
if (bounds.HasValidBounds)
{
Brush = Stroke,
Thickness = StrokeThickness,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset),
DashCap = StrokeDashCap
};
for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
{
latLabels.Add(new Label(lat, new FormattedText(
GetLabelText(lat, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(lat, bounds.West)),
projection.LocationToViewportPoint(new Location(lat, bounds.East)));
}
for (var lon = lonLabelStart; lon <= bounds.East; lon += lineDistance)
{
lonLabels.Add(new Label(lon, new FormattedText(
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(bounds.South, lon)),
projection.LocationToViewportPoint(new Location(bounds.North, lon)));
}
foreach (var latLabel in latLabels)
{
foreach (var lonLabel in lonLabels)
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
var latLabelStart = Math.Ceiling(bounds.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(bounds.West / lineDistance) * lineDistance;
var latLabels = new List<Label>((int)((bounds.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((bounds.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pen = new Pen
{
var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
Brush = Stroke,
Thickness = StrokeThickness,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset),
DashCap = StrokeDashCap
};
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
drawingContext.DrawText(latLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
drawingContext.DrawText(lonLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
drawingContext.Pop();
for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
{
latLabels.Add(new Label(lat, new FormattedText(
GetLabelText(lat, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(lat, bounds.West)),
projection.LocationToViewportPoint(new Location(lat, bounds.East)));
}
for (var lon = lonLabelStart; lon <= bounds.East; lon += lineDistance)
{
lonLabels.Add(new Label(lon, new FormattedText(
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(bounds.South, lon)),
projection.LocationToViewportPoint(new Location(bounds.North, lon)));
}
foreach (var latLabel in latLabels)
{
foreach (var lonLabel in lonLabels)
{
var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
drawingContext.DrawText(latLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
drawingContext.DrawText(lonLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
drawingContext.Pop();
}
}
}
}

View file

@ -1,52 +0,0 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl
{
public partial class MapImageLayer
{
protected void UpdateImage(ImageSource imageSource)
{
SetTopImage(imageSource);
var bitmapSource = imageSource as BitmapSource;
if (bitmapSource != null && !bitmapSource.IsFrozen && bitmapSource.IsDownloading)
{
bitmapSource.DownloadCompleted += BitmapDownloadCompleted;
bitmapSource.DownloadFailed += BitmapDownloadFailed;
}
else
{
SwapImages();
}
}
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
var bitmapSource = (BitmapSource)sender;
bitmapSource.DownloadCompleted -= BitmapDownloadCompleted;
bitmapSource.DownloadFailed -= BitmapDownloadFailed;
SwapImages();
}
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
var bitmapSource = (BitmapSource)sender;
bitmapSource.DownloadCompleted -= BitmapDownloadCompleted;
bitmapSource.DownloadFailed -= BitmapDownloadFailed;
((Image)Children[topImageIndex]).Source = null;
SwapImages();
}
}
}

View file

@ -42,7 +42,19 @@ namespace MapControl
if (buffer == null || expiration < DateTime.UtcNow)
{
loaded = await DownloadTileImageAsync(tile, uri, cacheKey);
try
{
loaded = await ImageLoader.LoadHttpTileImageAsync(uri, async (stream, maxAge) =>
{
await SetTileImageAsync(tile, stream); // create BitmapFrame before caching
SetCachedImage(cacheKey, stream, GetExpiration(maxAge));
});
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
}
}
if (!loaded && buffer != null) // keep expired image if download failed
@ -54,43 +66,6 @@ namespace MapControl
}
}
private async Task<bool> DownloadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
var success = false;
try
{
using (var response = await TileSource.HttpClient.GetAsync(uri))
{
success = response.IsSuccessStatusCode;
if (!success)
{
Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (TileSource.TileAvailable(response.Headers))
{
using (var stream = new MemoryStream())
{
await response.Content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
await SetTileImageAsync(tile, stream); // create BitmapFrame before caching
SetCachedImage(cacheKey, stream, GetExpiration(response));
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
}
return success;
}
private async Task SetTileImageAsync(Tile tile, MemoryStream stream)
{
var imageSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

View file

@ -1,76 +0,0 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl
{
public partial class TileSource
{
/// <summary>
/// The HttpClient instance used when image data is downloaded from a web resource.
/// </summary>
public static HttpClient HttpClient { get; set; } = new HttpClient();
/// <summary>
/// Check HTTP response headers for tile availability, e.g. X-VE-Tile-Info=no-tile
/// </summary>
public static bool TileAvailable(HttpResponseHeaders responseHeaders)
{
IEnumerable<string> tileInfo;
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
}
protected Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
return Task.Run(() =>
{
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
if (File.Exists(path))
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return (ImageSource)BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
return null;
});
}
protected async Task<ImageSource> LoadHttpImageAsync(Uri uri)
{
using (var response = await HttpClient.GetAsync(uri))
{
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("TileSource: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
}
else if (TileAvailable(response.Headers))
{
using (var stream = new MemoryStream())
{
await response.Content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
return await Task.Run(() => BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad));
}
}
}
return null;
}
}
}

View file

@ -89,6 +89,7 @@
</Style>
<map:WebMercatorProjection x:Key="WebMercatorProjection"/>
<map:WorldMercatorProjection x:Key="WorldMercatorProjection"/>
<map:EquirectangularProjection x:Key="EquirectangularProjection"/>
<map:OrthographicProjection x:Key="OrthographicProjection"/>
<map:GnomonicProjection x:Key="GnomonicProjection"/>
@ -181,6 +182,7 @@
SelectedIndex="0" SelectedValuePath="Tag"
SelectedValue="{Binding MapProjection, ElementName=map, Mode=TwoWay}">
<ComboBoxItem Content="Web Mercator" Tag="{StaticResource WebMercatorProjection}"/>
<ComboBoxItem Content="World Mercator" Tag="{StaticResource WorldMercatorProjection}"/>
<ComboBoxItem Content="Equirectangular" Tag="{StaticResource EquirectangularProjection}"/>
<ComboBoxItem Content="Orthographic" Tag="{StaticResource OrthographicProjection}"/>
<ComboBoxItem Content="Gnomonic" Tag="{StaticResource GnomonicProjection}"/>

View file

@ -108,6 +108,7 @@
</Style>
<map:WebMercatorProjection x:Key="WebMercatorProjection"/>
<map:WorldMercatorProjection x:Key="WorldMercatorProjection"/>
<map:EquirectangularProjection x:Key="EquirectangularProjection"/>
<map:OrthographicProjection x:Key="OrthographicProjection"/>
<map:GnomonicProjection x:Key="GnomonicProjection"/>
@ -212,6 +213,7 @@
<ComboBox x:Name="projectionComboBox" ToolTip="Map Projection" Width="120" Margin="5" VerticalAlignment="Bottom"
SelectedValuePath="Tag" SelectedIndex="0">
<ComboBoxItem Content="Web Mercator" Tag="{StaticResource WebMercatorProjection}"/>
<ComboBoxItem Content="World Mercator" Tag="{StaticResource WorldMercatorProjection}"/>
<ComboBoxItem Content="Equirectangular" Tag="{StaticResource EquirectangularProjection}"/>
<ComboBoxItem Content="Orthographic" Tag="{StaticResource OrthographicProjection}"/>
<ComboBoxItem Content="Gnomonic" Tag="{StaticResource GnomonicProjection}"/>

View file

@ -81,10 +81,6 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FileDbCache\WPF\FileDbCache.WPF.csproj">
<Project>{ad1cb53e-7aa4-4ec0-b901-b4e0e2665133}</Project>
<Name>FileDbCache.WPF</Name>
</ProjectReference>
<ProjectReference Include="..\..\MapControl\WPF\MapControl.WPF.csproj">
<Project>{a204a102-c745-4d65-aec8-7b96faedef2d}</Project>
<Name>MapControl.WPF</Name>