using System; #if WPF using System.Windows; using System.Windows.Media; #elif AVALONIA using Avalonia; #endif namespace MapControl { /// /// Defines the transformation between projected map coordinates in meters /// and view coordinates in pixels. /// public class ViewTransform { /// /// Gets the scaling factor from projected map coordinates to view coordinates, /// as pixels per meter. /// public double Scale { get; private set; } /// /// Gets the rotation angle of the transform matrix. /// public double Rotation { get; private set; } /// /// Gets the transform matrix from projected map coordinates to view coordinates. /// public Matrix MapToViewMatrix { get; private set; } /// /// Gets the transform matrix from view coordinates to projected map coordinates. /// public Matrix ViewToMapMatrix { get; private set; } /// /// Transforms a Point in projected map coordinates to a Point in view coordinates. /// public Point MapToView(Point point) => MapToViewMatrix.Transform(point); /// /// Transforms a Point in view coordinates to a Point in projected map coordinates. /// public Point ViewToMap(Point point) => ViewToMapMatrix.Transform(point); /// /// Gets an axis-aligned bounding box in projected map coordinates that contains /// a rectangle in view coordinates. /// public Rect ViewToMapBounds(Rect rect) => TransformBounds(ViewToMapMatrix, rect.X, rect.Y, rect.Width, rect.Height); /// /// 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. /// 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; } /// /// Gets the transform Matrix for the RenderTranform of a MapTileLayer or WmtsTileMatrixLayer. /// public Matrix GetTileLayerTransform(double tileMatrixScale, Point tileMatrixTopLeft, Point tileMatrixOrigin) { var scale = Scale / tileMatrixScale; var transform = new Matrix(scale, 0d, 0d, scale, 0d, 0d); transform.Rotate(Rotation); // 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); transform.Translate(viewOrigin.X, viewOrigin.Y); return transform; } /// /// Gets the pixel bounds of a tile matrix. /// public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, double viewWidth, double viewHeight) { var scale = tileMatrixScale / Scale; var transform = new Matrix(scale, 0d, 0d, scale, 0d, 0d); transform.Rotate(-Rotation); // View origin in map coordinates. // var origin = ViewToMapMatrix.Transform(new Point()); // Translation from origin to tile matrix origin in pixels. // transform.Translate( tileMatrixScale * (origin.X - tileMatrixTopLeft.X), tileMatrixScale * (tileMatrixTopLeft.Y - origin.Y)); // Transform view bounds to tile pixel bounds. // return TransformBounds(transform, 0d, 0d, viewWidth, viewHeight); } private static Rect TransformBounds(Matrix transform, double x, double y, double width, double height) { if (transform.M12 == 0d && transform.M21 == 0d) { return new Rect( x * transform.M11 + transform.OffsetX, y * transform.M22 + transform.OffsetY, width * transform.M11, height * transform.M22); } 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)); var x1 = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X))); var y1 = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y))); var x2 = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X))); var y2 = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y))); return new Rect(x1, y1, x2 - x1, y2 - y1); } } }