Version 5.0: Reworked MapBase and MapPath

This commit is contained in:
ClemensF 2020-03-28 21:53:38 +01:00
parent 06fd31c17b
commit 49e15ce424
41 changed files with 466 additions and 1068 deletions

View file

@ -2,6 +2,7 @@
// © 2020 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if WINDOWS_UWP
using Windows.Foundation;
using Windows.UI.Xaml.Media;
@ -13,22 +14,14 @@ using System.Windows.Media;
namespace MapControl
{
/// <summary>
/// Defines the transformation between cartesian map coordinates and viewport coordinates.
/// Defines the transformation between cartesian map coordinates in meters
/// and view coordinates in pixels.
/// </summary>
public class ViewTransform
{
/// <summary>
/// Gets the transform matrix from cartesian map coordinates to viewport coordinates.
/// </summary>
public Matrix MapToViewMatrix { get; private set; }
/// <summary>
/// Gets the transform matrix from viewport coordinates to cartesian map coordinates.
/// </summary>
public Matrix ViewToMapMatrix { get; private set; }
/// <summary>
/// Gets the scaling factor from cartesian map coordinates to viewport coordinates.
/// Gets the scaling factor from cartesian map coordinates to view coordinates,
/// i.e. pixels per meter.
/// </summary>
public double Scale { get; private set; }
@ -38,7 +31,17 @@ namespace MapControl
public double Rotation { get; private set; }
/// <summary>
/// Transforms a Point from cartesian map coordinates to viewport coordinates.
/// Gets the transform matrix from cartesian map coordinates to view coordinates.
/// </summary>
public Matrix MapToViewMatrix { get; private set; }
/// <summary>
/// Gets the transform matrix from view coordinates to cartesian map coordinates.
/// </summary>
public Matrix ViewToMapMatrix { get; private set; }
/// <summary>
/// Transforms a Point from cartesian map coordinates to view coordinates.
/// </summary>
public Point MapToView(Point point)
{
@ -46,14 +49,14 @@ namespace MapControl
}
/// <summary>
/// Transforms a Point from viewport coordinates to cartesian map coordinates.
/// Transforms a Point from view coordinates to cartesian map coordinates.
/// </summary>
public Point ViewToMap(Point point)
{
return ViewToMapMatrix.Transform(point);
}
public void SetTransform(Point mapCenter, Point viewportCenter, double scale, double rotation)
public void SetTransform(Point mapCenter, Point viewCenter, double scale, double rotation)
{
Scale = scale;
Rotation = rotation;
@ -61,7 +64,7 @@ namespace MapControl
var transform = new Matrix(Scale, 0d, 0d, -Scale, -Scale * mapCenter.X, Scale * mapCenter.Y);
transform.Rotate(Rotation);
transform.Translate(viewportCenter.X, viewportCenter.Y);
transform.Translate(viewCenter.X, viewCenter.Y);
MapToViewMatrix = transform;
@ -83,7 +86,7 @@ namespace MapControl
tileMatrixTopLeft.X + tileMatrixOrigin.X / tileMatrixScale,
tileMatrixTopLeft.Y - tileMatrixOrigin.Y / tileMatrixScale);
// tile matrix origin in viewport coordinates
// tile matrix origin in view coordinates
//
var viewOrigin = MapToView(mapOrigin);
@ -92,14 +95,14 @@ namespace MapControl
return transform;
}
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, Size viewportSize)
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, Size viewSize)
{
var transformScale = tileMatrixScale / Scale;
var transform = new Matrix(transformScale, 0d, 0d, transformScale, 0d, 0d);
transform.Rotate(-Rotation);
// viewport origin in map coordinates
// view origin in map coordinates
//
var origin = ViewToMap(new Point());
@ -109,10 +112,20 @@ namespace MapControl
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
// transform viewport bounds to tile pixel bounds
// transform view bounds to tile pixel bounds
//
return new MatrixTransform { Matrix = transform }
.TransformBounds(new Rect(0d, 0d, viewportSize.Width, viewportSize.Height));
.TransformBounds(new Rect(0d, 0d, viewSize.Width, viewSize.Height));
}
public static double ZoomLevelToScale(double zoomLevel)
{
return 256d * Math.Pow(2d, zoomLevel) / (360d * MapProjection.Wgs84MetersPerDegree);
}
public static double ScaleToZoomLevel(double scale)
{
return Math.Log(scale * 360d * MapProjection.Wgs84MetersPerDegree / 256d, 2d);
}
}
}