I am trying to build a plugin to load a print composer from file, generate an atlas and export to image. So far I have been successful in loading the template and exporting it to image.
I have been unable to add any of the layers in the legend (which are also in the toc) to the exported map, which results in a blank map and none of the field expressions working.
# Get layers in the legend and append, suspect there must be a cleaner way to do this
layers = self.iface.legendInterface().layers()
layerStringList = []
for layer in layers:
layerID = layer.id()
layerStringList.append(layerID)
# Add layer to map render
myMapRenderer = QgsMapRenderer()
myMapRenderer.setLayerSet(layerStringList)
myMapRenderer.setProjectionsEnabled(False)
# Load template
myComposition = QgsComposition(myMapRenderer)
myFile = os.path.join(os.path.dirname(__file__), 'MMR_Template.qpt')
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()
myDocument.setContent(myTemplateContent)
myComposition.loadFromTemplate(myDocument)
# Save image
myImagePath = os.path.join(os.path.dirname(__file__), 'come_on.png')
myImage = myComposition.printPageAsRaster(0)
myImage.save(myImagePath)
Here is a snippet from the loaded template which should setup the atlas:
<Atlas hideCoverage="false" featureFilter="reference = '61922'" coverageLayer="desktop_search20130615160118593" fixedScale="true" composerMap="0" singleFile="false" filenamePattern=""reference"" enabled="true" filterFeatures="true" sortFeatures="true" sortKey="0" sortAscending="true" margin="1"/>
I am also unsure of the best way to add all the layer in the toc to the instance of QgsMapRenderer().