diff --git a/src/core/layout/qgslayoutitemmap.cpp b/src/core/layout/qgslayoutitemmap.cpp index ddb653981e30..e4387707043d 100644 --- a/src/core/layout/qgslayoutitemmap.cpp +++ b/src/core/layout/qgslayoutitemmap.cpp @@ -540,7 +540,7 @@ bool QgsLayoutItemMap::writePropertiesToElement( QDomElement &mapElem, QDomDocum } // follow map theme - mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? "true" : "false" ); + mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? QStringLiteral( "true" ) : QStringLiteral( "false" ) ); mapElem.setAttribute( QStringLiteral( "followPresetName" ), mFollowVisibilityPresetName ); //map rotation @@ -1960,9 +1960,10 @@ void QgsLayoutItemMap::updateAtlasFeature() } newExtent = QgsRectangle( xa1, ya1, xa2, ya2 ); - if ( mAtlasMargin > 0.0 ) + const double evaluatedAtlasMargin = atlasMargin(); + if ( evaluatedAtlasMargin > 0.0 ) { - newExtent.scale( 1 + mAtlasMargin ); + newExtent.scale( 1 + evaluatedAtlasMargin ); } } diff --git a/tests/src/python/test_qgslayoutatlas.py b/tests/src/python/test_qgslayoutatlas.py index 32e131d3f63d..0372c6678d49 100644 --- a/tests/src/python/test_qgslayoutatlas.py +++ b/tests/src/python/test_qgslayoutatlas.py @@ -626,6 +626,51 @@ def rotation_test(self): QgsProject.instance().removeMapLayer(polygonLayer) + def test_datadefined_margin(self): + polygonLayer = QgsVectorLayer('Polygon?field=margin:int', 'test_polygon', 'memory') + poly = QgsFeature(polygonLayer.fields()) + poly.setAttributes([0]) + poly.setGeometry(QgsGeometry.fromWkt('Polygon((30 30, 40 30, 40 40, 30 40, 30 30))')) + polygonLayer.dataProvider().addFeatures([poly]) + poly = QgsFeature(polygonLayer.fields()) + poly.setAttributes([10]) + poly.setGeometry(QgsGeometry.fromWkt('Polygon((10 10, 20 10, 20 20, 10 20, 10 10))')) + polygonLayer.dataProvider().addFeatures([poly]) + poly = QgsFeature(polygonLayer.fields()) + poly.setAttributes([20]) + poly.setGeometry(QgsGeometry.fromWkt('Polygon((50 50, 60 50, 60 60, 50 60, 50 50))')) + polygonLayer.dataProvider().addFeatures([poly]) + QgsProject.instance().addMapLayer(polygonLayer) + + layout = QgsPrintLayout(QgsProject.instance()) + map = QgsLayoutItemMap(layout) + map.setCrs(polygonLayer.crs()) + map.attemptSetSceneRect(QRectF(20, 20, 130, 130)) + map.setFrameEnabled(True) + map.setLayers([polygonLayer]) + map.setExtent(QgsRectangle(0, 0, 100, 50)) + layout.addLayoutItem(map) + + atlas = layout.atlas() + atlas.setCoverageLayer(polygonLayer) + atlas.setEnabled(True) + + map.setAtlasDriven(True) + map.setAtlasScalingMode(QgsLayoutItemMap.Auto) + map.setAtlasMargin(77.0) + map.dataDefinedProperties().setProperty(QgsLayoutObject.MapAtlasMargin, QgsProperty.fromExpression('margin/2')) + + atlas.beginRender() + atlas.first() + + self.assertEqual(map.extent(), QgsRectangle(25, 30, 45, 40)) + self.assertTrue(atlas.next()) + self.assertEqual(map.extent(), QgsRectangle(4.5, 9.75, 25.5, 20.25)) + self.assertTrue(atlas.next()) + self.assertEqual(map.extent(), QgsRectangle(44, 49.5, 66, 60.5)) + + QgsProject.instance().removeMapLayer(polygonLayer) + if __name__ == '__main__': unittest.main()