QGIS-如何导出Atlas单独的pdf?

问题描述

我已使用以下帖子https://gis.stackexchange.com/questions/272839/export-a-configured-atlas-with-a-python-script-command-line中的代码将QGIS Atlas导出为png。效果很好,每个功能都有一个单独的png。

from qgis.core import  QgsApplication,QgsProject,QgsLayoutExporter
import os

def export_atlas(qgs_project_path,layout_name,outputs_folder):

    # Open existing project
    project = QgsProject.instance()
    project.read(qgs_project_path)

    print(f'Project in "{project.fileName()} loaded successfully')

    # Open prepared layout that as atlas enabled and set
    layout = project.layoutManager().layoutByName(layout_name)

    # Export atlas
    exporter = QgsLayoutExporter(layout)
    settings = QgsLayoutExporter.ImageExportSettings()
    exporter.exportToImage(layout.atlas(),outputs_folder,'png',settings)


def main():
    # Start a QGIS application without GUI
    qgs = QgsApplication([],False)
    qgs.initQgis()

    project_path = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\print_test_3_14.qgz'
    output_folder = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\Output\\'
    layout_name = 'Photoviewer_wildlife'

    export_atlas(project_path,output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

我正在尝试获取相同的pdf文件(每个功能只有一个pdf文件),但是由于有限的python知识,我在代码中苦苦挣扎。这将创建一个pdf文件,但没有文件名,并且不会提取一个地图集功能

import os

def export_atlas(qgs_project_path,outputs_folder):

    # Open existing project
    project = QgsProject.instance()
    project.read(qgs_project_path)

    print(f'Project in "{project.fileName()} loaded successfully')

    # Open prepared layout that has atlas enabled and set
    layout = project.layoutManager().layoutByName(layout_name)

    # Export atlas
    exporter = QgsLayoutExporter(layout)
       exporter.exportToPdf('D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\Output\\'+layout.atlas().currentFilename()+".pdf",QgsLayoutExporter.PdfExportSettings())

def main():
    # Start a QGIS application without GUI
    qgs = QgsApplication([],output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

我们将不胜感激。

解决方法

您需要遍历地图集的布局。我正在使用这样的东西

def print_atlas_as_individual_pdfs(layout,savepath):
    my_atlas = layout.atlas()
    #this is needed if running directly from python
    #i don't think it's necessary if run from within qgis
    my_atlas.beginRender()
    while my_atlas.next():
        pdfpath = getpdfpath(savepath,my_atlas.currentFilename())
        atlas_layout=my_atlas.layout()
        exporter = QgsLayoutExporter(atlas_layout)
        exporter.exportToPdf(pdfpath,exporter.PdfExportSettings())