Skip to content

Commit 1aa2a4e

Browse files
committed
Fix bounding box after rotating atlas geometry
1 parent e03b563 commit 1aa2a4e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/core/composer/qgsatlascomposition.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* (at your option) any later version. *
1515
* *
1616
***************************************************************************/
17+
#include <algorithm>
1718
#include <stdexcept>
1819
#include <QtAlgorithms>
1920

@@ -438,11 +439,30 @@ void QgsAtlasComposition::computeExtent( QgsComposerMap *map )
438439
// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
439440
// We have to transform the geometry to the destination CRS and ask for the bounding box
440441
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
441-
QgsGeometry g(currentGeometry( map->crs() ));
442+
QgsGeometry g = currentGeometry( map->crs() );
442443
// Rotating the geometry, so the bounding box is correct wrt map rotation
443444
if ( map->mapRotation() != 0.0 )
444-
g.rotate(map->mapRotation(), g.centroid().asPoint());
445-
mTransformedFeatureBounds = g.boundingBox();
445+
{
446+
QgsPointXY prevCenter = g.boundingBox().center();
447+
g.rotate( map->mapRotation(), g.boundingBox().center() );
448+
// Rotation center will be still the bounding box center of an unrotated geometry.
449+
// Which means, if the center of bbox moves after rotation, the viewport will
450+
// also be offset, and part of the geometry will fall out of bounds.
451+
// Here we compensate for that roughly: by extending the rotated bounds
452+
// so that its center is the same as the original.
453+
QgsRectangle bounds = g.boundingBox();
454+
double dx = std::max( std::abs( prevCenter.x() - bounds.xMinimum() ),
455+
std::abs( prevCenter.x() - bounds.xMaximum() ) );
456+
double dy = std::max( std::abs( prevCenter.y() - bounds.yMinimum() ),
457+
std::abs( prevCenter.y() - bounds.yMaximum() ) );
458+
QgsPointXY center = g.boundingBox().center();
459+
mTransformedFeatureBounds = QgsRectangle( center.x() - dx, center.y() - dy,
460+
center.x() + dx, center.y() + dy );
461+
}
462+
else
463+
{
464+
mTransformedFeatureBounds = g.boundingBox();
465+
}
446466
}
447467

448468
void QgsAtlasComposition::prepareMap( QgsComposerMap *map )

0 commit comments

Comments
 (0)