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

@ -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"));
}
}
}