12

I just want to export a configured Atlas (composer with Atlas option and cover layer enabled)

I tried this :

composerView = qgis.utils.iface.activeComposers()[0]
composition = composerView.composition()
atlas = composition.atlasComposition()
composition.setAtlasMode(QgsComposition.ExportAtlas)
atlas.beginRender()
for i in range(0,atlas.numFeatures()):
 atlas.prepareForFeature(i)
 composition.exportAsPDF('C:\xxxx\test.pdf' + str(format(i)))
atlas.endRender()

But nothing happens.

Have anybody a clue?

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Feb 27, 2018 at 8:46
0

2 Answers 2

12

In QGIS 3.4 (maybe 3.x in general), exporting atlas seems much more straight forward than before (at least from looking the available answers).

from qgis.core import QgsApplication, QgsProject, QgsLayoutExporter
import os
def export_atlas(qgs_project_path, layout_name, outputs_folder):
 # Open existing project
 project = QgsProject.instance()
 project.read(qgs_project_path)
 print(f'Project in "{project.fileName()} loaded successfully')
 # Open prepared layout that as atlas enabled and set
 layout = project.layoutManager().layoutByName(layout_name)
 # Export atlas
 exporter = QgsLayoutExporter(layout)
 settings = QgsLayoutExporter.ImageExportSettings()
 exporter.exportToImage(layout.atlas(),outputs_folder, 'png', settings)
def main():
 # Start a QGIS application without GUI
 qgs = QgsApplication([], False)
 qgs.initQgis()
 project_path = '/path/to/project.qgs'
 output_folder = '/path/to/existing/folder/'
 layout_name = 'my_layout_name'
 export_atlas(project_path, layout_name, output_folder)
 # Close the QGIS application
 qgs.exitQgis()
if __name__ == "__main__":
 main()
answered Jul 8, 2019 at 23:11
6
  • 5
    Actually QgsLayoutExporter can handle even more automatically for you -- you can just use QgsLayoutExporter.exportToImage(layout.atlas(), '/home/me/output/test','png', settings) and it'll handle the begin/endRender calls and iteration for you! Commented Jul 9, 2019 at 0:16
  • Wow!! that's automagic! I confess I was hoping for a comment like this from you :-) Commented Jul 9, 2019 at 22:58
  • Updated the answer using @ndawson suggestion Commented Jul 9, 2019 at 23:14
  • when I run this I get a windows error: python3. 'This application failed to start because no qt platform plugin could be initialized' Commented Dec 16, 2019 at 22:11
  • Where did you run it? Commented Dec 17, 2019 at 0:22
4

Your script ran successfully for me in the "Python script console" of QGIS.

