2026-01-08 22:34:42 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
#if WPF
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
2020-03-26 19:08:20 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Defines the transformation between projected map coordinates in meters
|
2020-03-28 21:53:38 +01:00
|
|
|
|
/// and view coordinates in pixels.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// </summary>
|
2026-01-01 12:50:19 +01:00
|
|
|
|
public class ViewTransform
|
2020-03-26 19:08:20 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <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.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
public double Scale { get; private set; }
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
/// Gets the rotation angle of the transform matrix.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
public double Rotation { get; private set; }
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Gets the transform matrix from projected map coordinates to view coordinates.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
public Matrix MapToViewMatrix { get; private set; }
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Gets the transform matrix from view coordinates to projected map coordinates.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
public Matrix ViewToMapMatrix { get; private set; }
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
2026-01-07 19:39:28 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Point in projected map coordinates to a Point in view coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point MapToView(Point point) => MapToViewMatrix.Transform(point);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Point in view coordinates to a Point in projected map coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point ViewToMap(Point point) => ViewToMapMatrix.Transform(point);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets an axis-aligned bounding box in projected map coordinates that contains
|
|
|
|
|
|
/// a rectangle in view coordinates.
|
|
|
|
|
|
/// </summary>
|
2026-01-08 22:49:25 +01:00
|
|
|
|
public Rect ViewToMapBounds(Rect rect) => TransformBounds(ViewToMapMatrix, rect.X, rect.Y, rect.Width, rect.Height);
|
2026-01-07 19:39:28 +01:00
|
|
|
|
|
2024-08-29 21:35:58 +02:00
|
|
|
|
/// <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;
|
2026-01-01 21:49:21 +01:00
|
|
|
|
|
|
|
|
|
|
var transform = new Matrix(scale, 0d, 0d, -scale, -scale * mapCenter.X, scale * mapCenter.Y);
|
2024-08-29 21:35:58 +02:00
|
|
|
|
transform.Rotate(Rotation);
|
|
|
|
|
|
transform.Translate(viewCenter.X, viewCenter.Y);
|
|
|
|
|
|
MapToViewMatrix = transform;
|
|
|
|
|
|
|
|
|
|
|
|
transform.Invert();
|
|
|
|
|
|
ViewToMapMatrix = transform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// <summary>
|
2026-01-06 11:56:28 +01:00
|
|
|
|
/// Gets the transform Matrix for the RenderTranform of a MapTileLayer or WmtsTileMatrixLayer.
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// </summary>
|
2026-01-06 11:56:28 +01:00
|
|
|
|
public Matrix GetTileLayerTransform(double tileMatrixScale, Point tileMatrixTopLeft, Point tileMatrixOrigin)
|
2024-05-22 09:42:21 +02:00
|
|
|
|
{
|
2026-01-06 11:56:28 +01:00
|
|
|
|
var scale = Scale / tileMatrixScale;
|
|
|
|
|
|
var transform = new Matrix(scale, 0d, 0d, scale, 0d, 0d);
|
2024-05-22 09:42:21 +02:00
|
|
|
|
transform.Rotate(Rotation);
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Tile matrix origin in map coordinates.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
//
|
|
|
|
|
|
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.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
//
|
2025-12-13 18:28:07 +01:00
|
|
|
|
var viewOrigin = MapToViewMatrix.Transform(mapOrigin);
|
2020-03-26 19:08:20 +01:00
|
|
|
|
transform.Translate(viewOrigin.X, viewOrigin.Y);
|
2026-01-06 11:56:28 +01:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
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>
|
2024-07-15 20:10:28 +02:00
|
|
|
|
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, double viewWidth, double viewHeight)
|
2020-03-26 19:08:20 +01:00
|
|
|
|
{
|
2026-01-06 11:56:28 +01:00
|
|
|
|
var scale = tileMatrixScale / Scale;
|
|
|
|
|
|
var transform = new Matrix(scale, 0d, 0d, scale, 0d, 0d);
|
|
|
|
|
|
transform.Rotate(-Rotation);
|
|
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// View origin in map coordinates.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
//
|
2025-12-13 18:28:07 +01:00
|
|
|
|
var origin = ViewToMapMatrix.Transform(new Point());
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
2026-01-01 21:49:21 +01:00
|
|
|
|
// Translation from origin to tile matrix origin in pixels.
|
|
|
|
|
|
//
|
2026-01-06 11:56:28 +01:00
|
|
|
|
transform.Translate(
|
|
|
|
|
|
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
|
|
|
|
|
|
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
|
2020-03-26 19:08:20 +01:00
|
|
|
|
|
2022-11-30 22:18:45 +01:00
|
|
|
|
// Transform view bounds to tile pixel bounds.
|
2020-03-26 19:08:20 +01:00
|
|
|
|
//
|
2026-01-08 22:49:25 +01:00
|
|
|
|
return TransformBounds(transform, 0d, 0d, viewWidth, viewHeight);
|
2026-01-05 21:27:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 22:49:25 +01:00
|
|
|
|
private static Rect TransformBounds(Matrix transform, double x, double y, double width, double height)
|
2026-01-05 21:27:00 +01:00
|
|
|
|
{
|
2026-01-08 22:34:42 +01:00
|
|
|
|
if (transform.M12 == 0d && transform.M21 == 0d)
|
|
|
|
|
|
{
|
2026-01-08 23:06:48 +01:00
|
|
|
|
x = x * transform.M11 + transform.OffsetX;
|
|
|
|
|
|
y = y * transform.M22 + transform.OffsetY;
|
|
|
|
|
|
width *= transform.M11;
|
|
|
|
|
|
height *= transform.M22;
|
2026-01-09 07:03:53 +01:00
|
|
|
|
|
|
|
|
|
|
if (width < 0d)
|
|
|
|
|
|
{
|
|
|
|
|
|
width = -width;
|
|
|
|
|
|
x -= width;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (height < 0d)
|
|
|
|
|
|
{
|
|
|
|
|
|
height = -height;
|
|
|
|
|
|
y -= height;
|
|
|
|
|
|
}
|
2026-01-08 23:06:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var p1 = transform.Transform(new Point(x, y));
|
|
|
|
|
|
var p2 = transform.Transform(new Point(x, y + height));
|
|
|
|
|
|
var p3 = transform.Transform(new Point(x + width, y));
|
|
|
|
|
|
var p4 = transform.Transform(new Point(x + width, y + height));
|
|
|
|
|
|
|
|
|
|
|
|
x = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X)));
|
|
|
|
|
|
y = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y)));
|
|
|
|
|
|
width = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X))) - x;
|
|
|
|
|
|
height = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y))) - y;
|
2026-01-08 22:34:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 23:06:48 +01:00
|
|
|
|
return new Rect(x, y, width, height);
|
2026-01-01 12:50:19 +01:00
|
|
|
|
}
|
2020-03-26 19:08:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|