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