Inspired by Save Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?, How to export a configurated Atlas with a python script / command line?, QGIS: Automatisation de la génération d'un Atlas avec script python, Search for "[qgis] standalone script" on gis stackexchange, How to run a simple python script for QGIS from outside (e.g. Sublime Text)?, Using PyQGIS in custom applications, Generate a QGIS map PDF using python, How to create a QGIS PDF report with a few lines of python, QGIS Server Plugin Filters: Add a new request to print a specific atlas feature, QGIS export "save as image" automate with python?, I wrote the following exportAtlas.py python script:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from qgis.core import QgsApplication, QgsProject, QgsComposition
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from PyQt4.QtCore import QFile, QFileInfo, QByteArray, QTextStream
from PyQt4.QtXml import QDomDocument
import os
import sys
projectPath = sys.argv[1]
renderingPath = sys.argv[2]
def printAtlas(projectPath, renderingPath):
 # Inspired by [Save Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?](https://gis.stackexchange.com/a/164196/73088), [How to export a configurated Atlas with a python script / command line?](https://gis.stackexchange.com/q/272839/73088), [QGIS: Automatisation de la génération d'un Atlas avec script python](https://georezo.net/forum/viewtopic.php?pid=284842), [Search for "[qgis] standalone script" on gis stackexchange](https://gis.stackexchange.com/search?page=2&tab=Relevance&q=%5bqgis%5d%20standalone%20script), [How to run a simple python script for QGIS from outside (e.g. Sublime Text)?](https://gis.stackexchange.com/a/29597/73088), [Using PyQGIS in custom applications](https://docs.qgis.org/2.18/en/docs/pyqgis_developer_cookbook/intro.html#using-pyqgis-in-custom-applications), [Generate a QGIS map PDF using python](https://gist.github.com/timlinux/486793ad61db4c1dec9d), [How to create a QGIS PDF report with a few lines of python](http://kartoza.com/en/blog/how-to-create-a-qgis-pdf-report-with-a-few-lines-of-python/), [QGIS Server Plugin Filters: Add a new request to print a specific atlas feature](https://github.com/3liz/qgis-atlasprint/blob/master/filters/atlasprintFilter.py), [QGIS export "save as image" automate with python?](https://gis.stackexchange.com/a/213065/73088)
 #Getting project as Qfile and the first composer of the project as a QDomElement from the .qgs
 projectAsFile = QFile(projectPath)
 projectAsDocument = QDomDocument()
 projectAsDocument.setContent(projectAsFile)
 composerAsNode = projectAsDocument.elementsByTagName("Composer").at(0)
 # Only way to convert a QDomNode to a QDomDocument root, inspired by https://gis.stackexchange.com/a/164196/73088 & [Convert QDomElement to QDomDocument and vs](https://stackoverflow.com/q/18868993/535203) & read the documentation http://doc.qt.io/archives/qt-4.8/qtextstream.html & http://doc.qt.io/archives/qt-4.8/qdomnode.html .
 # Using a QByteArray because QString pointer can't be passed in python and QString is not available by default in QGIS python scripts [QGIS PyQt4 missing QString class](https://stackoverflow.com/q/28632169/535203)
 composerAsString = QByteArray()
 composerAsNode.save(QTextStream(composerAsString), 2)
 composerAsDocument = QDomDocument()
 composerAsDocument.setContent(composerAsString)
 #Now that we got all we can open our project
 canvas = QgsMapCanvas()
 QgsProject.instance().read(QFileInfo(projectAsFile))
 bridge = QgsLayerTreeMapCanvasBridge(
 QgsProject.instance().layerTreeRoot(), canvas)
 bridge.setCanvasLayers()
 #Lets try load that composer template we just extracted
 composition = QgsComposition(canvas.mapSettings())
 composition.loadFromTemplate(composerAsDocument, {})
 atlas = composition.atlasComposition()
 composition.setAtlasMode(QgsComposition.ExportAtlas)
 print 'Found %d features to render.' % atlas.numFeatures()
 atlas.beginRender()
 for i in range(1, atlas.numFeatures()):
 print 'Rendering feature %d...' % i
 atlas.prepareForFeature(i)
 featureRenderingBasePath = os.path.join(renderingPath, str(format(i)))
 composition.exportAsPDF(featureRenderingBasePath + '.pdf')
 img = composition.printPageAsRaster(0)
 img.save(featureRenderingBasePath + '.jpg', 'jpg')
 atlas.endRender()
 #Some cleanup maybe?
 QgsProject.instance().clear()
# supply path to qgis install location
#QgsApplication.setPrefixPath("/usr", True) #already set in the right place "/usr" by default
# create a reference to the QgsApplication
# setting the second argument to True enables the GUI, which we need to do
# since this is a custom application
qgs = QgsApplication([], True)
# load providers
qgs.initQgis()
printAtlas(projectPath, renderingPath)
# When your script is complete, call exitQgis() to remove the provider and
# layer registries from memory
qgs.exitQgis()

It can be called this way: python exportAtlas.py '/path/to/the/project.qgs' '/path/to/the/folder/where/to/generate/images/and/pdfs/'

I was able to launch it into a Docker container with the following command: docker run -it --rm -v ${HOME}:/home/${USER} -v /tmp/.X11-unix:/tmp/.X11-unix -v /dev/shm:/dev/shm -e DISPLAY=unix$DISPLAY kartoza/qgis-desktop bash and then inside the terminal ran:

USER_ID=`ls -lahn /home | tail -1 | awk {'print 3ドル'}`
GROUP_ID=`ls -lahn /home | tail -1 | awk {'print 4ドル'}`
USER_NAME=`ls -lah /home/ | tail -1 | awk '{print 9ドル}'`
groupadd -g $GROUP_ID qgis
useradd --shell /bin/bash --uid $USER_ID --gid $GROUP_ID $USER_NAME
export LD_LIBRARY_PATH=/usr/lib
su $USER_NAME -c "PYTHONPATH=/usr/share/qgis/python python /home/$USER_NAME/exportAtlas.py '/path/to/the/project.qgs' '/path/to/the/folder/where/to/generate/images/and/pdfs/'"

The problem with this script is that it doesn't take into consideration a layer which uses the Mask plugin.

I've fixed this in another answer.

answered Jul 11, 2018 at 12:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.