1

I have an Atlas Layout and the process of creating the JPG or PDF is very long, and that is why I would be interested in the program informing me what file is being created at that moment and how much time is left to finish.

How to do that with PyQGIS?

I see examples with Atlas Composer, but not with Layout, and I see examples with Layout but not control the creation process.

1
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works.
    – Ian Turton
    Jun 6, 2020 at 9:15

1 Answer 1

3

The following code in PyQgis shows how:

  • Open a Qgis Project in a GeoPackage format.
  • Print Layouts in your project
  • Open a Layout.
  • Open a layout with atlas.
  • Filter witch features will show.
  • Control the process of atlas layout creation:

    • Number of Layouts will create.
    • Name of file is creating...
  • Creation of JPG or PDF with atlas Layout.

Here is my code:

from qgis.core import QgsApplication,QgsProject,QgsLayoutExporter

qgs = QgsApplication([], True)

# load providers
qgs.initQgis()
project = QgsProject.instance()
project.read('geopackage:C:/TEMP/your_project_name.GPKG?projectName=Your_QGIS_Project_Name')

# If you want to see wich layouts are in your project
manager = project.layoutManager()
layouts_list = manager.printLayouts()
for layout in layouts_list:
    print('Layout Name: ',layout.name())

print(layouts_list)

layoutName='Mapa_JPG_2'
layout = manager.layoutByName(layoutName)

myAtlas=layout.atlas()
myAtlas.setFilterFeatures(True)

# If you want to filter your Atlas Selection
myAtlas.setFilterExpression("Your_Field_Name = '%s'" % ('Your_Value'))

# Generate atlas
# It's a QgsLayoutAtlas class 
# https://qgis.org/api/classQgsLayoutAtlas.html#acb3052609fcff21e4f4cf3f9e93780e0

# Starts Layout Generation
myAtlas.beginRender()

# For 0 to Number of features in Atlas Selection
for i in range(0, myAtlas.count()):

    # Creata a exporter Layout for each layout generate with Atlas
    exporter = QgsLayoutExporter(myAtlas.layout())

    print('Saving File: '+str(myAtlas.currentFeatureNumber())+' of '+str(myAtlas.count()))

    # If you want create a PDF's Files
    exporter.exportToPdf('c:/temp/'+myAtlas.currentFilename()+".pdf", QgsLayoutExporter.PdfExportSettings())

    # If you want create a JPG's files
    exporter.exportToImage('c:/temp/'+myAtlas.currentFilename()+".jpg", QgsLayoutExporter.ImageExportSettings())

    # Show wich file is creating
    print('Create File: '+myAtlas.currentFilename())

    # Create Next Layout
    myAtlas.next()

# Close Atlas Creation
myAtlas.endRender()

# Close Qgis
qgs.exitQgis()

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.