4

I have found this code in this site and with the some changes I want to create a automate print PyQGIS composer using atlas.

The code looks like it should work, I have an error.

The code doesn't change my atlas feature layer zoom, any idea why?

Stay in the default zoom in my .qpt but my atlas change features correct

Here is the code:

from qgis.PyQt.QtXml import QDomDocument
from qgis.utils import iface


def quick_export():
    alayer=iface.activeLayer()
    # Add all layers in map canvas to render
    myMapRenderer = iface.mapCanvas().mapRenderer()

    # Load template from file
    myComposition = QgsComposition(myMapRenderer)
    template = 's4.qpt'

    myFile = 'C:/Users/name/Desktop/tt/s4.qpt'
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()

    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)

    # Get map composition and define scale
    myAtlasMap = myComposition.getComposerMapById(1)
    myAtlasMap.setAtlasDriven(True)
    #myAtlasMap.setNewScale(int(scale))

    # Setup Atlas.
    #myAtlas = QgsAtlasComposition(myComposition)
    myAtlas = myComposition.atlasComposition()
    myComposition.setAtlasMode(True)
    myAtlas.setCoverageLayer(alayer) 
    myAtlas.setComposerMap(myAtlasMap)
    #myAtlas.setFixedScale(True)
    #myAtlas.fixedScale()
    myAtlasMap.setAtlasScalingMode( QgsComposerMap.Auto )
    myAtlas.setHideCoverage(False)
    myAtlas.setFilterFeatures(True)
    myAtlas.setFilterFeatures(True)

    # Generate atlas
    myAtlas.beginRender()
    for i in range(0, myAtlas.numFeatures()):
        myAtlas.prepareForFeature( i )
        jobs = r"C:/Users/name/Desktop/test/"
        output_jpeg = jobs + str(i)+ "_BMS_plan.jpg"
        myImage = myComposition.printPageAsRaster(0)
        myImage.save(output_jpeg)
    myAtlas.endRender()

quick_export()

1 Answer 1

1

Using your code example,only need put myComposition.setAtlasMode(QgsComposition.ExportAtlas),for example after call myAtlas.beginRender()

Example using your code:

from qgis.PyQt.QtXml import QDomDocument
from qgis.utils import iface


def quick_export():
    alayer=iface.activeLayer()
    # Add all layers in map canvas to render
    myMapRenderer = iface.mapCanvas().mapRenderer()

    # Load template from file
    myComposition = QgsComposition(myMapRenderer)

    myFile = 'C:\\Users\\fran\\Desktop\\s4.qpt'
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()

    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)


    # Get map composition and define scale
    myAtlasMap = myComposition.getComposerMapById(0)
    myAtlasMap.setAtlasDriven(True)

    # Setup Atlas.
    myAtlas = myComposition.atlasComposition()
    myAtlas.setCoverageLayer(alayer) 
    myAtlas.setComposerMap(myAtlasMap)
    myAtlas.setEnabled(True)
    myAtlas.setHideCoverage(False)
    myAtlasMap.setAtlasScalingMode( QgsComposerMap.Auto )
    # Generate atlas
    myAtlas.beginRender()
    myComposition.setAtlasMode(QgsComposition.ExportAtlas)
    for i in range(0, myAtlas.numFeatures()):
        myAtlas.prepareForFeature( i )
        jobs = r"C:\\Users\\fran\\Desktop\\test\\"
        output_jpeg = jobs + str(i)+ "_BMS_plan.jpg"
        myImage = myComposition.printPageAsRaster(0)
        myImage.save(output_jpeg)
        myComposition.refreshItems()
    myAtlas.endRender()

quick_export()

The result is:

enter image description here

I hope it helps you.

Tested with QGIS 2.18.14 on W10

Your Answer

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

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