Message posté par : Pascal PLUVINET
----------------------------------------
Merci pour vos pistes. J'ai réussi à faire ce que je souhaitais avec QGIS 3.16. Je me
suis largement inspiré de plusieurs sources.
Je partage mes scripts mais n'ayant plus fait de python depuis 2-3 ans, il y a
surement des améliorations à faire. Je n'ai pas bien le temps d'aller + loin.
Dans mon cas, j'ai 12 projets QGZ avec pour chacun d'eux un atlas nommé
'page'.
J'ai créé un fichier Python "exportpage.py" et un fichier bat
"Export_page_QGIS3.16.bat" qui permet de lancer le script python sur chaque
projet qgz. Ces scripts sont dans le même répertoire que les projets QGIS.
Script Python "exportpage.py":
-----------------
Code :
from qgis.core import QgsApplication, QgsProject, QgsLayoutExporter
import os
from qgis.core import QgsApplication, QgsProject, QgsProject
from qgis.core import QgsProject
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from qgis.utils import iface
from qgis.core.contextmanagers import qgisapp
from PyQt5.QtCore import QFileInfo
import sys
def export_atlas(qgs_project_path, layout_name, outputs_folder):
# Open existing project
project = QgsProject.instance()
project.read(qgs_project_path)
# Open prepared layout that as atlas enabled and set
layout = project.layoutManager().layoutByName(layout_name)
# Start atlas iterator
atlas = layout.atlas()
if atlas.beginRender():
while atlas.next():
exporter = QgsLayoutExporter(layout)
settings = QgsLayoutExporter.ImageExportSettings()
filename = atlas.currentFilename()+'_.png'
#f'atlas.currentFilename().png'
filename_path = os.path.join(outputs_folder, filename)
print(f'Export '+atlas.currentFilename())
exporter.exportToImage(filename_path, settings)
print(f'finished')
atlas.endRender()
else:
print('WARNING - No atlas features available for export!')
def main():
# Start a QGIS application without GUI
qgs = QgsApplication([], False)
qgs.initQgis()
#Chemon des projets qgz, chemin du répertoire d'export et nom du composeur
project_path = sys.argv[1]
output_folder = 'O:/RESTITUTION/'
layout_name = 'page'
export_atlas(project_path, layout_name, output_folder)
# Close the QGIS application
qgs.exitQgis()
if __name__ == "__main__":
main()
-----------------
Script BAT "Export_page_QGIS3.16.bat"
-----------------
Code :
@echo off
call "C:\Program Files\QGIS 3.16\bin\o4w_env.bat"
call qt5_env.bat
call py3_env.bat
path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
SET
PYTHONPATH=C:\PROGRA~1\QGIS3~1.16\apps\Python37;C:\PROGRA~1\QGIS3~1.16\apps\Python37\Scripts;C:\Program
Files\QGIS 3.16\apps\qgis-ltr\python
SET QT_QPA_PLATFORM_PLUGIN_PATH=C:\PROGRA~1\QGIS3~1.16\apps\Qt5\plugins
SET
PATH=%PATH%;C:\PROGRA~1\QGIS3~1.16\apps\Qt5\bin;C:\PROGRA~1\QGIS3~1.16\apps\Python37\Lib\site-packages
@echo on
REM on boucle sur les projet QGIS
for %%i in (*.qgz) do (
echo %%i
python3 exportpage.py %%i
)
pause
-----------------
Le module atlas de QGIS est vraiment très puissant, notamment en jouant avec les
expressions et en intégrant du HTML. J'ai (re)découvert cela récemment !
La possibilité de lancer les scripts pour produire des cartes sans même ouvrir les projets
QGIS donne un gros + !
Bonne fin de semaine !
----------------------------------------
Le message est situé
https://georezo.net/forum/viewtopic.php?pid=342418#p342418
Pour y répondre : qgis_fr(a)ml.georezo.net ou reply de votre messagerie
Pour vous désabonner connectez-vous sur le forum puis Profil / Abonnement
--
Association GeoRezo - le portail géomatique
https://georezo.net