Skip to content
Permalink
Browse files

[layouts] Fix data defined atlas margin isn't evaluated

Fixes #19896
  • Loading branch information
nyalldawson committed Oct 22, 2018
1 parent 4030390 commit c94eefb6df0bf9e504452986da943d7b9f8574c8
Showing with 49 additions and 3 deletions.
  1. +4 −3 src/core/layout/qgslayoutitemmap.cpp
  2. +45 −0 tests/src/python/test_qgslayoutatlas.py
@@ -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 );
}
}

@@ -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()

0 comments on commit c94eefb

Please sign in to comment.
You can’t perform that action at this time.