Skip to content

Commit

Permalink
[composer] Fix incorrect calculation of map bounds when a map is swit…
Browse files Browse the repository at this point in the history
…ched to atlas control after atlas preview is enabled
  • Loading branch information
nyalldawson committed Jan 30, 2014
1 parent 78ea81a commit ffae8ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -443,38 +443,43 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
atlasMaps << currentMap;
}

//clear the transformed bounds of the previous feature
mTransformedFeatureBounds = QgsRectangle();

if ( atlasMaps.isEmpty() )
{
//no atlas enabled maps
return;
}

//
// compute the new extent
// keep the original aspect ratio
// and apply a margin

const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer->crs();
// transformation needed for feature geometries. This should be set on a per-atlas map basis,
// compute extent of current feature in the map CRS. This should be set on a per-atlas map basis,
// but given that it's not currently possible to have maps with different CRSes we can just
// calculate it once based on the first atlas maps' CRS.
const QgsCoordinateReferenceSystem& destination_crs = atlasMaps[0]->mapRenderer()->destinationCrs();
computeExtent( atlasMaps[0] );

//update atlas bounds of every atlas enabled composer map
for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin(); mit != atlasMaps.end(); ++mit )
{
prepareMap( *mit );
}
}

void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
{
// compute the extent of the current feature, in the crs of the specified map

const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer->crs();
// transformation needed for feature geometries
const QgsCoordinateReferenceSystem& destination_crs = map->mapRenderer()->destinationCrs();
mTransform.setSourceCrs( coverage_crs );
mTransform.setDestCRS( destination_crs );

// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
// We have to transform the grometry 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 tgeom( *mCurrentFeature.geometry() );
tgeom.transform( mTransform );
mTransformedFeatureBounds = tgeom.boundingBox();

//update atlas bounds of every atlas enabled composer map
for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin(); mit != atlasMaps.end(); ++mit )
{
prepareMap( *mit );
}
}

void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
Expand All @@ -484,6 +489,13 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
return;
}

if ( mTransformedFeatureBounds.isEmpty() )
{
//transformed extent of current feature hasn't been calculated yet. This can happen if
//a map has been set to be atlas controlled after prepare feature was called
computeExtent( map );
}

double xa1 = mTransformedFeatureBounds.xMinimum();
double xa2 = mTransformedFeatureBounds.xMaximum();
double ya1 = mTransformedFeatureBounds.yMinimum();
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgsatlascomposition.h
Expand Up @@ -201,6 +201,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject

//forces all atlas enabled maps to redraw
void updateAtlasMaps();

//computes the extent of the current feature, in the crs of the specified map
void computeExtent( QgsComposerMap *map );
};

#endif
Expand Down

0 comments on commit ffae8ac

Please sign in to comment.