Skip to content

Commit

Permalink
Fix bounding box after rotating atlas geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
Zverik committed Jun 18, 2017
1 parent e03b563 commit 1aa2a4e
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -14,6 +14,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include <algorithm>
#include <stdexcept>
#include <QtAlgorithms>

Expand Down Expand Up @@ -438,11 +439,30 @@ void QgsAtlasComposition::computeExtent( QgsComposerMap *map )
// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
// We have to transform the geometry to the destination CRS and ask for the bounding box
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
QgsGeometry g(currentGeometry( map->crs() ));
QgsGeometry g = currentGeometry( map->crs() );
// Rotating the geometry, so the bounding box is correct wrt map rotation
if ( map->mapRotation() != 0.0 )
g.rotate(map->mapRotation(), g.centroid().asPoint());
mTransformedFeatureBounds = g.boundingBox();
{
QgsPointXY prevCenter = g.boundingBox().center();
g.rotate( map->mapRotation(), g.boundingBox().center() );
// Rotation center will be still the bounding box center of an unrotated geometry.
// Which means, if the center of bbox moves after rotation, the viewport will
// also be offset, and part of the geometry will fall out of bounds.
// Here we compensate for that roughly: by extending the rotated bounds
// so that its center is the same as the original.
QgsRectangle bounds = g.boundingBox();
double dx = std::max( std::abs( prevCenter.x() - bounds.xMinimum() ),
std::abs( prevCenter.x() - bounds.xMaximum() ) );
double dy = std::max( std::abs( prevCenter.y() - bounds.yMinimum() ),
std::abs( prevCenter.y() - bounds.yMaximum() ) );
QgsPointXY center = g.boundingBox().center();
mTransformedFeatureBounds = QgsRectangle( center.x() - dx, center.y() - dy,
center.x() + dx, center.y() + dy );
}
else
{
mTransformedFeatureBounds = g.boundingBox();
}
}

void QgsAtlasComposition::prepareMap( QgsComposerMap *map )
Expand Down

0 comments on commit 1aa2a4e

Please sign in to comment.