【问题标题】:QGIS - How to export Atlas, separate pdfs?QGIS - 如何导出地图集,单独的 pdf?
【发布时间】:2020-08-16 21:35:37
【问题描述】:

我已使用以下帖子 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, layout_name, output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

我正在尝试为 pdfs(每个功能一个 pdf)获得相同的结果,但是由于 python 知识有限,我正在努力处理代码。这将创建一个 pdf,但没有文件名,并且它没有拾取第一个 atlas 功能。

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 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([], 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, layout_name, output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

任何帮助将不胜感激。

【问题讨论】:

    标签: python pdf qgis


    【解决方案1】:

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

    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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 2022-12-05
      • 2021-07-20
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多