2024-05-19 23:23:27 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2025-01-01 18:57:55 +01:00
|
|
|
|
// Copyright © Clemens Fischer
|
2024-05-19 23:23:27 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-08-29 21:35:58 +02:00
|
|
|
|
public partial class ViewTransform
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-22 09:42:21 +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;
|
|
|
|
|
|
|
|
|
|
|
|
MapToViewMatrix
|
|
|
|
|
|
= Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
|
|
|
|
|
|
* Matrix.CreateScale(scale, -scale)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
|
2024-05-22 09:42:21 +02:00
|
|
|
|
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
|
|
|
|
|
|
|
|
|
|
|
|
ViewToMapMatrix = MapToViewMatrix.Invert();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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-19 23:23:27 +02:00
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
return Matrix.CreateScale(scale.X, scale.Y)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
* Matrix.CreateRotation(Matrix.ToRadians(Rotation));
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the transform Matrix for the RenderTranform of a MapTileLayer.
|
|
|
|
|
|
/// </summary>
|
2024-05-19 23:23:27 +02:00
|
|
|
|
public Matrix GetTileLayerTransform(double tileMatrixScale, Point tileMatrixTopLeft, Point tileMatrixOrigin)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Tile matrix origin in map coordinates.
|
|
|
|
|
|
//
|
|
|
|
|
|
var mapOrigin = new Point(
|
|
|
|
|
|
tileMatrixTopLeft.X + tileMatrixOrigin.X / tileMatrixScale,
|
|
|
|
|
|
tileMatrixTopLeft.Y - tileMatrixOrigin.Y / tileMatrixScale);
|
|
|
|
|
|
|
|
|
|
|
|
// Tile matrix origin in view coordinates.
|
|
|
|
|
|
//
|
|
|
|
|
|
var viewOrigin = MapToView(mapOrigin);
|
|
|
|
|
|
|
|
|
|
|
|
var transformScale = Scale / tileMatrixScale;
|
|
|
|
|
|
|
2024-05-21 23:18:00 +02:00
|
|
|
|
return Matrix.CreateScale(transformScale, transformScale)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
|
2024-05-21 23:18:00 +02:00
|
|
|
|
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the index bounds of a tile matrix.
|
|
|
|
|
|
/// </summary>
|
2024-07-15 20:10:28 +02:00
|
|
|
|
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, double viewWidth, double viewHeight)
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
// View origin in map coordinates.
|
|
|
|
|
|
//
|
|
|
|
|
|
var origin = ViewToMap(new Point());
|
|
|
|
|
|
|
|
|
|
|
|
var transformScale = tileMatrixScale / Scale;
|
|
|
|
|
|
|
2024-05-21 23:18:00 +02:00
|
|
|
|
var transform
|
|
|
|
|
|
= Matrix.CreateScale(transformScale, transformScale)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
* Matrix.CreateRotation(Matrix.ToRadians(-Rotation));
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
// Translate origin to tile matrix origin in pixels.
|
|
|
|
|
|
//
|
2024-05-21 23:18:00 +02:00
|
|
|
|
transform *= Matrix.CreateTranslation(
|
2024-05-19 23:23:27 +02:00
|
|
|
|
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
|
2024-05-21 23:18:00 +02:00
|
|
|
|
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
// Transform view bounds to tile pixel bounds.
|
|
|
|
|
|
//
|
2024-07-15 20:10:28 +02:00
|
|
|
|
return new Rect(0d, 0d, viewWidth, viewHeight).TransformToAABB(transform);
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
2024-05-22 09:42:21 +02:00
|
|
|
|
|
|
|
|
|
|
internal static Matrix CreateTransformMatrix(
|
|
|
|
|
|
double translation1X, double translation1Y,
|
|
|
|
|
|
double rotation,
|
|
|
|
|
|
double translation2X, double translation2Y)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Matrix.CreateTranslation(translation1X, translation1Y)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
* Matrix.CreateRotation(Matrix.ToRadians(rotation))
|
2024-05-22 09:42:21 +02:00
|
|
|
|
* Matrix.CreateTranslation(translation2X, translation2Y);
|
|
|
|
|
|
}
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|