XAML-Map-Control/MapControl/Shared/ViewTransform.cs

165 lines
5.5 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
#if WPF
2024-05-22 11:25:32 +02:00
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl
{
/// <summary>
2022-11-05 17:32:29 +01:00
/// Defines the transformation between projected map coordinates in meters
/// and view coordinates in pixels.
/// </summary>
2024-08-29 21:35:58 +02:00
public partial class ViewTransform
{
/// <summary>
2022-11-05 17:32:29 +01:00
/// Gets the scaling factor from projected map coordinates to view coordinates,
2022-11-07 21:10:10 +01:00
/// as pixels per meter.
/// </summary>
public double Scale { get; private set; }
/// <summary>
/// Gets the rotation angle of the transform matrix.
/// </summary>
public double Rotation { get; private set; }
/// <summary>
2022-11-05 17:32:29 +01:00
/// Gets the transform matrix from projected map coordinates to view coordinates.
/// </summary>
public Matrix MapToViewMatrix { get; private set; }
/// <summary>
2022-11-05 17:32:29 +01:00
/// Gets the transform matrix from view coordinates to projected map coordinates.
/// </summary>
public Matrix ViewToMapMatrix { get; private set; }
/// <summary>
2022-11-05 17:32:29 +01:00
/// Transforms a Point from projected map coordinates to view coordinates.
/// </summary>
public Point MapToView(Point point)
{
return MapToViewMatrix.Transform(point);
}
/// <summary>
2022-11-05 17:32:29 +01:00
/// Transforms a Point from view coordinates to projected map coordinates.
/// </summary>
public Point ViewToMap(Point point)
{
return ViewToMapMatrix.Transform(point);
}
2024-05-22 09:42:21 +02:00
/// <summary>
2024-08-29 21:35:58 +02:00
/// Transform relative to absolute map scale. Returns horizontal and vertical
/// scaling factors from meters to view coordinates.
2024-05-22 09:42:21 +02:00
/// </summary>
public Point GetMapScale(Point relativeScale)
{
2024-05-22 09:42:21 +02:00
return new Point(Scale * relativeScale.X, Scale * relativeScale.Y);
}
2024-08-29 21:35:58 +02:00
#if WPF || UWP || WINUI
/// <summary>
/// Initializes a ViewTransform from a map center point in projected coordinates,
/// a view conter point, a scaling factor from projected coordinates to view coordinates
/// and a rotation angle in degrees.
/// </summary>
public void SetTransform(Point mapCenter, Point viewCenter, double scale, double rotation)
{
Scale = scale;
Rotation = ((rotation % 360d) + 360d) % 360d;
var transform = new Matrix(scale, 0d, 0d, -scale, -scale * mapCenter.X, scale * mapCenter.Y);
transform.Rotate(Rotation);
transform.Translate(viewCenter.X, viewCenter.Y);
MapToViewMatrix = transform;
transform.Invert();
ViewToMapMatrix = transform;
}
2024-05-22 09:42:21 +02:00
/// <summary>
/// Gets a transform Matrix from meters to view coordinates for a relative map scale.
/// </summary>
public Matrix GetMapTransform(Point relativeScale)
{
var scale = GetMapScale(relativeScale);
2024-05-22 09:42:21 +02:00
var transform = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
transform.Rotate(Rotation);
2024-05-22 09:42:21 +02:00
return transform;
}
2024-05-22 09:42:21 +02:00
/// <summary>
/// Gets the transform Matrix for the RenderTranform of a MapTileLayer.
/// </summary>
public Matrix GetTileLayerTransform(double tileMatrixScale, Point tileMatrixTopLeft, Point tileMatrixOrigin)
{
2022-11-30 22:18:45 +01:00
// Tile matrix origin in map coordinates.
//
var mapOrigin = new Point(
tileMatrixTopLeft.X + tileMatrixOrigin.X / tileMatrixScale,
tileMatrixTopLeft.Y - tileMatrixOrigin.Y / tileMatrixScale);
2022-11-30 22:18:45 +01:00
// Tile matrix origin in view coordinates.
//
var viewOrigin = MapToView(mapOrigin);
2024-05-18 22:03:22 +02:00
var transformScale = Scale / tileMatrixScale;
var transform = new Matrix(transformScale, 0d, 0d, transformScale, 0d, 0d);
transform.Rotate(Rotation);
transform.Translate(viewOrigin.X, viewOrigin.Y);
return transform;
}
2024-05-22 09:42:21 +02:00
/// <summary>
2025-11-21 15:52:38 +01:00
/// Gets the pixel bounds of a tile matrix.
2024-05-22 09:42:21 +02:00
/// </summary>
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, double viewWidth, double viewHeight)
{
2022-11-30 22:18:45 +01:00
// View origin in map coordinates.
//
var origin = ViewToMap(new Point());
2024-05-18 22:03:22 +02:00
var transformScale = tileMatrixScale / Scale;
var transform = new Matrix(transformScale, 0d, 0d, transformScale, 0d, 0d);
transform.Rotate(-Rotation);
2022-11-30 22:18:45 +01:00
// Translate origin to tile matrix origin in pixels.
//
transform.Translate(
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
2022-11-30 22:18:45 +01:00
// Transform view bounds to tile pixel bounds.
//
return new MatrixTransform { Matrix = transform }
.TransformBounds(new Rect(0d, 0d, viewWidth, viewHeight));
}
2024-05-22 09:42:21 +02:00
internal static Matrix CreateTransformMatrix(
double translation1X, double translation1Y,
double rotation,
double translation2X, double translation2Y)
{
var transform = new Matrix(1d, 0d, 0d, 1d, translation1X, translation1Y);
transform.Rotate(rotation);
transform.Translate(translation2X, translation2Y);
return transform;
}
2024-08-29 21:35:58 +02:00
#endif
}
}