2

I have a problem when I try to export a Map in the Layout of QGIS 3 with the Python API. Here is my code :

qpt = QDomDocument()
qpt.setContent(content)
layout = QgsLayout(QgsProject().instance())
layout.loadFromTemplate(qpt, QgsReadWriteContext())
mapItem = layout.referenceMap()
layoutExporter = QgsLayoutExporter(layout)
imageSettings = layoutExporter.ImageExportSettings()
imageSettings.dpi = 150
imageSettings.generateWorldFile = 1
path = "path/to/my/iamge.tif"
layoutExporter.exportToImage(path, settings=imageSettings)

When I export my layout's map I get an image with a white stripe at the bottom like in the following file :

enter image description here

The width of the stripe is variable when for example a widget (like the python console) is opened or not. It seams that the map of the layout doesn't fill its whole surface because its ratio is not the same as the map canvas' one.

Is there a way to say to the layout's map to fill completely ?

1

The solution is to use zoomToExtent() instead of setExtent() so the right line is :

mapItem.zoomToExtent(mapCanvas.extent())

setExtent() sets the exact same extent as mapCanvas (the extent is a QgsRectangle with 4 summits which have the coordinates of the 4 angles of mapCanvas).

zoomToExtent() : if the ratio width/height of mapCanvas is lower than mapItem's ratio, then QGIS will complete the white stripe from setExtent() with not displayed information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.