2

I'm using @Alexandre Neto's Python code (from July 8 2019) to automatically export predefined atlas layouts, see https://gis.stackexchange.com/questions/272839/export-a-configured-atlas-with-a-python-script-command-line.

I'm using QGIS 3.10.2 on a Windows 10 PC.

export_atlas.py is called by export_atlas.bat.

export_atlas.py (Edit: I have tried both / and \ in project_path and output_folder, both giving the same errors and wrong destination folder)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# linja over bruker vi for å kunne bruke æøå uten advarsler.

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


def export_atlas(qgs_project_path, layout_name, outputs_folder):

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

    print('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, 'jpg', settings)


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

    project_path = 'C:\\adhoc\\lst/lstp_shp.qgz'
    output_folder = 'C:\\adhoc\\lst\\n5g'
    layout_name = 'n5 NGO48 sone G'
    print('Starter atlas-eksport')
    export_atlas(project_path, layout_name, output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

export_atlas.bat

@echo off
chcp 65001

REM Change OSGEO4W_ROOT to point to the base install folder
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
set QGIS_PREFIX_PATH=%QGIS%
REM Gdal Setup
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal\
REM To find proj.db
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj\
REM Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set PYTHONPATH=%QGIS%\python;%PYTHONPATH%
REM For riktig PyQt med dll-er
call "C:\OSGeo4W64\bin\qt5_env.bat"

REM Launch python job
C:\OSGeo4W64\apps\Python37\python export_atlas.py

This works.

However: I get error-messages and the jpg-files are stored in C:\adhoc\lst and not in C:\adhoc\lst\n5g which is set as output_folder in the export_atlas.py (see above).

Active code page: 65001
Starter atlas eksport
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Project in C:/adhoc/lst/lstp_shp.qgz loaded successfully
ERROR 6: The JPEG driver does not support update access to existing datasets.
ERROR 6: The JPEG driver does not support update access to existing datasets.
ERROR 6: The JPEG driver does not support update access to existing datasets.
...

Is it possible to get rid of the error and/or to save the jpg-files in the n5g-folder?

enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
9ls1
  • 1,407
  • 11
  • 25

1 Answers1

2

Your paths in a Windows Python environment should look like:

project_path = 'C:\\adhoc\\lst\\lstp_shp.qgz'
output_folder = 'C:\\adhoc\\lst\\n5g\\'
HeikkiVesanto
  • 16,433
  • 2
  • 46
  • 68
  • Sorry for not mentioning in my post: I've already tried \ instead of /. They both give the same result (errors and jpg saved in wrong folder). – 9ls1 Feb 24 '20 at 11:35
  • I have updated my original post regarding / and \. – 9ls1 Feb 24 '20 at 11:57
  • You need the \ after the folder name as well (updated answer). You still get the error, but the export works. – HeikkiVesanto Feb 24 '20 at 12:39
  • Brilliant! The two "extra" \ at the end in the output_folder did the trick. Sorry, for not reading your answer carefully enough in the first place. – 9ls1 Feb 24 '20 at 12:49
  • I didn't have that in the original answer. Made an edit after testing. But I think the error message is to do with the setting "Single file export when possible" that you can find in the Atlas settings, but I can's see where that can be set in Python. But it tries to export a multi page jpg (like it can do for PDF), but fails, so just creates multiple jpgs instead. – HeikkiVesanto Feb 24 '20 at 12:53
  • I see. :-) That may be the issue. I have not checked the "Single file export when possible". Anyway, thanks, it works - I just have to ignore the ERROR 6. – 9ls1 Feb 24 '20 at 12:57