2

I'm trying to export a set of Image based on an atlas define by my plugin. Almost everything works well except that I have a rules based style on my coverage layer to highlight the current atlas feature as follow :

@atlas_featureid = $id

If I use my template composer inside QGIS the highlighted feature is updated for every map so everything seems right.

enter image description here

But when I generate the outputs from my plugin the highlighted feature does not update and stay always on the same feature... (no update)

enter image description here

Here is the code I use to instanciate and run the atlas:

composition = QgsComposition(renderer)
qpt_file = os.path.join(os.path.dirname(__file__), 'qpt','composer.qpt')
template_file = file(qpt_file, 'rt')
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition.loadFromTemplate(document)
atlas_map = composition.getComposerMapById(0)
atlas_map.setAtlasDriven(True)

atlas = QgsAtlasComposition(composition)
atlas.setEnabled(True)
atlas.setCoverageLayer(layer)
atlas.setHideCoverage(False)

atlas.setFilterFeatures(True)
filtre = "\"{com}\"||\"{sec}\"||\"{num}\" in ({idents})".format(idents=idents,com=config['shp_mapping']['COM'],sec=config['shp_mapping']['SEC'],num=config['shp_mapping']['NUM'])
atlas.setFeatureFilter(filtre)
atlas.setFilterFeatures(True)
atlas_map.setAtlasScalingMode(QgsComposerMap.Auto)
atlas_map.setAtlasMargin(6.0)
atlas_map.setUpdatesEnabled(True)

atlas.beginRender()   
composition.setAtlasMode(QgsComposition.ExportAtlas)

for i in range(0, atlas.numFeatures()):
     atlas.prepareForFeature(i)
     layer.triggerRepaint()
     feat = atlas.feature()

     exp_dir = os.path.dirname(QgsProject.instance().fileName())
     exp_dir = os.path.join(exp_dir, 
                        config["relative_export_directory"],
                        feat[config['shp_mapping']['COM']])
     if not os.path.exists(exp_dir):
           os.makedirs(exp_dir)
     filename = feat[config['shp_mapping']['SEC']]+feat[config['shp_mapping']['NUM']]+'.png'
     out = os.path.join(exp_dir, filename)
     image = composition.printPageAsRaster(0)
     image.save(out)
atlas.endRender()

I suspect that I need to udpdate something for every rendered maps but I can't figure out what... The most weird thing to me is that it works fine when the atlas is exported from qgis GUI and this is the only problem I get in my resulting images when the atlas is exported from my plugin (the extent, scale, labels,... are fine). It's seems to be a style problem...

1 Answer 1

0

I finally find the tips by digging in the pyqgis docs, especially here: https://qgis.org/api/2.18/classQgsAtlasComposition.html#details

It says that :

This class should not be created directly. For the atlas to function correctly the atlasComposition() property for QgsComposition should be used to retrieve a QgsAtlasComposition which is automatically created and attached to the composition.

So I only need to replace this statement:

atlas = QgsAtlasComposition(composition)

by:

atlas = composition.atlasComposition()

And everything works great!

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.