4

I am trying to generate an atlas with python in qgis 3.0. I have :

projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()
for comp in projectLayoutManager.printLayouts():
    if comp.atlas().enabled():
        comp.atlas().beginRender() 
        nbobj = comp.atlas().updateFeatures()
        comp.atlas().first()
        for i in range(0, comp.atlas().count ()):
            comp.atlas().refreshCurrentFeature ()
            exporter =  QgsLayoutExporter(comp.atlas().layout () )
            pdf_settings = exporter.ImageExportSettings () #dpi?
            exporter.exportToImage (<filename>, pdf_settings)
        comp.atlas().endRender() 

But I would like the filename to have some attributes of the atlas feature.

How do I get the value of the attribute?

4

You can simplify this script considerably in QGIS 3.0:

projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()

image_settings = exporter.ImageExportSettings()
image_settings.dpi = 300 # or whatever you want

for comp in projectLayoutManager.printLayouts():
    if comp.atlas().enabled():
        result, error = QgsLayoutExporter.exportToImage(comp.atlas(), 
                            baseFilePath='c:/temp/my_atlas', extension='.png', settings=image_settings)
        if not result == QgsLayoutExporter.Success:
            print(error)

To control the generated filenames, you need to set an expression for the atlas filenames, e.g.:

comp.atlas().setFilenameExpression('"some_attribute" || '_export' )
3

Great, it's gone! thank ndawson.

My new code write atlas result in subdirectories, if can help anyone

import os
folder = "D:\CARTE_THEMATIQUE\OUTPUT"
projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()

for comp in projectLayoutManager.printLayouts():
    comp.atlas().setFilenameExpression( "CODE_CENTRE  ||'/'||  CODE_CONTRAT"  )
    image_settings = QgsLayoutExporter(comp.atlas().layout () ).ImageExportSettings()
    image_settings.dpi = 100 # or whatever you want
    if comp.atlas().enabled():
        (filepath, filename) = os.path.split(folder +'\\' + comp.atlas().currentFilename () +'png')
        if not os.path.exists(filepath): 
            os.makedirs(filepath)
        result, error = QgsLayoutExporter.exportToImage(comp.atlas(), baseFilePath=filepath, extension='.png', settings=image_settings)
        if not result == QgsLayoutExporter.Success:
            print(error)

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.