Updated ViewTransform and WmsImageLayer

This commit is contained in:
ClemensFischer 2026-01-01 12:50:19 +01:00
parent 4628395c37
commit f8f6fdc973
5 changed files with 60 additions and 129 deletions

View file

@ -1,81 +0,0 @@
using Avalonia;
namespace MapControl
{
public partial class ViewTransform
{
/// <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)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* 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)
{
return Matrix.CreateScale(Scale * relativeScale.X, Scale * relativeScale.Y)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation));
}
/// <summary>
/// Gets the transform Matrix for the RenderTranform of a MapTileLayer.
/// </summary>
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 = MapToViewMatrix.Transform(mapOrigin);
var transformScale = Scale / tileMatrixScale;
return Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
}
/// <summary>
/// Gets the index bounds of a tile matrix.
/// </summary>
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, double viewWidth, double viewHeight)
{
// View origin in map coordinates.
//
var origin = ViewToMapMatrix.Transform(new Point());
var transformScale = tileMatrixScale / Scale;
var transform = Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(Matrix.ToRadians(-Rotation));
// Translate origin to tile matrix origin in pixels.
//
transform *= Matrix.CreateTranslation(
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
// Transform view bounds to tile pixel bounds.
//
return new Rect(0d, 0d, viewWidth, viewHeight).TransformToAABB(transform);
}
}
}

View file

@ -32,7 +32,7 @@ namespace MapControl
public override Point? LocationToMap(double latitude, double longitude)
{
#if NETFRAMEWORK || UWP
#if NETFRAMEWORK
static double Atanh(double x) => Math.Log((1d + x) / (1d - x)) / 2d;
#else
static double Atanh(double x) => Math.Atanh(x);

View file

@ -15,7 +15,7 @@ namespace MapControl
/// Defines the transformation between projected map coordinates in meters
/// and view coordinates in pixels.
/// </summary>
public partial class ViewTransform
public class ViewTransform
{
/// <summary>
/// Gets the scaling factor from projected map coordinates to view coordinates,
@ -38,7 +38,6 @@ namespace MapControl
/// </summary>
public Matrix ViewToMapMatrix { get; private set; }
#if !AVALONIA
/// <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
@ -48,16 +47,22 @@ namespace MapControl
{
Scale = scale;
Rotation = ((rotation % 360d) + 360d) % 360d;
#if AVALONIA
MapToViewMatrix = Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
* Matrix.CreateScale(scale, -scale)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
ViewToMapMatrix = MapToViewMatrix.Invert();
#else
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;
#endif
}
/// <summary>
@ -65,10 +70,14 @@ namespace MapControl
/// </summary>
public Matrix GetMapTransform(Point relativeScale)
{
#if AVALONIA
return Matrix.CreateScale(Scale * relativeScale.X, Scale * relativeScale.Y)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation));
#else
var transform = new Matrix(Scale * relativeScale.X, 0d, 0d, Scale * relativeScale.Y, 0d, 0d);
transform.Rotate(Rotation);
return transform;
#endif
}
/// <summary>
@ -87,12 +96,16 @@ namespace MapControl
var viewOrigin = MapToViewMatrix.Transform(mapOrigin);
var transformScale = Scale / tileMatrixScale;
#if AVALONIA
return Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
#else
var transform = new Matrix(transformScale, 0d, 0d, transformScale, 0d, 0d);
transform.Rotate(Rotation);
transform.Translate(viewOrigin.X, viewOrigin.Y);
return transform;
#endif
}
/// <summary>
@ -105,7 +118,17 @@ namespace MapControl
var origin = ViewToMapMatrix.Transform(new Point());
var transformScale = tileMatrixScale / Scale;
#if AVALONIA
var transform = Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(Matrix.ToRadians(-Rotation))
* Matrix.CreateTranslation(
tileMatrixScale * (origin.X - tileMatrixTopLeft.X),
tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y));
// Transform view bounds to tile pixel bounds.
//
return new Rect(0d, 0d, viewWidth, viewHeight).TransformToAABB(transform);
#else
var transform = new Matrix(transformScale, 0d, 0d, transformScale, 0d, 0d);
transform.Rotate(-Rotation);
@ -119,7 +142,7 @@ namespace MapControl
//
return new MatrixTransform { Matrix = transform }
.TransformBounds(new Rect(0d, 0d, viewWidth, viewHeight));
}
#endif
}
}
}

View file

@ -308,6 +308,26 @@ namespace MapControl
return uriBuilder.Uri;
}
protected virtual Uri GetRequestUri(IDictionary<string, string> queryParameters)
{
var query = ServiceUri.Query;
if (!string.IsNullOrEmpty(query))
{
// Parameters from ServiceUri.Query take higher precedence than queryParameters.
//
foreach (var param in query.Substring(1).Split('&'))
{
var pair = param.Split('=');
queryParameters[pair[0]] = pair.Length > 1 ? pair[1] : "";
}
}
query = string.Join("&", queryParameters.Select(kv => kv.Key + "=" + kv.Value));
return new Uri(ServiceUri.GetLeftPart(UriPartial.Path) + "?" + query);
}
protected virtual string GetCrsValue()
{
var projection = ParentMap.MapProjection;
@ -315,7 +335,7 @@ namespace MapControl
if (crs.StartsWith("AUTO2:") || crs.StartsWith("AUTO:"))
{
crs = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crs, projection.Center.Longitude, projection.Center.Latitude);
crs = string.Format(CultureInfo.InvariantCulture, "{0},1,{1:F8},{2:F8}", crs, projection.Center.Longitude, projection.Center.Latitude);
}
return crs;
@ -341,26 +361,5 @@ namespace MapControl
return string.Format(CultureInfo.InvariantCulture, format, x1, y1, x2, y2);
}
protected Uri GetRequestUri(IDictionary<string, string> queryParameters)
{
var query = ServiceUri.Query;
if (!string.IsNullOrEmpty(query))
{
// Parameters from ServiceUri.Query take higher precedence than queryParameters.
//
foreach (var param in query.Substring(1).Split('&'))
{
var pair = param.Split('=');
queryParameters[pair[0].ToUpper()] = pair.Length > 1 ? pair[1] : "";
}
}
var uri = ServiceUri.GetLeftPart(UriPartial.Path) + "?" +
string.Join("&", queryParameters.Select(kv => kv.Key + "=" + kv.Value));
return new Uri(uri.Replace(" ", "%20"));
}
}
}

View file

@ -10,24 +10,14 @@ namespace MapControl
/// <summary>
/// Replaces Windows.UI.Xaml.Media.Matrix for double floating point precision.
/// </summary>
public struct Matrix
public struct Matrix(double m11, double m12, double m21, double m22, double offsetX, double offsetY)
{
public Matrix(double m11, double m12, double m21, double m22, double offsetX, double offsetY)
{
M11 = m11;
M12 = m12;
M21 = m21;
M22 = m22;
OffsetX = offsetX;
OffsetY = offsetY;
}
public double M11 { get; set; }
public double M12 { get; set; }
public double M21 { get; set; }
public double M22 { get; set; }
public double OffsetX { get; set; }
public double OffsetY { get; set; }
public double M11 { get; private set; } = m11;
public double M12 { get; private set; } = m12;
public double M21 { get; private set; } = m21;
public double M22 { get; private set; } = m22;
public double OffsetX { get; private set; } = offsetX;
public double OffsetY { get; private set; } = offsetY;
public static implicit operator WindowsUI.Xaml.Media.Matrix(Matrix m)
{