Wednesday, 4 May 2016

pyqgis - Using QGIS 3.0 API for layout?


I am trying to generate an atlas with python in qgis 3.0. I have :


projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()

for comp in projectLayoutManager.printLayouts():
if comp.atlas().enabled():
comp.atlas().beginRender()
nbobj = comp.atlas().updateFeatures()
comp.atlas().first()
for i in range(0, comp.atlas().count ()):
comp.atlas().refreshCurrentFeature ()
exporter = QgsLayoutExporter(comp.atlas().layout () )
pdf_settings = exporter.ImageExportSettings () #dpi?
exporter.exportToImage (, pdf_settings)

comp.atlas().endRender()

But I would like the filename to have some attributes of the atlas feature.


How do I get the value of the attribute?



Answer



You can simplify this script considerably in QGIS 3.0:


projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()

image_settings = exporter.ImageExportSettings()

image_settings.dpi = 300 # or whatever you want

for comp in projectLayoutManager.printLayouts():
if comp.atlas().enabled():
result, error = QgsLayoutExporter.exportToImage(comp.atlas(),
baseFilePath='c:/temp/my_atlas', extension='.png', settings=image_settings)
if not result == QgsLayoutExporter.Success:
print(error)

To control the generated filenames, you need to set an expression for the atlas filenames, e.g.:



comp.atlas().setFilenameExpression('"some_attribute" || '_export' )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...