More

The QGIS atlas defined with PyQGIS and a QPT template exports the same maps

The QGIS atlas defined with PyQGIS and a QPT template exports the same maps


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">

You need to set your map to use the atlas feature's extent, using QgsComposerMap.setAtlasScalingMode, eg

myAtlasMap.setAtlasScalingMode( QgsComposerMap.Auto )

Place this line after you've setup your atlas, but before the beginRender call.


Watch the video: 2015 - Andreas Neumann: Neues vom QGIS Print Composer und Atlas-Seriendruck