@@ -443,38 +443,43 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
443443 atlasMaps << currentMap;
444444 }
445445
446+ // clear the transformed bounds of the previous feature
447+ mTransformedFeatureBounds = QgsRectangle ();
448+
446449 if ( atlasMaps.isEmpty () )
447450 {
448451 // no atlas enabled maps
449452 return ;
450453 }
451454
452- //
453- // compute the new extent
454- // keep the original aspect ratio
455- // and apply a margin
456-
457- const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer ->crs ();
458- // transformation needed for feature geometries. This should be set on a per-atlas map basis,
455+ // compute extent of current feature in the map CRS. This should be set on a per-atlas map basis,
459456 // but given that it's not currently possible to have maps with different CRSes we can just
460457 // calculate it once based on the first atlas maps' CRS.
461- const QgsCoordinateReferenceSystem& destination_crs = atlasMaps[0 ]->mapRenderer ()->destinationCrs ();
458+ computeExtent ( atlasMaps[0 ] );
459+
460+ // update atlas bounds of every atlas enabled composer map
461+ for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin (); mit != atlasMaps.end (); ++mit )
462+ {
463+ prepareMap ( *mit );
464+ }
465+ }
466+
467+ void QgsAtlasComposition::computeExtent ( QgsComposerMap* map )
468+ {
469+ // compute the extent of the current feature, in the crs of the specified map
470+
471+ const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer ->crs ();
472+ // transformation needed for feature geometries
473+ const QgsCoordinateReferenceSystem& destination_crs = map->mapRenderer ()->destinationCrs ();
462474 mTransform .setSourceCrs ( coverage_crs );
463475 mTransform .setDestCRS ( destination_crs );
464476
465477 // QgsGeometry::boundingBox is expressed in the geometry"s native CRS
466478 // We have to transform the grometry to the destination CRS and ask for the bounding box
467479 // Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
468-
469480 QgsGeometry tgeom ( *mCurrentFeature .geometry () );
470481 tgeom.transform ( mTransform );
471482 mTransformedFeatureBounds = tgeom.boundingBox ();
472-
473- // update atlas bounds of every atlas enabled composer map
474- for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin (); mit != atlasMaps.end (); ++mit )
475- {
476- prepareMap ( *mit );
477- }
478483}
479484
480485void QgsAtlasComposition::prepareMap ( QgsComposerMap* map )
@@ -484,6 +489,13 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
484489 return ;
485490 }
486491
492+ if ( mTransformedFeatureBounds .isEmpty () )
493+ {
494+ // transformed extent of current feature hasn't been calculated yet. This can happen if
495+ // a map has been set to be atlas controlled after prepare feature was called
496+ computeExtent ( map );
497+ }
498+
487499 double xa1 = mTransformedFeatureBounds .xMinimum ();
488500 double xa2 = mTransformedFeatureBounds .xMaximum ();
489501 double ya1 = mTransformedFeatureBounds .yMinimum ();
0 commit comments