Skip to content

Commit c94eefb

Browse files
committed
[layouts] Fix data defined atlas margin isn't evaluated
Fixes #19896
1 parent 4030390 commit c94eefb

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/core/layout/qgslayoutitemmap.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ bool QgsLayoutItemMap::writePropertiesToElement( QDomElement &mapElem, QDomDocum
540540
}
541541

542542
// follow map theme
543-
mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? "true" : "false" );
543+
mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
544544
mapElem.setAttribute( QStringLiteral( "followPresetName" ), mFollowVisibilityPresetName );
545545

546546
//map rotation
@@ -1960,9 +1960,10 @@ void QgsLayoutItemMap::updateAtlasFeature()
19601960
}
19611961
newExtent = QgsRectangle( xa1, ya1, xa2, ya2 );
19621962

1963-
if ( mAtlasMargin > 0.0 )
1963+
const double evaluatedAtlasMargin = atlasMargin();
1964+
if ( evaluatedAtlasMargin > 0.0 )
19641965
{
1965-
newExtent.scale( 1 + mAtlasMargin );
1966+
newExtent.scale( 1 + evaluatedAtlasMargin );
19661967
}
19671968
}
19681969

tests/src/python/test_qgslayoutatlas.py

+45
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,51 @@ def rotation_test(self):
626626

627627
QgsProject.instance().removeMapLayer(polygonLayer)
628628

629+
def test_datadefined_margin(self):
630+
polygonLayer = QgsVectorLayer('Polygon?field=margin:int', 'test_polygon', 'memory')
631+
poly = QgsFeature(polygonLayer.fields())
632+
poly.setAttributes([0])
633+
poly.setGeometry(QgsGeometry.fromWkt('Polygon((30 30, 40 30, 40 40, 30 40, 30 30))'))
634+
polygonLayer.dataProvider().addFeatures([poly])
635+
poly = QgsFeature(polygonLayer.fields())
636+
poly.setAttributes([10])
637+
poly.setGeometry(QgsGeometry.fromWkt('Polygon((10 10, 20 10, 20 20, 10 20, 10 10))'))
638+
polygonLayer.dataProvider().addFeatures([poly])
639+
poly = QgsFeature(polygonLayer.fields())
640+
poly.setAttributes([20])
641+
poly.setGeometry(QgsGeometry.fromWkt('Polygon((50 50, 60 50, 60 60, 50 60, 50 50))'))
642+
polygonLayer.dataProvider().addFeatures([poly])
643+
QgsProject.instance().addMapLayer(polygonLayer)
644+
645+
layout = QgsPrintLayout(QgsProject.instance())
646+
map = QgsLayoutItemMap(layout)
647+
map.setCrs(polygonLayer.crs())
648+
map.attemptSetSceneRect(QRectF(20, 20, 130, 130))
649+
map.setFrameEnabled(True)
650+
map.setLayers([polygonLayer])
651+
map.setExtent(QgsRectangle(0, 0, 100, 50))
652+
layout.addLayoutItem(map)
653+
654+
atlas = layout.atlas()
655+
atlas.setCoverageLayer(polygonLayer)
656+
atlas.setEnabled(True)
657+
658+
map.setAtlasDriven(True)
659+
map.setAtlasScalingMode(QgsLayoutItemMap.Auto)
660+
map.setAtlasMargin(77.0)
661+
map.dataDefinedProperties().setProperty(QgsLayoutObject.MapAtlasMargin, QgsProperty.fromExpression('margin/2'))
662+
663+
atlas.beginRender()
664+
atlas.first()
665+
666+
self.assertEqual(map.extent(), QgsRectangle(25, 30, 45, 40))
667+
self.assertTrue(atlas.next())
668+
self.assertEqual(map.extent(), QgsRectangle(4.5, 9.75, 25.5, 20.25))
669+
self.assertTrue(atlas.next())
670+
self.assertEqual(map.extent(), QgsRectangle(44, 49.5, 66, 60.5))
671+
672+
QgsProject.instance().removeMapLayer(polygonLayer)
673+
629674

630675
if __name__ == '__main__':
631676
unittest.main()

0 commit comments

Comments
 (0)