Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active February 1, 2021 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/28eedae0588548403aca2ba781ff443a to your computer and use it in GitHub Desktop.
Save ThomasG77/28eedae0588548403aca2ba781ff443a to your computer and use it in GitHub Desktop.
About PyQGIS processing to print atlas

There are various approaches to generate an atlas. Now (don't know since when...), there is a new processing script for this intend e.g scripting Atlas creation without complex boilerplate. The one for PDF export is native:atlaslayouttopdf and native:atlaslayouttoimage. You can see below an illustrated sample using native:atlaslayouttopdf

Generate an atlas for all features

from qgis import processing

parameters = {
   "COVERAGE_LAYER" : 'my_layer_name', # Could be also QgsVectorLayer
   "DISABLE_TILED" : False,
   "DPI" : None,
   "FILTER_EXPRESSION" : "",
   "FORCE_VECTOR" : False,
   "GEOREFERENCE" : True,
   "INCLUDE_METADATA" : True,
   "LAYERS" : None,
   "LAYOUT" : "your_layout_name", # Need your own layout
   "OUTPUT" : "/tmp/atlas.pdf", # Need to change path
   "SIMPLIFY" : True,
   "SORTBY_EXPRESSION" : "",
   "SORTBY_REVERSE" : False,
   "TEXT_FORMAT" : 0
}

# Use the following (uncomment if interested) to learn about argument you can provide for each key above
# processing.algorithmHelp("native:atlaslayouttopdf")

out = processing.run("native:atlaslayouttopdf", parameters)
print(out['OUTPUT'])

To ease the pain to discover the parameters above, I recommend to run manually the processing script. Then, go to "Processing" menu and "History..." and select the algorithm to see parameters like in below screenshot. They are quite similar to the one I provided (blurry part for customer anonymity)

Processing Atlas Export to image

Alternate solution to export to image using native:atlaslayouttoimage

from qgis import processing

parameters_img = {
    "ANTIALIAS" : True,
    "COVERAGE_LAYER" : "my_layer_name",
    "DPI" : None,
    "EXTENSION" : 8, # 10 for tif 
    "FILENAME_EXPRESSION" : "\"output_\"||@atlas_featurenumber",
    "FILTER_EXPRESSION" : "",
    "FOLDER" : "/tmp/tryout", # Change directory
    "GEOREFERENCE" : True,
    "INCLUDE_METADATA" : True,
    "LAYERS" : None,
    "LAYOUT" : "your_layout_name",
    "SORTBY_EXPRESSION" : "",
    "SORTBY_REVERSE" : False
}
out_img = processing.run("native:atlaslayouttoimage", parameters_img)
print(out_img['OUTPUT'])

Create a PDF per feature of coverage layer

from qgis import processing

coverage_layer = iface.activeLayer() # Change the ref for your own

# Use the following (uncomment if interested) to learn about argument you can provide for each key in parameters below
# processing.algorithmHelp("native:atlaslayouttopdf")

for feature in coverage_layer.getFeatures():
    parameters = {
       "COVERAGE_LAYER" : coverage_layer, # Could be also QgsVectorLayer
       "DISABLE_TILED" : False,
       "DPI" : None,
       "FILTER_EXPRESSION" : "id='{}'".format(feature['id']), #id is the key property in your feature
       "FORCE_VECTOR" : False,
       "GEOREFERENCE" : True,
       "INCLUDE_METADATA" : True,
       "LAYERS" : None,
       "LAYOUT" : "rapport-basias-positif", # Need your own layout
       "OUTPUT" : "/tmp/atlas_{}.pdf".format(feature['id']), # Need to change path
       "SIMPLIFY" : True,
       "SORTBY_EXPRESSION" : "",
       "SORTBY_REVERSE" : False,
       "TEXT_FORMAT" : 0
    }
    out = processing.run("native:atlaslayouttopdf", parameters)
    print(out['OUTPUT'])

In fact, for PDF export, if you untick "Single file export when possible", QGIS will magically create for each feature a separate PDF for each feature when exporting. Work in the GUI but not tested in the processing script as OUTPUT expect a file and not a directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment