Skip to content

Commit

Permalink
Add QgsMapToPixel method to transform bounding boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 16, 2024
1 parent ba2ff0c commit afa8297
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
10 changes: 10 additions & 0 deletions python/PyQt6/core/auto_generated/qgsmaptopixel.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ This method modifies the given coordinates in place. It is intended as a fast wa
transform.
%End

QRectF transformBounds( const QRectF &bounds ) const;
%Docstring
Transforms a bounding box from map coordinates to device coordinates.

The returns bounding box will always completely enclose the transformed input bounding box (i.e. this
method will grow the bounds wherever required).

.. versionadded:: 3.40
%End



QgsPointXY toMapCoordinates( int x, int y ) const;
Expand Down
10 changes: 10 additions & 0 deletions python/core/auto_generated/qgsmaptopixel.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ This method modifies the given coordinates in place. It is intended as a fast wa
transform.
%End

QRectF transformBounds( const QRectF &bounds ) const;
%Docstring
Transforms a bounding box from map coordinates to device coordinates.

The returns bounding box will always completely enclose the transformed input bounding box (i.e. this
method will grow the bounds wherever required).

.. versionadded:: 3.40
%End



QgsPointXY toMapCoordinates( int x, int y ) const;
Expand Down
32 changes: 30 additions & 2 deletions src/core/qgsmaptopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include <QTransform>
#include <vector>
#include "qgis.h"
#include "qgspointxy.h"

#include <cassert>
#include <memory>

class QPoint;

Expand Down Expand Up @@ -131,6 +129,36 @@ class CORE_EXPORT QgsMapToPixel
y = my;
}

/**
* Transforms a bounding box from map coordinates to device coordinates.
*
* The returns bounding box will always completely enclose the transformed input bounding box (i.e. this
* method will grow the bounds wherever required).
*
* \since QGIS 3.40
*/
QRectF transformBounds( const QRectF &bounds ) const
{
QPointF topLeft = bounds.topLeft();
QPointF topRight = bounds.topRight();
QPointF bottomLeft = bounds.bottomLeft();
QPointF bottomRight = bounds.bottomRight();

transformInPlace( topLeft.rx(), topLeft.ry() );
transformInPlace( topRight.rx(), topRight.ry() );
transformInPlace( bottomLeft.rx(), bottomLeft.ry() );
transformInPlace( bottomRight.rx(), bottomRight.ry() );

auto minMaxX = std::minmax( { topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x() } );
auto minMaxY = std::minmax( { topLeft.y(), topRight.y(), bottomLeft.y(), bottomRight.y() } );

const double left = minMaxX.first;
const double right = minMaxX.second;
const double top = minMaxY.first;
const double bottom = minMaxY.second;
return QRectF( left, top, right - left, bottom - top );
}

/**
* Transforms device coordinates to map coordinates.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/src/core/testqgsmaptopixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestQgsMapToPixel: public QObject
void fromScale();
void equality();
void toMapCoordinates();
void transformBounds();
};

void TestQgsMapToPixel::isValid()
Expand Down Expand Up @@ -184,6 +185,26 @@ void TestQgsMapToPixel::toMapCoordinates()
QCOMPARE( p, QgsPointXY( 20, 20 ) );
}

void TestQgsMapToPixel::transformBounds()
{
// no rotation
const QgsMapToPixel m2p( 1.5, 5, 15, 20, 10, 0 );
QRectF result = m2p.transformBounds( QRectF( 10, 20, 30, 40 ) );

QGSCOMPARENEAR( result.left(), 13.3333333333, 6 );
QGSCOMPARENEAR( result.right(), 33.3333333333, 6 );
QCOMPARE( result.top(), -25 );
QGSCOMPARENEAR( result.bottom(), 1.66666666667, 6 );

// with rotation
const QgsMapToPixel m2pRotated( 1.5, 5, 15, 20, 10, 45 );
result = m2pRotated.transformBounds( QRectF( 10, 20, 30, 40 ) );
QGSCOMPARENEAR( result.left(), 14.7140452079, 6 );
QGSCOMPARENEAR( result.right(), 47.7123616633, 6 );
QGSCOMPARENEAR( result.top(), -13.8561808316, 6 );
QGSCOMPARENEAR( result.bottom(), 19.1421356237, 6 );
}

QGSTEST_MAIN( TestQgsMapToPixel )
#include "testqgsmaptopixel.moc"

Expand Down

0 comments on commit afa8297

Please sign in to comment.