mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 06:26:41 +00:00
Added WorldMercatorProjection, fixed bounding box problems when changing projection.
This commit is contained in:
parent
2f123886ff
commit
156ebfe177
21 changed files with 586 additions and 469 deletions
|
|
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
116
MapControl/Shared/WorldMercatorProjection.cs
Normal file
116
MapControl/Shared/WorldMercatorProjection.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue