@@ -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
630675if __name__ == '__main__' :
631676 unittest .main ()
0 commit comments