I need to generate an atlas for each feature from a layer for reporting. There will be created a few maps. The centre of each map must be focused on each feature. In this task I use a predefined QPT template. In QGIS Composer Manager everything works perfectly. Then I save the QPT file as a template and I try to automate the process with a plugin using this function. It's based on this thread: Programmatically load composer from template and generate atlas using pyQgis
alayer=iface.activeLayer()
# Add all layers in map canvas to render
myMapRenderer = self.iface.mapCanvas().mapRenderer()
# Load template from file
myComposition = QgsComposition(myMapRenderer)
template = 'atlas16.qpt'
myFile = 'C://Users//maria//.qgis2//composer_templates//atlas16.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)
# Setup Atlas
myAtlas = QgsAtlasComposition(myComposition)
myAtlas.setCoverageLayer(alayer)
myAtlas.setComposerMap(myAtlasMap)
myAtlas.setEnabled(True)
myAtlas.setHideCoverage(False)
# Generate atlas
myAtlas.beginRender()
for i in range(0, myAtlas.numFeatures()):
myAtlas.prepareForFeature( i )
jobs = "D://Krzysztof//Skrypty//Archiwum//"
output_jpeg = jobs + str(i)+ "_BMS_plan.jpg"
myImage = myComposition.printPageAsRaster(0)
myImage.save(output_jpeg)
myAtlas.endRender()
if os.path.isfile(output_jpeg) is False:
QMessageBox.warning(self.iface.mainWindow(), "Uh Oh!", "Something went wrong, no image exported!")
self.dlg.close()
In the result I get some reports, but they look same - so the focusing on points isn't performed.
I suspected that I might be caused by the extent which is present in the QPT file. Then I changed my function, so that the extent will be adjusted for each feature. However, at the end I got only empty reports. The template content is as follows:
Does anyone know what might be wrong? Could you give any hints how a QPT template should be adjusted?